bibibi12345 commited on
Commit
c769bac
·
1 Parent(s): d4b8661

proxy pool retry added

Browse files
Files changed (1) hide show
  1. freeplay2api.py +52 -35
freeplay2api.py CHANGED
@@ -159,13 +159,6 @@ class FreeplayClient:
159
 
160
  def register(self) -> Optional[Dict]:
161
  url = "https://app.freeplay.ai/app_data/auth/signup"
162
- payload = {
163
- "email": self.faker.email(),
164
- "password": f"aA1!{uuid.uuid4().hex[:8]}",
165
- "account_name": self.faker.name(),
166
- "first_name": self.faker.first_name(),
167
- "last_name": self.faker.last_name(),
168
- }
169
  headers = {
170
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
171
  "Accept": "application/json",
@@ -173,35 +166,59 @@ class FreeplayClient:
173
  "origin": "https://app.freeplay.ai",
174
  "referer": "https://app.freeplay.ai/signup",
175
  }
176
- try:
177
- proxy_info = self.proxy_pool.get_proxy() if self.proxy_pool else None
178
- proxies = {"http": proxy_info['full'], "https": proxy_info['full']} if proxy_info else None
179
- response = requests.post(
180
- url,
181
- data=json.dumps(payload),
182
- headers=headers,
183
- proxies=proxies,
184
- timeout=20,
185
- )
186
- response.raise_for_status()
187
- project_id = response.json()["project_id"]
188
- session = response.cookies.get("session")
189
- if project_id and session:
190
- return {
191
- "email": payload["email"],
192
- "password": payload["password"],
193
- "session_id": session,
194
- "project_id": project_id,
195
- "balance": 5.0, # 新注册账号默认5刀
196
  }
197
- except requests.exceptions.ProxyError as e:
198
- logging.warning(f"Proxy error during registration: {e}")
199
- if self.proxy_pool and proxy_info:
200
- self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
201
- return None
202
- except Exception as e:
203
- logging.error(f"Account registration failed: {e}")
204
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
  def chat(
207
  self,
 
159
 
160
  def register(self) -> Optional[Dict]:
161
  url = "https://app.freeplay.ai/app_data/auth/signup"
 
 
 
 
 
 
 
162
  headers = {
163
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36",
164
  "Accept": "application/json",
 
166
  "origin": "https://app.freeplay.ai",
167
  "referer": "https://app.freeplay.ai/signup",
168
  }
169
+
170
+ while True:
171
+ proxy_info = None
172
+ try:
173
+ payload = {
174
+ "email": self.faker.email(),
175
+ "password": f"aA1!{uuid.uuid4().hex[:8]}",
176
+ "account_name": self.faker.name(),
177
+ "first_name": self.faker.first_name(),
178
+ "last_name": self.faker.last_name(),
 
 
 
 
 
 
 
 
 
 
179
  }
180
+
181
+ proxy_info = self.proxy_pool.get_proxy() if self.proxy_pool else None
182
+ proxies = {"http": proxy_info['full'], "https": proxy_info['full']} if proxy_info else None
183
+
184
+ response = requests.post(
185
+ url,
186
+ data=json.dumps(payload),
187
+ headers=headers,
188
+ proxies=proxies,
189
+ timeout=20,
190
+ )
191
+
192
+ if response.status_code == 200:
193
+ data = response.json()
194
+ project_id = data.get("project_id")
195
+ session = response.cookies.get("session")
196
+ if project_id and session:
197
+ logging.info(f"Successfully registered account: {payload['email']}")
198
+ return {
199
+ "email": payload["email"],
200
+ "password": payload["password"],
201
+ "session_id": session,
202
+ "project_id": project_id,
203
+ "balance": 5.0,
204
+ }
205
+
206
+ logging.warning(f"Registration attempt failed with status {response.status_code}: {response.text}")
207
+
208
+ except requests.exceptions.ProxyError as e:
209
+ logging.warning(f"Proxy error during registration: {e}. Retrying with a new proxy...")
210
+ if self.proxy_pool and proxy_info:
211
+ self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
212
+
213
+ except requests.exceptions.RequestException as e:
214
+ logging.warning(f"Request exception during registration: {e}. Retrying...")
215
+ if self.proxy_pool and proxy_info:
216
+ self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
217
+
218
+ except Exception as e:
219
+ logging.error(f"An unexpected error occurred during registration: {e}. Retrying...")
220
+
221
+ time.sleep(1)
222
 
223
  def chat(
224
  self,