-
Notifications
You must be signed in to change notification settings - Fork 0
/
gony.py
executable file
·178 lines (137 loc) · 4.04 KB
/
gony.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# -*- coding: utf-8 -*
import re
import requests
from BeautifulSoup import BeautifulSoup
class Game:
def __init__(self):
self.firstq = list()
self.secondq = list()
self.thirdq = list()
self.fourthq = list()
self.currentq = self.firstq
def add_event(self, event):
self.currentq.append(event)
def print_plays(self):
for play in self.firstq:
print play
class Play:
def __init__(self, action, time, home_score, away_score):
self.action = action
self.time = time
self.home_score = home_score
self.away_score = away_score
def __repr__(self):
return ','.join((self.time, self.home_score, self.away_score, repr(self.action)))
class Action(object):
def __init__(self, action_type):
super(Action, self).__init__()
self.action_type = action_type
class Event:
def __init__(self, time, play, home_score, away_score):
self.time = time
self.play = play
self.home_score = home_score
self.away_score = away_score
def string(self):
return ','.join(self.time, self.play, self.score)
class Player:
def __init__(self, name):
self.name = name
self.fga = 0
self.fgm = 0
self.tfga = 0
self.tfgm = 0
def madeThree(self):
self.tfga = self.tfga + 1
self.tfgm = self.tfgm + 1
def missedThree(self):
self.tfga = self.tfga + 1
def missedTwo(self):
self.fga = self.fga + 1
def madeTwo(self):
self.fga = self.fga + 1
self.fgm = self.fgm + 1
def __key(self):
return (self.name)
def __eq__(x, y):
return x.__key() == y.__key()
def __hash__(self):
return hash(self.__key())
def fg_line(self):
return 'name', self.name, ': ', self.fgm, ' of ', self.fga
r = requests.get('http://espn.go.com/nba/playbyplay?gameId=320309015&period=0')
doc = r.text
soup = BeautifulSoup(doc)
play_rows = soup.findAll('tr', { "class" : ['even','odd'] })
events = list()
# parse html
for tag in play_rows:
time = tag.find(style='text-align:center;').string
play = tag.find(style='text-align:left;')
if play is not None:
if play.string is None:
play = play.next.string
else:
play = play.string
score = tag.find(nowrap=True)
home_score = None
away_score = None
if score is not None:
score = score.string
m = re.match('(\d+)-(\d+)', score)
home_score = m.group(1)
away_score = m.group(2)
if play is None and score is None:
play = tag.td.nextSibling.next.string
event = Event(time, play, home_score, away_score)
events.append(event)
class Shot(Action):
def __init__(self, shot_type, made, player, assist_player):
super(Shot, self).__init__('shot')
self.shot_type = shot_type
self.made = made
self.player = player
self.assist_player = assist_player
def __repr__(self):
return ','.join((self.action_type, self.shot_type, repr(self.made), self.player, repr(self.assist_player)))
game = Game()
for event in events:
m = re.match('(.*?) (makes|misses) (.*?)( \((.*?) assists\))?$', event.play)
if m is not None:
player = m.group(1)
made_string = m.group(2)
made = made_string is 'makes'
shot_type = m.group(3)
assist_player = None
if len(m.groups()) is 5:
assist_player = m.group(5)
shot = Shot(shot_type, made, player, assist_player)
play = Play(shot, event.time, event.home_score, event.away_score)
game.add_event(play)
game.print_plays()
"""
players = dict()
for event in events:
m = re.match('(.*?) (makes|misses) (.*?) \(.*? assists\)$', event.play)
if m is not None and m.group(2)[0:10] == 'free throw':
else:
name = m.group(1)
if name not in players:
players[name] = Player(name)
player = players[name]
player.madeTwo()
continue
m = re.match('(.*?) misses (.*)$', event.play)
if m is not None:
print m.group(2)[0:10]
if m is not None and m.group(2)[0:10] != 'free throw':
name = m.group(1)
if name not in players:
players[name] = Player(name)
player = players[name]
player.missedTwo()
continue
for player in players.values():
print player.fg_line()
"""