-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckan.py
58 lines (49 loc) · 1.35 KB
/
ckan.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import ckanclient
import json
api_key='<your-api-key>'
#base_location='http://thedatahub.org/api'
#base_location='http://data.gov.uk/api'
def get_pkg_tag(ckan, tag):
pkgs = ckan.tag_entity_get(tag)
i=0
for pkg_name in pkgs:
print "retrieving",i,len(pkgs),pkg_name
yield get_pkg(ckan,pkg_name)
i=i+1
def get_pkg(ckan, pkg_name):
try:
pkg=ckan.package_entity_get(pkg_name)
return pkg['resources']
except Exception as e:
print "error", str(e)
def list_tags(ckan):
return ckan.tag_register_get()
def write_pkgs(ckan, tag, type=['CSV','XLS']):
resources = {}
print "tag=",tag
print "types=",type
with open('ckan-'+tag+'.json', 'w+') as f:
for resource in get_pkg_tag(ckan,tag):
if resource is not None:
for el in resource:
format = el['format'] or ''
mimetype = el['mimetype'] or ''
if (any([t in format.upper() for t in type])
or any([t in mimetype.upper() for t in type])):
id=el['id']
url=el['url']
print {id:url}
resources[id]=url
f.write(json.dumps(resources,indent=4))
if __name__ == "__main__":
import sys
if (len(sys.argv) != 4):
print "Usage:",sys.argv[0],"<base_location> <tag> <api_key>"
sys.exit(1)
else:
base_location=sys.argv[1]
tag=sys.argv[2]
api_key=sys.argv[3]
print "init:",base_location,api_key
ckan=ckanclient.CkanClient(base_location,api_key)
write_pkgs(ckan,tag)