92 lines
3.3 KiB
Python
Executable File
92 lines
3.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# encoding: utf-8
|
|
#
|
|
# 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
|
|
#
|
|
# Argumentos opcionales:
|
|
# -f, --festive Comprobar festivo
|
|
# -h, --help Esta ayuda
|
|
#
|
|
#
|
|
# Creado: Omar Sánchez 04-05-2019
|
|
|
|
# Importamos librerias
|
|
import os
|
|
import json
|
|
import sys
|
|
from datetime import datetime, timedelta
|
|
import requests
|
|
import urllib.parse
|
|
import argparse
|
|
|
|
# Introducimos los argumentos obligatorios y opcionales
|
|
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", required=True)
|
|
obligatoryArgs.add_argument('-t', '--type', help="Tipo marcado. 0 = Entrada, 1 = Salida", required=True)
|
|
|
|
optionalArgs = parser.add_argument_group("Argumentos opcionales")
|
|
optionalArgs.add_argument('-f', '--festive', action="store_false", help="Comprobar festivo")
|
|
optionalArgs.add_argument('-h', '--help', action="help", help="Esta ayuda")
|
|
|
|
args = parser.parse_args()
|
|
|
|
class AutoFicher():
|
|
# URL de la api
|
|
url = "https://timenet.gpisoftware.com/api/v1/cp/"
|
|
# Definimos las variables de tiempo
|
|
date = datetime.now().strftime("%d/%m/%Y+%H:%M:%S")
|
|
#calendar = (datetime.now() + timedelta(days=3)).strftime("%d/%m/%Y")
|
|
calendar = datetime.now().strftime("%d/%m/%Y")
|
|
|
|
token = ""
|
|
|
|
def __init__(self, task = ''):
|
|
# Iniciamos Sesión y obtenemos el token
|
|
headers = {'user': args.user, 'pass': args.pin}
|
|
response = requests.get(self.url+'login', headers=headers)
|
|
self.token = response.text.replace('"','')
|
|
|
|
def isFestive(self):
|
|
# Comprobamos si esta habilitado el comprobar festivo (Por defecto es True)
|
|
if args.festive:
|
|
# Revisamos el dia en el calendario y comprobamos si es festivo o no
|
|
headers = {'token': self.token}
|
|
response = requests.get(self.url+"calendar?start="+self.calendar+"&end="+self.calendar, headers=headers)
|
|
dayType = json.loads(response.text)["DayTypes"][0]["dayMode"]
|
|
|
|
if dayType == "NO_WORK":
|
|
return True
|
|
else:
|
|
return False
|
|
else:
|
|
return False
|
|
|
|
def sendUpdate(self):
|
|
# Si no es festivo..
|
|
if not self.isFestive():
|
|
# Hacemos la llamada a la api para marcar o desmarcar
|
|
print("Dia de curro")
|
|
headers = {"Content-type": "application/x-www-form-urlencoded", "token": self.token}
|
|
data = {"typ": args.type, "date": urllib.parse.quote(self.date), "geoLatitude": "41.3908992", "geoLongitude": "2.154496", "geoErrors": ""}
|
|
|
|
response = requests.post(self.url+"checks", data=data, headers=headers)
|
|
if response.status_code != 200:
|
|
print("Error "+str(response.status_code)+" al realizar el envio: "+ response.request.body)
|
|
else:
|
|
print(response.text)
|
|
else:
|
|
print("Hoy no se trabaja")
|
|
|
|
if __name__ == '__main__':
|
|
AutoFicher().sendUpdate()
|