Skip to content

Instantly share code, notes, and snippets.

@mstyne
Created April 24, 2012 02:57
Show Gist options
  • Save mstyne/2475819 to your computer and use it in GitHub Desktop.
Save mstyne/2475819 to your computer and use it in GitHub Desktop.
Simple ESPN Headlines API call - New York Mets
#!/usr/bin/env python
import httplib
import json
# import socket
import pprint
import sys
apikey = 'XXX'
apiurl = 'api.espn.com'
headers = {"Accept": "application/json"}
#call = "/v1/sports/baseball/mlb/news/"
call = "/v1/sports/baseball/mlb/teams/21/news/"
#call = "/v1/sports/baseball/mlb/leagues/groups"
conn = httplib.HTTPConnection(apiurl)
try:
conn.request('GET', call + '?apikey=' + apikey, '', headers)
# except socket.gaierror:
except:
print "Couldn't query ESPN! Sorry!\n"
sys.exit()
try:
response = conn.getresponse()
except:
print "Couldn't get a response from ESPN! Sorry!\n"
sys.exit()
result = response.read()
try:
jresult = json.loads(result)
except:
print "Couldn't parse JSON from ESPN! Sorry!\n"
sys.exit()
#print json.dumps(jresult, indent=4)
#pp = pprint.PrettyPrinter(indent=2)
#pp.pprint(jresult)
headlines = jresult['headlines']
for headline in headlines:
print headline['headline'] + " (" + headline['source'] + ")\n"
print "\t -" + headline['description'] + "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment