- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScoreSrapeAPI.py
More file actions
Latest commit
115 lines (93 loc) · 3.41 KB
/
Copy pathScoreSrapeAPI.py
File metadata and controls
115 lines (93 loc) · 3.41 KB
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
importpytz
importdatetime
importtime
importurllib2
importjson
importos
importxml.etree.ElementTreeasET
##Needs for testing Baseball season
##4 weeks of season schedule starting 1 week from today 4/29/15
# e.g. http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?jsonp=true&sport=MLB&period=20120929
deftoday(league):
url='http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?jsonp=true&sport=%s&period=%d'
yyyymmdd=int(datetime.datetime.now(pytz.timezone('US/Mountain')).strftime("%Y%m%d"))
games= []
try:
currentTime=time.strftime('%I:%M %p',time.localtime())
currentDate=time.strftime('%m/%d/%Y',time.localtime())
f=urllib2.urlopen(url% (league, yyyymmdd))
jsonp=f.read()
f.close()
count=0
json_str=jsonp.replace('shsMSNBCTicker.loadGamesData(', '').replace(');', '')
json_parsed=json.loads(json_str)
forgame_strinjson_parsed.get('games', []):
count+=1
game_tree=ET.XML(game_str)
visiting_tree=game_tree.find('visiting-team')
home_tree=game_tree.find('home-team')
gamestate_tree=game_tree.find('gamestate')
home=home_tree.get('nickname')
away=visiting_tree.get('nickname')
visitingScore=visiting_tree.get('score')
homeTeamScore=home_tree.get('score')
os.environ['TZ'] ='US/Eastern'
start=int(time.mktime(time.strptime('%s %d'% (gamestate_tree.get('gametime'), yyyymmdd), '%I:%M %p %Y%m%d')))
delos.environ['TZ']
ifvisitingScore:
pass
else:
visitingScore='0'
ifhomeTeamScore:
pass
else:
homeTeamScore='0'
ifcount==1:
score=ET.Element('score')
score.attrib['filedate'] =currentDate
score.attrib['filetime'] =currentTime
game=ET.SubElement(score, 'game')
game.attrib['hometeam']=home
game.attrib['awayteam']=away
awayscore=ET.SubElement(game, 'awayscore')
awayscore.text=visitingScore
homescore=ET.SubElement(game, 'homescore')
homescore.text=homeTeamScore
gameTime=ET.SubElement(game, 'starttime')
gameTime.text=gamestate_tree.get('status')
else:
game=ET.Element('game')
game.attrib['hometeam']=home
game.attrib['awayteam']=away
awayscore=ET.SubElement(game, 'awayscore')
awayscore.text=visitingScore
homescore=ET.SubElement(game, 'homescore')
homescore.text=homeTeamScore
gameTime=ET.SubElement(game, 'starttime')
gameTime.text=gamestate_tree.get('status')
score.append(game)
gameXML=ET.tostring(score)
## print gameXML
## print count
## games.append({
## 'league': league,
## 'start': start,
## 'home': home,
## 'away': away,
## 'home-score': home_tree.get('score'),
## 'away-score': visiting_tree.get('score'),
## 'status': gamestate_tree.get('status'),
## 'clock': gamestate_tree.get('display_status1'),
## 'clock-section': gamestate_tree.get('display_status2')
## })
##
withopen('scoringUpdateFile.xml','w') asscoreData:
scoreData.write(str(gameXML))
exceptException, e:
printe
## return games
if__name__=="__main__":
## for league in ['NFL', 'MLB', 'NBA', 'NHL']:
## print today(league)
today('MLB')
time.sleep(10)