-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgetData.py
executable file
·162 lines (135 loc) · 5.34 KB
/
getData.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
import json data from API
IMPORTANT!! you must turn off pagination for this to work from a URL and get all
country records
Install module django-extensions
Runs twice via function calls at bottom once
"""
from django.db import connection, transaction
cursor = connection.cursor()
from os.path import exists
import json
import unicodedata
import sys
import urllib2
from datetime import date
from workflow.models import Country, Program
import urllib
def run():
print "Uploading JSON data"
type = "Program"
program_country = 1
def getAllData(url, type, program_country):
# set url for json feed here
json_file = urllib2.urlopen(url)
#load data
data = json.load(json_file)
json_file.close()
#print data
#query to mysql database after parsing json data
def saveCountries(keys_to_sql, vars_to_sql):
#save the original keys list for update in case we need to run that
save_keys = keys_to_sql
keys_to_sql = ", ".join(map(str, keys_to_sql))
query = "INSERT INTO activitydb_country (country,code) VALUES ('%s','%s')" % (vars_to_sql[0], vars_to_sql[1])
print query
try:
cursor.execute(query)
transaction.commit()
except Exception, err:
sys.stderr.write('ERROR: %s\n' % str(err))
column = save_keys[1]
value = 1
country = vars_to_sql[0]
if type == "country":
query_update = "UPDATE activitydb_country set country = %s where lower(%(type)s) = '%s'" % (
column, value, country.lower())
try:
cursor.execute(query_update)
transaction.commit()
except Exception, err:
sys.stderr.write('ERROR: %s\n' % str(err))
return 1
pass
#query to mysql database after parsing json data
def savePrograms(keys_to_sql, vars_to_sql,program_country):
#save the original keys list for update in case we need to run that
save_keys = keys_to_sql
keys_to_sql = ", ".join(map(str, keys_to_sql))
var_to_tuple = tuple(vars_to_sql)
print var_to_tuple
query = "INSERT INTO activitydb_program (%s) VALUES %s" % (keys_to_sql, var_to_tuple)
print query
try:
cursor.execute(query)
transaction.commit()
except Exception, err:
sys.stderr.write('ERROR: %s\n' % str(err))
pass
latest = Program.objects.latest('id')
query2 = "INSERT INTO activitydb_program_country (country_id,program_id) VALUES (%s,%s)" % (program_country, latest.id)
print query2
try:
cursor.execute(query2)
transaction.commit()
except Exception, err:
sys.stderr.write('ERROR: %s\n' % str(err))
pass
for row in data:
print row
vars_to_sql = []
keys_to_sql = []
for new_key, new_value in row.iteritems():
try:
new_key = new_key.encode('ascii','ignore')
new_value = new_value.encode('ascii','ignore')
except Exception, err:
sys.stderr.write('ERROR: %s\n' % str(err))
#print new_key
#print new_value
if type == "Country":
if new_value:
#country or region related columns only
if new_key in ('country','region','iso_code'):
#change iso_code to code for DB table
if new_key == 'iso_code':
new_key = 'code'
keys_to_sql.append(new_key)
vars_to_sql.append(new_value)
elif type == "Program":
if new_value:
#country or region related columns only
if new_key in ('gaitid','funding_status','granttitle','cost_center'):
#change iso_code to code for DB table
if new_key == 'granttitle':
new_key = 'name'
keys_to_sql.append(new_key)
vars_to_sql.append(new_value)
#add create_date to comma seperated list of columns
keys_to_sql.append("create_date")
#append todays date to var
today = date.today()
today.strftime('%Y-%m-%d')
today = str(today)
vars_to_sql.append(today)
#add description to comma separated list of columns
keys_to_sql.append("description")
#append todays date to var
vars_to_sql.append("")
if type == "Country":
saveCountries(keys_to_sql, vars_to_sql)
elif type == "Program":
savePrograms(keys_to_sql, vars_to_sql, program_country)
# get an updated json data file for the hub and update or insert new records
#print "Country"
#getAllData("https://mcapi.mercycorps.org/authoritativecountry/?gait=True&format=json", "Country")
#get an updated json data file for the hub and update or insert new records
print "Program"
getCountries = Country.objects.all()
for country in getCountries:
print country.country
safe_country = urllib.quote_plus(country.country)
program_url = "http://mcapi.mercycorps.org/gaitprogram/?country=%s&format=json" % (safe_country)
print program_url
getAllData(program_url, "Program", int(country.id))
print "Alright, all done."