зеркало из
https://github.com/viginum-datalab/twscrape.git
synced 2025-10-29 13:06:13 +02:00
feat: add processing of api error when code=200
Этот коммит содержится в:
родитель
42357d0233
Коммит
7866e44ade
@ -2,9 +2,8 @@ from httpx import Response
|
||||
|
||||
from .accounts_pool import AccountsPool
|
||||
from .constants import GQL_FEATURES, GQL_URL
|
||||
from .logger import logger
|
||||
from .models import Tweet, User
|
||||
from .queue_client import QueueClient, req_id
|
||||
from .queue_client import QueueClient
|
||||
from .utils import encode_params, find_obj, get_by_path, to_old_obj, to_old_rep
|
||||
|
||||
SEARCH_FEATURES = {
|
||||
|
||||
@ -76,6 +76,10 @@ async def main(args):
|
||||
await pool.load_from_file(args.file_path, args.line_format)
|
||||
return
|
||||
|
||||
if args.command == "del_accounts":
|
||||
await pool.delete_accounts(args.usernames)
|
||||
return
|
||||
|
||||
if args.command == "login_accounts":
|
||||
print(await pool.login_all())
|
||||
return
|
||||
|
||||
@ -25,6 +25,16 @@ class Ctx:
|
||||
self.req_count = 0
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(self, rep: httpx.Response, res: dict):
|
||||
self.rep = rep
|
||||
self.res = res
|
||||
self.errors = [x["message"] for x in res["errors"]]
|
||||
|
||||
def __str__(self):
|
||||
return f"ApiError ({self.rep.status_code}) {' ~ '.join(self.errors)}"
|
||||
|
||||
|
||||
class QueueClient:
|
||||
def __init__(self, pool: AccountsPool, queue: str, debug=False):
|
||||
self.pool = pool
|
||||
@ -98,7 +108,12 @@ class QueueClient:
|
||||
rep = await ctx.clt.request(method, url, params=params)
|
||||
setattr(rep, "__username", ctx.acc.username)
|
||||
self._push_history(rep)
|
||||
|
||||
rep.raise_for_status()
|
||||
res = rep.json()
|
||||
if "errors" in res:
|
||||
raise ApiError(rep, res)
|
||||
|
||||
ctx.req_count += 1 # count only successful
|
||||
retry_count = 0
|
||||
return rep
|
||||
@ -125,11 +140,15 @@ class QueueClient:
|
||||
|
||||
else:
|
||||
known_code = False
|
||||
logger.debug(f"HTTP Error {rep.status_code} {e.request.url}\n{rep.text}")
|
||||
logger.warning(f"HTTP Error {rep.status_code} {e.request.url}\n{rep.text}")
|
||||
|
||||
await self._close_ctx(reset_ts)
|
||||
if not known_code:
|
||||
raise e
|
||||
except ApiError as e:
|
||||
reset_ts = utc_ts() + 60 * 60 * 4 # 4 hours
|
||||
await self._close_ctx(reset_ts)
|
||||
logger.warning(e)
|
||||
except Exception as e:
|
||||
logger.warning(f"Unknown error, retrying. Err ({type(e)}): {str(e)}")
|
||||
retry_count += 1
|
||||
|
||||
Загрузка…
x
Ссылка в новой задаче
Block a user