-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectUsers.py
42 lines (38 loc) · 1.26 KB
/
CollectUsers.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
import json
import time
from pathlib import Path
import tweepy
path = Path('C:/Data/Python/JobLoss')
mentions = []
with open(path / 'Processed.json') as f:
data = json.load(f)
for tweet in data:
mentions.append(tweet['mentions'])
# loaded = []
# with open(path / 'Users.json', encoding='utf-8') as f:
# for line in f:
# loaded.append(json.loads(line.rstrip('\n|\r')))
def get_users(start_ind):
with open(path / 'Auth.json') as f:
auth_tokens = json.load(f)
auth = tweepy.OAuthHandler(auth_tokens[0], auth_tokens[1])
auth.set_access_token(auth_tokens[2], auth_tokens[3])
api = tweepy.API(auth)
for ind in range(start_ind, len(mentions)):
tweet_mentions = mentions[ind]
tweet_users = []
for user_id in tweet_mentions:
time.sleep(1)
try:
user = api.get_user(user_id)
tweet_users.append(user._json)
except tweepy.error.TweepError as e:
print(e)
with open(path / 'Users.jsonl', 'a') as f:
f.write(json.dumps(tweet_users) + '\n')
print(ind)
if __name__ == '__main__':
start_time = time.perf_counter()
get_users(0)
time_elapsed = time.perf_counter() - start_time
print(time_elapsed)