-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrosters.py
85 lines (73 loc) · 2.59 KB
/
rosters.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
import bs4
import json
import datetime
from pprint import pprint
import urllib
import urllib2
from league_info import *
class Lineup:
def __init__(self, team, scoring_period, year=2013):
self.error = False
self.team = team
self.scoring_period = scoring_period
self.year = year
self.get_lineup()
def get_lineup(self):
BASE = "http://games.espn.go.com/flb/boxscorequick?"
params = dict(leagueId=LEAGUE_ID, view='scoringperiod',
version='quick')
params['seasonId'] = self.year
params['teamId'] = self.team
params['scoringPeriodId'] = self.scoring_period
url = BASE + urllib.urlencode(params)
source = urllib2.urlopen(url).read()
if source.find('games-error-red-alert') <> -1:
self.error = True
return
resp = urllib2.urlopen(url)
soup = bs4.BeautifulSoup(resp)
header = soup.find("div", "games-pageheader").find("h1")
date = header.find("em").get_text()
if date == "Offseason":
self.error = True
return
fmt = '%A, %B %d'
dt = datetime.datetime.strptime(date, fmt)
fmt = '%m-%d'
self.date = "{}-{}".format(self.year, dt.strftime(fmt))
trs = soup.findAll("tr", "pncPlayerRow")
tds = map(lambda x: x.find('td'), trs)
self.lineup = map(self.get_player, trs)
def get_player(self, tr):
player_name = None
slot_node = tr.find("td", "playerSlot")
slot = slot_node.get_text()
player_id = slot_node.get('id') # No id if slot empty
if player_id:
player_id = player_id.split('_')[1]
name_node = tr.find("td", "playertablePlayerName")
player_name = name_node.find("a").get_text()
return slot, player_id, player_name
def csv(self):
output = ""
for row in self.lineup:
output += "{},{},{},{},{},{}\n".format(
self.team, self.date, self.scoring_period, *row)
return output
def main():
year = 2014
output = "teamId,date,scoringPeriodId,slot,playerId,playerName\n"
with open("{}_lineups.csv".format(year), 'w') as f:
f.write(output)
for team in TEAMS:
scoring_period = 1
while scoring_period <= 12:
lineup = Lineup(team, scoring_period, year=year)
if lineup.error:
break
output = lineup.csv()
with open("{}_lineups.csv".format(year), 'a') as f:
f.write(output)
scoring_period += 1
#if __name__ == "__main__":
# main()