Skip to content

Instantly share code, notes, and snippets.

@timwis timwis/list.py
Created Dec 28, 2016

Embed
What would you like to do?
Get a list of OpenDataPhilly datasets that have resources hosted on Socrata
import csv
import sys
import requests
odp_packages_url = 'https://opendataphilly.org/api/3/action/package_search?rows=10000'
odp_dataset_prefix = 'https://opendataphilly.org/package/'
socrata_keyword = '//data.phila.gov'
response = requests.get(odp_packages_url)
response_data = response.json()
packages = response_data['result']['results']
socrata_packages = []
for package in packages:
basic_package = {
'metadata_created': package['metadata_created'],
'metadata_modified': package['metadata_modified'],
'title': package['title'],
'slug': package['name'],
'url': odp_dataset_prefix + package['name'],
'socrata_resources': [],
}
for resource in package['resources']:
if socrata_keyword in resource['url']:
resource_string = '({0}) {1}'.format(resource['format'], resource['url'])
basic_package['socrata_resources'].append(resource_string)
if len(basic_package['socrata_resources']) > 0:
basic_package['socrata_resources'] = '\n'.join(basic_package['socrata_resources'])
socrata_packages.append(basic_package)
fieldnames = ['title', 'slug', 'url', 'metadata_created', 'metadata_modified', 'socrata_resources']
writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(socrata_packages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.