-
-
Save Robotto/9db27ceead01c3ce7ece6406881f8c92 to your computer and use it in GitHub Desktop.
Schedules & Scores API for Streaming Live Sports Stats - MSNBC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytz | |
import datetime | |
import time | |
import urllib2 | |
import json | |
import os | |
import elementtree.ElementTree as ET | |
url = 'http://scores.nbcsports.com/ticker/data/gamesNEW.js.asp?jsonp=true&sport=%s&period=%d&random=%d' | |
def today(league): | |
yyyymmdd = int(datetime.datetime.now(pytz.timezone('US/Pacific')).strftime("%Y%m%d")) | |
games = [] | |
timestamp = int(round(time.time() * 1000)) | |
try: | |
f = urllib2.urlopen(url % (league, yyyymmdd,timestamp)) | |
jsonp = f.read() | |
f.close() | |
json_str = jsonp.replace('shsMSNBCTicker.loadGamesData(', '').replace(');', '') | |
json_parsed = json.loads(json_str) | |
for game_str in json_parsed.get('games', []): | |
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') | |
os.environ['TZ'] = 'US/Eastern' | |
start = int(time.mktime(time.strptime('%s %d' % (gamestate_tree.get('gametime'), yyyymmdd), '%I:%M %p %Y%m%d'))) | |
del os.environ['TZ'] | |
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') | |
}) | |
except Exception, e: | |
print e | |
return games | |
if __name__ == "__main__": | |
for league in ['NFL', 'MLB', 'NBA', 'NHL']: | |
print today(league) | |
time.sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment