-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.py
executable file
·63 lines (47 loc) · 1.56 KB
/
twitter.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
63
#!/usr/local/bin/python
import sys, getopt
import string
from twython import Twython
import json
def main(argv):
username = ''
outputfile = 'profile.json'
try:
opts, args = getopt.getopt(sys.argv[1:],"hu:o:",["help","username=","ofile="])
except getopt.GetoptError:
print '[usage] twitter.py -u <username> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print '[usage] twitter.py -u <username> -o <outputfile>'
sys.exit()
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
else:
assert False, "unhandled option"
# my keys to use twitter API
key = 'sYrvrdr26JPhw6kTCiGMd0sgl';
sec_key = 'Zx27S0BZAlGlUu0Dk2w9FFmlbRmcFFLd7RXZu3de6fM2IZp0bw';
profile = {}
tweets = []
twitter = Twython(key,
sec_key,oauth_version=2)
Access_token = twitter.obtain_access_token()
t = Twython(key, access_token=Access_token)
user_timeline = t.search(q=username, count=100)
for tweet in user_timeline['statuses']:
item = {}
item["content"] = tweet['text']
tweets.append(item.copy())
profile['contentItems'] = tweets
if (len(profile['contentItems']) == 0):
print '[*] invalid username'
return 2
f = open(outputfile, 'w')
f.write( json.dumps(profile, ensure_ascii=True) )
f.close()
print username+".json created with", "numTweets = ", len(tweets)
if __name__ == "__main__":
main(sys.argv[1:])