From 7866e44ade4ec8507ae0f658355ad2af2110e3fa Mon Sep 17 00:00:00 2001 From: Vlad Pronsky Date: Mon, 10 Jul 2023 23:34:07 +0300 Subject: [PATCH] feat: add processing of api error when code=200 --- twscrape/api.py | 3 +-- twscrape/cli.py | 4 ++++ twscrape/queue_client.py | 21 ++++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/twscrape/api.py b/twscrape/api.py index 52eb02d..a9411bb 100644 --- a/twscrape/api.py +++ b/twscrape/api.py @@ -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 = { diff --git a/twscrape/cli.py b/twscrape/cli.py index 6b58292..a2bdbcd 100644 --- a/twscrape/cli.py +++ b/twscrape/cli.py @@ -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 diff --git a/twscrape/queue_client.py b/twscrape/queue_client.py index 70aec63..fa59767 100644 --- a/twscrape/queue_client.py +++ b/twscrape/queue_client.py @@ -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