Skip to content

Commit

Permalink
fix #107, v0.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Jan 8, 2024
1 parent d4a65c1 commit 32f83ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "twscrape"
version = "0.10.0"
version = "0.10.1"
authors = [{name = "vladkens", email = "[email protected]"}]
description = "Twitter GraphQL and Search API implementation with SNScrape data models"
readme = "readme.md"
Expand Down
48 changes: 24 additions & 24 deletions twscrape/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,27 @@ async def login(acc: Account, cfg: LoginConfig | None = None) -> Account:
if cfg.email_first and not cfg.manual:
imap = await imap_login(acc.email, acc.email_password)

client = acc.make_client()
guest_token = await get_guest_token(client)
client.headers["x-guest-token"] = guest_token

rep = await login_initiate(client)
ctx = TaskCtx(client, acc, cfg, None, imap)
while True:
if not rep:
break

try:
rep = await next_login_task(ctx, rep)
except HTTPStatusError as e:
if e.response.status_code == 403:
logger.error(f"403 error {log_id}")
return acc

client.headers["x-csrf-token"] = client.cookies["ct0"]
client.headers["x-twitter-auth-type"] = "OAuth2Session"

acc.active = True
acc.headers = {k: v for k, v in client.headers.items()}
acc.cookies = {k: v for k, v in client.cookies.items()}
return acc
async with acc.make_client() as client:
guest_token = await get_guest_token(client)
client.headers["x-guest-token"] = guest_token

rep = await login_initiate(client)
ctx = TaskCtx(client, acc, cfg, None, imap)
while True:
if not rep:
break

try:
rep = await next_login_task(ctx, rep)
except HTTPStatusError as e:
if e.response.status_code == 403:
logger.error(f"403 error {log_id}")
return acc

client.headers["x-csrf-token"] = client.cookies["ct0"]
client.headers["x-twitter-auth-type"] = "OAuth2Session"

acc.active = True
acc.headers = {k: v for k, v in client.headers.items()}
acc.cookies = {k: v for k, v in client.cookies.items()}
return acc
18 changes: 9 additions & 9 deletions twscrape/queue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from typing import Any

import httpx
from httpx import AsyncClient, HTTPStatusError, ProxyError, ReadTimeout, Response

from .accounts_pool import Account, AccountsPool
from .logger import logger
Expand All @@ -13,7 +13,7 @@


class Ctx:
def __init__(self, acc: Account, clt: httpx.AsyncClient):
def __init__(self, acc: Account, clt: AsyncClient):
self.acc = acc
self.clt = clt
self.req_count = 0
Expand All @@ -27,7 +27,7 @@ class AbortReqError(Exception):
pass


def req_id(rep: httpx.Response):
def req_id(rep: Response):
lr = str(rep.headers.get("x-rate-limit-remaining", -1))
ll = str(rep.headers.get("x-rate-limit-limit", -1))
sz = max(len(lr), len(ll))
Expand All @@ -37,7 +37,7 @@ def req_id(rep: httpx.Response):
return f"{lr}/{ll} - {username}"


def dump_rep(rep: httpx.Response):
def dump_rep(rep: Response):
count = getattr(dump_rep, "__count", -1) + 1
setattr(dump_rep, "__count", count)

Expand Down Expand Up @@ -108,7 +108,7 @@ async def _get_ctx(self):
self.ctx = Ctx(acc, clt)
return self.ctx

async def _check_rep(self, rep: httpx.Response) -> None:
async def _check_rep(self, rep: Response) -> None:
"""
This function can raise Exception and request will be retried or aborted
Or if None is returned, response will passed to api parser as is
Expand Down Expand Up @@ -186,18 +186,18 @@ async def _check_rep(self, rep: httpx.Response) -> None:

try:
rep.raise_for_status()
except httpx.HTTPStatusError:
except HTTPStatusError:
logger.error(f"Unhandled API response code: {log_msg}")
await self._close_ctx(utc.ts() + 60 * 15) # 15 minutes
raise HandledError()

async def get(self, url: str, params: ReqParams = None):
return await self.req("GET", url, params=params)

async def req(self, method: str, url: str, params: ReqParams = None) -> httpx.Response | None:
async def req(self, method: str, url: str, params: ReqParams = None) -> Response | None:
retry_count = 0
while True:
ctx = await self._get_ctx()
ctx = await self._get_ctx() # not need to close client, class implements __aexit__
if ctx is None:
return None

Expand All @@ -215,7 +215,7 @@ async def req(self, method: str, url: str, params: ReqParams = None) -> httpx.Re
except HandledError:
# retry with new account
continue
except (httpx.ReadTimeout, httpx.ProxyError):
except (ReadTimeout, ProxyError):
# http transport failed, just retry with same account
continue
except Exception as e:
Expand Down

0 comments on commit 32f83ab

Please sign in to comment.