-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_standings.py
98 lines (63 loc) · 2.42 KB
/
retrieve_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
"""
Retrieve the roto standings from Yahoo
"""
import datetime
import pandas
from lxml import etree
from config import YAHOO_LEAGUE_ID, YAHOO_SPORT_ID
from yahoo_util import get_yahoo_session, NS
STATS_TYPES = {
'gp': 'int',
'fg%': 'float',
'ft%': 'float',
'3ptm': 'int',
'pts': 'int',
'reb': 'int',
'ast': 'int',
'st': 'int',
'blk': 'int',
'to': 'int',
}
def get_stats(session):
url = f'https://fantasysports.yahooapis.com/fantasy/v2/league/{YAHOO_SPORT_ID}.l.{YAHOO_LEAGUE_ID}/settings'
r = session.get(url)
root = etree.fromstring(r.content)
stats_mapping = {}
for stat in root.xpath("//f:stat", namespaces=NS):
stats_mapping[stat.findtext("f:stat_id", namespaces=NS)] = stat.findtext("f:display_name", namespaces=NS)
stats_mapping['0'] = 'GP'
return stats_mapping
def get_standings(session, stats_mapping):
url = f'https://fantasysports.yahooapis.com/fantasy/v2/league/{YAHOO_SPORT_ID}.l.{YAHOO_LEAGUE_ID}/standings'
r = session.get(url)
root = etree.fromstring(r.content)
standings = []
for team in root.xpath("//f:team", namespaces=NS):
stats = {}
for stat in team.xpath("f:team_stats//f:stat", namespaces=NS):
stats[
stats_mapping.get(stat.findtext("f:stat_id", namespaces=NS), 'unknown').lower()
] = stat.findtext("f:value", namespaces=NS)
stats['team_id'] = team.findtext("f:team_id", namespaces=NS)
standings.append(stats)
standings = pandas.DataFrame(standings)
for stat_name, value_type in STATS_TYPES.items():
standings[stat_name] = standings[stat_name].astype(value_type)
fg = standings['fgm/a'].str.split('/', n=1, expand=True)
standings['fgm'] = fg[0]
standings['fga'] = fg[1]
ft = standings['ftm/a'].str.split('/', n=1, expand=True)
standings['ftm'] = ft[0]
standings['fta'] = ft[1]
standings.drop('fgm/a', axis=1, inplace=True)
standings.drop('ftm/a', axis=1, inplace=True)
return standings
def main():
session = get_yahoo_session()
stats_mapping = get_stats(session)
standings = get_standings(session, stats_mapping)
today = datetime.datetime.today()
standings.to_csv("standings.csv", encoding='utf8', index=False)
standings.to_csv("historical/standings_{:%Y-%m-%d}.csv".format(today), encoding='utf8', index=False)
if __name__ == '__main__':
main()