-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_utils.py
56 lines (34 loc) · 1.27 KB
/
twitter_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from dotenv import load_dotenv
load_dotenv()
bearer_token = os.getenv("TOKEN")
import sys
from dotenv import load_dotenv
load_dotenv()
USER = os.getenv("USER")
import requests
sys.path.append(f"/Users/{USER}/Python/metaurl")
from get.soup import without_js_rendering, with_js_rendering
def url_from_twitter(handle):
global bearer_token
if handle.startswith('https://www.twitter.com/'):
handle = handle.replace('https://www.twitter.com/', '')
handle = handle.replace('@', '')
print(f"\nGetting website for {handle}")
search_url = f"https://api.twitter.com/2/users/by/username/{handle}"
headers = {"Authorization": f"Bearer {bearer_token}"}
query_params = {'user.fields': 'entities'}
response = requests.get(search_url, headers=headers, params=query_params)
if response.status_code == 200:
endpoint_response = response.json()
try:
user = endpoint_response["data"]
website_url = user["entities"]["url"]["urls"][0]["expanded_url"]
print(website_url)
return website_url
except KeyError:
return None
else:
print(f"Error: {response.status_code}, {response.json()}")
test = 'https://www.twitter.com/thebpf'
url_from_twitter(test)