forked from mcharters/hockeypool-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.py
150 lines (125 loc) · 4.64 KB
/
init.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
from bs4 import BeautifulSoup
from pymongo import MongoClient
from urllib2 import urlopen
import pprint
print 'connecting to mongo'
# set up the db
client = MongoClient()
db = client.hockeypool
players = db.players
teams = db.teams
print 'removing data'
players.remove()
teams.remove()
print 'starting stuff'
# set up a team dictionary
teamDict = {}
# set up playoff teams => opponents
playoffTeams = {
'/nhl/teams/pit': '/nhl/teams/ott',
'/nhl/teams/mon': '/nhl/teams/nyi',
'/nhl/teams/was': '/nhl/teams/nyr',
'/nhl/teams/bos': '/nhl/teams/tor',
'/nhl/teams/tor': '/nhl/teams/bos',
'/nhl/teams/nyr': '/nhl/teams/was',
'/nhl/teams/nyi': '/nhl/teams/mon',
'/nhl/teams/ott': '/nhl/teams/pit',
'/nhl/teams/chi': '/nhl/teams/min',
'/nhl/teams/ana': '/nhl/teams/det',
'/nhl/teams/van': '/nhl/teams/san',
'/nhl/teams/stl': '/nhl/teams/los',
'/nhl/teams/los': '/nhl/teams/stl',
'/nhl/teams/san': '/nhl/teams/van',
'/nhl/teams/det': '/nhl/teams/ana',
'/nhl/teams/min': '/nhl/teams/chi'
}
print 'Finding teams'
#parse teams
teamsURL = "http://sports.yahoo.com/nhl/stats/byteam?cat=teamstats"
soup = BeautifulSoup(urlopen(teamsURL), "lxml")
headerRow = soup.select(".ysptblthbody1")
for teamRow in headerRow[0].find_next_siblings("tr"):
teamData = teamRow.find_all("td")
print teamData[0].a['href']
if teamData[0].a['href'] in playoffTeams:
team = {
'name': teamData[0].a.get_text(),
'gamesPlayed': teamData[2].get_text(),
'goals': teamData[4].get_text(),
'assists': teamData[6].get_text(),
'wins': teamData[28].get_text(),
'losses': teamData[30].get_text(),
'otLosses': teamData[34].get_text()
}
# insert team in the db, record the id for later use
team_id = teams.insert(team)
teamDict[teamData[0].a['href']] = team_id
print 'Inserted team {0} with ID {1}'.format(team['name'], team_id)
else:
print '{0} is not a playoff team'.format(teamData[0].a['href'])
for team in teamDict:
teams.update({'_id': teamDict[team]}, {'$set': {'opponent': teamDict[playoffTeams[team]]}});
# parse defenders
playersURL = "http://sports.yahoo.com/nhl/stats/byposition?pos=D&conference=NHL&year=season_2012&qualified=1"
soup = BeautifulSoup(urlopen(playersURL), "lxml")
headerRow = soup.select(".ysptblthbody1")
for playerRow in headerRow[0].find_next_siblings("tr"):
playerData = playerRow.find_all("td")
if playerData[1].a['href'] in playoffTeams:
player = {
'name': playerData[0].a.get_text(),
'team': teamDict[playerData[1].a['href']], # this is the team's id in the db, not the team's name
'gamesPlayed': playerData[2].get_text(),
'goals': playerData[4].get_text(),
'assists': playerData[6].get_text(),
'points': playerData[8].get_text(),
'plusMinus': playerData[10].get_text(),
'penaltyMinutes': playerData[12].get_text(),
'hits': playerData[14].get_text(),
'bks': playerData[16].get_text(),
'fw': playerData[18].get_text(),
'fl': playerData[20].get_text(),
'fo': playerData[22].get_text(),
'ppg': playerData[24].get_text(),
'ppa': playerData[26].get_text(),
'shg': playerData[28].get_text(),
'sha': playerData[30].get_text(),
'gw': playerData[32].get_text(),
'sog': playerData[34].get_text(),
'pct': playerData[36].get_text(),
'isDefender': True
}
player_id = players.insert(player)
print 'Inserted player {0} with ID {1}'.format(player['name'], player_id)
# parse forwards
playersURL = "http://sports.yahoo.com/nhl/stats/byposition?pos=C,RW,LW&conference=NHL&year=season_2012&qualified=1"
soup = BeautifulSoup(urlopen(playersURL), "lxml")
headerRow = soup.select(".ysptblthbody1")
for playerRow in headerRow[0].find_next_siblings("tr"):
playerData = playerRow.find_all("td")
if playerData[1].a['href'] in playoffTeams:
player = {
'name': playerData[0].a.get_text(),
'team': teamDict[playerData[1].a['href']], # this is the team's id in the db, not the team's name
'gamesPlayed': playerData[2].get_text(),
'goals': playerData[4].get_text(),
'assists': playerData[6].get_text(),
'points': playerData[8].get_text(),
'plusMinus': playerData[10].get_text(),
'penaltyMinutes': playerData[12].get_text(),
'hits': playerData[14].get_text(),
'bks': playerData[16].get_text(),
'fw': playerData[18].get_text(),
'fl': playerData[20].get_text(),
'fo': playerData[22].get_text(),
'ppg': playerData[24].get_text(),
'ppa': playerData[26].get_text(),
'shg': playerData[28].get_text(),
'sha': playerData[30].get_text(),
'gw': playerData[32].get_text(),
'sog': playerData[34].get_text(),
'pct': playerData[36].get_text(),
'isDefender': False
}
player_id = players.insert(player)
print 'Inserted player {0} with ID {1}'.format(player['name'], player_id)