-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathreceive_bu_data.py
More file actions
70 lines (51 loc) · 2.03 KB
/
receive_bu_data.py
File metadata and controls
70 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import json
from pathlib import Path
from fastapi import FastAPI, Request
import prettyprinter
prettyprinter.install_extras()
app = FastAPI()
@app.post("/post_agent_history_step")
async def post_agent_history_step(request: Request):
data = await request.json()
print("-> Browser Use Recorder. Recording received!")
# prettyprinter.cpprint(data)
# Retrieve the folder name from app.state
recordings_folder_name = app.state.folder_name
print("RECORDINGS FOLDER NAME")
print(recordings_folder_name)
# Ensure the folder exists
recordings_folder = Path(recordings_folder_name)
recordings_folder.mkdir(parents=True, exist_ok=True)
# Determine the next file number by examining existing .json files
existing_numbers = []
for item in recordings_folder.iterdir():
if item.is_file() and item.suffix == ".json":
try:
file_num = int(item.stem)
existing_numbers.append(file_num)
except ValueError:
# Ignore files that don't have an integer as the stem
pass
if existing_numbers:
next_number = max(existing_numbers) + 1
else:
next_number = 1
# Construct the file path
file_path = recordings_folder / f"{next_number}.json"
# Save the JSON data to the file
with file_path.open("w") as f:
json.dump(data, f, indent=2)
return {"status": "ok", "message": f"Saved to {file_path}"}
if __name__ == "__main__":
import uvicorn
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=8000,
help="Port number on which the API will run")
parser.add_argument("--folder_name", type=str, default="recordings",
help="Name of the folder where recordings will be saved")
args = parser.parse_args()
# Store folder name in app's state so the route can see it
app.state.folder_name = args.folder_name
uvicorn.run(app, host="0.0.0.0", port=args.port)