From 17ee6bd96e79af8371d6a3dc9e9ab4200b318cac Mon Sep 17 00:00:00 2001 From: novenary Date: Tue, 9 May 2023 15:24:59 +0300 Subject: [PATCH] feat: `--auth-token` for authenticated requests --- twint/cli.py | 2 ++ twint/config.py | 1 + twint/get.py | 12 +++++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/twint/cli.py b/twint/cli.py index 9833265..19e1688 100644 --- a/twint/cli.py +++ b/twint/cli.py @@ -123,6 +123,7 @@ def initialize(args): c.Media = args.media c.Replies = args.replies c.Pandas_clean = args.pandas_clean + c.Auth_token = args.auth_token c.Proxy_host = args.proxy_host c.Proxy_port = args.proxy_port c.Proxy_type = args.proxy_type @@ -190,6 +191,7 @@ def options(): ap.add_argument("--followers", help="Scrape a person's followers.", action="store_true") ap.add_argument("--following", help="Scrape a person's follows", action="store_true") ap.add_argument("--favorites", help="Scrape Tweets a user has liked.", action="store_true") + ap.add_argument("--auth-token", help="Twitter login cookie.") ap.add_argument("--proxy-type", help="Socks5, HTTP, etc.") ap.add_argument("--proxy-host", help="Proxy hostname or IP.") ap.add_argument("--proxy-port", help="The port of the proxy server.") diff --git a/twint/config.py b/twint/config.py index 48bcc36..bfc1337 100644 --- a/twint/config.py +++ b/twint/config.py @@ -60,6 +60,7 @@ class Config: Pandas_clean: bool = True Lowercase: bool = True Pandas_au: bool = True + Auth_token: str = "" Proxy_host: str = "" Proxy_port: int = 0 Proxy_type: object = None diff --git a/twint/get.py b/twint/get.py index 006cd74..2f1f7f8 100644 --- a/twint/get.py +++ b/twint/get.py @@ -111,9 +111,13 @@ async def RequestUrl(config, init): _connector = get_connector(config) _serialQuery = "" params = [] + cookies = {} _url = "" _headers = [("authorization", config.Bearer_token), ("x-guest-token", config.Guest_token)] + if config.Auth_token: + cookies["auth_token"] = config.Auth_token + # TODO : do this later if config.Profile: logme.debug(__name__ + ':RequestUrl:Profile') @@ -133,7 +137,7 @@ async def RequestUrl(config, init): _url = await url.Favorites(config.Username, init) _serialQuery = _url - response = await Request(_url, params=params, connector=_connector, headers=_headers) + response = await Request(_url, params=params, connector=_connector, headers=_headers, cookies=cookies) if config.Debug: print(_serialQuery, file=open("twint-request_urls.log", "a", encoding="utf-8")) @@ -156,9 +160,9 @@ def ForceNewTorIdentity(config): sys.stderr.write('If you want to rotate Tor ports automatically - enable Tor control port\n') -async def Request(_url, connector=None, params=None, headers=None): +async def Request(_url, connector=None, params=None, headers=None, cookies=None): logme.debug(__name__ + ':Request:Connector') - async with aiohttp.ClientSession(connector=connector, headers=headers) as session: + async with aiohttp.ClientSession(connector=connector, headers=headers, cookies=cookies) as session: return await Response(session, _url, params) @@ -173,6 +177,8 @@ async def Response(session, _url, params=None): resp = await response.text() if response.status == 429: # 429 implies Too many requests i.e. Rate Limit Exceeded raise TokenExpiryException(loads(resp)['errors'][0]['message']) + if response.status == 403: + raise ConnectionError("Access forbidden, try passing --auth-token.") return resp except aiohttp.client_exceptions.ClientConnectorError as exc: if attempt < retries: