-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_data.py
More file actions
215 lines (170 loc) · 7.58 KB
/
user_data.py
File metadata and controls
215 lines (170 loc) · 7.58 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""BotCloud User Data Management - Python Example
This example demonstrates how to create, use, and manage persistent User Data
for preserving browser state (cookies, localStorage, login sessions) across sessions.
Prerequisites:
pip install playwright aiohttp
playwright install chromium
Usage:
1. Modify the CONFIG section below with your token and proxy
2. Run: python examples/user-data/python/user_data.py
What this example does:
1. Creates a new User Data directory
2. Lists all User Data entries and displays quota
3. Connects to browser using the User Data (first visit)
4. Saves some data to localStorage
5. Reconnects using the same User Data (second visit)
6. Verifies that localStorage data persisted
7. Deletes the User Data when done
"""
# ============ Configuration (Modify these) ============
CONFIG = {
"token": "your-token-here",
"proxy": "username:password@proxy.example.com:4600",
"device_type": "mac", # or "win", "android"
"api_base": "https://cloud.bots.win",
}
# ======================================================
import asyncio
import urllib.parse
from typing import Optional, Dict, Any
import aiohttp
from playwright.async_api import async_playwright
async def api_request(
session: aiohttp.ClientSession,
endpoint: str,
method: str = "GET"
) -> Dict[str, Any]:
"""Make an authenticated API request to BotCloud."""
url = f"{CONFIG['api_base']}{endpoint}"
headers = {
"Authorization": f"Bearer {CONFIG['token']}",
"Content-Type": "application/json",
}
async with session.request(method, url, headers=headers) as response:
if not response.ok:
text = await response.text()
raise RuntimeError(f"API Error ({response.status}): {text}")
return await response.json()
async def create_user_data(session: aiohttp.ClientSession) -> str:
"""Create a new User Data entry."""
print("📦 Creating new User Data...")
data = await api_request(session, "/api/user-data", method="POST")
print(f"✅ Created User Data: {data['id']}")
print(f" Created at: {data['createdAt']}")
return data["id"]
async def list_user_data(session: aiohttp.ClientSession) -> None:
"""List all User Data entries."""
print("\n📋 Listing User Data entries...")
data = await api_request(session, "/api/user-data")
print(f"\nTotal entries: {data['total']}")
quota = data["quota"]
print(f"Quota: {quota['used']}/{quota['max']} (Can create: {quota['canCreate']})")
if data["items"]:
print("\nUser Data entries:")
for item in data["items"]:
print(f" - {item['id']}")
print(f" Created: {item['createdAt']}")
print(f" Last used: {item.get('lastUsedAt', 'Never')}")
locked = "Yes (in use)" if item["isLocked"] else "No"
print(f" Locked: {locked}")
async def delete_user_data(session: aiohttp.ClientSession, user_data_id: str) -> None:
"""Delete a User Data entry."""
print(f"\n🗑️ Deleting User Data: {user_data_id}...")
await api_request(session, f"/api/user-data/{user_data_id}", method="DELETE")
print("✅ User Data deleted successfully")
def build_endpoint(user_data_id: Optional[str] = None) -> str:
"""Build BotCloud WebSocket endpoint URL with optional User Data."""
if not CONFIG["token"] or not CONFIG["proxy"]:
raise RuntimeError("Please configure token and proxy in CONFIG section above")
params = {
"token": CONFIG["token"],
"--proxy-server": CONFIG["proxy"],
"device_type": CONFIG["device_type"],
}
# Add User Data ID if provided
if user_data_id:
params["user_data_id"] = user_data_id
query_string = urllib.parse.urlencode(params)
return f"wss://cloud.bots.win?{query_string}"
async def run_browser_session(user_data_id: str, session_name: str) -> None:
"""Connect to browser and perform automation."""
print(f"\n🌐 {session_name}...")
async with async_playwright() as playwright:
browser = None
try:
browser = await playwright.chromium.connect_over_cdp(
build_endpoint(user_data_id)
)
print(" Connected! Opening page...")
context = browser.contexts[0] if browser.contexts else await browser.new_context()
page = await context.new_page()
print(" Navigating to example.com...")
await page.goto("https://example.com")
# On first visit, set some data in localStorage
if "First" in session_name:
print(" Setting test data in localStorage...")
await page.evaluate("""() => {
localStorage.setItem('botcloud_test', 'persistent_data');
localStorage.setItem('session_count', '1');
}""")
print(" ✅ Data saved to localStorage")
# On second visit, verify data persisted
if "Second" in session_name:
print(" Checking if localStorage data persisted...")
data = await page.evaluate("""() => {
return {
testData: localStorage.getItem('botcloud_test'),
sessionCount: localStorage.getItem('session_count')
};
}""")
if data["testData"] == "persistent_data":
print(" ✅ SUCCESS! Data persisted across sessions:")
print(f" botcloud_test: {data['testData']}")
print(f" session_count: {data['sessionCount']}")
else:
print(" ❌ FAILURE: Data did not persist")
print(" Closing browser...")
await browser.close()
print(" ✅ Browser closed")
except Exception as error:
if browser:
await browser.close()
raise error
async def main() -> None:
"""Main execution function."""
print("=" * 60)
print("BotCloud User Data Management Example")
print("=" * 60)
user_data_id = None
async with aiohttp.ClientSession() as session:
try:
# 1. Create User Data
user_data_id = await create_user_data(session)
# 2. List all User Data
await list_user_data(session)
# 3. First browser session - save data
await run_browser_session(user_data_id, "First visit - Saving data")
# 4. Second browser session - verify persistence
await run_browser_session(user_data_id, "Second visit - Verifying persistence")
# 5. List User Data again (check lastUsedAt updated)
await list_user_data(session)
print("\n" + "=" * 60)
print("✅ All operations completed successfully!")
print("=" * 60)
print("\nKey takeaways:")
print(" • User Data preserves cookies, localStorage, and login states")
print(" • Each User Data has a unique ID (udd_xxx)")
print(" • User Data persists until explicitly deleted")
print(" • Check quota limits before creating new User Data")
except Exception as error:
print(f"\n❌ Error: {error}")
raise
finally:
# 6. Clean up - delete the User Data
if user_data_id:
try:
await delete_user_data(session, user_data_id)
except Exception as error:
print(f"Failed to delete User Data: {error}")
if __name__ == "__main__":
asyncio.run(main())