-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_search.py
More file actions
76 lines (54 loc) · 2.27 KB
/
resource_search.py
File metadata and controls
76 lines (54 loc) · 2.27 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Python command line script for querying the resources in a CKAN instance..
This program will conduct a query and print a subset of the data found.
The base URL for the API to use (without the trailing "/api/action" text)
can be specified in an environment variable named 'CKAN_URL'. The value for
the URL will be prompted for input if the environment variable is not set.
The API key to use for authentication can be specified in an environment
variable named 'CKAN_KEY'. The value for the API key will be prompted for
input if the environment variable is not set.
The program accepts a single command line argument, the query string to
use in conducting the search.
Note that any fields added to the extras portion of the CKAN schema can
be referenced in the return field list by prefacing the field name with
"extras_".
Example: Query for a resource with id "9dc70e6b-8426-4d71-b9d5-70ce6094a3f4".
python resource_search.py id:9dc70e6b-8426-4d71-b9d5-70ce6094a3f4
Example: Find all resources for which the extras data_quality field is false.
python resource_search.py extras_data_quality:false
"""
import json
import logging
import os
import sys
import re
from ckanapi import RemoteCKAN
def do_resource_search(ckan_connection, search_term = "*:*"):
result = None
try:
result = ckan_connection.call_action(action='resource_search', data_dict={
'query': search_term
})
except:
return result
return result.get('results',None)
if __name__ == '__main__':
url = os.getenv('ED_CKAN_URL', None)
api_key = os.getenv('ED_CKAN_KEY', None)
errors = []
if not url:
errors.append('ED_CKAN_URL environment variable is needed.')
if not api_key:
errors.append('ED_CKAN_KEY environment variable is needed.')
search_text = "*:*"
if len(sys.argv) > 1:
search_text = sys.argv[1]
else:
errors.append('No query string specified on command line.')
if len(errors):
for e in errors:
logging.error(e)
sys.exit(1)
remote_ckan = RemoteCKAN(address=url, apikey=api_key)
# Perform the package search.
resource_list = do_resource_search(remote_ckan, search_text)
print(json.dumps(resource_list, indent=2))