Skip to content

Instantly share code, notes, and snippets.

@cclauss
Last active December 26, 2015 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cclauss/7179270 to your computer and use it in GitHub Desktop.
Save cclauss/7179270 to your computer and use it in GitHub Desktop.
earthQuakes - A starter experiment with Socrata Open Data
#!/usr/bin/env python
import json, requests, pprint
minMagnitude = 5.5
rootURL = 'https://soda.demo.socrata.com/resource/'
theURL = rootURL + 'earthquakes.json?$where=magnitude>' + str(minMagnitude)
fmt = '{datetime} {earthquake_id} {version} {magnitude} {depth:>6} {location[latitude]:>8} {location[longitude]:>8} {region}'
def soda2FieldDictFromHeaders(inRequestsHeader):
fieldNames = eval(inRequestsHeader['x-soda2-fields'])
fieldTypes = eval(inRequestsHeader['x-soda2-types'])
fieldDict = {}
for i in xrange(len(fieldNames)):
fieldDict[fieldNames[i]] = fieldTypes[i]
return fieldDict
theRequest = requests.get(theURL)
fieldDict = soda2FieldDictFromHeaders(theRequest.headers)
pprint.pprint(fieldDict)
print('')
for quakeDict in json.loads(theRequest.text):
# pprint.pprint(quakeDict)
print(fmt.format(**quakeDict))
print('=' * 75)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment