Skip to content

Instantly share code, notes, and snippets.

@criccomini
Created September 29, 2012 23:50
Show Gist options
  • Save criccomini/3805436 to your computer and use it in GitHub Desktop.
Save criccomini/3805436 to your computer and use it in GitHub Desktop.
Schedules & Scores API for Streaming Live Sports Stats - MSNBC
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)
@kevinsoltis41
Copy link

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

@kmatesi
Copy link

kmatesi commented Jan 26, 2015

This would be considered a breach of their terms of use correct? Even if using it for strictly personal use?

@mixographer
Copy link

I think MSNBC finally dropped this 'API' yesterday.

@Robotto
Copy link

Robotto commented Aug 28, 2019

Yeah, too bad, this isn't working anymore. :(
Has anyone found an alternative?

@Robotto
Copy link

Robotto commented Aug 28, 2019

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))

@Robotto
Copy link

Robotto commented Aug 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment