-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_partai.py
47 lines (34 loc) · 1016 Bytes
/
get_partai.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
import urllib.request
import json
import ssl
import csv
from Constants import *
from util_logging import *
def getData():
context = ssl._create_unverified_context()
print("Request Daftar Partai ...")
try:
url = urllib.request.urlopen(BASE_URL + 'allparpol.json?', context = context)
data = json.loads(url.read().decode())
except Exception as e:
message_string = "ERROR request daftar partai --" + str(e)
pecker(LOG_PARTAI_DATA, message_string)
data = ""
return data
def generateCSV():
data = getData()
print("Generating CSV Daftar Partai ...")
partai_data = open(BASE_LOCAL_PATH + 'data_partai.csv', 'w')
csvwriter = csv.writer(partai_data)
count = 0
for partai in data:
if count == 0:
header = partai.keys()
csvwriter.writerow(header)
count += 1
csvwriter.writerow(partai.values())
partai_data.close()
message_string = "––– CSV Daftar Partai created ({} data) –––".format(len(data))
print(message_string)
pecker(LOG_PARTAI_DATA, message_string)
return