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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
#!/usr/bin/env python
import string, urllib.request, urllib.parse, urllib.error, sys
if len(sys.argv) > 1:
sport = sys.argv[1]
else:
sport = "NHL" # Must be one of NHL, NBA, MLB
class SportsParser:
def __init__(self):
print("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>")
print("<rdf:RDF")
print("xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"")
print("xmlns=\"http://my.netscape.com/rdf/simple/0.9/\">")
print("<channel>")
print("<title>%s</title>" %self.title())
print("<description>%s</description>" %self.description())
print("<link>%s</link>" %self.link())
print("</channel>")
self.processScores(self.inputFile())
print("</rdf:RDF>")
def processScores(self, inputFile):
try:
input = urllib.request.urlopen(self.inputFile())
lines = input.readlines()
except:
sys.exit(5)
for line in lines:
if self.isScoreLine(line):
self.handleScoreLine(line)
def handleGame(self, headline, link):
print("<item>")
print("<title>%s</title>" %headline)
print("<link>%s</link>" %link)
print("</item>")
class NHLParser(SportsParser):
def isScoreLine(self, line):
if line.startswith("nhl_scores_current[") or line.startswith("nhl_scores_previous["):
return 1
else:
return 0
def handleScoreLine(self, line):
__line = string.strip(line)
__line = string.split(__line, "|")
__date = __line[1]
__dayOfWeek = __line[3]
__teamA = __line[4]
__teamB = __line[5]
__teamAAbbr = __line[7]
__teamBAbbr = __line[8]
__gameState = __line[9]
__hour = int(__line[10])
__minutes = int(__line[11])
__timeZone = __line[12]
__scoreA = __line[13]
__scoreB = __line[15]
if __gameState == "pre-game":
self.handleGame("%s at %s (%i:%02i %s)" %(__teamAAbbr, __teamBAbbr, __hour, __minutes, __timeZone), self.link())
elif __gameState == "Final":
self.handleGame("%s %s %s %s (Final)" %(__teamBAbbr, __scoreB, __teamAAbbr, __scoreA), self.link())
# I'm tired of wget'ing the HTML file just to wait for an in-progress
# entry. Moo.
def inputFile(self):
return "http://sports.excite.com/nhl/index.html"
def title(self):
return "NHL Scores"
def description(self):
return "Official scores from the National Hockey League"
def link(self):
return "http://sports.excite.com/nhl"
class NBAParser(SportsParser):
def isScoreLine(self, line):
if line.startswith("nba_scores_current[") or line.startswith("nba_scores_previous["):
return 1
else:
return 0
def period(self, period):
if period > 5:
return "%sOT" %(period - 4)
elif period == 5:
return "OT"
elif period == 4:
return "4th"
elif period == 3:
return "3rd"
elif period == 2:
return "2nd"
elif period == 1:
return "1st"
def handleScoreLine(self, line):
__line = string.strip(line)
__line = string.split(__line, "|")
date = __line[1]
dayOfWeek = __line[3]
homeTeam = __line[4]
awayTeam = __line[5]
homeTeamAbbrev = __line[7]
awayTeamAbbrev = __line[8]
hourUpdated = int(__line[10])
minuteUpdated = int(__line[11])
timeZoneUpdated = __line[12]
homeTeamScore = __line[13]
try:
period = int(__line[14])
except:
period = 0
awayTeamScore = __line[15]
minutesRemaining = __line[16]
secondsRemaining = __line[17]
headline = ""
if __line[9].count("pre") > 0:
# game hasn't started yet, print time it starts
headline = "%s at %s (%s:%02i %s)" %(awayTeamAbbrev, homeTeamAbbrev, hourUpdated, minuteUpdated, timeZoneUpdated)
elif __line[9].count("inal") > 0:
# game over
headline = "%s %s %s %s" %(awayTeamAbbrev, awayTeamScore, homeTeamAbbrev, homeTeamScore)
if period > 4:
headline += " (%s)" %self.period(period)
else:
headline += " (Final)"
else:
# game in progress
minutesRemaining = int(minutesRemaining)
secondsRemaining = int(secondsRemaining)
headline += " %s %s %s %s" %(awayTeamAbbrev, awayTeamScore, homeTeamAbbrev, homeTeamScore)
headline += " (%i:%02i %s)" %(minutesRemaining, secondsRemaining, self.period(period))
self.handleGame(headline, self.link())
def inputFile(self):
return "http://sports.excite.com/nba/index.html"
def title(self):
return "NBA Scores"
def description(self):
return "Official scores from the National Basketball Association"
def link(self):
return "http://sports.excite.com/nba"
class MLBParser(SportsParser):
def isScoreLine(self, line):
if line.startswith("mlb_scores_current[") or line.startswith("mlb_scores_previous["):
return 1
else:
return 0
def inning(self, inning):
# I wonder if there's some nice i18n-friendly way to do this
if inning > 3:
return "%sth" %(inning)
elif inning == 3:
return "3rd"
elif inning == 2:
return "2nd"
elif inning == 1:
return "1st"
def handleScoreLine(self, line):
__line = string.strip(line)
__line = string.split(__line, "|")
date = __line[1]
dayOfWeek = __line[3]
homeTeam = __line[4]
awayTeam = __line[5]
homeTeamAbbrev = __line[7]
awayTeamAbbrev = __line[8]
try:
hourUpdated = int(__line[10])
except:
hourUpdated = 0
try:
minuteUpdated = int(__line[11])
except:
minuteUpdated = 0
timeZoneUpdated = __line[12]
homeTeamScore = __line[13]
try:
inning = int(__line[14])
except:
inning = 0
awayTeamScore = __line[15]
minutesRemaining = __line[16]
secondsRemaining = __line[17]
headline = ""
if __line[9].count("pre") > 0:
# game hasn't started yet, print time it starts
headline = "%s at %s (%s:%02i %s)" %(awayTeamAbbrev, homeTeamAbbrev, hourUpdated, minuteUpdated, timeZoneUpdated)
elif __line[9].count("inal") > 0:
# game over
headline = "%s %s %s %s" %(awayTeamAbbrev, awayTeamScore, homeTeamAbbrev, homeTeamScore)
if inning > 4:
headline += " (%s)" %self.inning(inning)
else:
headline += " (Final)"
else:
# Game in progress
# TODO: find out if it's the top or bottom of the inning
# Need to wget while a game's in progress to see.
headline += " %s %s %s %s" %(awayTeamAbbrev, awayTeamScore, homeTeamAbbrev, homeTeamScore)
headline += " (%s)" %(self.inning(inning))
self.handleGame(headline, self.link())
def inputFile(self):
return "http://sports.excite.com/mlb/index.html"
def title(self):
return "MLB Scores"
def description(self):
return "Official scores from Major League Baseball"
def link(self):
return "http://sports.excite.com/mlb"
try:
parser = globals()["%sParser" %(sport)]()
except:
print("Invalid sport type '%s' selected." %sport)
sys.exit(2)
sys.exit(0)
|