Skip to content

Instantly share code, notes, and snippets.

@tgherzog
Last active April 3, 2019 19:52
Show Gist options
  • Save tgherzog/193c1a4f36d0d06eb45908417fcacc0e to your computer and use it in GitHub Desktop.
Save tgherzog/193c1a4f36d0d06eb45908417fcacc0e to your computer and use it in GitHub Desktop.
DDH module example
# from github.com/tgherzog/pyddh - be sure to get the dkan-2018-api branch
import ddh
ddh.load('ddh1stg.prod.acquia-sites.com')
ds = ddh.dataset.ds_template()
# populate required fields
ds['title'] = 'Python test'
ds['body'] = 'Blah blah blah'
ds['field_wbddh_dsttl_upi'] = 63576 # Rochelle
# add required taxonomy terms that don't have defaults, or good defaults
# NB: ds_template populates taxonomy fields with "unspecified" wherever possible,
# which may not be what you want
ddh.taxonomy.update(ds, {
'field_wbddh_data_type': 'other',
'field_wbddh_data_class': 'public',
'field_license_wbddh': 'Creative Commons Attribution 4.0',
})
# add a resource (uploaded file)
rs = ddh.dataset.rs_template()
rs['title'] = 'Data file'
ddh.taxonomy.update(rs, {'field_wbddh_resource_type': 'Download', 'field_wbddh_data_class': 'public'})
rs['upload'] = 'dataset.csv'
ds['resources'].append(rs)
# add another resource (external link)
rs = ddh.dataset.rs_template()
rs['title'] = 'Data browser'
ddh.taxonomy.update(rs, {'field_wbddh_resource_type': 'Query tool', 'field_wbddh_data_class': 'public'})
rs['field_link_api'] = 'http://datatopics.worldbank.org/gender'
ds['resources'].append(rs)
try:
ddh.dataset.new_dataset(ds)
except ddh.dataset.APIError as err:
print 'ERROR: {}'.format(err.response)
# from github.com/tgherzog/pyddh - be sure to get the dkan-2018-api branch
import ddh
# set this to the target nodeID
target_dataset_id = 123456
ddh.load('datacatalog.worldbank.org')
rs = ddh.dataset.rs_template()
rs['title'] = 'Data file'
ddh.taxonomy.update(rs, {'field_wbddh_resource_type': 'Download', 'field_wbddh_data_class': 'public'})
rs['upload'] = 'path/to/file.csv'
# set the 3rd argument to an increasing integer to ensure the resources appear in the order added
ddh.dataset.append_resource(target_dataset_id, rs, 0)
rs = ddh.dataset.rs_template()
rs['title'] = 'Data browser'
ddh.taxonomy.update(rs, {'field_wbddh_resource_type': 'Query tool', 'field_wbddh_data_class': 'public'})
rs['field_link_api'] = 'http://datatopics.worldbank.org/gender'
ddh.dataset.append_resource(target_dataset_id, rs, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment