-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
402 lines (342 loc) · 13.4 KB
/
app.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from pprint import pprint
from operator import itemgetter
from typing import List
from datetime import datetime, timedelta
from modules.web_scraper import get_tweets
from modules.Preprocessing import handle_tweet_data
from modules.Sentiment_Analysis import train_model_if_necessary, analyze_many_tweets
from modules.presentation import get_tweets_in_daterange, get_by_key_value, remove_sentiment, get_sentiment, bar_plot, line_plot, pie_chart
import argparse
import re
_REGEX_CHAR_MATCHER_HASHTAGS = re.compile('[^A-Za-z0-9]')
#########CUSTOM TYPES#########
def _restricted_float(val: float):
"""
Only allow float values within our range [0.00-1.00]
"""
try:
val = float(val)
except ValueError:
raise argparse.ArgumentTypeError(f"{val} not a floating-point literal")
if 0.0 < val > 1.0:
raise argparse.ArgumentTypeError(f"{val} not in range [0.0, 1.0]")
return val
def _restricted_dates(date):
"""
This method is called by argparse on a per-argument basis, meaning it calls it twice
Argparse itself validates nargs=2
"""
_dates = list(date)
try:
return_date = datetime.strptime(date, '%Y-%m-%d').date()
# end_date = datetime.strptime(dates[1], '%Y-%m-%d').date()
except ValueError:
raise argparse.ArgumentTypeError(
f"Could not parse dates. Did you format them yyyy-mm-dd? Dates received:\n{date}")
# if start_date > end_date:
# raise argparse.ArgumentTypeError(
# f"Start date {start_date} may not be later than end date {end_date}")
# return [start_date, end_date, 55]
return return_date
def _restricted_sentiment(val: str):
"""
Only allow given sentiments
"""
sentiments = ["Positive", "Negative", "Uncertain"]
try:
val = str(val).title()
except ValueError:
raise argparse.ArgumentTypeError(f"{val} could not be parsed to a string")
if val not in sentiments:
raise argparse.ArgumentTypeError(f"{val} is not a valid sentiment. Possible values: {', '.join(sentiments)}")
return val
def _restricted_plots(val: str):
"""
Only allow given plots
"""
plots = ["bar", "line", "pie"]
try:
val = str(val).lower()
except ValueError:
raise argparse.ArgumentTypeError(f"{val} could not be parsed to a string")
if val not in plots:
raise argparse.ArgumentTypeError(f"{val} is not a valid plot type. Possible values: {', '.join(plots)}")
return val
def _restricted_hashtags(val: str):
"""
Only allow hashtags that follow our standards.
Typically we remove #-symbols even if a user added them.
Used for hashtags arg
"""
try:
val = str(val).lower()
except ValueError:
raise argparse.ArgumentTypeError(f"{val} could not be parsed to a string")
val = re.sub(_REGEX_CHAR_MATCHER_HASHTAGS, "", val)
return val
def _restricted_search_hashtags(val: str):
"""
Only allow hashtags that follow our standards.
Typically we add a # even if user forgot them.
Important to use .lower to match program output.
Used for search_hashtags arg
"""
try:
val = str(val).lower()
except ValueError:
raise argparse.ArgumentTypeError(f"{val} could not be parsed to a string")
if not val.startswith('#'):
return '#' + val
return val
def _restricted_search_mentions(val: str):
"""
Only allow mentions that follow our standards.
Typically we add @-symbol even if an user forgot them.
important to keep structure to match program output.
Used for search_mentions arg
"""
try:
val = str(val)
except ValueError:
raise argparse.ArgumentTypeError(f"{val} could not be parsed to a string")
if not val.startswith('@'):
return '@' + val
return val
#########CUSTOM TYPES#########
#########HELPER METHODS#########
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter):
"""
Allows us to use two formatters for argparse instead of only the default 1 (hackish)
https://stackoverflow.com/a/18462760
"""
def _split_lines(self, text, width):
# https://stackoverflow.com/a/29498894
# modify super to add newline between arguments
lines = super()._split_lines(text, width)
if text.endswith('\n'): # custom newline
lines += ['']
else:
lines += [''] # also add newline between different arguments
return lines
def _default_dates():
"""
Generates today and five days from now for use with default values of the date-argument
"""
today = datetime.now().date()
five_days_from_now = today + timedelta(days=5)
# create readable format, as should be input
# return [today.strftime('%Y-%m-%d'), five_days_from_now.strftime('%Y-%m-%d')]
return [today, five_days_from_now]
def _filter_search_values(key: str, values: list, collection: list):
"""
Filters tweet data by a single key and many values.
## Returns a list of items which [key]-collection holds any of the given values
### Example:
collection holds ['#trump', '#biden', '#uselection'].
values hold ['#trump', '#uselection'].
This item would be returned as it matches 2 of 3 values.
>>> list (original collection)
"""
return_data = []
for item in collection:
if any(val in values for val in item[key]):
return_data.append(item)
return return_data
def _filter_data(analyzed_tweet_data: list, start_date, end_date, hashtags, mentions, urls):
"""
Filters data according to given arguments.
Filters by date per default.
Other arguments default to None and will not execute unless specifically passed.
"""
# filter by dates
filtered_data = get_tweets_in_daterange(
analyzed_tweet_data, start_date, end_date)
print("Done filtering on date...")
if hashtags:
filtered_data = _filter_search_values(
'hashtags', hashtags, filtered_data)
print(f'Done filtering on hashtags: {hashtags}')
if mentions:
filtered_data = _filter_search_values(
'mentions', mentions, filtered_data)
print(f'Done filtering on mentions: {mentions}')
if urls:
filtered_data = _filter_search_values(
'tweet_urls', urls, filtered_data)
print(f'Done filtering on urls: {urls}')
return filtered_data
#########HELPER METHODS#########
def prepare_data(hashtags: List,
tweet_amount: int,
fresh_search: bool,
save_plot: bool,
dates: List[datetime.date],
plot_type: str,
search_mentions: List,
search_hashtags: List,
search_urls: List,
remove_sentiment: str,
certainty_low: float,
certainty_high: float):
"""
Main method that ties all the components together. Takes use of above helper methods.
"""
# prepare model for analysis
train_model_if_necessary()
# Verify data
start_date, end_date = dates
if (start_date > end_date):
raise ValueError(f'Start date {start_date} may not be later than end date {end_date}')
# negate bool as to get meaning worthy of get_tweets
# (app asks, "do you want local search?" get_tweets asks, "do you want online search?")
fresh_search = not fresh_search
# Scrape data
tweet_list = get_tweets(tweet_amount, fresh_search, hashtags)
print("Done scraping...")
# Preprocess data
clean_tweets, hashtag_stats, mention_stats = handle_tweet_data(tweet_list)
print("Done preprocessing...")
# analyze the clean data
analyzed_tweets = analyze_many_tweets(
clean_tweets, certainty_low, certainty_high)
print("Done analyzing...")
# filter data to specifics
filtered_data = _filter_data(
analyzed_tweets, start_date, end_date, search_hashtags, search_mentions, search_urls)
print('Done filtering data...')
# filter sentiment
if (remove_sentiment):
filtered_data = remove_sentiment(filtered_data, remove_sentiment)
print("Done removing sentiment...")
# Getting plot data from the get_sentiment function
plot_data = get_sentiment(filtered_data)
print("Done getting sentiment df for plotting...")
# Warn user that plt.show() is blocking
if not save_plot:
print('\tNOTICE\n\tShowing the plot will block the main thread.\n\tExit the plot display to continue program.')
# Create plot
file_name = '_'.join(hashtags)
if plot_type == "bar":
if save_plot:
bar_plot(plot_data, file_name, file_name)
else:
bar_plot(plot_data, file_name)
if plot_type == "line":
if save_plot:
line_plot(plot_data, file_name, file_name)
else:
line_plot(plot_data, file_name)
if plot_type == "pie":
if save_plot:
pie_chart(plot_data, file_name, file_name)
else:
pie_chart(plot_data, file_name)
# print statistics
print('--------------------------------------')
print('Printing statistics')
print('\tTOP 5 HASHTAGS')
pprint(list(hashtag_stats.items())[:5], width=1)
print('--------------------------------------')
print('\tTOP 5 MENTIONS')
pprint(list(mention_stats.items())[:5], width=1)
print('--------------------------------------')
print('\t EXAMPLE OBJECT')
pprint(analyzed_tweets[0], width=1)
print('--------------------------------------')
if __name__ == "__main__":
# region ARGPARSE
parser = argparse.ArgumentParser(
prog='TweetScraper9000',
formatter_class=CustomFormatter,
description="""
A program that scrapes Twitter for hashtags and performs a sentiment analysis on the results.
Presents results in a chosen chart format.
""",
usage='%(prog)s',
epilog='Source: https://github.com/Hold-Krykke/PythonExam\nCreated by: Camilla, Malte, Asger, Rúni')
parser.add_argument(
'hashtags',
help="The hashtags to scrape.\nEXAMPLE: 'trump biden'\n-REQUIRED-",
nargs='+',
type=_restricted_hashtags)
parser.add_argument(
'-p', '--plot',
help="Plot chart type, choose one. VALUES=[bar, line, pie]\n",
type=_restricted_plots,
default='pie',
dest='plot_type')
parser.add_argument(
'-l', '--local',
help="Pass to attempt scraping from local files.\n",
action='store_true',
dest='fresh_search',
default=False)
parser.add_argument(
'-s', '--save',
help="Pass to save plots locally\n(if omitted will show plots instead)\n",
action='store_true',
dest='save_plot',
default=False)
parser.add_argument(
'-c', '--count',
help="The amount of tweets to search for.\n",
type=int,
default=300,
dest='tweet_count')
parser.add_argument(
'-d', '--date',
help="The date range (yyyy-mm-dd) to search for. \nEXAMPLE: '2020-05-01 2020-05-05'.\n",
nargs=2,
type=_restricted_dates,
default=_default_dates(),
dest='date')
parser.add_argument(
'-se', '--sentiment',
help="Ignore specific sentiment.\nVALUES=[Positive, Negative, Uncertain]\n",
type=_restricted_sentiment,
dest='remove_sentiment')
parser.add_argument(
'-sh',
help="Filter result data by specific hashtags.\nEXAMPLE: '#Trump #Biden'\n",
type=_restricted_search_hashtags,
nargs='+',
dest='search_hashtags')
parser.add_argument(
'-sm',
help="Filter result data by specific mentions.\nEXAMPLE: '@JoeBiden @folketinget'\n",
type=_restricted_search_mentions,
nargs='+',
dest='search_mentions')
parser.add_argument(
'-su',
help="Filter result data by specific URLs.\nEXAMPLE: 'https://pic.twitter.com/'\n",
type=str,
nargs='+',
dest='search_urls')
parser.add_argument(
'-cl',
help="The lower float value for determining if a sentiment is deemed uncertain.\nEXAMPLE: 0.15.\nVALUES: [0.00-1.00]",
type=_restricted_float,
default=0.25,
metavar='ADVANCED: certainty low',
dest='certainty_low')
parser.add_argument(
'-ch',
help="The higher float value for determining if a sentiment is deemed uncertain.\nEXAMPLE: 0.65.\nVALUES: [0.00-1.00]",
type=_restricted_float,
default=0.75,
metavar='ADVANCED: certainty high',
dest='certainty_high')
# endregion
# turn Namespace object into usable dict
args_dict = vars(parser.parse_args())
print('--------------------------------------')
print('Received Values:')
pprint(args_dict, width=1)
print('--------------------------------------')
# extract items from dict
hashtags, plot_type, fresh_search, tweet_count, date, save_plot, remove_sentiment, search_hashtags, search_mentions, search_urls, certainty_low, certainty_high = itemgetter(
'hashtags', 'plot_type', 'fresh_search', 'tweet_count', 'date', 'save_plot', 'remove_sentiment', 'search_hashtags', 'search_mentions', 'search_urls', 'certainty_low', 'certainty_high')(args_dict)
# call main method
prepare_data(hashtags, tweet_count, fresh_search, save_plot, date, plot_type, search_mentions,
search_hashtags, search_urls, remove_sentiment, certainty_low, certainty_high)