-
-
Save criccomini/3805436 to your computer and use it in GitHub Desktop.
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) |
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
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
Hi I was wondering how active this is?
I'm looking to develop something for this upcoming MLB season, however I can't get the json games to return when manipulating the API call.
Meaning if I'm trying to get the MLB scores from May 20th, 2014, I would manipulate the yyyymmdd to 20140520 and hardcode the sport to be MLB. My resulting call is: http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?jsonp=true&sport=MLB&period=20140520
the return is: shsMSNBCTicker.loadGamesData({ "sport": "MLB", "period": "20140520", "games": [ ]} );
I was wondering if this is due to '2014' being a legacy year and the upcoming year will be active once the season starts?
Thanks for your help,
Kevin