-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.py
More file actions
58 lines (44 loc) · 1.72 KB
/
Source.py
File metadata and controls
58 lines (44 loc) · 1.72 KB
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
from twython import Twython
import pickle
import os
def create_file(filename, query):
"""This function creates a new pickle file containing a list of tweets
resulting from the input query.
filename - This is the name of the file you want to use to save the tweets in
query - This is the twitter search query to use to search for tweets.
"""
# Replace the following strings with your own keys and secrets
TOKEN = '979038392083197952-NgdBxjQVFX1tK1HR8GH5E3Za5XpxFWP'
TOKEN_SECRET = 'NKJUnbWIlhSmpIHMnxuIFgAI447yfi2ON7SFx7ClG2dXP'
CONSUMER_KEY = '11OsLJ0nhOvmZPmDkYVRdD8mI'
CONSUMER_SECRET = 'JXTBOCm7Ub0eBTEFaynXrjhbFAQrLyANfzIL0dVL8M2GvvqOJ2'
t = Twython(CONSUMER_KEY, CONSUMER_SECRET, TOKEN, TOKEN_SECRET)
result = t.search(q=query, count=100)
tweets = list()
for status in result['statuses']:
tweets.append(status['text'])
filename += '.pickle'
if not os.path.exists(filename):
f = open(filename,'wb')
pickle.dump(tweets,f)
f.close()
print('File created as {}.' .format(filename))
else:
prompt = input("File {} exists. Replace existing? (Yes/No):" .format(filename))
if prompt.lower() == 'yes':
f = open(filename,'wb')
pickle.dump(tweets,f)
f.close()
print('File replaced as {}.' .format(filename))
elif prompt.lower() == 'no':
print('No File Created.')
def open_file(filename):
input_file = open(filename,'rb')
tweets = pickle.load(input_file)
input_file.close()
return tweets
def main():
create_file('elontweets', '@elonmusk')
create_file('trumptweets', '@realDonaldTrump')
if __name__ == '__main__':
main()