-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
166 lines (150 loc) · 6.18 KB
/
api.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
163
164
165
166
import requests
import traceback
import json
url = 'https://pemilu2024.kpu.go.id/'
api_url = 'https://sirekap-obj-data.kpu.go.id/'
enpoint_ppwp = 'wilayah/pemilu/ppwp/'
enpoint_hhcw_ppwp = 'pemilu/hhcw/ppwp/'
api_enpoint_list_province = 'wilayah/pemilu/ppwp/0.json'
endpoint_candidate = 'pemilu/ppwp.json'
# Example fetch url Kabupaten/Kota 'wilayah/pemilu/ppwp/11.json'
# wilayah/pemilu/ppwp/${province_code}.json
# Example fetch url Kecamatan 'wilayah/pemilu/ppwp/11/1105.json'
# wilayah/pemilu/ppwp/${province_code}/${city_code}.json
# Example fetch url Kelurahan/Desa 'wilayah/pemilu/ppwp/11/1105/110507.json'
# wilayah/pemilu/ppwp/${province_code}/${city_code}/${district_code}.json
# Example fetch url TPS 'pemilu/hhcw/ppwp/11/1105/110507/1105072002.json'
# pemilu/hhcw/ppwp/${province_code}/${city_code}/${district_code}/${village_code}/${tps_code}.json
# Example fetch url TPS Detail 'pemilu/hhcw/ppwp/11/1105/110507/1105072002/1105072002002.json'
# pemilu/hhcw/ppwp/${province_code}/${city_code}/${district_code}/${village_code}/${tps_code}/${tps_code}.json
def get_province_list():
try:
url = api_url + api_enpoint_list_province
response = requests.get(url, timeout=30)
if response.status_code == 200:
return response.json()
else:
get_province_list()
except:
print(f'Error get_province_list: {url}, {traceback.format_exc()}')
get_province_list()
def get_city_list(province_id):
json_file = f'data/{province_id}.json'
try:
with open(json_file, 'r') as f:
data = json.load(f)
return data
except:
pass
url = api_url + enpoint_ppwp + province_id + '.json'
try:
response = requests.get(url, timeout=30)
if response.status_code == 200:
with open(json_file, 'w') as f:
json.dump(response.json(), f)
return response.json()
else:
get_city_list(province_id)
except:
print(f'Error get_city_list: {url}, {traceback.format_exc()}')
get_city_list(province_id)
def get_district_list(province_id, city_id):
url = api_url + enpoint_ppwp + province_id + '/' + city_id + '.json'
json_file = f'data/{city_id}.json'
try:
with open(json_file, 'r') as f:
data = json.load(f)
return data
except:
pass
try:
response = requests.get(url, timeout=30)
if response.status_code == 200:
with open(json_file, 'w') as f:
json.dump(response.json(), f)
return response.json()
else:
get_district_list(province_id, city_id)
except:
print(f'Error get_district_list: {url}, {traceback.format_exc()}')
get_district_list(province_id, city_id)
def get_village_list(province_id, city_id, district_id):
json_file = f'data/{district_id}.json'
url = api_url + enpoint_ppwp + province_id + '/' + city_id + '/' + district_id + '.json'
try:
with open(json_file, 'r') as f:
data = json.load(f)
return data
except:
pass
try:
response = requests.get(url, timeout=30)
if response.status_code == 200:
with open(json_file, 'w') as f:
json.dump(response.json(), f)
return response.json()
else:
get_village_list(province_id, city_id, district_id)
except:
print(f'Error get_village_list: {url}, {traceback.format_exc()}')
get_village_list(province_id, city_id, district_id)
def get_tps_list(province_id, city_id, district_id, village_id):
json_file = f'data/{village_id}.json'
url = api_url + enpoint_ppwp + province_id + '/' + city_id + '/' + district_id + '/' + village_id + '.json'
try:
with open(json_file, 'r') as f:
data = json.load(f)
return data
except:
pass
try:
response = requests.get(url, timeout=30)
if response.status_code == 200:
with open(json_file, 'w') as f:
json.dump(response.json(), f)
return response.json()
else:
get_tps_list(province_id, city_id, district_id, village_id)
except:
print(f'Error get_tps_list: {url}, {traceback.format_exc()}')
get_tps_list(province_id, city_id, district_id, village_id)
def get_tps_detail(province_id, city_id, district_id, village_id, tps_id):
url = api_url + enpoint_hhcw_ppwp + province_id + '/' + city_id + '/' + district_id + '/' + village_id + '/' + tps_id + '.json'
try:
response = requests.get(url, timeout=30)
if response.status_code == 200:
return response.json()
else:
get_tps_detail(province_id, city_id, district_id, village_id, tps_id)
except:
print(f'Error get_tps_detail: {url}, {traceback.format_exc()}')
get_tps_detail(province_id, city_id, district_id, village_id, tps_id)
def get_kelurahan_detail(province_id, city_id, district_id, village_id):
url = api_url + enpoint_hhcw_ppwp + province_id + '/' + city_id + '/' + district_id + '/' + village_id + '.json'
try:
response = requests.get(url, timeout=30)
if response.status_code == 200:
return response.json()
else:
get_kelurahan_detail(province_id, city_id, district_id, village_id)
except:
print(f'Error get_kelurahan_detail: {url}, {traceback.format_exc()}')
get_kelurahan_detail(province_id, city_id, district_id, village_id)
def get_kecamatan_detail(province_id, city_id, district_id):
url = api_url + enpoint_hhcw_ppwp + province_id + '/' + city_id + '/' + district_id + '.json'
try:
response = requests.get(url, timeout=30)
if response.status_code == 200:
return response.json()
else:
get_kecamatan_detail(province_id, city_id, district_id)
except:
print(f'Error get_kelurahan_detail: {url}, {traceback.format_exc()}')
get_kecamatan_detail(province_id, city_id, district_id)
def get_candidate_list():
url = api_url + endpoint_candidate
response = requests.get(url, timeout=30)
if response.status_code == 200:
return response.json()
else:
get_candidate_list()