Skip to content

Instantly share code, notes, and snippets.

@andkamau
Last active December 6, 2016 16:09
Show Gist options
  • Save andkamau/3c6d3458bccdb99a6ebc5837c3342728 to your computer and use it in GitHub Desktop.
Save andkamau/3c6d3458bccdb99a6ebc5837c3342728 to your computer and use it in GitHub Desktop.
import os, sys
from datetime import datetime
from ckanapi import RemoteCKAN
API_KEY = os.getenv("CKAN_API_KEY", None)
USER_AGENT = "openafrica-client"
URL = "https://africaopendata.org"
def upload_files(directory, dataset):
"""
upload files in `directory` to `dataset`
@directory: the absolute path of the directory with files to upload
@dataset: the slug of the dataset to upload to.
"""
try:
files = os.listdir(directory)
print "uploading %s files..." % len(files)
mysite = RemoteCKAN(URL, apikey=API_KEY, user_agent=USER_AGENT)
for file_ in files:
file_path = "%s/%s" % (directory, file_)
uploaded = mysite.action.resource_create(
package_id=dataset, url='%s-uploads' % dataset, upload=open(file_path, 'rb'), name=file_.strip())
print "%s - %s" % (file_, uploaded)
print "===" * 30
except Exception, err:
print "ERROR: upload failed - %s" % err
if __name__ == "__main__":
try:
directory = sys.argv[1]
dataset = sys.argv[2]
print "Upload files in %s to %s" % (directory, dataset)
upload_files(directory, dataset)
except IndexError:
print "\n\nUsage:\n\npython ckan_upload.py <directory> <dataset>\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment