-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_player_data.py
77 lines (59 loc) · 2.23 KB
/
get_player_data.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
#!/usr/bin/env python
import collections
import itertools
import json
import os
import re
import sys
import requests
from utils import memoize_to_disk
FANDUEL_BASE_URL = "https://wwww.fanduel.com/e/Game/{}"
def get_fanduel_game(game_id):
print "Getting markup"
response = requests.get(FANDUEL_BASE_URL.format(game_id))
html = response.text
return html
@memoize_to_disk
def get_fanduel_players_for_game(game_id):
html = get_fanduel_game(game_id)
regex = re.compile(r'allPlayersFullData.*=([^;]+);', re.MULTILINE)
matches = regex.findall(html)
print matches
print "parsing json"
players = json.loads(matches[0])
print "returning players"
return players
if __name__=="__main__":
game_id = sys.argv[1]
players = get_fanduel_players_for_game(game_id)
def sort_by_position(data):
player_id, player = data
return player[0]
players_by_position = collections.defaultdict(dict)
for position, players in itertools.groupby(sorted(players.iteritems(), key=sort_by_position), sort_by_position):
#print "position:", position
for player_id, player in players:
#print player_id, player[0], player[1], player[5], player[6], player[7]
# keyed by position, then name:
players_by_position[position][player[1]] = dict(
player_id=player_id,
position=player[0],
name=player[1],
salary=int(player[5]),
fantasy_points_per_game=float(player[6]),
played=int(player[7])
)
# let's calculate best values at each position just based on fanduel data:
for position in ('QB', 'RB', 'WR', 'TE', 'D', 'K'):
values = []
for name, player in players_by_position[position].iteritems():
points_per_thousand = player['fantasy_points_per_game'] / (player['salary'] / 1000.0)
stats = (player['name'],
player['fantasy_points_per_game'],
player['salary'],
points_per_thousand)
values.append(stats)
values.sort(key=lambda x: x[3], reverse=True)
print "top 10 %ss:"%(position,)
for p in values[:10]:
print p