Skip to content

Commit 32f83ab

Browse files
committed
fix #107, v0.10.1
1 parent d4a65c1 commit 32f83ab

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "twscrape"
7-
version = "0.10.0"
7+
version = "0.10.1"
88
authors = [{name = "vladkens", email = "v.pronsky@gmail.com"}]
99
description = "Twitter GraphQL and Search API implementation with SNScrape data models"
1010
readme = "readme.md"

twscrape/login.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,27 @@ async def login(acc: Account, cfg: LoginConfig | None = None) -> Account:
217217
if cfg.email_first and not cfg.manual:
218218
imap = await imap_login(acc.email, acc.email_password)
219219

220-
client = acc.make_client()
221-
guest_token = await get_guest_token(client)
222-
client.headers["x-guest-token"] = guest_token
223-
224-
rep = await login_initiate(client)
225-
ctx = TaskCtx(client, acc, cfg, None, imap)
226-
while True:
227-
if not rep:
228-
break
229-
230-
try:
231-
rep = await next_login_task(ctx, rep)
232-
except HTTPStatusError as e:
233-
if e.response.status_code == 403:
234-
logger.error(f"403 error {log_id}")
235-
return acc
236-
237-
client.headers["x-csrf-token"] = client.cookies["ct0"]
238-
client.headers["x-twitter-auth-type"] = "OAuth2Session"
239-
240-
acc.active = True
241-
acc.headers = {k: v for k, v in client.headers.items()}
242-
acc.cookies = {k: v for k, v in client.cookies.items()}
243-
return acc
220+
async with acc.make_client() as client:
221+
guest_token = await get_guest_token(client)
222+
client.headers["x-guest-token"] = guest_token
223+
224+
rep = await login_initiate(client)
225+
ctx = TaskCtx(client, acc, cfg, None, imap)
226+
while True:
227+
if not rep:
228+
break
229+
230+
try:
231+
rep = await next_login_task(ctx, rep)
232+
except HTTPStatusError as e:
233+
if e.response.status_code == 403:
234+
logger.error(f"403 error {log_id}")
235+
return acc
236+
237+
client.headers["x-csrf-token"] = client.cookies["ct0"]
238+
client.headers["x-twitter-auth-type"] = "OAuth2Session"
239+
240+
acc.active = True
241+
acc.headers = {k: v for k, v in client.headers.items()}
242+
acc.cookies = {k: v for k, v in client.cookies.items()}
243+
return acc

twscrape/queue_client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from typing import Any
44

5-
import httpx
5+
from httpx import AsyncClient, HTTPStatusError, ProxyError, ReadTimeout, Response
66

77
from .accounts_pool import Account, AccountsPool
88
from .logger import logger
@@ -13,7 +13,7 @@
1313

1414

1515
class Ctx:
16-
def __init__(self, acc: Account, clt: httpx.AsyncClient):
16+
def __init__(self, acc: Account, clt: AsyncClient):
1717
self.acc = acc
1818
self.clt = clt
1919
self.req_count = 0
@@ -27,7 +27,7 @@ class AbortReqError(Exception):
2727
pass
2828

2929

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

3939

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

@@ -108,7 +108,7 @@ async def _get_ctx(self):
108108
self.ctx = Ctx(acc, clt)
109109
return self.ctx
110110

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

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

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

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

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

0 commit comments

Comments
 (0)