Spaces:
Sleeping
Sleeping
Commit ·
9ed488f
1
Parent(s): 421e7e4
proxy pool race condition fixed
Browse files- freeplay2api.py +11 -9
freeplay2api.py
CHANGED
|
@@ -167,8 +167,9 @@ class FreeplayClient:
|
|
| 167 |
"referer": "https://app.freeplay.ai/signup",
|
| 168 |
}
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
|
|
|
| 172 |
proxy_info = None
|
| 173 |
try:
|
| 174 |
# Wait for proxy pool to be ready
|
|
@@ -187,6 +188,8 @@ class FreeplayClient:
|
|
| 187 |
proxy_info = self.proxy_pool.get_proxy() if self.proxy_pool else None
|
| 188 |
proxies = {"http": proxy_info['full'], "https": proxy_info['full']} if proxy_info else None
|
| 189 |
|
|
|
|
|
|
|
| 190 |
response = requests.post(
|
| 191 |
url,
|
| 192 |
data=json.dumps(payload),
|
|
@@ -200,7 +203,7 @@ class FreeplayClient:
|
|
| 200 |
project_id = data.get("project_id")
|
| 201 |
session = response.cookies.get("session")
|
| 202 |
if project_id and session:
|
| 203 |
-
logging.info(f"Successfully registered account: {payload['email']}")
|
| 204 |
return {
|
| 205 |
"email": payload["email"],
|
| 206 |
"password": payload["password"],
|
|
@@ -209,24 +212,23 @@ class FreeplayClient:
|
|
| 209 |
"balance": 5.0,
|
| 210 |
}
|
| 211 |
|
| 212 |
-
logging.warning(f"Registration attempt {attempt
|
| 213 |
|
| 214 |
except requests.exceptions.ProxyError as e:
|
| 215 |
-
logging.warning(f"Proxy error during registration on attempt {attempt
|
| 216 |
if self.proxy_pool and proxy_info:
|
| 217 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 218 |
|
| 219 |
except requests.exceptions.RequestException as e:
|
| 220 |
-
logging.warning(f"Request exception during registration on attempt {attempt
|
| 221 |
if self.proxy_pool and proxy_info:
|
| 222 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 223 |
|
| 224 |
except Exception as e:
|
| 225 |
-
logging.error(f"An unexpected error occurred during registration on attempt {attempt
|
| 226 |
|
| 227 |
-
logging.error("Failed to register a new account after
|
| 228 |
return None
|
| 229 |
-
|
| 230 |
def chat(
|
| 231 |
self,
|
| 232 |
session_id: str,
|
|
|
|
| 167 |
"referer": "https://app.freeplay.ai/signup",
|
| 168 |
}
|
| 169 |
|
| 170 |
+
attempt = 0
|
| 171 |
+
while attempt < 50: # Try up to 50 times
|
| 172 |
+
attempt += 1
|
| 173 |
proxy_info = None
|
| 174 |
try:
|
| 175 |
# Wait for proxy pool to be ready
|
|
|
|
| 188 |
proxy_info = self.proxy_pool.get_proxy() if self.proxy_pool else None
|
| 189 |
proxies = {"http": proxy_info['full'], "https": proxy_info['full']} if proxy_info else None
|
| 190 |
|
| 191 |
+
logging.info(f"Registration attempt {attempt}/50 using proxy {proxy_info['full'] if proxy_info else 'None'}")
|
| 192 |
+
|
| 193 |
response = requests.post(
|
| 194 |
url,
|
| 195 |
data=json.dumps(payload),
|
|
|
|
| 203 |
project_id = data.get("project_id")
|
| 204 |
session = response.cookies.get("session")
|
| 205 |
if project_id and session:
|
| 206 |
+
logging.info(f"Successfully registered account: {payload['email']} on attempt {attempt}")
|
| 207 |
return {
|
| 208 |
"email": payload["email"],
|
| 209 |
"password": payload["password"],
|
|
|
|
| 212 |
"balance": 5.0,
|
| 213 |
}
|
| 214 |
|
| 215 |
+
logging.warning(f"Registration attempt {attempt}/50 failed with status {response.status_code}: {response.text}")
|
| 216 |
|
| 217 |
except requests.exceptions.ProxyError as e:
|
| 218 |
+
logging.warning(f"Proxy error during registration on attempt {attempt}/50: {e}. Retrying with a new proxy...")
|
| 219 |
if self.proxy_pool and proxy_info:
|
| 220 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 221 |
|
| 222 |
except requests.exceptions.RequestException as e:
|
| 223 |
+
logging.warning(f"Request exception during registration on attempt {attempt}/50: {e}. Retrying...")
|
| 224 |
if self.proxy_pool and proxy_info:
|
| 225 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 226 |
|
| 227 |
except Exception as e:
|
| 228 |
+
logging.error(f"An unexpected error occurred during registration on attempt {attempt}/50: {e}. Retrying...")
|
| 229 |
|
| 230 |
+
logging.error("Failed to register a new account after 50 attempts.")
|
| 231 |
return None
|
|
|
|
| 232 |
def chat(
|
| 233 |
self,
|
| 234 |
session_id: str,
|