From 585a8b9a5dce70ada7aac158037d105b6faafa98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20S=C3=A1nchez?= Date: Fri, 15 Oct 2021 14:21:13 +0200 Subject: [PATCH] config replacement form arguments --- autoficher.py | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/autoficher.py b/autoficher.py index 8855ce1..954718d 100755 --- a/autoficher.py +++ b/autoficher.py @@ -3,12 +3,12 @@ # # Automarcaje para timenet.gpisoftware.com # usage: autoficher.py -u USER -p PIN -t TYPE [-f] [-h] -# +# # Argumentos obligatorios: # -u USER, --user USER Usuario # -p PIN, --pin PIN Contraseña # -t TYPE, --type TYPE Tipo marcado. 0 = Entrada, 1 = Salida (Si no se utiliza -bt) -# +# # # Creado: Omar Sánchez 04-05-2019 @@ -27,7 +27,6 @@ parser = argparse.ArgumentParser(add_help=False) parser._action_groups.pop() obligatoryArgs = parser.add_argument_group("Argumentos obligatorios") -# obligatoryArgs.add_argument('-u', '--user', help="Usuario", required=True) obligatoryArgs.add_argument('-p', '--pin', help="Contraseña", type=int, required=True) obligatoryArgs.add_argument('-t', '--type', help="Tipo marcado. 0 = Entrada, 1 = Salida", type=int) @@ -35,6 +34,12 @@ optionalArgs = parser.add_argument_group("Argumentos opcionales") optionalArgs.add_argument('-bt', '--basedtime', help="Hacer check o descheck según la hora del dia", action="store_true") optionalArgs.add_argument('-h', '--help', action="help", help="Esta ayuda") +optionalArgs.add_argument('-d', '--debug', action='store_true') + +optionalArgs.add_argument('-c', '--config', help="Rúta al archivo de configuración") +optionalArgs.add_argument('-u', '--user', help="Usuario") +optionalArgs.add_argument('-glo', '--geoLongitude', help="Longitud") +optionalArgs.add_argument('-gla', '--geoLatitude', help="Latitud") args = parser.parse_args() @@ -56,9 +61,23 @@ class AutoFicher(): exit(20) def loadConfig(self): - with open('config.json', 'r') as f: + path = 'config.json' + + if args.config: + path = args.config + + with open(path, 'r') as f: self.config = json.load(f) + if args.geoLatitude: + self.config['geo']['latitude'] = args.geoLatitude + if args.geoLongitude: + self.config['geo']['longitude'] = args.geoLongitude + if args.user: + self.config['user'] = args.user + + print(self.config) + def sendReport(self, message): bot = telegram.Bot(token=self.config['telegram']['token']) bot.sendMessage(chat_id=self.config['telegram']['chat_id'], text=str(message)) @@ -118,18 +137,32 @@ class AutoFicher(): "geoAccuracy": self.config['geo']['accuracy'] } - response = self.post("check", data, self.headers) - rj = json.loads(response.text) + response = None + if not args.debug: + print("####HACIENDO CHECK####") + ##response = self.post("check", data, self.headers) + try: + rj = json.loads(response.text) + except: + print("La respuesta al hacer check no es correcta... algo ha pasado :/") + exit(20) + if args.debug: + self.sendReport('Corriendo en modo debug. No se realizará ninguna acción') if response.status_code == 200: if rj['Repeated']: time = datetime.strptime(rj['RepeatedTime'], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=pytz.utc).astimezone( pytz.timezone("Europe/Madrid")).strftime("%H:%M") self.sendReport("Se ha realizado un marcaje antes a las %s" % time) else: - time = datetime.strptime(self.headers["tn-d"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=pytz.utc).astimezone(pytz.timezone("Europe/Madrid")).strftime("%H:%M") - self.sendReport('Has fichado correctamente a las %s' % time) + time = datetime.strptime(self.headers["tn-d"], "'%Y-%m-%dT%H:%M:%SZ'").replace( + tzinfo=pytz.utc).astimezone(pytz.timezone("Europe/Madrid")).strftime("%H:%M") + + rtext = 'fichado' + if typ: + rtext = 'desfichado' + self.sendReport('Has %s correctamente a las %s' % (rtext, time)) else: self.sendReport("No se ha podido fichar, la web no ha devuelto 200 OK")