Created
September 10, 2020 14:18
-
-
Save jkcgs/349a6c6fcd6f3835a5adf33f3295938d to your computer and use it in GitHub Desktop.
Seguimiento Bluex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from json import JSONDecodeError | |
import requests | |
import time | |
# Importante: instalar requests! Solo python 3! | |
ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36' | |
apikey = '' # API key de telegram | |
tg_endpoint = 'https://api.telegram.org/bot{apikey}/{method}' | |
tg_channel = '' # Canal de telegram donde enviar los mensajes | |
bluex_seg = '' # Código de seguimiento de bluex | |
bluex_api = 'http://www.bluex.cl/PortalPublico/nacional.xhtml?documentos='+bluex_seg | |
current_data = '' | |
def find_str(cont, ini, end): | |
try: | |
idx_ini = cont.index(ini) + len(ini) | |
idx_end = cont[idx_ini:].index(end) + idx_ini | |
return cont[idx_ini:idx_end] | |
except ValueError: | |
return None | |
def telegram_call(method, payload=None): | |
if payload is None: | |
payload = {} | |
response = requests.post(tg_endpoint.format(apikey=apikey, method=method), json=payload) | |
return response | |
if __name__ == '__main__': | |
while True: | |
try: | |
print('cargando datos...') | |
req = requests.get(bluex_api, headers={'User-Agent': ua}) | |
print('datos cargados') | |
data = req.text | |
new_data = find_str(data, 'tableCaption">Detalle Progreso</span>', '</t') | |
new_time = find_str(data, 'tableCaption">Fecha Últ. Evento</span>', '</t') | |
print(new_data, new_time) | |
if new_data and new_data != current_data: | |
print(new_data) | |
msg = f'Nuevo estado seguimiento {bluex_seg}: "{new_data}" ({new_time})' | |
r = telegram_call('sendMessage', {'chat_id': tg_channel, 'text': msg}) | |
current_data = new_data+'' | |
else: | |
print('no se encontraron nuevos datos') | |
time.sleep(60) | |
except (JSONDecodeError, requests.exceptions.RequestException): | |
print('error de json o requests, esperando 10 segundos') | |
time.sleep(10) | |
except KeyboardInterrupt: | |
print('bye') | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment