-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-persons.py
More file actions
126 lines (88 loc) · 3.41 KB
/
update-persons.py
File metadata and controls
126 lines (88 loc) · 3.41 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
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
#!/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import pandas
import utils
base_url = "http://api.openhluttaw.org"
#store key in token.txt Don't commit this file
key = open('token.txt')
token = 'Token '+key.read().rstrip()
headers = {'Authorization': token }
#can't have single function right now because columns are not the same for
#different languages
#Update English
def update_en():
lang = 'en'
df = pandas.DataFrame.from_csv('data/mp-en.csv', header=0, index_col=False)
df = df.where((pandas.notnull(df)), None)
MPs = df.to_dict(orient='records')
for mp in MPs:
hluttaw_id = mp['identifier__hluttaw']
if mp['popit_id']:
popit_id = mp['popit_id']
else:
popit_id = utils.hluttaw_to_popitid(hluttaw_id, base_url)
if popit_id:
url = base_url + "/" + lang + "/persons/" + popit_id
honorific_prefix = mp['honorific_prefix']
name = mp['name']
gender = mp['gender']
national_identity = mp['national_identity']
image = mp['image']
if type(image) == float:
print "nan"
payload = {
'honorific_prefix': honorific_prefix,
'name': name,
'gender': gender,
'national_identity': national_identity,
'image' : image
}
r = requests.put(url, headers=headers, json=payload)
print r.content
else:
print "Missing hluttaw_id match: " + hluttaw_id
#Update Myanmar translations
def update_my():
lang = 'my'
df = pandas.DataFrame.from_csv('data/mp-my.csv', header=1, index_col=False)
df = df.where((pandas.notnull(df)), None)
MPs = df.to_dict(orient='records')
for mp in MPs:
hluttaw_id = mp['identifier__hluttaw']
popit_id = utils.hluttaw_to_popitid(hluttaw_id, base_url)
print hluttaw_id
print popit_id
if popit_id:
url = base_url + "/" + lang + "/persons/" + popit_id
honorific_prefix = mp['honorific_prefix']
name = mp['name']
gender = mp['gender']
national_identity = mp['national_identity']
payload = {
'honorific_prefix': honorific_prefix,
'name': name,
'gender': gender,
'national_identity': national_identity,
}
r = requests.put(url, headers=headers, json=payload)
print r.content
def clean_duplicate_sms():
#Getting total number of pages via REST request
page_request = requests.get('http://api.openhluttaw.org/en/persons')
pages = page_request.json()['num_pages']
#Fetch and build list of all representatives
persons =[]
for page in range(1,pages+1):
req_representatives = requests.get('http://api.openhluttaw.org/en/persons?page='+str(page))
for person in json.loads(req_representatives.content)['results']:
persons.append(person)
for person in persons:
sms_ids = [ contact['id'] for contact in person['contact_details'] if contact['label'] == 'SMS']
if len(sms_ids) == 2:
url = base_url + '/en/persons/' + person['id'] + "/contact_details/" + sms_ids[0]
#r = requests.delete(url,headers=headers)
#print r.content
update_en()
update_my()