Search code examples
pythonapisocrata

Socrata SODA and Python


Socrata confirms there is no official Python API. I would like to know if there is some reason for this that would make it worthwhile to learn one of the languages which does have official API support, ie is Python (and associated packages like Pandas) not the best choice for data wrangling.

Specific things I'd like to do include looking at sites like data.lacity.gov Whatever databases I look at on the browser dont let me set dates or other parameters. I get a few months of results where I'd expect years or decades. That led to look for the API and to the lack of Python support apart from an emerging fork of a deprecated project. I'm assuming that the APIs allow this higher visibility, of course. ..

The Old Guy in the Club


Solution

  • For a little bit more background about why we don't have a Python library yet, you might want to check out this thread on Github.

    The short version is - it's not because there is anything in particular wrong with Python or because we have any dislike of it (I think it's a great language), it's more that we just don't have the in-house expertise to build one right now. I'd love to write one but frankly I'm more of a Rubyist than a Pythonista so I'd probably mess it up in some way that would make everybody unhappy. :)

    That said, it is pretty easy to use the Socrata API directly using something like Requests or urllib2. For example, here's how to make a simple call using the Requests library against the [data.lacity.org Building Inspections dataset]:

    import requests
    r = requests.get(
        "https://data.lacity.org/resource/9w5z-rg2h.json?$where=within_circle(lat_lon, 34.053714, -118.242653, 1000)", 
        headers={"X-App-Token":"[YOUR APP TOKEN]"}
    )
    r.json()[0] # {u'permit_status': u'Permit Finaled', u'inspection_result': u'Partial Inspection', u'lat_lon': {u'latitude': u'34.04866', u'needs_recoding': False, u'longitude': u'-118.23787'}, u'address': u'100 S ALAMEDA ST', u'inspection': u'Excavation/Setback/Form/Re-Bar', u'inspection_date': u'2013-08-27T00:00:00', u'permit': u'13016-30000-09747'}
    

    You'll need to structure your own SoQL query to use with Requests, but there are lots of resources on how to do so on dev.socrata.com.