-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_trends_twarc1.py
36 lines (32 loc) · 1.06 KB
/
get_trends_twarc1.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
import os
import twarc
import json
from dotenv import load_dotenv
load_dotenv()
consumer_key = os.environ.get("CONSUMER_KEY")
consumer_secret = os.environ.get("CONSUMER_SECRET")
access_token = os.environ.get("ACCESS_TOKEN")
access_token_secret = os.environ.get("ACCESS_TOKEN_SECRET")
client1 = twarc.Twarc(consumer_key, consumer_secret,
access_token, access_token_secret)
location_codes = {
'Global': 1,
'US': 23424977,
'Sydney': 1105779
}
chosen_location = 'Sydney'
trends = client1.trends_place(location_codes[chosen_location])[0]["trends"]
for i in range(len(trends)):
if trends[i]["tweet_volume"] is None:
trends[i]["tweet_volume"] = 0
trends.sort(key=lambda trend: -trend["tweet_volume"])
data = {'data': trends}
serialized_json = json.dumps(data,
indent=4,
sort_keys=True)
print(serialized_json)
filename = input("Filename to output to: ")
if filename == "":
filename = "trends_cache"
with open(f'outputs/{filename}.json', 'w') as f:
f.writelines(serialized_json)