2024-04-28 06:05:38 +02:00
import re
import requests
2024-02-07 03:05:58 +01:00
from dataclasses import dataclass
from requests import Session
from argparse import ArgumentParser
2024-02-14 05:06:59 +01:00
from sys import argv , stdout
2024-02-07 03:05:58 +01:00
from colorama import Fore , init
from telegram import Update
from telegram . ext import ApplicationBuilder , ContextTypes , CommandHandler
import logging
import os
2024-02-08 05:16:59 +01:00
from datetime import datetime
2024-02-07 03:05:58 +01:00
2024-02-17 04:18:57 +01:00
CUSTOM_INIT_PATH = ' /custom-cont_init.d/ '
2024-03-06 04:27:35 +01:00
CUSTOM_SERVICES_PATH = ' /custom-services.d/ '
STATUS_FALLBACK_LOCATION = ' /custom-services.d/python/ARLStatus.txt '
2024-02-17 04:18:57 +01:00
EXTENDED_CONF_PATH = ' /config/extended.conf '
NOT_FOUND_PATH = ' /config/extended/logs/notfound '
FAILED_DOWNLOADS_PATH = ' /config/extended/logs/downloaded/failed/deezer '
2024-02-17 05:50:21 +01:00
LOG_FILES_DIRECTORY = ' /config/logs '
2024-03-06 04:27:35 +01:00
DEBUG_ROOT_PATH = ' ./env '
2024-02-07 03:05:58 +01:00
2024-02-07 04:59:13 +01:00
# Web agent used to access Deezer
2024-02-07 03:05:58 +01:00
USER_AGENT = ' Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/110.0 '
2024-03-08 04:52:44 +01:00
2024-02-07 03:05:58 +01:00
@dataclass
class Plan :
name : str
expires : str
active : bool
download : bool
lossless : bool
explicit : bool
@dataclass
class Account :
id : int
token : str
country : str
plan : Plan
class AuthError ( Exception ) :
pass
class ParseError ( Exception ) :
pass
class ServiceError ( Exception ) :
pass
class DeezerPlatformProvider :
NAME = ' Deezer '
BASE_URL = ' http://www.deezer.com '
API_PATH = ' /ajax/gw-light.php '
SESSION_DATA = {
' api_token ' : ' null ' ,
' api_version ' : ' 1.0 ' ,
' input ' : ' 3 ' ,
' method ' : ' deezer.getUserData '
}
def __init__ ( self ) :
super ( ) . __init__ ( )
2024-02-17 05:50:21 +01:00
self . log = logging . getLogger ( ' ARLChecker ' )
2024-02-07 03:05:58 +01:00
self . session = Session ( )
self . session . headers . update ( { ' User-Agent ' : USER_AGENT } )
def login ( self , username , secret ) :
try :
res = self . session . post (
self . BASE_URL + self . API_PATH ,
cookies = { ' arl ' : secret } ,
data = self . SESSION_DATA
)
res . raise_for_status ( )
except Exception as error :
2024-04-28 04:51:05 +02:00
self . log . error ( Fore . RED + ' Could not connect! Service down, API changed, wrong credentials or code-related issue. ' + Fore . RESET )
2024-02-07 04:59:13 +01:00
raise ConnectionError ( )
2024-02-07 03:05:58 +01:00
self . session . cookies . clear ( )
try :
res = res . json ( )
except Exception as error :
2024-04-28 04:51:05 +02:00
self . log . error ( Fore . RED + " Could not parse JSON response from DEEZER! " + Fore . RESET )
2024-02-07 04:59:13 +01:00
raise ParseError ( )
2024-02-07 03:05:58 +01:00
if ' error ' in res and res [ ' error ' ] :
2024-04-28 04:51:05 +02:00
self . log . error ( Fore . RED + " Deezer returned the following error: {} " . format ( res [ " error " ] ) + Fore . RESET )
2024-02-07 04:59:13 +01:00
raise ServiceError ( )
2024-02-07 03:05:58 +01:00
res = res [ ' results ' ]
if res [ ' USER ' ] [ ' USER_ID ' ] == 0 :
2024-02-07 04:59:13 +01:00
raise AuthError ( )
2024-02-07 03:05:58 +01:00
return Account ( username , secret , res [ ' COUNTRY ' ] , Plan (
res [ ' OFFER_NAME ' ] ,
' Unknown ' ,
True ,
True ,
res [ ' USER ' ] [ ' OPTIONS ' ] [ ' web_sound_quality ' ] [ ' lossless ' ] ,
res [ ' USER ' ] [ ' EXPLICIT_CONTENT_LEVEL ' ]
) )
class LidarrExtendedAPI :
# sets new token to extended.conf
2024-02-17 05:50:21 +01:00
def __init__ ( self ) :
self . root = ' '
self . log = logging . getLogger ( ' ARLChecker ' )
self . newARLToken = None
self . currentARLToken = None
2024-02-07 03:05:58 +01:00
self . arlLineText = None
self . arlLineIndex = None
self . fileText = None
self . enable_telegram_bot = False
self . telegram_bot_running = False
self . telegram_bot_token = None
self . telegram_user_chat_id = None
2024-04-24 05:29:45 +02:00
self . telegram_bot_enable_line_text = None
self . telegram_bot_enable_line_index = None
2024-04-28 06:05:38 +02:00
self . bot = None
2024-04-24 05:29:45 +02:00
self . enable_pushover_notify = False
self . pushover_user_key = None
self . pushover_app_api_key = None
self . enable_ntfy_notify = False
2024-04-27 06:36:09 +02:00
self . ntfy_sever_topic = None
self . ntfy_user_token = None
2024-02-07 03:05:58 +01:00
def parse_extended_conf ( self ) :
2024-02-17 05:50:21 +01:00
self . currentARLToken = None
2024-02-07 03:05:58 +01:00
arl_token_match = None
2024-02-17 05:50:21 +01:00
deezer_active = False
2024-02-07 03:05:58 +01:00
re_search_pattern = r ' " ([^ " ]*) " '
try : # Try to open extended.conf and read all text into a var.
2024-02-17 05:50:21 +01:00
with open ( self . root + EXTENDED_CONF_PATH , ' r ' , encoding = ' utf-8 ' ) as file :
2024-02-07 03:05:58 +01:00
self . fileText = file . readlines ( )
file . close ( )
except :
2024-02-17 05:50:21 +01:00
self . log . error ( f " Could not find { self . root + EXTENDED_CONF_PATH } " )
2024-02-07 03:05:58 +01:00
exit ( 1 )
# Ensure Deezer is enabled and ARL token is populated
for line in self . fileText :
if ' dlClientSource= " deezer " ' in line or ' dlClientSource= " both " ' in line :
deezer_active = True
if ' arlToken= ' in line :
self . arlLineText = line
self . arlLineIndex = self . fileText . index ( self . arlLineText )
arl_token_match = re . search ( re_search_pattern , line )
break
# ARL Token wrong flag error handling.
if arl_token_match is None :
2024-03-08 04:48:02 +01:00
self . log . error ( " ARL Token not found in extended.conf. Exiting. " )
2024-02-07 03:05:58 +01:00
exit ( 1 )
elif deezer_active is False :
2024-03-08 04:48:02 +01:00
self . log . error ( " Deezer not set as an active downloader in extended.conf. Exiting. " )
2024-02-07 03:05:58 +01:00
file . close ( )
exit ( 1 )
2024-02-17 05:50:21 +01:00
self . currentARLToken = arl_token_match [ 0 ]
self . log . info ( ' ARL Found in extended.conf ' )
2024-02-07 03:05:58 +01:00
for line in self . fileText :
if ' telegramBotEnable= ' in line :
2024-04-24 05:29:45 +02:00
self . telegram_bot_enable_line_text = line
self . telegram_bot_enable_line_index = self . fileText . index ( self . telegram_bot_enable_line_text )
2024-02-07 03:05:58 +01:00
self . enable_telegram_bot = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' ) . lower ( ) in ' true '
if ' telegramBotToken= ' in line :
self . telegram_bot_token = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' )
if ' telegramUserChatID= ' in line :
self . telegram_user_chat_id = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' )
2024-04-24 05:29:45 +02:00
if ' pushoverEnable= ' in line :
2024-04-27 07:37:54 +02:00
self . enable_pushover_notify = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' ) . lower ( ) in ' true '
2024-04-24 05:29:45 +02:00
if ' pushoverUserKey= ' in line :
self . pushover_user_key = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' )
if ' pushoverAppAPIKey= ' in line :
self . pushover_app_api_key = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' )
if ' ntfyEnable= ' in line :
2024-04-27 07:37:54 +02:00
self . enable_ntfy_notify = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' ) . lower ( ) in ' true '
2024-04-27 06:36:09 +02:00
if ' ntfyServerTopic= ' in line :
self . ntfy_sever_topic = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' )
if ' ntfyUserToken= ' in line :
self . ntfy_user_token = re . search ( re_search_pattern , line ) [ 0 ] . replace ( ' " ' , ' ' )
2024-04-24 05:29:45 +02:00
2024-04-27 07:37:54 +02:00
if self . enable_telegram_bot :
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . GREEN + ' Telegram bot is enabled. ' + Fore . RESET )
2024-02-07 03:05:58 +01:00
if self . telegram_bot_token is None or self . telegram_user_chat_id is None :
2024-02-17 05:50:21 +01:00
self . log . error ( ' Telegram bot token or user chat ID not set in extended.conf. Exiting ' )
2024-02-07 03:05:58 +01:00
exit ( 1 )
else :
2024-04-27 07:37:54 +02:00
self . log . info ( ' Telegram bot is disabled. ' )
# Report Notify/Bot Enable
if self . enable_pushover_notify :
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . GREEN + ' Pushover notify is enabled. ' + Fore . RESET )
2024-04-27 07:37:54 +02:00
else :
self . log . info ( ' Pushover notify is disabled. ' )
if self . enable_ntfy_notify :
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . GREEN + ' ntfy notify is enabled. ' + Fore . RESET )
2024-04-27 07:37:54 +02:00
else :
self . log . info ( ' ntfy notify is disabled. ' )
2024-02-07 03:05:58 +01:00
2024-03-08 04:52:44 +01:00
def check_token_wrapper ( self ) : # adds Lidarr_extended specific logging and actions around check_token
2024-03-07 05:50:56 +01:00
self . log . info ( " Checking ARL Token from extended.conf " )
if self . currentARLToken == ' " " ' :
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . YELLOW + " No ARL Token set in Extended.conf " + Fore . RESET )
2024-02-08 05:16:59 +01:00
self . report_status ( " NOT SET " )
2024-02-07 05:31:14 +01:00
exit ( 0 )
2024-03-07 05:50:56 +01:00
if self . currentARLToken is None :
2024-02-17 05:50:21 +01:00
self . log . error ( ' Invalid ARL Token Entry (None Object) ' )
2024-02-07 03:05:58 +01:00
return False
2024-03-07 05:50:56 +01:00
validity_results = check_token ( self . currentARLToken )
if validity_results is True :
2024-03-08 04:52:44 +01:00
self . report_status ( ' VALID ' ) # For text fallback method
2024-03-07 05:50:56 +01:00
else :
2024-02-08 05:16:59 +01:00
self . report_status ( ' EXPIRED ' )
2024-04-28 04:51:05 +02:00
self . log . error ( Fore . RED + ' Update the token in extended.conf ' + Fore . RESET )
2024-03-08 04:52:44 +01:00
if self . telegram_bot_running : # Don't re-start the telegram bot if it's already running after bot invalid token entry
2024-02-07 03:05:58 +01:00
return False
2024-04-24 05:50:21 +02:00
if self . enable_pushover_notify :
2024-04-28 06:05:38 +02:00
pushover_notify ( self . pushover_app_api_key , self . pushover_user_key , ' --- \U0001F6A8 WARNING \U0001F6A8 ----- \n ARL TOKEN EXPIRED \n Update arlToken in extended.conf " \n You can find a new ARL at: \n https://rentry.org/firehawk52#deezer-arls ' )
2024-04-24 05:50:21 +02:00
if self . enable_ntfy_notify :
2024-04-28 06:05:38 +02:00
ntfy_notify ( self . ntfy_sever_topic , ' --- \U0001F6A8 WARNING \U0001F6A8 ----- \n ARL TOKEN EXPIRED \n Update arlToken in extended.conf \n You can find a new ARL at: \n https://rentry.org/firehawk52#deezer-arls ' , self . ntfy_user_token )
2024-02-07 03:05:58 +01:00
if self . enable_telegram_bot :
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . YELLOW + ' Starting Telegram bot...Check Telegram and follow instructions. ' + Fore . RESET )
2024-02-07 03:05:58 +01:00
self . telegram_bot_running = True
self . start_telegram_bot ( )
exit ( 420 )
def set_new_token ( self ) : # Re-writes extended.conf with previously read-in text, replacing w/ new ARL
2024-02-17 05:50:21 +01:00
self . fileText [ self . arlLineIndex ] = self . arlLineText . replace ( self . currentARLToken , self . newARLToken )
with open ( self . root + EXTENDED_CONF_PATH , ' w ' , encoding = ' utf-8 ' ) as file :
2024-02-07 03:05:58 +01:00
file . writelines ( self . fileText )
file . close ( )
2024-02-17 05:50:21 +01:00
self . log . info ( " New ARL token written to extended.conf " )
self . parse_extended_conf ( )
2024-02-07 03:05:58 +01:00
2024-02-07 04:59:13 +01:00
# After new token is set, clean up notfound and failed downloads to bypass the default 30 day wait
2024-02-07 03:05:58 +01:00
def clear_not_found ( self ) :
2024-03-08 04:52:44 +01:00
paths = [ self . root + NOT_FOUND_PATH , self . root + FAILED_DOWNLOADS_PATH ]
2024-02-07 03:05:58 +01:00
for path in paths :
for file in os . listdir ( path ) :
2024-03-08 04:52:44 +01:00
file_to_delete = os . path . join ( path , file )
2024-02-07 03:05:58 +01:00
os . remove ( file_to_delete )
2024-02-08 05:16:59 +01:00
def report_status ( self , status ) :
2024-02-17 05:50:21 +01:00
f = open ( self . root + STATUS_FALLBACK_LOCATION , " w " )
2024-03-08 04:52:44 +01:00
now = datetime . strftime ( datetime . now ( ) , " % b- %d - % Y at % H: % M: % S " )
2024-02-08 05:16:59 +01:00
f . write ( f " { now } : ARL Token is { status } . { ' Please update arlToken in extended.conf ' if status == ' EXPIRED ' else ' ' } " )
f . close ( )
2024-02-07 03:05:58 +01:00
def start_telegram_bot ( self ) :
2024-04-27 07:37:54 +02:00
try :
self . bot = TelegramBotControl ( self , self . telegram_bot_token , self . telegram_user_chat_id )
except Exception as e :
if ' Chat not found ' in str ( e ) or ' Chat_id ' in str ( e ) :
self . log . error (
2024-04-28 04:51:05 +02:00
Fore . RED + " Telegram Bot: Chat not found. Check your chat ID in extended.conf, or start a chat with your bot. " + Fore . RESET )
2024-04-27 07:37:54 +02:00
elif ' The token ' in str ( e ) :
2024-04-28 04:51:05 +02:00
self . log . error ( Fore . RED + " Telegram Bot: Check your Bot Token in extended.conf. " + Fore . RESET )
2024-04-27 07:37:54 +02:00
else :
2024-04-28 06:05:38 +02:00
self . log . error ( ' Telegram Bot: ' + str ( e ) )
2024-02-07 03:05:58 +01:00
2024-02-07 04:59:13 +01:00
def disable_telegram_bot ( self ) :
compiled = re . compile ( re . escape ( ' true ' ) , re . IGNORECASE )
2024-04-24 05:29:45 +02:00
self . fileText [ self . telegram_bot_enable_line_index ] = compiled . sub ( ' false ' , self . telegram_bot_enable_line_text )
2024-02-17 05:50:21 +01:00
with open ( self . root + EXTENDED_CONF_PATH , ' w ' , encoding = ' utf-8 ' ) as file :
2024-02-07 04:59:13 +01:00
file . writelines ( self . fileText )
file . close ( )
2024-02-17 05:50:21 +01:00
self . log . info ( " Telegram Bot Disabled. " )
2024-02-07 04:59:13 +01:00
2024-02-07 03:05:58 +01:00
class TelegramBotControl :
2024-02-17 05:50:21 +01:00
def __init__ ( self , parent , telegram_bot_token , telegram_user_chat_id ) :
self . log = logging . getLogger ( ' ARLChecker ' )
self . parent = parent
self . telegram_bot_token = telegram_bot_token
self . telegram_chat_id = telegram_user_chat_id
2024-02-07 03:05:58 +01:00
2024-02-17 05:50:21 +01:00
# Send initial notification
2024-02-07 03:05:58 +01:00
async def send_expired_token_notification ( application ) :
2024-03-08 04:52:44 +01:00
await application . bot . sendMessage ( chat_id = self . telegram_chat_id , text = ' --- \U0001F6A8 WARNING \U0001F6A8 ----- \n ARL TOKEN EXPIRED \n Update Token by running " /set_token <TOKEN> " \n You can find a new ARL at: \n https://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 )
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . YELLOW + " Telegram Bot Sent ARL Token Expiry Message " + Fore . RESET )
2024-02-07 03:05:58 +01:00
# TODO: Get Chat ID/ test on new bot
# start bot control
self . application = ApplicationBuilder ( ) . token ( self . telegram_bot_token ) . post_init ( send_expired_token_notification ) . build ( )
token_handler = CommandHandler ( ' set_token ' , self . set_token )
2024-02-07 04:59:13 +01:00
cancel_handler = CommandHandler ( ' cancel ' , self . cancel )
disable_handler = CommandHandler ( ' disable ' , self . disable_bot )
2024-02-07 03:05:58 +01:00
self . application . add_handler ( token_handler )
2024-02-07 04:59:13 +01:00
self . application . add_handler ( cancel_handler )
self . application . add_handler ( disable_handler )
2024-02-07 03:05:58 +01:00
self . application . run_polling ( allowed_updates = Update . ALL_TYPES )
2024-03-08 04:52:44 +01:00
async def disable_bot ( self , update , context : ContextTypes . DEFAULT_TYPE ) :
2024-02-07 04:59:13 +01:00
self . parent . disable_telegram_bot ( )
await update . message . reply_text ( ' Disabled Telegram Bot. \U0001F614 \n If you would like to re-enable, \n set telegramBotEnable to true \n in extended.conf ' )
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . YELLOW + ' Telegram Bot: Send Disable Bot Message :( ' + Fore . RESET )
2024-02-07 04:59:13 +01:00
self . application . stop_running ( )
2024-02-07 03:05:58 +01:00
2024-03-06 06:10:57 +01:00
async def cancel ( self , update , context : ContextTypes . DEFAULT_TYPE ) :
2024-02-07 04:59:13 +01:00
await update . message . reply_text ( ' Canceling...ARLToken is still expired. ' )
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . YELLOW + ' Telegram Bot: Canceling...ARLToken is still expired. ' + Fore . RESET )
2024-02-07 04:59:13 +01:00
try :
self . application . stop_running ( )
except Exception :
pass
2024-03-08 04:52:44 +01:00
2024-02-07 03:05:58 +01:00
async def set_token ( self , update , context : ContextTypes . DEFAULT_TYPE ) :
2024-03-06 06:10:57 +01:00
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 )
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . YELLOW + " Telegram Bot: " + text + Fore . RESET )
2024-02-07 03:05:58 +01:00
try :
new_token = update . message . text . split ( ' /set_token ' ) [ 1 ]
if new_token == ' ' :
raise Exception
except :
2024-03-06 06:10:57 +01:00
await update . message . reply_text ( ' Invalid Entry... Please try again. ' )
2024-02-07 03:05:58 +01:00
return
2024-04-28 04:51:05 +02:00
self . log . info ( Fore . YELLOW + f " Telegram Bot:Token received: { new_token } " + Fore . RESET )
2024-03-07 05:50:56 +01:00
token_validity = check_token ( new_token )
2024-02-07 03:05:58 +01:00
if token_validity :
2024-03-06 06:10:57 +01:00
await send_message ( " ARL valid, applying... " )
2024-02-07 03:05:58 +01:00
self . parent . newARLToken = ' " ' + new_token + ' " '
2024-03-06 06:10:57 +01:00
self . parent . set_new_token ( )
2024-03-07 05:50:56 +01:00
await send_message ( " Checking configuration... " )
2024-02-07 03:05:58 +01:00
# reparse extended.conf
self . parent . parse_extended_conf ( )
2024-03-07 05:50:56 +01:00
token_validity = check_token ( self . parent . currentARLToken )
2024-02-07 03:05:58 +01:00
if token_validity :
2024-03-08 04:52:44 +01:00
await send_message ( " ARL Token Updated! \U0001F44D " , reply = True )
2024-02-07 03:05:58 +01:00
try :
2024-03-06 04:27:35 +01:00
self . application . stop_running ( )
2024-02-07 03:05:58 +01:00
except Exception :
pass
2024-02-17 05:50:21 +01:00
else : # If Token invalid
2024-03-06 06:10:57 +01:00
await send_message ( " Token expired or invalid. Try another token. " , reply = True )
2024-02-07 03:05:58 +01:00
return
2024-02-08 05:16:59 +01:00
2024-04-28 06:05:38 +02:00
def pushover_notify ( api_token , user_key , message ) : # Send Notification to Pushover
2024-04-24 05:50:21 +02:00
log = logging . getLogger ( ' ARLChecker ' ) # Get Logging
2024-04-28 04:51:05 +02:00
log . info ( Fore . YELLOW + ' Attempting Pushover notification ' + Fore . RESET )
2024-04-24 05:50:21 +02:00
response = requests . post ( " https://api.pushover.net/1/messages.json " , data = {
" token " : api_token ,
" user " : user_key ,
" message " : message
} )
if response . json ( ) [ ' status ' ] == 1 :
2024-04-28 04:51:05 +02:00
log . info ( Fore . GREEN + " Pushover notification sent successfully " + Fore . RESET )
2024-04-24 05:50:21 +02:00
else :
for message_error in response . json ( ) [ ' errors ' ] :
2024-04-28 04:51:05 +02:00
log . error ( Fore . RED + f " Pushover Response: { message_error } " + Fore . RESET )
2024-04-24 05:50:21 +02:00
2024-04-27 06:36:09 +02:00
def ntfy_notify ( server_plus_topic , message , token ) : # Send Notification to ntfy topic
2024-04-24 05:50:21 +02:00
log = logging . getLogger ( ' ARLChecker ' ) # Get Logging
2024-04-28 04:51:05 +02:00
log . info ( Fore . YELLOW + ' Attempting ntfy notification ' + Fore . RESET )
2024-04-27 07:37:54 +02:00
try :
requests . post ( server_plus_topic ,
data = message . encode ( encoding = ' utf-8 ' ) ,
headers = { " Authorization " : f " Bearer { token } " }
)
2024-04-28 04:51:05 +02:00
log . info ( Fore . GREEN + ' ntfy notification sent successfully ' + Fore . RESET )
2024-04-27 07:37:54 +02:00
except Exception as e :
if " Failed to resolve " in str ( e ) :
2024-04-28 04:56:06 +02:00
log . error ( Fore . RED + " ntfy ERROR: Check if server and user token correct in extended.conf " + Fore . RESET )
2024-04-27 07:37:54 +02:00
else :
2024-04-28 04:56:06 +02:00
log . error ( Fore . RED + " NTFY ERROR: " + str ( e ) + Fore . RESET )
2024-04-24 05:29:45 +02:00
2024-04-28 06:05:38 +02:00
2024-03-07 05:50:56 +01:00
def check_token ( token = None ) :
log = logging . getLogger ( ' ARLChecker ' )
log . info ( f " ARL Token to check: { token } " )
log . info ( ' Checking ARL Token Validity... ' )
try :
deezer_check = DeezerPlatformProvider ( )
2024-03-08 04:52:44 +01:00
account = deezer_check . login ( ' ' , token . replace ( ' " ' , ' ' ) )
2024-03-07 05:50:56 +01:00
if account . plan :
2024-04-28 04:51:05 +02:00
log . info ( Fore . GREEN + f ' Deezer Account Found. ' + Fore . RESET )
2024-03-07 05:50:56 +01:00
log . info ( ' ------------------------------- ' )
log . info ( f ' Plan: { account . plan . name } ' )
log . info ( f ' Expiration: { account . plan . expires } ' )
2024-04-28 04:51:05 +02:00
log . info ( f ' Active: { Fore . GREEN + " Y " if account . plan . active else " N " } ' + Fore . RESET )
log . info ( f ' Download: { Fore . GREEN + " Y " if account . plan . download else Fore . RED + " N " } ' + Fore . RESET )
log . info ( f ' Lossless: { Fore . GREEN + " Y " if account . plan . lossless else Fore . RED + " N " } ' + Fore . RESET )
log . info ( f ' Explicit: { Fore . GREEN + " Y " if account . plan . explicit else Fore . RED + " N " } ' + Fore . RESET )
2024-03-07 05:50:56 +01:00
log . info ( ' ------------------------------- ' )
return True
except Exception as e :
2024-03-08 04:52:44 +01:00
if type ( e ) is AuthError :
2024-04-28 04:51:05 +02:00
log . error ( Fore . RED + ' ARL Token Invalid/Expired. ' + Fore . RESET )
2024-03-07 05:50:56 +01:00
return False
else :
log . error ( e )
return
2024-02-17 05:50:21 +01:00
def parse_arguments ( ) :
2024-03-06 04:27:35 +01:00
parser = ArgumentParser ( prog = ' Account Checker ' , description = ' Lidarr Extended Deezer ARL Token Tools ' )
2024-03-08 04:52:44 +01:00
parser . add_argument ( ' -c ' , ' --check ' , help = ' Check if currently set ARL Token is active/valid ' , required = False , default = False , action = ' store_true ' )
2024-03-06 04:32:09 +01:00
parser . add_argument ( ' -n ' , ' --new_token ' , help = ' Set new ARL Token ' , type = str , required = False , default = False )
2024-03-06 04:27:35 +01:00
parser . add_argument ( ' -t ' , ' --test_token ' , help = ' Test any token for validity ' , type = str , required = False , default = False )
2024-03-08 04:52:44 +01:00
parser . add_argument ( ' -d ' , ' --debug ' , help = ' For debug and development, sets root path to match testing env. See DEBUG_ROOT_PATH ' , required = False , default = False , action = ' store_true ' )
2024-02-07 03:05:58 +01:00
if not argv [ 1 : ] :
parser . print_help ( )
parser . exit ( )
2024-03-06 06:10:57 +01:00
return parser , parser . parse_args ( )
2024-02-17 05:50:21 +01:00
def get_version ( root ) :
# Pull script version from bash script. will likely change this to a var passthrough
with open ( root + CUSTOM_SERVICES_PATH + " ARLChecker " , " r " ) as r :
for line in r :
if ' scriptVersion ' in line :
2024-03-06 04:27:35 +01:00
return re . search ( r ' " ([A-Za-z0-9_./ \\ -]*) " ' , line ) [ 0 ] . replace ( ' " ' , ' ' )
2024-02-17 05:50:21 +01:00
logging . error ( ' Script Version not found! Exiting... ' )
exit ( 1 )
def get_active_log ( root ) :
# Get current log file
path = root + LOG_FILES_DIRECTORY
latest_file = max ( [ os . path . join ( path , f ) for f in os . listdir ( path ) if ' ARLChecker ' in f ] , key = os . path . getctime )
return latest_file
def init_logging ( version , log_file_path ) :
# Logging Setup
logging . basicConfig (
format = f ' %(asctime)s :: ARLChecker :: { version } :: %(levelname)s :: %(message)s ' ,
datefmt = ' % Y- % m- %d % H: % M: % S ' ,
level = logging . INFO ,
handlers = [
logging . StreamHandler ( stdout ) ,
2024-03-06 06:10:57 +01:00
logging . FileHandler ( log_file_path , mode = " a " , encoding = ' utf-8 ' )
2024-02-17 05:50:21 +01:00
]
)
logger = logging . getLogger ( ' ARLChecker ' )
# Initialize colorama
init ( autoreset = True )
2024-04-28 04:51:05 +02:00
logger . info ( Fore . GREEN + ' Logger initialized ' + Fore . RESET )
2024-02-17 05:50:21 +01:00
return logger
def main ( ) :
root = ' '
2024-03-06 06:10:57 +01:00
parser , args = parse_arguments ( )
2024-03-06 04:27:35 +01:00
if args . debug is True : # If debug flag set, works with IDE structure
root = DEBUG_ROOT_PATH
2024-02-17 05:50:21 +01:00
log = init_logging ( get_version ( root ) , get_active_log ( root ) )
2024-03-06 04:27:35 +01:00
2024-02-17 05:50:21 +01:00
try :
2024-03-07 05:13:18 +01:00
if args . test_token :
2024-04-28 04:51:05 +02:00
log . info ( Fore . CYAN + " CLI Token Tester " + Fore . RESET )
2024-03-07 05:50:56 +01:00
check_token ( args . test_token )
2024-03-06 04:27:35 +01:00
exit ( 0 )
arl_checker_instance = LidarrExtendedAPI ( )
arl_checker_instance . root = root
2024-02-17 05:50:21 +01:00
if args . check is True :
if arl_checker_instance . currentARLToken == ' ' :
log . error ( " ARL Token not set. re-run with -n flag " )
2024-04-27 07:37:54 +02:00
arl_checker_instance . parse_extended_conf ( )
arl_checker_instance . check_token_wrapper ( )
2024-02-17 05:50:21 +01:00
2024-03-06 06:10:57 +01:00
elif args . new_token :
2024-03-07 05:13:18 +01:00
if args . new_token == ' ' :
2024-03-06 06:10:57 +01:00
log . error ( ' Please pass new ARL token as an argument ' )
2024-02-17 05:50:21 +01:00
exit ( 96 )
2024-03-07 05:13:18 +01:00
arl_checker_instance . newARLToken = ' " ' + args . new_token + ' " '
arl_checker_instance . parse_extended_conf ( )
2024-02-17 05:50:21 +01:00
arl_checker_instance . set_new_token ( )
2024-02-07 03:05:58 +01:00
2024-02-17 05:50:21 +01:00
else :
2024-03-06 06:10:57 +01:00
parser . print_help ( )
2024-02-17 05:50:21 +01:00
except Exception as e :
logging . error ( e , exc_info = True )
exit ( 1 )
2024-02-17 04:18:57 +01:00
2024-02-07 03:05:58 +01:00
if __name__ == ' __main__ ' :
2024-03-08 04:52:44 +01:00
main ( )