Created
September 29, 2012 23:50
-
-
Save criccomini/3805436 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 | |
# e.g. http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?jsonp=true&sport=MLB&period=20120929 | |
url = 'http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?jsonp=true&sport=%s&period=%d' | |
def today(league): | |
yyyymmdd = int(datetime.datetime.now(pytz.timezone('US/Pacific')).strftime("%Y%m%d")) | |
games = [] | |
try: | |
f = urllib2.urlopen(url % (league, yyyymmdd)) | |
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) |
This would be considered a breach of their terms of use correct? Even if using it for strictly personal use?
I think MSNBC finally dropped this 'API' yesterday.
Yeah, too bad, this isn't working anymore. :(
Has anyone found an alternative?
New url:
url = 'http://scores.nbcsports.com/ticker/data/gamesNEW.js.asp?jsonp=true&sport=%s&period=%d&random=%d'
yyyymmdd = int(dt.strftime("%Y%m%d"))
timestamp = int(round(time.time() * 1000))
games = []
and:
f = urllib2.urlopen(url % (league, yyyymmdd,timestamp))
Here's a forked gist: https://gist.github.com/Robotto/9db27ceead01c3ce7ece6406881f8c92
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What what I can tell msnbc has changed their endpoint rendering this code disabled.
I found msnbc has add a random key at the end of their calls like the call below.
/ticker/data/gamesNEW.js.asp?json=true&sport=NBA$period=20150119&random=1421714943151