Last active
October 23, 2015 18:40
-
-
Save ptsefton/ef0d0098876283005344 to your computer and use it in GitHub Desktop.
SImple file uploader (with hard-coded paths) for CKAN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib2 | |
import urllib | |
import json | |
import pprint | |
import requests | |
import random | |
import string | |
base_url = "http://w0414.uws.edu.au/" | |
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(response.read()) | |
assert response_dict['success'] is True | |
# package_create returns the created package as its result. | |
created_package = response_dict['result'] | |
pprint.pprint(created_package) | |
def add_file(name, filename): | |
""" Upload a file (filename) to an existing set (name)""" | |
requests.post(base_url + '/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) | |
post_dataset(my_set) | |
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