Skip to content

Commit

Permalink
desarrollando log online y abstracción del Check para tener más de uno
Browse files Browse the repository at this point in the history
  • Loading branch information
tetradog committed Jul 13, 2021
1 parent d2d642d commit 82c2049
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 58 deletions.
72 changes: 14 additions & 58 deletions CheckFenix.TelegramBot.Python/CheckFenix.TelegramBot.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,20 @@
from Capitulo import Capitulo
from os.path import exists
import numpy
import time
from Checker import Checker
import sys
import telegram



pathConfig="config";
if exists(pathConfig):
fConfig = open(pathConfig, "r");
config = fConfig.readlines();
fConfig.close();
pathFile=config[0].replace("\n","");
uriWeb=config[1].replace("\n","");
channel=config[2].replace("\n","");
apiKey=config[3].replace("\n","");
elif len(sys.argv)==5:
pathFile=sys.argv[1];
uriWeb=sys.argv[2];
channel=sys.argv[3];
apiKey=sys.argv[4];
fConfig = open(pathConfig, 'w');
fConfig.writelines([pathFile+"\n",uriWeb+"\n",channel+"\n",apiKey+"\n"]);
fConfig.close();
elif len(sys.argv)>0:
raise Exception("Se necesitan más parametros: file,web,channel,apiKey");
else:
raise Exception("Se necesita la configuración ya sea por parametro o por archivo de file,web,channel,apiKey");

import asyncio

async def Main():
checker=Checker();
checker.Load(sys.argv);
await checker.Update();

if __name__ == '__main__':
mainLoop = asyncio.get_event_loop();
try:
print("CheckFenix V2.0 Telegram bot");
task_object_loop = mainLoop.create_task(Main());
mainLoop.run_until_complete(task_object_loop);
finally:
mainLoop.close();

print("CheckFenix V1.0 Telegram bot");
#leo los capitulos ya publicados y los añado al diccionario
capitulosPublicados={};
channel="@"+channel;
bot = telegram.Bot(apiKey);
if exists(pathFile):
fCapitulos = open(pathFile, "r");
capitulosGuardados = fCapitulos.readlines();
fCapitulos.close();
for capitulo in capitulosGuardados:
capitulosPublicados[capitulo.replace("\n","")]=capitulo;


while True:
try:
for capitulo in numpy.flip(numpy.array(Capitulo.GetCapitulos(uriWeb))):
if capitulo.Name not in capitulosPublicados:
print(capitulo.Name);
capitulosPublicados[capitulo.Name]=capitulo.Name;
fCapitulos = open(pathFile, 'a');
fCapitulos.write(capitulo.Name+"\n");
fCapitulos.close();
#publico el capitulo
bot.send_photo(channel, capitulo.Picture,capitulo.Name+"\n"+capitulo.GetLinkMega());
print("Descanso de 5 min");
time.sleep(5*60);
except:
print("Sin conexión, vuelvo a intentarlo en 10 segundos");
time.sleep(10);

166 changes: 166 additions & 0 deletions CheckFenix.TelegramBot.Python/Checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
from os.path import exists
from Capitulo import Capitulo
import os
import numpy
import time
import telegram


class Checker(object):
CheckLog="@CheckLog";

def __init__(self,configFileName="config"):
self.ConfigFileName=configFileName;
self.TotalLoop =-1;
self.TotalLoopLoaded=False;
self.Bot=None;
self.ChatLogId=-1;
self.ChatLogIdLoaded=False;

def Load(self,args):
if len(args)>=4:
self.Web=args[0];
self.Channel=args[1];
self.ApiBotKey=args[2];
if len(args)>3:
self.TotalLoop=args[3];
if len(args)>4:
self.ChatLogId=args[4];
self.ChatLogIdLoaded=True;

elif exists(self.ConfigFileName):
fConfig = open(self.ConfigFileName, "r");
config = fConfig.readlines();
fConfig.close();
self.Web=config[0].replace("\n","");
self.Channel=config[1].replace("\n","");
self.ApiBotKey=config[2].replace("\n","");
if len(config)>4:
self.TotalLoop=config[3].replace("\n","");
self.TotalLoopLoaded=True;
if len(config)>=5:
self.ChatLogId=config[4].replace("\n","");
self.ChatLogIdLoaded=True;

else:
raise Exception("Se necesita informacion para iniciar el BOT!");

if len(args)>1:
self.TotalLoop=args[0];

self.UpdateConfig();

if not isinstance(self.TotalLoop, int):
self.TotalLoop=int(self.TotalLoop);
if not isinstance(self.ChatLogId, int):
self.ChatLogId=int(self.ChatLogId);
if not self.Channel.startswith("@"):
self.Channel="@"+str(self.Channel);

def UpdateConfig(self):
if exists(self.ConfigFileName):
os.remove(self.ConfigFileName);
config=[self.Web+"\n",self.Channel+"\n",self.ApiBotKey+"\n"];
fConfig = open(self.ConfigFileName, 'w');
if(self.TotalLoopLoaded):
config.append(str(self.TotalLoop)+"\n");
else:
config.append("-1\n");
if(self.ChatLogIdLoaded):
config.append(str(self.ChatLogId)+"\n");
fConfig.writelines(config);
fConfig.close();

async def InitUpdate(self):
if self.Bot is None:
self.Bot=telegram.Bot(self.ApiBotKey);

self.DicCapitulos={};
if self.ChatLogId>=0:
self.ChatLog=self.Bot.getChat(Checker.CheckLog);#-1001318966076
messageLog=self.Bot.forward_message(self.ChatLog.id,self.Bot.id,self.ChatLogId);
self.ChatChannel=self.Bot.getChat(self.Channel);
for postId in messageLog.split('\n'):
if Checker.isNumber(postId):
post=self.Bot.forward_message(self.ChatChannel.id,self.Bot.id,int(postId));
capitulo=post.split('\n')[0];
self.DicCapitulos[capitulo]=postId;
else:
message=self.Bot.send_message(Checker.CheckLog,"Init "+str(self.Channel)+"\n");
self.ChatLogId=message.message_id;
self.ChatLogIdLoaded=True;
self.UpdateConfig();


async def Update(self):
init=True;
hasAnError=False;

if self.TotalLoop>=0:
for i in range(0,self.TotalLoop):
self._WaitDescanso(init,hasAnError);
hasAnError=self._WaitAnError(await self.OneLoopUpdate());
init=False;
else:
while True:
self._WaitDescanso(init,hasAnError);
hasAnError=self._WaitAnError(await self.OneLoopUpdate());
init=False;

def _WaitAnError(self,hasAnError):
if hasAnError:
print("Sin conexión, vuelvo a intentarlo en 10 segundos");
time.sleep(10);
return hasAnError;
def _WaitDescanso(self,init,hasAnError):
if not init and not hasAnError:
print("Descanso de 5 min");
time.sleep(5*60);


async def OneLoopUpdate(self):
await self.InitUpdate();
try:
for capitulo in numpy.array(Capitulo.GetCapitulos(self.Web)):
if capitulo.Name not in self.DicCapitulos:
print(capitulo.Name);
self.DicCapitulos[capitulo.Name]=self.Bot.send_photo(self.ChatChannel.id, capitulo.Picture,capitulo.Name+"\n"+capitulo.GetLinkMega()).message_id;
self.UpdateLog();

hasAnError=False;
except:
hasAnError=True;
return hasAnError;
def UpdateLog(self):
strMessageLog="";
for idPost in self.DicCapitulos.values():
strMessageLog+=str(idPost)+"\n";
self.Bot.edit_message_text(strMessageLog,Checker.CheckLog,self.ChatLogId);




@staticmethod #https://www.geeksforgeeks.org/implement-isnumber-function-in-python/
# Implementation of isNumber() function
def isNumber(s):

# handle for negative values
isNum=s!=None and s!="";
if isNum:
negative = False
if(s[0] =='-'):
negative = True;

if negative == True:
s = s[1:];

# try to convert the string to int
try:
dummy = int(s)
isNum= True;
# catch exception if cannot be converted
except ValueError:
isNum= False;
return isNum;


0 comments on commit 82c2049

Please sign in to comment.