From 6b7a7a24e4e9f31837c76c6ac6283b0834b99ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20S=C3=A1nchez?= Date: Fri, 8 Oct 2021 14:09:18 +0200 Subject: [PATCH] json config --- .gitignore | 1 + autoficher.py | 27 +++++++++++++++++++-------- config.json | 0 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 config.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0cffcb3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json \ No newline at end of file diff --git a/autoficher.py b/autoficher.py index 518ef25..e532892 100755 --- a/autoficher.py +++ b/autoficher.py @@ -36,11 +36,13 @@ obligatoryArgs.add_argument('-p', '--pin', help="Contraseña", type=int, require obligatoryArgs.add_argument('-t', '--type', help="Tipo marcado. 0 = Entrada, 1 = Salida", type=int) 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('-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") args = parser.parse_args() + class AutoFicher(): # URL de la api headers = { @@ -51,7 +53,11 @@ class AutoFicher(): config = None def __init__(self): - self.loadConfig() + try: + self.loadConfig() + except: + print("No se ha podido cargar el archivo de configuración") + exit(20) def loadConfig(self): with open('sample.config.json', 'r') as f: @@ -62,17 +68,19 @@ class AutoFicher(): bot.sendMessage(chat_id=self.config['telegram']['chat_id'], text=str(message)) def get(self, url, data={}, headers={}): + furl = self.config['api_url'] + url if url != "version": - self.url = self.url + "v1/" - return requests.get(self.config['api_url']+url, data=data, headers=headers) + furl = self.config['api_url'] + "v1/" + url + return requests.get(furl, data=data, headers=headers) def post(self, url, data={}, headers={}): + furl = self.config['api_url'] + url if url != "version": - self.url = self.url + "v1/" - return requests.post(self.config['api_url']+url, data=data, headers=headers) + furl = self.config['api_url'] + "v1/" + url + return requests.post(furl, data=data, headers=headers) def updateTime(self): - self.headers["tn-d"] = "\""+datetime.now().astimezone(pytz.UTC).strftime("%Y-%m-%dT%H:%M:%SZ")+"\"" + self.headers["tn-d"] = "\"" + datetime.now().astimezone(pytz.UTC).strftime("%Y-%m-%dT%H:%M:%SZ") + "\"" def getVersion(self): self.updateTime() @@ -97,7 +105,8 @@ class AutoFicher(): elif hour in hOUT: typ = 1 else: - print("No se ha fichado ni desfichado ya que estamos en horario laboral, fuerze el estado con el parametro --type <0 = Entrada, 1 = Salida>") + print( + "No se ha fichado ni desfichado ya que estamos en horario laboral, fuerze el estado con el parametro --type <0 = Entrada, 1 = Salida>") else: typ = args.type @@ -120,5 +129,7 @@ class AutoFicher(): else: self.sendReport("No se ha podido fichar, la web no ha devuelto 200 OK") + if __name__ == '__main__': af = AutoFicher() + af.sendUpdate() diff --git a/config.json b/config.json new file mode 100644 index 0000000..e69de29