-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_groups.py
90 lines (71 loc) · 1.95 KB
/
make_groups.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
import urllib2
import urllib
import json
import pprint
import ckan_api
# Put the details of the dataset we're going to create into a dict.
group_dict1 = {
'name': 'access-to-health-care',
'title': 'Access to Health Care'
}
group_dict2 = {
'name': 'basic-needs',
'title': 'Basic Needs'
}
group_dict3 = {
'name': 'built-environment',
'title': 'Built Environment'
}
group_dict4 = {
'name': 'cleanliness-and-beautification',
'title': 'Cleanliness and Beautification'
}
group_dict5 = {
'name': "crime-and-safety",
'title': 'Crime and Safety'
}
group_dict6 = {
'name': "diversity",
'title': 'Diversity'
}
group_dict7 = {
'name': "employment-opportunities",
'title': 'Employment Opportunities'
}
group_dict8 = {
'name': "food-and-nutrition",
'title': 'Food and Nutrition'
}
group_dict9 = {
'name': "mental-health",
'title': 'Mental Health'
}
group_dict10 = {
'name': "sense-of-community",
'title': 'Sense of Community'
}
group_dict11 = {
'name': "substance-abuse-prevention",
'title': 'Substance Abuse Prevention'
}
groups_dict = ['group_dict1', 'group_dict2']
for i in range(len(groups_dict)):
# Use json module to dump the dictionary to a string for posting.
data_string = urllib.quote(json.dumps(i))
# Use package_create function to create new dataset.
request = urllib2.Request(
'http://ckan.codefornova.org/api/action/group_create'
)
# Creating a dataset requires an authorization header.
# Replace *** with API key
request.add_header('authorization', ckan_api.myapi)
# Make 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
# group_create returns the created group as its result.
create_group = response_dict['result']
pprint.pprint(create_group)