XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

update(quora/api): Modified a request_with_retries function. Added delay between retries (maybe it's better to wrap it in a context manager). Error logging. Function renamed. If not 200 status - raise_for_status() exception

12a639fb
valerii@valeriis-air.(none) <valery.chirkov99@gmail.com>
提交于

代码差异

1 个文件 +21 -14
Modified gpt4free/quora/api.py +21 -14
@@ -56,18 +56,25 @@ def generate_payload(query_name, variables):
56 56 return {"query": queries[query_name], "variables": variables}
57 57
58 58
59 def request_with_retries(method, *args, **kwargs):
60 attempts = kwargs.get("attempts") or 10
59 def retry_request(method, *args, **kwargs):
60 """Retry a request with 10 attempts by default, delay increases exponentially"""
61 max_attempts: int = kwargs.pop("max_attempts", 10)
62 delay = kwargs.pop("delay", 1)
61 63 url = args[0]
62 for i in range(attempts):
63 r = method(*args, **kwargs)
64 if r.status_code == 200:
65 return r
66 logger.warn(
67 f"Server returned a status code of {r.status_code} while downloading {url}. Retrying ({i + 1}/{attempts})..."
68 )
69 64
70 raise RuntimeError(f"Failed to download {url} too many times.")
65 for attempt in range(1, max_attempts + 1):
66 try:
67 response = method(*args, **kwargs)
68 response.raise_for_status()
69 return response
70 except Exception as error:
71 logger.warning(
72 f"Attempt {attempt}/{max_attempts} failed with error: {error}. "
73 f"Retrying in {delay} seconds..."
74 )
75 time.sleep(delay)
76 delay *= 2
77 raise RuntimeError(f"Failed to download {url} after {max_attempts} attempts.")
71 78
72 79
73 80 class Client:
@@ -134,7 +141,7 @@ class Client:
134 141 def get_next_data(self, overwrite_vars=False):
135 142 logger.info("Downloading next_data...")
136 143
137 r = request_with_retries(self.session.get, self.home_url)
144 r = retry_request(self.session.get, self.home_url)
138 145 json_regex = r'<script id="__NEXT_DATA__" type="application\/json">(.+?)</script>'
139 146 json_text = re.search(json_regex, r.text).group(1)
140 147 next_data = json.loads(json_text)
@@ -149,7 +156,7 @@ class Client:
149 156 def get_bot(self, display_name):
150 157 url = f'https://poe.com/_next/data/{self.next_data["buildId"]}/{display_name}.json'
151 158
152 r = request_with_retries(self.session.get, url)
159 r = retry_request(self.session.get, url)
153 160
154 161 chat_data = r.json()["pageProps"]["payload"]["chatOfBotDisplayName"]
155 162 return chat_data
@@ -198,7 +205,7 @@ class Client:
198 205
199 206 def get_channel_data(self, channel=None):
200 207 logger.info("Downloading channel data...")
201 r = request_with_retries(self.session.get, self.settings_url)
208 r = retry_request(self.session.get, self.settings_url)
202 209 data = r.json()
203 210
204 211 return data["tchannelData"]
@@ -222,7 +229,7 @@ class Client:
222 229 }
223 230 headers = {**self.gql_headers, **headers}
224 231
225 r = request_with_retries(self.session.post, self.gql_url, data=payload, headers=headers)
232 r = retry_request(self.session.post, self.gql_url, data=payload, headers=headers)
226 233
227 234 data = r.json()
228 235 if data["data"] is None: