From cacca2ab9fa4418175d557321da31ae916e1b458 Mon Sep 17 00:00:00 2001 From: viginum-datalab <194823950+viginum-datalab@users.noreply.github.com> Date: Wed, 5 Mar 2025 16:43:34 +0100 Subject: [PATCH] clean: Lint code --- twscrape/accounts_pool.py | 4 ++-- twscrape/api.py | 8 ++++---- twscrape/models.py | 8 ++++---- twscrape/queue_client.py | 13 ++++++++----- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/twscrape/accounts_pool.py b/twscrape/accounts_pool.py index 905e8aa..8ba9f40 100644 --- a/twscrape/accounts_pool.py +++ b/twscrape/accounts_pool.py @@ -116,7 +116,7 @@ class AccountsPool: logger.warning("No usernames provided") return - qs = f"""DELETE FROM accounts WHERE username IN ({','.join([f'"{x}"' for x in usernames])})""" + qs = f"""DELETE FROM accounts WHERE username IN ({",".join([f'"{x}"' for x in usernames])})""" await execute(self._db_file, qs) async def delete_inactive(self): @@ -201,7 +201,7 @@ class AccountsPool: headers = json_object(), cookies = json_object(), user_agent = "{UserAgent().safari}" - WHERE username IN ({','.join([f'"{x}"' for x in usernames])}) + WHERE username IN ({",".join([f'"{x}"' for x in usernames])}) """ await execute(self._db_file, qs) diff --git a/twscrape/api.py b/twscrape/api.py index f57ab98..832b2f0 100644 --- a/twscrape/api.py +++ b/twscrape/api.py @@ -1,9 +1,9 @@ from contextlib import aclosing - -from httpx import Response from json import loads + +import zstd +from httpx import Response from typing_extensions import deprecated -from zstd import decompress from .accounts_pool import AccountsPool from .logger import set_log_level @@ -129,7 +129,7 @@ class API: return encoding: str | None = rep.headers.get("content-encoding") - obj = loads(decompress(rep.content)) if encoding == "zstd" else rep.json() + obj = loads(zstd.decompress(rep.content)) if encoding == "zstd" else rep.json() els = get_by_path(obj, "entries") or [] els = [ x diff --git a/twscrape/models.py b/twscrape/models.py index a4658ba..adf24ee 100644 --- a/twscrape/models.py +++ b/twscrape/models.py @@ -135,7 +135,7 @@ class User(JSONTrait): return User( id=int(obj["id_str"]), id_str=obj["id_str"], - url=f'https://x.com/{obj["screen_name"]}', + url=f"https://x.com/{obj['screen_name']}", username=obj["screen_name"], displayname=obj["name"], rawDescription=obj["description"], @@ -217,7 +217,7 @@ class Tweet(JSONTrait): rt_obj = get_or(res, f"tweets.{_first(obj, rt_id_path)}") qt_obj = get_or(res, f"tweets.{_first(obj, qt_id_path)}") - url = f'https://x.com/{tw_usr.username}/status/{obj["id_str"]}' + url = f"https://x.com/{tw_usr.username}/status/{obj['id_str']}" doc = Tweet( id=int(obj["id_str"]), id_str=obj["id_str"], @@ -507,8 +507,8 @@ def _parse_card(obj: dict, url: str): options = [] for x in range(20): - label = _parse_card_get_str(val, f"choice{x+1}_label") - votes = _parse_card_get_str(val, f"choice{x+1}_count") + label = _parse_card_get_str(val, f"choice{x + 1}_label") + votes = _parse_card_get_str(val, f"choice{x + 1}_count") if label is None or votes is None: break diff --git a/twscrape/queue_client.py b/twscrape/queue_client.py index baa89d3..4ab6ddb 100644 --- a/twscrape/queue_client.py +++ b/twscrape/queue_client.py @@ -1,9 +1,9 @@ -from json import JSONDecodeError, dumps, loads import os +from json import JSONDecodeError, dumps, loads from typing import Any -from zstd import decompress import httpx +import zstd from httpx import AsyncClient, Response from .accounts_pool import Account, AccountsPool @@ -58,7 +58,7 @@ def dump_rep(rep: Response): try: encoding: str | None = rep.headers.get("content-encoding") - obj = loads(decompress(rep.content)) if encoding == "zstd" else rep.json() + obj = loads(zstd.decompress(rep.content)) if encoding == "zstd" else rep.json() msg.append(dumps(obj, indent=2)) except JSONDecodeError: msg.append(rep.text) @@ -124,7 +124,7 @@ class QueueClient: try: encoding: str | None = rep.headers.get("content-encoding") - res = loads(decompress(rep.content)) if encoding == "zstd" else rep.json() + res = loads(zstd.decompress(rep.content)) if encoding == "zstd" else rep.json() except JSONDecodeError: res: Any = {"_raw": rep.text} @@ -134,7 +134,7 @@ class QueueClient: err_msg = "OK" if "errors" in res: - err_msg = set([f'({x.get("code", -1)}) {x["message"]}' for x in res["errors"]]) + err_msg = set([f"({x.get('code', -1)}) {x['message']}" for x in res["errors"]]) err_msg = "; ".join(list(err_msg)) log_msg = f"{rep.status_code:3d} - {req_id(rep)} - {err_msg}" @@ -235,6 +235,9 @@ class QueueClient: connection_retry += 1 if connection_retry >= 3: raise e + except zstd.Error as e: + logger.error(f"Failed to decompress ZSTD: {e}") + return except Exception as e: unknown_retry += 1 if unknown_retry >= 3: