Repository navigation
Expand file tree
/
Copy pathtweepy_data_dump.py
More file actions
60 lines (51 loc) · 2.44 KB
/
Copy pathtweepy_data_dump.py
File metadata and controls
60 lines (51 loc) · 2.44 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
59
60
import tweepy
import json
#Variables that contains the user credentials to access Twitter API
access_token = "Fill in Access Token"
access_token_secret = "Fill in Access Secret"
consumer_key = "Fill in Consumer Key"
consumer_secret = "Fill in Consumer Secret"
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
try:
self.process(status._json)
except KeyError as e:
print ("KeyError: ", e)
def on_error(self, status_code, data):
print ("Error: ", status_code, ": ", data)
# Want to stop trying to get data because of the error?
# Uncomment the next line!
if status_code == 420: # Rate limited!
return False
def process(self, data):
keywords = ["music", "food", "sport", "show", "movie", "car", "commercial", "party", "war", "hello"]
content = data['text'].lower()
print("+++++")
if any(x in content for x in keywords):
if (data['coordinates'] is not None) and (('lang' not in data) or (data['lang']=='en')): # data[coordinates] may be null, want to filter it out
print("------")
print(data['text'])
print(data['coordinates'])
print(data['created_at'])
print(data['timestamp_ms'])
print(data['created_at'])
print(data['user']['name'])
print(data['user']['screen_name'])
print("------")
tweet_dict = {'text': data['text'],
'coordinates': data['coordinates']['coordinates'],
'created_at': data['created_at'],
'timestamp_ms': data['timestamp_ms'],
'user_name': data['user']['name'],
'user_screen_name': data['user']['screen_name']}
with open('tweets.txt', 'a') as outfile:
json.dump(tweet_dict, outfile)
outfile.write('\n')
if __name__ == '__main__':
myStreamListener = MyStreamListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
myStream = tweepy.Stream(auth, myStreamListener)
keywords = ["music", "food", "sport", "show", "movie", "car", "commercial", "party", "war", "hello"]
locations = [-180,-90,180,90]
myStream.filter(track=keywords, locations=locations)