-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_elf_standings.py
133 lines (102 loc) · 4.2 KB
/
get_elf_standings.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
import json
import time
# from datetime import datetime
from urllib.request import urlopen
import numpy as np
import pandas as pd
# import requests
# from bs4 import BeautifulSoup
# from tqdm import tqdm
def get_elf_standings(save=False):
print('')
url = "https://elf-app-89392.web.app/apiPublic/dump/standings?"
row_df = pd.DataFrame()
standings_df = pd.DataFrame()
standings_df_arr = []
time.sleep(0.5)
response = urlopen(url)
json_string = response.read()
# json_string = str(json_string).replace('\\"','"')
json_data = json.loads(json_string)
del json_string
for (key, value) in json_data.items():
team_id = value['teamId']
row_df = pd.DataFrame({'team_id': team_id}, index=[0])
row_df['season'] = int(value['season'])
row_df['conference_id'] = value['conferenceId']
row_df['division_id'] = value['divisionId']
row_df['rank'] = int(value['rank'])
row_df['team_abv'] = str(value['shortname']).upper()
row_df['team_name'] = value['name']
row_df['overall_wins'] = int(value['wins'])
row_df['overall_losses'] = int(value['losses'])
row_df['overall_win_pct'] = row_df['overall_wins'] / \
(row_df['overall_wins'] + row_df['overall_losses'])
row_df['overall_win_pct'] = row_df['overall_win_pct'].round(3)
row_df['points_for'] = int(value['pf'])
row_df['points_against'] = int(value['pa'])
row_df['point_diff'] = row_df['points_for'] - row_df['points_against']
row_df['conference_wins'] = int(value['confW'])
row_df['conference_losses'] = int(value['confL'])
row_df['conference_win_pct'] = row_df['conference_wins'] / \
(row_df['conference_wins'] + row_df['conference_losses'])
row_df['conference_win_pct'] = row_df['conference_win_pct'].round(3)
row_df['conference_points_for'] = int(value['confPf'])
row_df['conference_points_against'] = int(value['confPa'])
row_df['conference_point_diff'] = row_df['conference_points_for'] - \
row_df['conference_points_against']
home_w, home_l = str(value['home']).split('-')
home_w = int(home_w)
home_l = int(home_l)
row_df['home_wins'] = home_w
row_df['home_losses'] = home_l
if (home_w + home_l) > 0:
row_df['home_win_pct'] = home_w / (home_w + home_l)
else:
row_df['home_win_pct'] = None
del home_w, home_l
away_w, away_l = str(value['away']).split('-')
away_w = int(away_w)
away_l = int(away_l)
row_df['away_wins'] = away_w
row_df['away_losses'] = away_l
if (away_w + away_l) > 0:
row_df['away_win_pct'] = away_w / (away_w + away_l)
else:
row_df['away_win_pct'] = None
del away_w, away_l
neutral_w, neutral_l = str(value['neutral']).split('-')
try:
neutral_w = int(neutral_w)
neutral_l = int(neutral_l)
except Exception:
neutral_w = 0
neutral_l = 0
row_df['neutral_wins'] = neutral_w
row_df['neutral_losses'] = neutral_l
if (neutral_w + neutral_l) > 0:
row_df['neutral_win_pct'] = neutral_w / (neutral_w + neutral_l)
else:
row_df['neutral_win_pct'] = None
del neutral_w, neutral_l
try:
row_df['streak'] = value['streak']
except Exception:
row_df['streak'] = None
standings_df_arr.append(row_df)
del row_df
standings_df = pd.concat(standings_df_arr, ignore_index=True)
standings_df = standings_df.sort_values(
['season', 'conference_id', 'division_id', 'rank'], ascending=True)
if save is True:
seasons_arr = standings_df['season'].to_numpy()
seasons_arr = np.unique(seasons_arr)
for i in seasons_arr:
seasons_df = standings_df.loc[standings_df['season'] == i]
seasons_df.to_csv(
f'standings/{i}_elf_standings.csv', index=False)
# seasons_df.to_parquet(
# f'standings/{i}_elf_standings.parquet', index=False)
return standings_df
if __name__ == "__main__":
get_elf_standings(True)