зеркало из
https://github.com/viginum-datalab/twscrape.git
synced 2025-10-29 13:06:13 +02:00
clean: Lint code
Этот коммит содержится в:
родитель
b590d46525
Коммит
cacca2ab9f
@ -116,7 +116,7 @@ class AccountsPool:
|
|||||||
logger.warning("No usernames provided")
|
logger.warning("No usernames provided")
|
||||||
return
|
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)
|
await execute(self._db_file, qs)
|
||||||
|
|
||||||
async def delete_inactive(self):
|
async def delete_inactive(self):
|
||||||
@ -201,7 +201,7 @@ class AccountsPool:
|
|||||||
headers = json_object(),
|
headers = json_object(),
|
||||||
cookies = json_object(),
|
cookies = json_object(),
|
||||||
user_agent = "{UserAgent().safari}"
|
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)
|
await execute(self._db_file, qs)
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
from contextlib import aclosing
|
from contextlib import aclosing
|
||||||
|
|
||||||
from httpx import Response
|
|
||||||
from json import loads
|
from json import loads
|
||||||
|
|
||||||
|
import zstd
|
||||||
|
from httpx import Response
|
||||||
from typing_extensions import deprecated
|
from typing_extensions import deprecated
|
||||||
from zstd import decompress
|
|
||||||
|
|
||||||
from .accounts_pool import AccountsPool
|
from .accounts_pool import AccountsPool
|
||||||
from .logger import set_log_level
|
from .logger import set_log_level
|
||||||
@ -129,7 +129,7 @@ class API:
|
|||||||
return
|
return
|
||||||
|
|
||||||
encoding: str | None = rep.headers.get("content-encoding")
|
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 = get_by_path(obj, "entries") or []
|
||||||
els = [
|
els = [
|
||||||
x
|
x
|
||||||
|
|||||||
@ -135,7 +135,7 @@ class User(JSONTrait):
|
|||||||
return User(
|
return User(
|
||||||
id=int(obj["id_str"]),
|
id=int(obj["id_str"]),
|
||||||
id_str=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"],
|
username=obj["screen_name"],
|
||||||
displayname=obj["name"],
|
displayname=obj["name"],
|
||||||
rawDescription=obj["description"],
|
rawDescription=obj["description"],
|
||||||
@ -217,7 +217,7 @@ class Tweet(JSONTrait):
|
|||||||
rt_obj = get_or(res, f"tweets.{_first(obj, rt_id_path)}")
|
rt_obj = get_or(res, f"tweets.{_first(obj, rt_id_path)}")
|
||||||
qt_obj = get_or(res, f"tweets.{_first(obj, qt_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(
|
doc = Tweet(
|
||||||
id=int(obj["id_str"]),
|
id=int(obj["id_str"]),
|
||||||
id_str=obj["id_str"],
|
id_str=obj["id_str"],
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
from json import JSONDecodeError, dumps, loads
|
|
||||||
import os
|
import os
|
||||||
|
from json import JSONDecodeError, dumps, loads
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from zstd import decompress
|
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
import zstd
|
||||||
from httpx import AsyncClient, Response
|
from httpx import AsyncClient, Response
|
||||||
|
|
||||||
from .accounts_pool import Account, AccountsPool
|
from .accounts_pool import Account, AccountsPool
|
||||||
@ -58,7 +58,7 @@ def dump_rep(rep: Response):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
encoding: str | None = rep.headers.get("content-encoding")
|
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))
|
msg.append(dumps(obj, indent=2))
|
||||||
except JSONDecodeError:
|
except JSONDecodeError:
|
||||||
msg.append(rep.text)
|
msg.append(rep.text)
|
||||||
@ -124,7 +124,7 @@ class QueueClient:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
encoding: str | None = rep.headers.get("content-encoding")
|
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:
|
except JSONDecodeError:
|
||||||
res: Any = {"_raw": rep.text}
|
res: Any = {"_raw": rep.text}
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ class QueueClient:
|
|||||||
|
|
||||||
err_msg = "OK"
|
err_msg = "OK"
|
||||||
if "errors" in res:
|
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))
|
err_msg = "; ".join(list(err_msg))
|
||||||
|
|
||||||
log_msg = f"{rep.status_code:3d} - {req_id(rep)} - {err_msg}"
|
log_msg = f"{rep.status_code:3d} - {req_id(rep)} - {err_msg}"
|
||||||
@ -235,6 +235,9 @@ class QueueClient:
|
|||||||
connection_retry += 1
|
connection_retry += 1
|
||||||
if connection_retry >= 3:
|
if connection_retry >= 3:
|
||||||
raise e
|
raise e
|
||||||
|
except zstd.Error as e:
|
||||||
|
logger.error(f"Failed to decompress ZSTD: {e}")
|
||||||
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
unknown_retry += 1
|
unknown_retry += 1
|
||||||
if unknown_retry >= 3:
|
if unknown_retry >= 3:
|
||||||
|
|||||||
Загрузка…
x
Ссылка в новой задаче
Block a user