- Added custom dry-run logger
- Fixed double logging.
- Fixed description to have proper text on --log-file
This commit is contained in:
Visorask 2021-02-24 12:28:07 -06:00
parent 41725e3702
commit c00f1d44b0
2 changed files with 71 additions and 57 deletions

View file

@ -14,9 +14,15 @@ cat:
# Tag Parameters # Tag Parameters
tags: tags:
# <Tracker URL Keyword>: <Tag Name> # <Tracker URL Keyword>: <Tag Name>
awesome-hd: AHD
beyond-hd: Beyond-HD beyond-hd: Beyond-HD
privatehd: PrivateHD
animebytes.tv: AnimeBytes animebytes.tv: AnimeBytes
avistaz: Avistaz avistaz: Avistaz
landof.tv: BroadcasTheNet landof.tv: BroadcasTheNet
passthepopcorn: PassThePopcorn passthepopcorn: PassThePopcorn
torrentleech: TorrentLeech
tleechreload: TorrentLeech
gazellegames: GGn
blutopia: Blutopia
hdts: HDTorrents
tv-vault: TV-Vault

View file

@ -1,12 +1,14 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys
from qbittorrentapi import Client
import yaml import yaml
import argparse import argparse
import logging import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import sys from qbittorrentapi import Client
import urllib3
# import apprise # import apprise
parser = argparse.ArgumentParser("qBittorrent Manager.", parser = argparse.ArgumentParser("qBittorrent Manager.",
@ -15,12 +17,12 @@ parser.add_argument('-c', '--config-file',
dest='config', dest='config',
action='store', action='store',
default='config.yml', default='config.yml',
help='This is used if you want to use different names for your config.yml. Example: tv.yml') help='This is used if you want to use a different name for your config.yml. Example: tv.yml')
parser.add_argument('-l', '--log-file', parser.add_argument('-l', '--log-file',
dest='logfile', dest='logfile',
action='store', action='store',
default='activity.log', default='activity.log',
help='This is used if you want to use different names for your config.yml. Example: tv.yml') help='This is used if you want to use a different name for your log file. Example: tv.log')
parser.add_argument('-m', '--manage', parser.add_argument('-m', '--manage',
dest='manage', dest='manage',
action='store_const', action='store_const',
@ -55,76 +57,78 @@ parser.add_argument('--log',
help='Change your log level. ') help='Change your log level. ')
args = parser.parse_args() args = parser.parse_args()
with open(args.config, "r") as cfg_file: with open(args.config, "r") as cfg_file:
cfg = yaml.load(cfg_file, Loader=yaml.FullLoader) cfg = yaml.load(cfg_file, Loader=yaml.FullLoader)
# Logging # Logging
log_lev = getattr(logging, args.loglevel.upper()) log_lev = getattr(logging, args.loglevel.upper())
file_handler = logging.FileHandler(filename=args.logfile) file_handler = logging.FileHandler(filename=args.logfile)
stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler = logging.StreamHandler(sys.stderr)
rotate_handler = RotatingFileHandler(filename=args.logfile, rotate_handler = RotatingFileHandler(filename=args.logfile, maxBytes=1024 * 1024 * 2, backupCount=5)
maxBytes=1024 * 1024 * 2,
backupCount=5)
handlers = [file_handler, stdout_handler, rotate_handler] handlers = [file_handler, stdout_handler, rotate_handler]
# noinspection PyArgumentList # noinspection PyArgumentList
logging.basicConfig(level=log_lev, logging.basicConfig(level=log_lev, format='%(asctime)s - %(levelname)s: %(message)s', handlers=handlers)
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=handlers)
logger = logging.getLogger('qBit Manage') logger = logging.getLogger('qBit Manage')
# Add dry-run to logging.
logging.DRYRUN = 25
logging.addLevelName(logging.DRYRUN, 'DRY-RUN')
setattr(logger, 'dryrun', lambda message, *args: logger._log(logging.DRYRUN, message, args))
# Actual API call to connect to qbt. # Actual API call to connect to qbt.
client = Client(host=cfg["qbt"]["host"], client = Client(host=cfg["qbt"]["host"], username=cfg["qbt"]["user"], password=cfg["qbt"]["pass"])
username=cfg["qbt"]["user"],
password=cfg["qbt"]["pass"]) urllib3.disable_warnings()
torrent_list = client.torrents.info()
def get_category(path): def get_category(path):
cat_path = cfg["cat"] cat_path = cfg["cat"]
for p, c in cat_path.items(): for i, f in cat_path.items():
if p in path: if i in path:
category = c category = f
return category return category
else: else:
category = '' category = ''
logger.warning('No categories matched. Check your config.yml file. - Setting tag to NULL')
return category return category
def get_tags(url): def get_tags(url):
tag_path = cfg["tags"] tag_path = cfg["tags"]
for t, n in tag_path.items(): for i, f in tag_path.items():
if t in url: if i in url:
tag = n tag = f
return tag return tag
else: else:
tag = '' tag = ''
logger.warning('No tags matched. Check your config.yml file. Setting category to NULL')
return tag return tag
def update_category(): def update_category():
if args.manage == 'manage' or args.cat_update == 'cat_update': if args.manage == 'manage' or args.cat_update == 'cat_update':
num_cat = 0 num_cat = 0
torrent_list = client.torrents.info()
for torrent in torrent_list: for torrent in torrent_list:
if torrent.category == '': if torrent.category == '':
for x in torrent.trackers:
if x.url.startswith('http'):
new_cat = get_category(torrent.save_path) new_cat = get_category(torrent.save_path)
if args.dry_run == 'dry_run': if args.dry_run == 'dry_run':
logger.info('DRY-RUN: Torrent Name: %s', torrent.name) logger.dryrun('\n - Torrent Name: %s \n - New Category: %s \n - Tracker: %s',
logger.info('DRY-RUN: - New Category: %s', new_cat) torrent.name, new_cat, x.url)
num_cat += 1 num_cat += 1
else: else:
logger.info('Torrent Name: %s', torrent.name) logger.info('\n - Torrent Name: %s \n - New Category: %s \n - Tracker: %s',
logger.info(' - New Category: %s', new_cat) torrent.name, new_cat, x.url)
torrent.set_category(category=new_cat) torrent.set_category(category=new_cat)
num_cat += 1 num_cat += 1
if args.dry_run == 'dry_run': if args.dry_run == 'dry_run':
if num_cat >= 1: if num_cat >= 1:
logger.info('DRY-RUN: Did not update %s new categories.', num_cat) logger.dryrun('Did not update %s new categories.', num_cat)
else: else:
logger.info('DRY-RUN: No new torrents to categorize.') logger.dryrun('No new torrents to categorize.')
else: else:
if num_cat >= 1: if num_cat >= 1:
logger.info('Updated %s new categories.', num_cat) logger.info('Updated %s new categories.', num_cat)
@ -135,25 +139,26 @@ def update_category():
def update_tags(): def update_tags():
if args.manage == 'manage' or args.tag_update == 'tag_update': if args.manage == 'manage' or args.tag_update == 'tag_update':
num_tags = 0 num_tags = 0
torrent_list = client.torrents.info()
for torrent in torrent_list: for torrent in torrent_list:
if torrent.tags == '': if torrent.tags == '':
for x in torrent.trackers: for x in torrent.trackers:
if x.url.startswith('http'): if x.url.startswith('http'):
new_tag = get_tags(x.url) new_tag = get_tags(x.url)
if args.dry_run == 'dry_run': if args.dry_run == 'dry_run':
logger.info('DRY-RUN: Torrent Name: %s', torrent.name) logger.dryrun('\n - Torrent Name: %s \n - New Tag: %s \n - Tracker: %s',
logger.info('DRY-RUN: - New Tag: %s', new_tag) torrent.name, new_tag, x.url)
num_tags += 1 num_tags += 1
else: else:
logger.info('Torrent Name: %s', torrent.name) logger.info('\n - Torrent Name: %s \n - New Tag: %s \n - Tracker: %s',
logger.info(' - New Tag: %s', new_tag) torrent.name, new_tag, x.url)
torrent.add_tags(tags=new_tag) torrent.add_tags(tags=new_tag)
num_tags += 1 num_tags += 1
if args.dry_run == 'dry_run': if args.dry_run == 'dry_run':
if num_tags >= 1: if num_tags >= 1:
logger.info('DRY-RUN: Did not update %s new tags.', num_tags) logger.dryrun('Did not update %s new tags.', num_tags)
else: else:
logger.info('DRY-RUN: No new torrents to tag.') logger.dryrun('No new torrents to tag.')
else: else:
if num_tags >= 1: if num_tags >= 1:
logger.info('Updated %s new tags.', num_tags) logger.info('Updated %s new tags.', num_tags)
@ -164,24 +169,27 @@ def update_tags():
def rem_unregistered(): def rem_unregistered():
if args.manage == 'manage' or args.rem_unregistered == 'rem_unregistered': if args.manage == 'manage' or args.rem_unregistered == 'rem_unregistered':
rem_unr = 0 rem_unr = 0
torrent_list = client.torrents.info()
for torrent in torrent_list: for torrent in torrent_list:
for status in torrent.trackers: for status in torrent.trackers:
for x in torrent.trackers:
if x.url.startswith('http'):
if 'Unregistered torrent' in status.msg: if 'Unregistered torrent' in status.msg:
if args.dry_run == 'dry_run': if args.dry_run == 'dry_run':
logger.info('DRY-RUN: %s -> %s', torrent.name, status.msg) logger.dryrun('\n - Torrent Name: %s \n - Status: %s \n - Tracker: %s \n '
logger.info('DRY-RUN: - NOT Deleted') '- File NOT Deleted', torrent.name, status.msg, x.url)
rem_unr += 1 rem_unr += 1
else: else:
logger.info('%s -> %s', torrent.name, status.msg) logger.info('\n - Torrent Name: %s \n - Status: %s \n - Tracker: %s \n '
logger.info(' - Deleted') '- Deleted', torrent.name, status.msg, x.url)
torrent.delete(hash=torrent.hash, torrent.delete(hash=torrent.hash,
delete_files=True) delete_files=True)
rem_unr += 1 rem_unr += 1
if args.dry_run == 'dry_run': if args.dry_run == 'dry_run':
if rem_unr >= 1: if rem_unr >= 1:
logger.info('DRY-RUN: Did not delete %s torrents.', rem_unr) logger.dryrun('Did not delete %s torrents.', rem_unr)
else: else:
logger.info('DRY-RUN: No unregistered torrents found.') logger.dryrun('No unregistered torrents found.')
else: else:
if rem_unr >= 1: if rem_unr >= 1:
logger.info('Deleted %s torrents.', rem_unr) logger.info('Deleted %s torrents.', rem_unr)