-
Notifications
You must be signed in to change notification settings - Fork 0
/
getTweets.py
62 lines (55 loc) · 1.78 KB
/
getTweets.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
57
58
59
60
61
62
import tweepy
import tweet_dumper
import sys
import re
import json
# looks for json file with the proper credentials for the user
# {
# "consumer_key" : "value",
# "consumer_secret" : "value",
# "access_token" : "value",
# "access_token_secret" : "value"
# }
def get_credentials(filename):
with open(filename) as file:
cfg = json.load(file)
return cfg
def get_api(cfg):
auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
return tweepy.API(auth)
def main():
try:
users = 1
usernames = []
amount = 0
place_in_named_file = False
for i in range(len(sys.argv)):
try:
# if I can cast to integer, interpret
# as amount
amount = int(sys.argv[users])
break;
except:
# otherwise, interpret as twitterhandle
usernames.append(sys.argv[users])
users+=1
if sys.argv[-2] == "-f":
named_file = sys.argv[-1]
place_in_named_file = True
if len(usernames) == 0 or amount == 0:
raise IndexError
except IndexError as e:
print "Usage: python getTweets.py username [username...] amount [-f filename]"
sys.exit(1)
# Fill in the values noted in previous step here
cfg = get_credentials("./authentication_files/the_Tmonster.json")
api = get_api(cfg)
for handle in usernames:
user = api.get_user(handle)
if place_in_named_file:
tweet_dumper.get_all_tweets4args(handle, api, amount, named_file)
else:
tweet_dumper.get_all_tweets3args(handle, api, amount)
if __name__ == "__main__":
main()