-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathckan-upload.py
29 lines (23 loc) · 1.41 KB
/
ckan-upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import argparse, ckanapi
class MyParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
self.print_help()
self.exit(2)
parser = MyParser(description='Uploads a file to update an existing resource in data.gov.au or other CKAN portal.',
epilog="Source code and issues: https://github.com/OpenCouncilData/Ckan-Upload")
parser.add_argument('--apikey', required=True, help='Your API key for the portal. Find this on your data.gov.au user page.')
parser.add_argument('--resource', required=True, help='URL of the resource to update (eg http://data.gov.au/dataset/geelong-drain-pipes/resource/970d4dfd-4313-45ee-be9a-6b69b47483f1)')
parser.add_argument('filename', help='The file that will be uploaded in place of the existing resource.')
try:
args = parser.parse_args()
except:
parser.print_help()
raise SystemExit
server = args.resource.split('/dataset')[0]
resourceid = args.resource.split('/')[-1]
print "Connecting to %s to update resource ID %s" % (server, resourceid)
ckan = ckanapi.RemoteCKAN(server, apikey=args.apikey,user_agent='opencouncildata testing')
resourceinfo = ckan.action.resource_show(id=resourceid)
print "Uploading %s to \"%s\". (Previously modified at %s)" % (args.filename, resourceinfo["name"], resourceinfo["last_modified"])
ckan.action.resource_update(id=resourceid,upload=open(args.filename,'rb'), format=resourceinfo["format"])