From e9ec438183e8c4a1cb37fe2845a7f37a1cbc4f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20S=C3=A1nchez=20Pizarro?= Date: Thu, 9 Nov 2023 18:48:07 +0100 Subject: [PATCH] cosas bonitas con el fiching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Omar SΓ‘nchez Pizarro --- .idea/misc.xml | 3 +++ app/telegram_bot.py | 1 + app/timenet_manager.py | 25 ++++++++++++++----------- app/utils.py | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 app/utils.py diff --git a/.idea/misc.xml b/.idea/misc.xml index a15a73d..9839cf3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/app/telegram_bot.py b/app/telegram_bot.py index fc3d9e3..22915ab 100644 --- a/app/telegram_bot.py +++ b/app/telegram_bot.py @@ -1,5 +1,6 @@ from app import config_parser import telegram +import random config = config_parser.ConfigParser().loadConfig() diff --git a/app/timenet_manager.py b/app/timenet_manager.py index 8447454..c49b983 100644 --- a/app/timenet_manager.py +++ b/app/timenet_manager.py @@ -4,7 +4,7 @@ import requests, json, pytz from datetime import datetime from time import sleep import random -from app import arg_parser, config_parser, telegram_bot, google_calendar +from app import arg_parser, config_parser, telegram_bot, google_calendar, utils args = arg_parser.ArgParser().parse() config = config_parser.ConfigParser().loadConfig() @@ -41,6 +41,7 @@ class timenetManager: def sendReport(self): # self.telegram.sendMessage(self.message_acumulator) asyncio.run(self.telegram.sendMessage(self.message_acumulator)) + self.message_acumulator = "" def updateTime(self): self.headers["tn-d"] = "\"" + datetime.now().astimezone(pytz.UTC).strftime("%Y-%m-%dT%H:%M:%SZ") + "\"" @@ -58,6 +59,15 @@ class timenetManager: return self.get('check/info', {"guid": args.user}) def sendUpdate(self): + conditional_response = [{ + "text": "fichado", + "text2": "fichar", + "emoji": "πŸ•πŸ’πŸƒβ€β™‚οΈπŸ‘" + },{ + "text": "desfichado", + "text2": "desfichar", + "emoji": "πŸ‘πŸƒβ€β™‚οΈπŸ’πŸ•" + }] if args.pin is None: # if pin is None, prompt for it @@ -89,9 +99,10 @@ class timenetManager: response = None if not args.debug: self.addMessage("####HACIENDO CHECK####") - rand = random.randint(0, 230); + rand = utils.numero_aleatorio_con_probabilidad(230, 0.5) + self.addMessage("Esperando %s segundos para %s en un minuto aleatorio" % (rand,conditional_response[int(typ)]["text2"])) + self.sendReport() sleep(rand) - self.addMessage("Esperando %s segundos para fichar/desfichar en un minuto aleatorio" % rand) self.updateTime() data = { "cp": config['user'], @@ -129,14 +140,6 @@ class timenetManager: self.addMessage("πŸ• Ya se ha realizado este marcaje antes a las %s" % time) else: time = datetime.now().astimezone(pytz.timezone("Europe/Madrid")).strftime("%H:%M") - - conditional_response = [{ - "text": "fichado", - "emoji": "πŸ•πŸ’πŸƒβ€β™‚οΈπŸ‘" - },{ - "text": "desfichado", - "emoji": "πŸ‘πŸƒβ€β™‚οΈπŸ’πŸ•" - }] self.addMessage('%s Has %s correctamente a las %s' % (conditional_response[int(typ)]['emoji'], conditional_response[int(typ)]['text'], time)) else: self.addMessage("πŸŸ₯πŸ• No se ha podido fichar, la web no ha devuelto 200 OK") diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..dd53805 --- /dev/null +++ b/app/utils.py @@ -0,0 +1,15 @@ +import random + + +def numero_aleatorio_con_probabilidad(rango, probabilidad_alta): + # Generar un nΓΊmero aleatorio entre 0 y 1 + probabilidad = random.random() + + # Calcular el rango ajustado + rango_ajustado = int(rango * probabilidad_alta) + + # Generar un nΓΊmero aleatorio ponderado + numero = random.randint(0, rango_ajustado) if probabilidad < probabilidad_alta else random.randint( + rango_ajustado, rango) + + return numero \ No newline at end of file