forked from yachaycode/api-resultados-onpe-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextraccion_actas_mesas_onpe.py
64 lines (58 loc) · 1.96 KB
/
extraccion_actas_mesas_onpe.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
import requests
import time
import json
from random import randint
from driver import DriverChrome
from utils import Utils
class ApiOnpe(DriverChrome):
def __init__(self, *args, **kwargs):
"""It only works in the foreground, please respect the security policies of ONPE """
self.driver=self.init_driver_chrome()
self.url_api = 'https://api.resultadossep.eleccionesgenerales2021.pe/results/10/000002'
self.url_home = 'https://www.resultadossep.eleccionesgenerales2021.pe/SEP2021/EleccionesPresidenciales/RePres/T'
# self.driver.get(self.url_home)
def get_data_onpe(self, url_api):
"""getting raw data """
self.driver.get(url_api)
def curation_data(self):
# extract data in json from html
data_json = {}
data = ''
try:
data_html = self.driver.page_source
data = data_html.split('pre-wrap;">')[1].split('</pre>')[0]
if 'statusCode' in data:
print ("Posiblimente no existe la mesa..")
return {}
data_json = json.loads(data)
except Exception as e:
print ("Error, You may have changed the structure of the html", e)
if 'statusCode' in str(e):
print ("Posiblimente no exste la mesa.")
raise e
return data_json
api_onpe = ApiOnpe()
utils = Utils()
count =1 # Reintents
sleep_gets = 0
base_count_act = 1 # Nuber acts
while True:
id_act = str(base_count_act).zfill(6) # 000001, 000002...etc
url_base_api_act = "https://api.resultadossep.eleccionesgenerales2021.pe/mesas/detalle/{}?name=param".format(id_act)
base_count_act +=1
sleep_gets +=1
id_mongo = utils.get_id_table(id_act)
api_onpe.get_data_onpe(url_base_api_act)
date_complete_api = api_onpe.curation_data()
if date_complete_api:
utils.save_data_mongo_table(date_complete_api, id_mongo, id_act)
else:
print('Posiblimente no existe acta ...', count)
count +=1
if count>=10:
print("Posiblimente exedio número de reintentos....")
exit()
if sleep_gets>=500:
time.sleep(randint(30, 60)) # sleep every 500 requests..
sleep_gets = 0
print ("Insertando acta..:", id_act)