Skip to content

Commit

Permalink
fix(twitter): fix get user ID from user screen name due to API shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelei8291 committed Jan 22, 2023
1 parent 4db855a commit 7069fee
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions twspace_dl/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import requests

AUTH_HEADER = {
"authorization": (
"Bearer "
"AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs"
"=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
)
}


def guest_token() -> str:
"""Generate a guest token to authorize twitter api requests"""
headers = {
"authorization": (
"Bearer "
"AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs"
"=1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
)
}
response = requests.post(
"https://api.twitter.com/1.1/guest/activate.json", headers=headers, timeout=30
"https://api.twitter.com/1.1/guest/activate.json", headers=AUTH_HEADER, timeout=30
).json()
token = response["guest_token"]
if not token:
Expand All @@ -24,12 +25,27 @@ def guest_token() -> str:
def user_id(user_url: str) -> str:
"""Get the id of a twitter using the url linking to their account"""
screen_name = re.findall(r"(?<=twitter.com/)\w*", user_url)[0]
params = {"screen_names": screen_name}
params = {
"variables": (
"{"
f'"screen_name":"{screen_name}",'
'"withSafetyModeUserFields":true,'
'"withSuperFollowsUserFields":true'
"}"
),
"features": (
"{"
'"responsive_web_twitter_blue_verified_badge_is_enabled":true,'
'"verified_phone_label_enabled":false,'
'"responsive_web_graphql_timeline_navigation_enabled":true'
"}"
)
}
response = requests.get(
"https://cdn.syndication.twimg.com/widgets/followbutton/info.json",
"https://api.twitter.com/graphql/hVhfo_TquFTmgL7gYwf91Q/UserByScreenName",
params=params,
headers=AUTH_HEADER | {"x-guest-token": guest_token()},
timeout=30,
)
user_data = response.json()
usr_id = user_data[0]["id"]
).json()
usr_id = response["data"]["user"]["result"]["rest_id"]
return usr_id

0 comments on commit 7069fee

Please sign in to comment.