From b612fcb4accf4f2a81cb413b368190d469d46cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20S=C3=A1nchez=20Pizarro?= Date: Mon, 1 Dec 2025 20:03:46 +0100 Subject: [PATCH] feat: now check last check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Omar Sánchez Pizarro --- app/telegram_bot.py | 34 +++++++++++++++++++---- app/timenet_manager.py | 61 +++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 21 deletions(-) diff --git a/app/telegram_bot.py b/app/telegram_bot.py index 22915ab..92b8715 100644 --- a/app/telegram_bot.py +++ b/app/telegram_bot.py @@ -1,14 +1,38 @@ from app import config_parser import telegram -import random +from importlib.metadata import version +import asyncio config = config_parser.ConfigParser().loadConfig() - class telegramBot: bot = None async def sendMessage(self, message): - self.bot = telegram.Bot(config['telegram']['token']) - async with self.bot: - await self.bot.sendMessage(chat_id=config['telegram']['chat_id'], text=str(message)) + ver = version("python-telegram-bot") + major = int(ver.split(".")[0]) + + + token = config["telegram"]["token"] + chat_id = config["telegram"]["chat_id"] + + # -------------------- + # PTB v20+ (async) + # -------------------- + if major >= 20: + self.bot = telegram.Bot(token) + await self.bot.send_message(chat_id=chat_id, text=str(message)) + return + + # -------------------- + # PTB v12/v13 (sync) + # -------------------- + else: + self.bot = telegram.Bot(token) + + # ejecuta el método sync en un hilo para no romper asyncio.run() + loop = asyncio.get_running_loop() + await loop.run_in_executor( + None, + lambda: self.bot.send_message(chat_id=chat_id, text=str(message)) + ) diff --git a/app/timenet_manager.py b/app/timenet_manager.py index dacabe1..467acc6 100644 --- a/app/timenet_manager.py +++ b/app/timenet_manager.py @@ -9,10 +9,20 @@ from app import arg_parser, config_parser, telegram_bot, google_calendar, utils args = arg_parser.ArgParser().parse() config = config_parser.ConfigParser().loadConfig() - tnv_string = "Tn-V" tnd_string = "Tn-D" + +conditional_response = [{ + "text": "fichado", + "text2": "fichar", + "emoji": "🕐🏢🏃‍♂️🏡" +},{ + "text": "desfichado", + "text2": "desfichar", + "emoji": "🏡🏃‍♂️🏢🕐" +}] + class timenetManager: telegram = None message_acumulator = "" @@ -32,8 +42,12 @@ class timenetManager: "Sec-Ch-Ua-Mobile": "?0", "Sec-Ch-Ua-Platform": "\"Linux\"", "Sec-Ch-Ua": "\"Chromium\";v=\"138\", \"Not=A?Brand\";v=\"8\", \"Google Chrome\";v=\"138\"", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-origin", tnv_string: "wcp_8.0.0.2", - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36" + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36", + "Tn-U": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36" } self.login() @@ -65,7 +79,7 @@ class timenetManager: print("Error al iniciar sesión: %s" % response.text) exit(20) - self.headers["Token"] = response.text + self.headers["Token"] = response.text.replace("\"", "") def addMessage(self, message): self.message_acumulator += message + "\n" @@ -76,7 +90,9 @@ class timenetManager: self.message_acumulator = "" def updateTime(self): - self.headers[tnd_string] = "\"" + datetime.now().astimezone(pytz.UTC).strftime("%Y-%m-%dT%H:%M:%SZ") + "\"" + now = datetime.now().astimezone(pytz.UTC) + self.headers[tnd_string] = "\"" + now.strftime("%Y-%m-%dT%H:%M:%SZ") + "\"" + self.headers["dStr"] = "\"" + now.strftime("%d/%m/%Y %H:%M:%S") + "\"" def updateVersion(self): self.headers[tnv_string] = self.getVersion() @@ -87,17 +103,26 @@ class timenetManager: def getInfo(self): return self.get('check/info?guid=' + config['user'], {}, self.headers) + + def checkLastCheck(self, typ): + response = self.get('cp/checks?start=' + datetime.now().strftime("%d/%m/%Y") + '&end=' + datetime.now().strftime("%d/%m/%Y"), {}, self.headers) + if response.status_code != 200: + self.addMessage("Error al obtener los checks: %s" % response.text) + self.sendReport() + return False + + + today_checks = json.loads(response.text) + if today_checks['C'] and today_checks['C'][0]['C'][-1]['T'] == typ: + self.addMessage("Ya se ha realizado este marcaje antes a las %s" % today_checks['C'][0]['C'][-1]['H']) + self.sendReport() + return True + + + return False + def sendUpdate(self): - conditional_response = [{ - "text": "fichado", - "text2": "fichar", - "emoji": "🕐🏢🏃‍♂️🏡" - },{ - "text": "desfichado", - "text2": "desfichar", - "emoji": "🏡🏃‍♂️🏢🕐" - }] calendar = google_calendar.GoogleCalendar() calendar = calendar.getEvent() @@ -123,6 +148,10 @@ class timenetManager: else: typ = args.type + + if self.checkLastCheck(typ): + return; + response = None if not args.debug: self.addMessage("####HACIENDO CHECK####") @@ -137,9 +166,9 @@ class timenetManager: "geoLatitude": config['geo']['latitude'], "geoLongitude": config['geo']['longitude'], "geoError": "", - "geoAccuracy": config['geo']['accuracy'] + "dStr": self.headers["dStr"].replace("\"", "") } - #response = self.post("cp/checks", data, self.headers) + response = self.post("cp/checks", data, self.headers) else: self.addMessage('Corriendo en modo debug. No se realizará ninguna acción') self.sendReport() @@ -153,7 +182,7 @@ class timenetManager: try: rj = json.loads(response.text) except: - self.addMessage("La respuesta al hacer check no es correcta... algo ha pasado :/ - %s" % (response.text)) + self.addMessage("La respuesta al hacer check no es correcta... algo ha pasado :/ - %s - %s" % (response.text, response.status_code)) self.sendReport() exit(20)