1.6 dev: Added Telegram Bot Messages to Logs, Colored yellow in Terminal output.

This commit is contained in:
hockeygoalie35 2024-03-06 00:10:57 -05:00
parent ff1131e471
commit 612fa48d9e

View file

@ -98,7 +98,6 @@ class DeezerPlatformProvider:
res = res['results'] res = res['results']
if res['USER']['USER_ID'] == 0: if res['USER']['USER_ID'] == 0:
self.log.error(Fore.RED+"ARL Token Expired. Update the token in extended.conf"+Fore.LIGHTWHITE_EX)
raise AuthError() raise AuthError()
return Account(username, secret, res['COUNTRY'], Plan( return Account(username, secret, res['COUNTRY'], Plan(
@ -207,12 +206,15 @@ class LidarrExtendedAPI:
self.report_status('VALID') self.report_status('VALID')
return True return True
except Exception as e: except Exception as e:
print(e) if type(e) == AuthError:
self.log.error(Fore.RED+"ARL Token Expired/Invalid. Update the token in extended.conf"+Fore.LIGHTWHITE_EX)
else:
self.log.error(e)
self.report_status('EXPIRED') self.report_status('EXPIRED')
if self.telegram_bot_running: if self.telegram_bot_running:
return False return False
if self.enable_telegram_bot: if self.enable_telegram_bot:
self.log.info('Starting Telegram bot...Check Telegram and follow instructions.') self.log.info(Fore.YELLOW + 'Starting Telegram bot...Check Telegram and follow instructions.' + Fore.LIGHTWHITE_EX)
self.telegram_bot_running = True self.telegram_bot_running = True
self.start_telegram_bot() self.start_telegram_bot()
exit(420) exit(420)
@ -261,6 +263,7 @@ class TelegramBotControl:
# Send initial notification # Send initial notification
async def send_expired_token_notification(application): async def send_expired_token_notification(application):
await application.bot.sendMessage(chat_id=self.telegram_chat_id,text='---\U0001F6A8WARNING\U0001F6A8-----\nARL TOKEN EXPIRED\n Update Token by running "/set_token <TOKEN>"\n You can find a new ARL at:\nhttps://rentry.org/firehawk52#deezer-arls\n\n\n Other Commands:\n/cancel - Cancel this session\n/disable - Disable Telegram Bot',disable_web_page_preview=True) await application.bot.sendMessage(chat_id=self.telegram_chat_id,text='---\U0001F6A8WARNING\U0001F6A8-----\nARL TOKEN EXPIRED\n Update Token by running "/set_token <TOKEN>"\n You can find a new ARL at:\nhttps://rentry.org/firehawk52#deezer-arls\n\n\n Other Commands:\n/cancel - Cancel this session\n/disable - Disable Telegram Bot',disable_web_page_preview=True)
self.log.info(Fore.YELLOW + "Telegram Bot Sent ARL Token Expiry Message " + Fore.LIGHTWHITE_EX)
# TODO: Get Chat ID/ test on new bot # TODO: Get Chat ID/ test on new bot
# start bot control # start bot control
@ -273,44 +276,54 @@ class TelegramBotControl:
self.application.add_handler(disable_handler) self.application.add_handler(disable_handler)
self.application.run_polling(allowed_updates=Update.ALL_TYPES) self.application.run_polling(allowed_updates=Update.ALL_TYPES)
async def disable_bot(self, update): async def disable_bot(self, update, context: ContextTypes.DEFAULT_TYPE):
self.parent.disable_telegram_bot() self.parent.disable_telegram_bot()
await update.message.reply_text('Disabled Telegram Bot. \U0001F614\nIf you would like to re-enable,\nset telegramBotEnable to true\nin extended.conf') await update.message.reply_text('Disabled Telegram Bot. \U0001F614\nIf you would like to re-enable,\nset telegramBotEnable to true\nin extended.conf')
self.log.info(Fore.YELLOW + 'Telegram Bot: Send Disable Bot Message :(' + Fore.LIGHTWHITE_EX)
self.application.stop_running() self.application.stop_running()
async def cancel(self, update): async def cancel(self, update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Canceling...ARLToken is still expired.') await update.message.reply_text('Canceling...ARLToken is still expired.')
self.log.info(Fore.YELLOW + 'Telegram Bot: Canceling...ARLToken is still expired.' + Fore.LIGHTWHITE_EX)
try: try:
self.application.stop_running() self.application.stop_running()
except Exception: except Exception:
pass pass
async def set_token(self, update, context: ContextTypes.DEFAULT_TYPE): async def set_token(self, update, context: ContextTypes.DEFAULT_TYPE):
async def send_message(text, reply=False):
if reply is True:
await update.message.reply_text(text=text)
else:
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
self.log.info(Fore.YELLOW+"Telegram Bot: " + text + Fore.LIGHTWHITE_EX)
try: try:
new_token = update.message.text.split('/set_token ')[1] new_token = update.message.text.split('/set_token ')[1]
if new_token == '': if new_token == '':
raise Exception raise Exception
except: except:
await update.message.reply_text('Invalid Entry... please try again.') await update.message.reply_text('Invalid Entry... Please try again.')
return return
print(new_token) self.log.info(Fore.YELLOW+f"Telegram Bot:Token received: {new_token}" + Fore.LIGHTWHITE_EX)
self.log.info("Testing ARL Token Validity...")
token_validity = self.parent.check_token(new_token) token_validity = self.parent.check_token(new_token)
if token_validity: if token_validity:
await context.bot.send_message(chat_id=update.effective_chat.id, text="ARL valid, applying...") # await context.bot.send_message(chat_id=update.effective_chat.id, text="ARL valid, applying...")
# self.log.info(Fore.YELLOW+"TELEGRAM BOT SENT: ARL valid, applying..."+Fore.LIGHTWHITE_EX)
await send_message("ARL valid, applying...")
self.parent.newARLToken = '"'+new_token+'"' self.parent.newARLToken = '"'+new_token+'"'
await context.bot.send_message(chat_id=update.effective_chat.id, text="Checking configuration") self.parent.set_new_token()
await send_message("Checking configuration")
# reparse extended.conf # reparse extended.conf
self.parent.parse_extended_conf() self.parent.parse_extended_conf()
token_validity = self.parent.check_token(self.parent.arlToken) token_validity = self.parent.check_token(self.parent.currentARLToken)
if token_validity: if token_validity:
await context.bot.send_message(chat_id=update.effective_chat.id, text="ARL Updated! \U0001F44D") await send_message("ARL Token Updated! \U0001F44D",reply=True)
try: try:
self.application.stop_running() self.application.stop_running()
except Exception: except Exception:
pass pass
else: # If Token invalid else: # If Token invalid
await update.message.reply_text(text="Token expired or inactive. try another token.") await send_message("Token expired or invalid. Try another token.", reply=True)
return return
@ -326,7 +339,7 @@ def parse_arguments():
parser.print_help() parser.print_help()
parser.exit() parser.exit()
return parser.parse_args() return parser, parser.parse_args()
def get_version(root): def get_version(root):
@ -354,7 +367,7 @@ def init_logging(version, log_file_path):
level=logging.INFO, level=logging.INFO,
handlers=[ handlers=[
logging.StreamHandler(stdout), logging.StreamHandler(stdout),
logging.FileHandler(log_file_path, mode="a") logging.FileHandler(log_file_path, mode="a", encoding='utf-8')
] ]
) )
logger = logging.getLogger('ARLChecker') logger = logging.getLogger('ARLChecker')
@ -368,13 +381,13 @@ def init_logging(version, log_file_path):
def main(): def main():
root = '' root = ''
args = parse_arguments() parser, args = parse_arguments()
if args.debug is True: # If debug flag set, works with IDE structure if args.debug is True: # If debug flag set, works with IDE structure
root = DEBUG_ROOT_PATH root = DEBUG_ROOT_PATH
log = init_logging(get_version(root), get_active_log(root)) log = init_logging(get_version(root), get_active_log(root))
try: try:
if args.test is True: if args.test_token is True:
log.info("Test flag not currently functioning. Exiting.") log.info("Test flag not currently functioning. Exiting.")
exit(0) exit(0)
arl_checker_instance = LidarrExtendedAPI() arl_checker_instance = LidarrExtendedAPI()
@ -394,15 +407,15 @@ def main():
else: else:
log.error(e) log.error(e)
elif args.new: elif args.new_token:
if args.new == '': if args.new == '':
print("Please pass new ARL token as an argument") log.error('Please pass new ARL token as an argument')
exit(96) exit(96)
arl_checker_instance.newARLToken = '"'+args.new+'"' arl_checker_instance.newARLToken = '"'+args.new+'"'
arl_checker_instance.set_new_token() arl_checker_instance.set_new_token()
else: else:
args.print_help() parser.print_help()
except Exception as e: except Exception as e:
logging.error(e, exc_info=True) logging.error(e, exc_info=True)
exit(1) exit(1)