-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake_test_tweet_jsons.py
41 lines (36 loc) · 1.44 KB
/
make_test_tweet_jsons.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
import argparse
import json
import os
from collector import Connection, Collector
from tweepy.error import TweepError
parser = argparse.ArgumentParser(description='SparseTwitter user_details Downloader')
parser.add_argument('-s', '--seed',
help='''Provide a seed (=Twitter user ID). Its friends details will
be downloaded.''',
required=False,
type=int,
default=1670174994)
# Setup the Collector
seed = parser.parse_args().seed # Swap seeds with another Twitter User ID if you like
if seed == 1670174994:
print("No seed given. Using default seed " + str(seed) + ".")
else:
print("Downloading and saving friends' details of user " + str(seed) + ".")
con = Connection()
collector = Collector(con, seed)
# Get the friends and details of the specified seed
try:
friends = collector.get_friend_list()
except TweepError as e:
if "'code': 34" in e.reason:
raise TweepError("The seed you have given is not a valid Twitter user ID")
friends_details = collector.get_details(friends)
# Check for the relevant directory
if not os.path.isdir(os.path.join("tests", "tweet_jsons")):
os.mkdir(os.path.join("tests", "tweet_jsons"))
# Write details in json files
ct = 1
for friend_details in friends_details:
with open(os.path.join("tests", "tweet_jsons", "user_" + str(ct) + ".json"), "w") as f:
json.dump(friend_details._json, f)
ct += 1