Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SImple file uploader (with hard-coded paths) for CKAN
#!/usr/bin/env python
import urllib2
import urllib
import json
import pprint
import requests
import random
import string
base_url = ""
api_key = open(".ckankey").read() # Put your key in a file called ".ckan"
def make_dataset(name,notes):
dataset_dict = {
'name': name,
'notes': notes,
# dataset_dict['groups'] = groups
# Use the json module to dump the dictionary to a string for posting.
data_string = urllib.quote(json.dumps(dataset_dict))
return data_string
def post_dataset(data_string):
# We'll use the package_create function to create a new dataset.
request = urllib2.Request(
base_url + '/api/action/package_create')
# Creating a dataset requires an authorization header.
# Replace *** with your API key, from your user account on the CKAN site
# that you're creating the dataset on.
request.add_header('Authorization', api_key)
# Make the HTTP request.
response = urllib2.urlopen(request, data_string)
assert response.code == 200
# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(
assert response_dict['success'] is True
# package_create returns the created package as its result.
created_package = response_dict['result']
def add_file(name, filename):
""" Upload a file (filename) to an existing set (name)""" + '/api/action/resource_create',
data={"package_id": name},
headers={"X-CKAN-API-Key": api_key},
files=[('upload', file(filename))])
for i in range(0, 5):
set_name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
print set_name
my_set = make_dataset(set_name, set_name)
add_file(set_name, "/Users/pt/Desktop/21/P7210019.JPG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment