I Hate Closed APIs for Simple Data Requests

There is nothing more annoying than wanting some simple data and needing to signup as developer only to manage another API key, etc.

A few weeks back I wanted to know who was playing in the upcoming week NFL game. I watch Steven Rose YT channel and play his Tournament of Champions NFL game.

First Google search "NFL API"
When it's a JSON endpoint like everything is these days with a signup, etc, try another route the big bad XML.

Second Google search "NFL games XML"
The second search lead me to a Stack Overflow question and the XML I was looking for.

A few lines of Python later that looked something like... mission accomplished.

#!/usr/bin/env python3

from urllib.request import urlopen
from lxml import etree

# Regular Season = 17 Weeks
y = sys.argv[1]
w = sys.argv[2]

parser = etree.XMLParser()
url = 'http://www.nfl.com/ajax/scorestrip?season='+ y +'&seasonType=REG&week='+ w
with urlopen(url) as f:
    tree = etree.parse(f, parser)

teams = []
for team in tree.findall('.//g'):
    h = team.get('hnn')
    v = team.get('vnn')
    hscore = team.get('hs')
    vscore = team.get('vs')
    teams.append({'hs' : hscore, 'h' : h, 'vs' : vscore, 'v' : v})

Comments