Skip to content

Commit

Permalink
feat: --auth-token for authenticated requests
Browse files Browse the repository at this point in the history
  • Loading branch information
9ary authored and bb010g committed May 10, 2023
1 parent 2022b6a commit 17ee6bd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions twint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
1 change: 1 addition & 0 deletions twint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions twint/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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"))
Expand All @@ -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)


Expand All @@ -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:
Expand Down

0 comments on commit 17ee6bd

Please sign in to comment.