-
Notifications
You must be signed in to change notification settings - Fork 0
/
emoji_tweets.py
378 lines (328 loc) · 10.8 KB
/
emoji_tweets.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# emoji_tweets
# Based off final proj for LING 23700: Langugage & the Internet (3/9/18)
# Last Modified by Alex Markowitz on 6/17/18.
import os # For report logging
import json # JSON formatting
import emoji # emoji database
import tweepy # twitter parsing
import pprint # Data Pretty Printer
import config # For API keys stored in separate file
import argparse # command line interface
import googlemaps # googlemaps api
import matplotlib.pyplot as plt # graphing
import pandas as pd # data
import numpy as np # data
from datetime import datetime # Timer
from operator import itemgetter # Dictionary sort
from tqdm import tqdm # progress bar
# Parse command line options and set timer
startTime = datetime.now()
parser = argparse.ArgumentParser(
"python emoji_tweets.py",
usage="%(prog)s [-h] [-v, --version] [-n, --numtweets] [-q, --query] [-usa]",
)
parser.add_argument(
"-v", "--version", help="version number of application", action="store_true"
)
parser.add_argument(
"-n", "--num", type=int, default=100, help="amount of tweets to scrape"
)
parser.add_argument("-q", "--query", help="query to search on", metavar="")
parser.add_argument("-usa", help="international by default", action="store_true")
args = parser.parse_args()
if args.version:
print("Version 2.1 of emoji_tweets")
exit()
if not args.query:
print("You must enter a search query. Try again...")
exit()
#########################
### Global Variables ###
#########################
international = not args.usa # False searches domestically
search_query = args.query # query to search for on twitter
MAX_TWEETS = args.num # Total number of tweets to scrape
tweets_per_query = 100 # 100 tweets at once
# dictionaries
emoji_map = {}
location_map = {}
emoji_count_map = {}
states = {
"Alabama",
"Alaska",
"Arizona",
"Arkansas",
"California",
"Colorado",
"Connecticut",
"Delaware",
"Florida",
"Georgia",
"Hawaii",
"Idaho",
"Illinois",
"Indiana",
"Iowa",
"Kansas",
"Kentucky",
"Louisiana",
"Maine",
"Maryland",
"Massachusetts",
"Michigan",
"Minnesota",
"Mississippi",
"Missouri",
"Montana",
"Nebraska",
"Nevada",
"New Hampshire",
"New Jersey",
"New Mexico",
"New York",
"North Carolina",
"North Dakota",
"Ohio",
"Oklahoma",
"Oregon",
"Pennsylvania",
"Rhode Island",
"South Carolina",
"South Dakota",
"Tennessee",
"Texas",
"Utah",
"Vermont",
"Virginia",
"Washington",
"West Virginia",
"Wisconsin",
"Wyoming",
}
#################
##### APIs #####
#################
# Google Maps Key: 4 keys to deal with Google's limits
gmaps = googlemaps.Client(key=config.gmapkey2)
# Twitter API keys: using a Tweepy wrapper
auth = tweepy.AppAuthHandler(config.consumer_key, config.consumer_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
if not api:
print("Can't Authenticate")
sys.exit(-1)
# pause tweets
def limit_handled(cursor):
while True:
try:
yield cursor.next()
except tweepy.RateLimitError:
time.sleep(10 * 30)
##########################
##### Emoji Methods #####
##########################
# Return true if char is an emoji
def char_is_emoji(character):
return character in emoji.UNICODE_EMOJI
# Return true if string contains an emoji
def status_has_emoji(text):
for character in text:
if character in emoji.UNICODE_EMOJI:
return True
return False
#############################
##### Location Methods #####
#############################
# parse GoogleMaps JSON response
def getLocation(addr):
loc = ""
addCompLen = len(addr)
for i in range(0, addCompLen):
try:
if international:
if addr[i]["types"][0] == "country":
loc += addr[i]["long_name"]
else:
if addr[i]["long_name"] in states:
loc += addr[i]["long_name"]
except:
print(" Error: country doesn't exist in JSON")
report(stat, emojistat, hasLoc)
return loc
# Return true if tweet came from valid location as determined by GoogleMaps API
def status_has_location(status):
initloc = status.user.location
if initloc:
try:
# TODO: try saving this as a local var: geoloc = gmaps.geocode(loc)
if gmaps.geocode(initloc):
location = getLocation(gmaps.geocode(initloc)[0]["address_components"])
if location is not "":
insert_location_map(location, status.text)
insert_emoji_map(status.text, location)
return True
except Exception as e:
print(" GoogleMaps API error: " + str(e))
report(stat, emojistat, hasLoc)
else:
return False
###############################
##### Dictionary Methods #####
###############################
# insert country-emoji pair into map
def insert_location_map(location, status):
for char in status:
if char_is_emoji(char):
if location in location_map:
location_map[location].append(char)
else:
location_map[location] = [char]
# insert emoji-country pair into map
def insert_emoji_map(status, location):
for char in status:
if char_is_emoji(char):
if char in emoji_map:
emoji_count_map[char] += 1
else:
emoji_count_map[char] = 1
if location is not "none":
if char in emoji_map:
# if location in emoji_map[char]:
# temp = emoji_map[char][location].get()
# emoji_map[char][location] = temp + 1
# else:
# emoji_map[char].append({location, 1})
emoji_map[char].append(location)
else:
emoji_map[char] = [location]
# emoji_map[char] = {location, 1};
# Buils the forward trie out of all words from the corpus
# help from: https://www.programiz.com/python-programming/methods/dictionary/setdefault
def build_trie(words, trie):
for word in words:
temp = trie
wordtemp = temp
temp = temp.setdefault(word[:k], {"count": 0})
for char in word[k:]:
wordtemp = temp
temp = temp.setdefault(char, {"count": 0})
wordtemp["count"] = len(wordtemp) - 1
temp = temp.setdefault("#", "#") # end of word signal
################################
##### main script methods #####
################################
# Helper for print formatting of timer in final report
def timehelper(endtime):
hours, remainder = divmod(endtime.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
if hours == 0.0:
hours = ""
else:
hours = str(int(hours)) + "h "
if minutes == 0.0:
minutes = ""
else:
minutes = str(int(minutes)) + "m "
seconds = str(round(seconds, 2)) + "s"
return hours + minutes + seconds
# Print final report with statistics to console
def report(stat, emojistat, hasLoc):
# Output set up
filename = "report_" + str(stat) + "_tweets_on_" + search_query + ".txt"
file = open(os.path.join("reports", filename), "w")
pp = pprint.PrettyPrinter(indent=4, stream=file)
# Output header
file.write("Final Report:\n\n")
locale = "internationally" if international else "domestically"
file.write(
'%s Total tweets were looked at on search query "%s" %s\n'
% (stat, search_query, locale)
)
file.write("%s of those tweets contained emojis\n" % (emojistat))
file.write("%s of Emoji Tweets had locations\n" % (hasLoc))
time = timehelper(datetime.now() - startTime)
file.write("In %s \n\n" % (time))
# Output maps
file.write("Location Map: ")
pp.pprint(location_map)
file.write("\n Emoji Map: ")
pp.pprint(emoji_map)
file.write("\n Emoji Count Map: ")
pp.pprint(sorted(emoji_count_map.items(), key=itemgetter(1), reverse=True))
# Command line finish
time = timehelper(datetime.now() - startTime)
print(
'Report published: looked at %s tweets on "%s" %s in %s\n'
% (stat, search_query, locale, time)
)
# loop through MAX_TWEETS tweets to gather data
def main():
# global counts
emojistat = 0
hasLoc = 0
stat = 0
try:
if international:
for status in tqdm(
limit_handled(
tweepy.Cursor(
api.search, q=search_query, count=tweets_per_query
).items(MAX_TWEETS)
)
):
stat += 1
if status_has_emoji(status.text):
emojistat += 1
if status_has_location(status):
hasLoc += 1
else:
insert_emoji_map(status.text, "none")
else:
# Geocode is [lat,long,rad] of geographic center of continental US - geocode="39.50,-98.35,1500mi"
for status in tqdm(
limit_handled(
tweepy.Cursor(
api.search,
q=search_query,
count=tweets_per_query,
geocode="39.50,-98.35,1500mi",
).items(MAX_TWEETS)
)
):
stat += 1
if status_has_emoji(status.text):
emojistat += 1
if status_has_location(status):
hasLoc += 1
else:
insert_emoji_map(status.text, "none")
except tweepy.error.TweepError:
print(" Twiiter API error: Too many tweet requests")
except tweepy.TweepError as e:
print(" Twitter API error: " + str(e))
report(stat, emojistat, hasLoc)
# Run main program
if __name__ == "__main__":
main()
# TODO:
# clump repeated values into array of arrays -- Look into set default
# 'New YorkNew York' error?
# add graphs to final report
#############################
##### Graphing Methods #####
#############################
# # # Bar Chart
# labels = 'Brady', 'Gronkowski', 'Amendola'
# y_pos = np.arange(len(labels)) #same as range(len(labels))
# retweets = [brady, gronk, dola]
# plt.bar(y_pos, retweets, align='center', alpha=1)
# plt.xticks(y_pos, retweets)
# plt.ylabel('Mentions')
# plt.xlabel('Player')
# plt.title('Patriots Mentions')
# plt.show()
# # #pie chart, where slices will be ordered and plotted counter-clockwise
# # explode = (0, .1, 0)
# # fi1, ax1 = plt.subplots()
# # ax1.pie(retweets, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
# # ax1.axis('equal')
# # plt.show()