Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to Create Demo Resources for Testing Resource Viewer
'''Simple script for creating demo data in CKAN
Requires existence of a tester user. You can create this by doing::
paster create-test-data user
'''
import ckanclient
base_location = 'http://localhost:5000/api'
api_key = 'tester'
client = ckanclient.CkanClient(base_location, api_key)
datastore_resource_id = '4f1299ab-a100-4e5f-ba81-e6d234a2f3bd'
pkg = dict(
name='test-data-viewer',
title='Test Data Viewer',
resources=[
dict(
id=datastore_resource_id,
url='made-up-url',
name='For Datastore try-out'
),
dict(
url='http://afghanistanelectiondata.org/sites/default/files/district_centerpoints.csv',
description='Afghanistan (csv)'
),
dict(
url='https://commondatastorage.googleapis.com/ckannet-storage/2012-08-17T081442/data',
format='csv',
description='csv, no extension but format'
),
dict(
url='https://commondatastorage.googleapis.com/ckannet-storage/2012-08-17T081442/data',
format='text/csv',
description='Thatcher wages (text/csv)'
),
# now n3 / rdf (what about sparql?)
dict(
url='http://lod.taxonconcept.org/ses/iuCXz.rdf',
format='example/rdf+xml',
description='Link to an example insect'
),
dict(
url='http://www.ggdc.net/MADDISON/Historical_Statistics/vertical-file_02-2010.xls',
description='Maddison World Pop (xls)'
),
dict(
url='http://ckan.net/',
description='An html page',
format='text/html'
),
dict(
url='http://data.gov.uk/sparql',
description='A SPARQL API',
format='api/sparql'
),
dict(
url='http://ckan.net/mydata.zip',
description='Zip file',
format='zip'
),
dict(
url='http://ckan.net/mydata.csv.zip',
description='Zipped csv file',
format='zip:csv'
),
]
)
def create_dataset():
try:
client.package_register_post(pkg)
except ckanclient.CkanApiError:
client.package_entity_put(pkg)
# Datastore
import ckanclient.datastore
import StringIO
fields = [
{'id': 'id'},
{'id': 'date', 'type': 'date'},
{'id': 'x'},
{'id': 'y'},
{'id': 'z'},
{'id': 'country'},
{'id': 'title'},
{'id': 'lat'},
{'id': 'lon'}
];
data = [
{'id': 0, 'date': '2011-01-01', 'x': 1, 'y': 2, 'z': 3, 'country': 'DE', 'title': 'first', 'lat':52.56, 'lon':13.40},
{'id': 1, 'date': '2011-02-02', 'x': 2, 'y': 4, 'z': 24, 'country': 'UK', 'title': 'second', 'lat':54.97, 'lon':-1.60},
{'id': 2, 'date': '2011-03-03', 'x': 3, 'y': 6, 'z': 9, 'country': 'US', 'title': 'third', 'lat':40.00, 'lon':-75.5},
{'id': 3, 'date': '2011-04-04', 'x': 4, 'y': 8, 'z': 6, 'country': 'UK', 'title': 'fourth', 'lat':57.27, 'lon':-6.20},
{'id': 4, 'date': '2011-05-04', 'x': 5, 'y': 10, 'z': 15, 'country': 'UK', 'title': 'fifth', 'lat':51.58, 'lon':0},
{'id': 5, 'date': '2011-06-02', 'x': 6, 'y': 12, 'z': 18, 'country': 'DE', 'title': 'sixth', 'lat':51.04, 'lon':7.9}
];
def create_datastore():
client.action('datastore_create',
resource_id=datastore_resource_id,
fields=fields,
records=data
)
if __name__ == '__main__':
create_dataset()
create_datastore()
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.