/
tweepy_stream_listener.py
160 lines (135 loc) · 6.36 KB
/
tweepy_stream_listener.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import tweepy
from tweepy.utils import import_simplejson
from database.database import Database
from shell_arguments import ShellArguments
from datetime import datetime
json = import_simplejson()
class StreamListener(tweepy.StreamListener):
verbose = False
def __init__(self):
self.verbose = ShellArguments.get_args().verbose
def on_data(self, raw_data):
data = json.loads(raw_data)
# Extract tweet info
if self.verbose: print ('Running new ')
print (raw_data)
db = Database('twitter-geo')
id = data.get('id')
# Stop if is not tweet
if id == None:
if self.verbose: print('Recieved message that is not a tweet')
with open('messages-that-are-not-tweets.log', 'a') as log_file:
log_file.write(datetime.now().__str__() + "\n" + str(raw_data) + "\n\n")
if self.verbose: print('Saved non-tweet-message to messages-that-are-not-tweets.log')
return
created_at = data.get('created_at')
if created_at != None:
created_at = datetime.strptime(created_at, "%a %b %d %H:%M:%S %z %Y")
else:
print('DATE IS NONE FOR TWEET WITH ID {}'.format(id))
print(raw_data)
# print(created_at)
if id == None:
return 1
text = data.get('text')
hashtags_array = data.get('entities', {}).get('hashtags', [])
hashtags = []
for entry in hashtags_array:
hashtags.append(entry.get('text'))
geo = data.get('geo')
geo_longitude = None
geo_latitude = None
if geo != None:
print ('Got Geo')
geo_coordinate = geo.get('coordinates')
geo = True
print (geo_coordinate)
geo_latitude = geo_coordinate[0]
geo_longitude = geo_coordinate[1]
longitude = None
latitude = None
coordinates = data.get('coordinates')
if coordinates != None:
coordinates = coordinates.get('coordinates')
longitude = coordinates[0]
latitude = coordinates[1]
#Handle place data
place = data.get('place')
place_id = None
if place != None:
place_name = place.get('name', None)
place_country = place.get('country', None)
place_contry_code = place.get('country_code', None)
place_url = place.get('url', None)
place_id = place.get('id', None)
place_full_name = place.get('full_name', None)
place_type = place.get('place_type', None)
place_street_address = None
place_locality = None
place_region = None
place_iso3_country_code = None
place_postal_code = None
attributes = place.get('attributes', None)
if attributes != None:
place_street_address = attributes.get('street_address', None)
place_locality = attributes.get('locality', None)
place_region = attributes.get('region', None)
place_iso3_country_code = attributes.get('iso3', None)
place_postal_code = attributes.get('postal_code', None)
if db.place_exists(place_id):
db.update_place(place_id, place_name, place_country, place_full_name, place_type, place_street_address, place_locality, place_region, place_iso3_country_code, place_postal_code)
else:
db.save_place(place_id, place_name, place_country, place_full_name, place_type, place_street_address, place_locality, place_region, place_iso3_country_code, place_postal_code)
bounding_box = place.get('bounding_box', None)
if bounding_box != None:
bound_coordinates = bounding_box.get('coordinates', None)
bound_type = bounding_box.get('type', None)
if bound_coordinates != None:
for coordinate_middleware in bound_coordinates:
for coordinate in coordinate_middleware:
bound_longitude = coordinate[0]
bound_latitude = coordinate[1]
db.insertBoundingBox(place_id, bound_longitude, bound_latitude, bound_type)
#Handle retweets
retweeted_id = data.get('retweeted_status', {}).get('id')
original_tweet_retweet_count = None
if retweeted_id != None:
original_tweet_retweet_count = data.get('retweeted_status', {}).get('retweet_count')
else:
retweeted_id = data.get('quoted_status', {}).get('id')
in_reply_to_status_id = data.get('in_reply_to_status_id')
in_reply_to_user_id = data.get('in_reply_to_user_id')
lang = data.get('lang')
if latitude != None:
print(longitude)
print(latitude)
# Extract user info
user = data.get('user', {})
user_id = user.get('id')
user_name = user.get('name')
user_screen_name = user.get('screen_name')
user_location = user.get('location')
user_description = user.get('description')
user_followers_count = user.get('followers_count')
user_friends_count = user.get('friends_count')
user_time_zone = user.get('time_zone')
user_lang = user.get('lang')
user_url = user.get('url')
user_geo_enabled = user.get('geo_enabled')
if user_id == None:
print('GOT NULL')
print(raw_data)
# Save user to database
if db.user_exists(user_id):
db.update_user(user_id, user_screen_name, user_name, user_location,
user_description, user_followers_count, user_friends_count,
user_time_zone, user_lang, user_url, user_geo_enabled)
else:
db.save_user(user_id, user_screen_name, user_name, user_location,
user_description, user_followers_count, user_friends_count,
user_time_zone, user_lang, user_url, user_geo_enabled)
# Save tweet if it does not exist in database
if not(db.tweet_exists(id)):
db.save_tweet(id, text, geo, geo_longitude, geo_latitude, user_id, longitude, latitude, place_id,
retweeted_id, original_tweet_retweet_count,
in_reply_to_status_id, in_reply_to_user_id, lang, created_at)