feat: now check last check
Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
@@ -1,14 +1,38 @@
|
|||||||
from app import config_parser
|
from app import config_parser
|
||||||
import telegram
|
import telegram
|
||||||
import random
|
from importlib.metadata import version
|
||||||
|
import asyncio
|
||||||
|
|
||||||
config = config_parser.ConfigParser().loadConfig()
|
config = config_parser.ConfigParser().loadConfig()
|
||||||
|
|
||||||
|
|
||||||
class telegramBot:
|
class telegramBot:
|
||||||
bot = None
|
bot = None
|
||||||
|
|
||||||
async def sendMessage(self, message):
|
async def sendMessage(self, message):
|
||||||
self.bot = telegram.Bot(config['telegram']['token'])
|
ver = version("python-telegram-bot")
|
||||||
async with self.bot:
|
major = int(ver.split(".")[0])
|
||||||
await self.bot.sendMessage(chat_id=config['telegram']['chat_id'], text=str(message))
|
|
||||||
|
|
||||||
|
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))
|
||||||
|
)
|
||||||
|
|||||||
@@ -9,10 +9,20 @@ from app import arg_parser, config_parser, telegram_bot, google_calendar, utils
|
|||||||
args = arg_parser.ArgParser().parse()
|
args = arg_parser.ArgParser().parse()
|
||||||
config = config_parser.ConfigParser().loadConfig()
|
config = config_parser.ConfigParser().loadConfig()
|
||||||
|
|
||||||
|
|
||||||
tnv_string = "Tn-V"
|
tnv_string = "Tn-V"
|
||||||
tnd_string = "Tn-D"
|
tnd_string = "Tn-D"
|
||||||
|
|
||||||
|
|
||||||
|
conditional_response = [{
|
||||||
|
"text": "fichado",
|
||||||
|
"text2": "fichar",
|
||||||
|
"emoji": "🕐🏢🏃♂️🏡"
|
||||||
|
},{
|
||||||
|
"text": "desfichado",
|
||||||
|
"text2": "desfichar",
|
||||||
|
"emoji": "🏡🏃♂️🏢🕐"
|
||||||
|
}]
|
||||||
|
|
||||||
class timenetManager:
|
class timenetManager:
|
||||||
telegram = None
|
telegram = None
|
||||||
message_acumulator = ""
|
message_acumulator = ""
|
||||||
@@ -32,8 +42,12 @@ class timenetManager:
|
|||||||
"Sec-Ch-Ua-Mobile": "?0",
|
"Sec-Ch-Ua-Mobile": "?0",
|
||||||
"Sec-Ch-Ua-Platform": "\"Linux\"",
|
"Sec-Ch-Ua-Platform": "\"Linux\"",
|
||||||
"Sec-Ch-Ua": "\"Chromium\";v=\"138\", \"Not=A?Brand\";v=\"8\", \"Google Chrome\";v=\"138\"",
|
"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",
|
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()
|
self.login()
|
||||||
|
|
||||||
@@ -65,7 +79,7 @@ class timenetManager:
|
|||||||
print("Error al iniciar sesión: %s" % response.text)
|
print("Error al iniciar sesión: %s" % response.text)
|
||||||
exit(20)
|
exit(20)
|
||||||
|
|
||||||
self.headers["Token"] = response.text
|
self.headers["Token"] = response.text.replace("\"", "")
|
||||||
|
|
||||||
def addMessage(self, message):
|
def addMessage(self, message):
|
||||||
self.message_acumulator += message + "\n"
|
self.message_acumulator += message + "\n"
|
||||||
@@ -76,7 +90,9 @@ class timenetManager:
|
|||||||
self.message_acumulator = ""
|
self.message_acumulator = ""
|
||||||
|
|
||||||
def updateTime(self):
|
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):
|
def updateVersion(self):
|
||||||
self.headers[tnv_string] = self.getVersion()
|
self.headers[tnv_string] = self.getVersion()
|
||||||
@@ -87,17 +103,26 @@ class timenetManager:
|
|||||||
|
|
||||||
def getInfo(self):
|
def getInfo(self):
|
||||||
return self.get('check/info?guid=' + config['user'], {}, self.headers)
|
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):
|
def sendUpdate(self):
|
||||||
conditional_response = [{
|
|
||||||
"text": "fichado",
|
|
||||||
"text2": "fichar",
|
|
||||||
"emoji": "🕐🏢🏃♂️🏡"
|
|
||||||
},{
|
|
||||||
"text": "desfichado",
|
|
||||||
"text2": "desfichar",
|
|
||||||
"emoji": "🏡🏃♂️🏢🕐"
|
|
||||||
}]
|
|
||||||
|
|
||||||
calendar = google_calendar.GoogleCalendar()
|
calendar = google_calendar.GoogleCalendar()
|
||||||
calendar = calendar.getEvent()
|
calendar = calendar.getEvent()
|
||||||
@@ -123,6 +148,10 @@ class timenetManager:
|
|||||||
else:
|
else:
|
||||||
typ = args.type
|
typ = args.type
|
||||||
|
|
||||||
|
|
||||||
|
if self.checkLastCheck(typ):
|
||||||
|
return;
|
||||||
|
|
||||||
response = None
|
response = None
|
||||||
if not args.debug:
|
if not args.debug:
|
||||||
self.addMessage("####HACIENDO CHECK####")
|
self.addMessage("####HACIENDO CHECK####")
|
||||||
@@ -137,9 +166,9 @@ class timenetManager:
|
|||||||
"geoLatitude": config['geo']['latitude'],
|
"geoLatitude": config['geo']['latitude'],
|
||||||
"geoLongitude": config['geo']['longitude'],
|
"geoLongitude": config['geo']['longitude'],
|
||||||
"geoError": "",
|
"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:
|
else:
|
||||||
self.addMessage('Corriendo en modo debug. No se realizará ninguna acción')
|
self.addMessage('Corriendo en modo debug. No se realizará ninguna acción')
|
||||||
self.sendReport()
|
self.sendReport()
|
||||||
@@ -153,7 +182,7 @@ class timenetManager:
|
|||||||
try:
|
try:
|
||||||
rj = json.loads(response.text)
|
rj = json.loads(response.text)
|
||||||
except:
|
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()
|
self.sendReport()
|
||||||
exit(20)
|
exit(20)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user