-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtamuhack.py
62 lines (49 loc) · 2.24 KB
/
tamuhack.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
#time, location
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from geopy.geocoders import Nominatim
import http.client
access_token = "937000559843987463-DgmKxkTMnHmwRqFNKPISh4OWcupFRtH"
access_token_secret = "VYJI7K4QMd65hUg2bfoNoFDSDdG3c43vPjUSZ8Rysox1a"
consumer_key = "Vlqd8jWbRFEQx3wCLlGvkGz3E"
consumer_key_secret = "BoTIT6r6QORMCcamSyvzgS9aJ6zlDL8GUaMPqxzUQzPQYC0VMX"
disasters = ('flood', 'earthquake', 'wildfire', 'tornado', 'hurricane', 'tsunami', 'avalanche', 'wild fire', 'forest fire')
class StdOutListener(StreamListener):
def on_status(self, status):
tweet = status.text
add = True
if (status.coordinates or status.place) and ((status.coordinates is not None) or (status.place is not None)):
geolocator = Nominatim(scheme='http', timeout = 10)
if status.place:
loc = geolocator.geocode(status.place.full_name)
if loc is not None:
coord = ('[' + repr(loc.longitude) + ',' + repr(loc.latitude) + ']')
else:
add = False
if status.coordinates:
val = list(status.coordinates.values())
coord = str(val[1])
add = True
tag = 'IGNORE'
for dis in disasters:
if dis in tweet:
tag = dis
if add and tag is not 'IGNORE':
with open("output.txt", "a") as myfile:
result = coord[1:-1] + ',' + str(status.created_at) + ',' + tag + ',' + tweet + '\n'
myfile.write(result)
return True
def on_error(self, status_code):
print('Error: ' + str(status_code))
return True
def on_timeout(self):
print('Timeout')
return True
if __name__ == '__main__':
listener = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, listener)
stream.filter(track=['flood', 'earthquake', 'wildfire', 'tornado', 'hurricane', 'tsunami', 'avalanche', 'wild fire', 'forest fire'])