2024-07-29 15:29:15 -04:00
|
|
|
# coding: utf-8
|
|
|
|
from apscheduler.schedulers.background import BlockingScheduler
|
2023-12-10 19:17:03 -03:00
|
|
|
from datetime import datetime
|
2024-07-29 15:29:15 -04:00
|
|
|
from main import read_file_proto_x_stop, r, tz
|
2023-12-10 19:17:03 -03:00
|
|
|
import requests
|
2024-07-29 15:29:15 -04:00
|
|
|
import logging
|
2023-12-10 19:17:03 -03:00
|
|
|
import json
|
|
|
|
|
2024-07-29 15:29:15 -04:00
|
|
|
import os
|
2023-12-10 19:17:03 -03:00
|
|
|
|
2024-07-29 15:29:15 -04:00
|
|
|
|
|
|
|
def rutina_principal():
|
2023-12-10 19:17:03 -03:00
|
|
|
print(datetime.now(tz))
|
|
|
|
|
2024-07-29 15:29:15 -04:00
|
|
|
fileproto = download_file_backend()
|
2023-12-13 10:15:53 -03:00
|
|
|
if fileproto == None:
|
2023-12-10 19:17:03 -03:00
|
|
|
return False
|
2023-12-13 10:15:53 -03:00
|
|
|
|
|
|
|
namefile = r.get('fileproto')
|
|
|
|
if namefile == fileproto['name']:
|
2024-03-02 18:10:51 -03:00
|
|
|
print(namefile, flush=True)
|
2023-12-13 10:15:53 -03:00
|
|
|
return
|
2024-07-29 15:29:15 -04:00
|
|
|
|
2024-03-02 18:10:51 -03:00
|
|
|
# eliminar toda la informacion actual
|
|
|
|
r.flushdb()
|
2024-07-29 15:29:15 -04:00
|
|
|
|
2023-12-13 10:15:53 -03:00
|
|
|
r.set('fileproto', fileproto['name'])
|
2024-03-02 18:10:51 -03:00
|
|
|
print(fileproto['name'], flush=True)
|
2023-12-13 10:15:53 -03:00
|
|
|
data = read_file_proto_x_stop(fileproto['content'])
|
2023-12-10 19:17:03 -03:00
|
|
|
|
|
|
|
# save data to redis db
|
2023-12-12 22:49:30 -03:00
|
|
|
for key in data:
|
|
|
|
r.set(key, json.dumps(data[key]))
|
|
|
|
|
2024-07-29 15:29:15 -04:00
|
|
|
# print(json.dumps(data['route:549-1'], indent=4))
|
2023-12-10 19:17:03 -03:00
|
|
|
|
|
|
|
|
2024-07-29 15:29:15 -04:00
|
|
|
def download_file_backend():
|
|
|
|
response = requests.get(os.environ.get('BACKEND'))
|
2023-12-10 19:17:03 -03:00
|
|
|
if response.status_code == 200:
|
2023-12-13 10:15:53 -03:00
|
|
|
content_disposition = response.headers['Content-Disposition']
|
|
|
|
nombre_archivo = content_disposition.split('filename=')[1]
|
|
|
|
return { 'name': nombre_archivo, 'content': response.content }
|
2023-12-10 19:17:03 -03:00
|
|
|
return None
|
|
|
|
|
2024-07-29 15:29:15 -04:00
|
|
|
sched = BlockingScheduler()
|
|
|
|
sched.add_job(rutina_principal, 'interval', seconds=int(os.environ.get('INTERVAL'))) #will do the print_t work for every 30 seconds
|
2023-12-12 22:49:30 -03:00
|
|
|
|
2024-07-29 15:29:15 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sched.start()
|