mirror of
https://github.com/StuffAnThings/qbit_manage.git
synced 2025-03-01 10:25:03 +08:00
Big Update
This commit is contained in:
parent
e45a3f1064
commit
c371b1cd38
3 changed files with 169 additions and 22 deletions
|
@ -1,12 +1,17 @@
|
||||||
|
# qBittorrent parameters
|
||||||
qbt:
|
qbt:
|
||||||
host: 'localhost:8080'
|
host: 'localhost:8080'
|
||||||
user: 'username'
|
user: 'username'
|
||||||
pass: 'password'
|
pass: 'password'
|
||||||
|
|
||||||
|
# Category Parameters
|
||||||
cat:
|
cat:
|
||||||
# <File path keyword>: <Category Name>
|
# <File path keyword>: <Category Name>
|
||||||
anime: anime
|
anime: anime
|
||||||
movies: movies
|
movies: movies
|
||||||
tv: tv
|
tv: tv
|
||||||
|
|
||||||
|
# Tag Parameters
|
||||||
tags:
|
tags:
|
||||||
# <Tracker URL Keyword>: <Tag Name>
|
# <Tracker URL Keyword>: <Tag Name>
|
||||||
awesome-hd: AHD
|
awesome-hd: AHD
|
||||||
|
|
179
qbit_manage.py
179
qbit_manage.py
|
@ -3,27 +3,85 @@
|
||||||
from qbittorrentapi import Client
|
from qbittorrentapi import Client
|
||||||
import yaml
|
import yaml
|
||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
# import apprise
|
||||||
|
|
||||||
parser = argparse.ArgumentParser("qBittorrent Manager.",
|
parser = argparse.ArgumentParser("qBittorrent Manager.",
|
||||||
description='A mix of scripts combined for managing qBittorrent.')
|
description='A mix of scripts combined for managing qBittorrent.')
|
||||||
parser.add_argument('-c', '--config-file', dest='config', action='store', default='config.yml',
|
parser.add_argument('-c', '--config-file',
|
||||||
|
dest='config',
|
||||||
|
action='store',
|
||||||
|
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 different names for your config.yml. Example: tv.yml')
|
||||||
parser.add_argument('-m', '--manage', dest='command', action='store_const', const='manage',
|
parser.add_argument('-l', '--log-file',
|
||||||
help='Use this if you would like to update your tags AND categories and remove unregistered '
|
dest='logfile',
|
||||||
'torrents.')
|
action='store',
|
||||||
parser.add_argument('--cat-update', dest='command', action='store_const', const='cat-update',
|
default='activity.log',
|
||||||
|
help='This is used if you want to use different names for your config.yml. Example: tv.yml')
|
||||||
|
parser.add_argument('-m', '--manage',
|
||||||
|
dest='manage',
|
||||||
|
action='store_const',
|
||||||
|
const='manage',
|
||||||
|
help='Use this if you would like to update your tags AND'
|
||||||
|
' categories AND remove unregistered torrents.')
|
||||||
|
parser.add_argument('-g', '--cat-update',
|
||||||
|
dest='cat_update',
|
||||||
|
action='store_const',
|
||||||
|
const='cat_update',
|
||||||
help='Use this if you would like to update your categories.')
|
help='Use this if you would like to update your categories.')
|
||||||
parser.add_argument('--tag-update', dest='command', action='store_const', const='tag-update',
|
parser.add_argument('-t', '--tag-update',
|
||||||
|
dest='tag_update',
|
||||||
|
action='store_const',
|
||||||
|
const='tag_update',
|
||||||
help='Use this if you would like to update your tags.')
|
help='Use this if you would like to update your tags.')
|
||||||
parser.add_argument('--rem-unregistered', dest='command', action='store_const', const='rem-unregistered',
|
parser.add_argument('-r', '--rem-unregistered',
|
||||||
|
dest='rem_unregistered',
|
||||||
|
action='store_const',
|
||||||
|
const='rem_unregistered',
|
||||||
help='Use this if you would like to remove unregistered torrents.')
|
help='Use this if you would like to remove unregistered torrents.')
|
||||||
|
parser.add_argument('--dry-run',
|
||||||
|
dest='dry_run',
|
||||||
|
action='store_const',
|
||||||
|
const='dry_run',
|
||||||
|
help='If you would like to see what is gonna happen but not actually delete or '
|
||||||
|
'tag/categorize anything.')
|
||||||
|
parser.add_argument('--log',
|
||||||
|
dest='loglevel',
|
||||||
|
action='store',
|
||||||
|
default='INFO',
|
||||||
|
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
|
||||||
|
log_lev = getattr(logging, args.loglevel.upper())
|
||||||
|
file_handler = logging.FileHandler(filename=args.logfile)
|
||||||
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
||||||
|
handlers = [file_handler, stdout_handler]
|
||||||
|
|
||||||
|
# noinspection PyArgumentList
|
||||||
|
logging.basicConfig(level=log_lev,
|
||||||
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||||
|
handlers=handlers)
|
||||||
|
logger = logging.getLogger('qBit Manage')
|
||||||
|
|
||||||
|
|
||||||
# Actual API call to connect to qbt.
|
# Actual API call to connect to qbt.
|
||||||
|
<<<<<<< HEAD
|
||||||
|
client = Client(host=cfg["qbt"]["host"],
|
||||||
|
username=cfg["qbt"]["user"],
|
||||||
|
password=cfg["qbt"]["pass"])
|
||||||
|
=======
|
||||||
client = Client(host=cfg["qbt"]["host"], username=cfg["qbt"]["user"], password=cfg["qbt"]["pass"])
|
client = Client(host=cfg["qbt"]["host"], username=cfg["qbt"]["user"], password=cfg["qbt"]["pass"])
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
=======
|
||||||
|
>>>>>>> e45a3f1064b6e19c352a0230194ff0293d13f548
|
||||||
|
>>>>>>> Stashed changes
|
||||||
torrent_list = client.torrents.info()
|
torrent_list = client.torrents.info()
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,6 +96,8 @@ def get_category(path):
|
||||||
return category
|
return category
|
||||||
|
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
|
=======
|
||||||
def update_category():
|
def update_category():
|
||||||
num_cat = 0
|
num_cat = 0
|
||||||
for torrent in torrent_list:
|
for torrent in torrent_list:
|
||||||
|
@ -53,6 +113,7 @@ def update_category():
|
||||||
print('No new torrents to categorize.')
|
print('No new torrents to categorize.')
|
||||||
|
|
||||||
|
|
||||||
|
>>>>>>> e45a3f1064b6e19c352a0230194ff0293d13f548
|
||||||
def get_tags(url):
|
def get_tags(url):
|
||||||
tag_path = cfg["tags"]
|
tag_path = cfg["tags"]
|
||||||
for t, n in tag_path.items():
|
for t, n in tag_path.items():
|
||||||
|
@ -64,7 +125,93 @@ def get_tags(url):
|
||||||
return tag
|
return tag
|
||||||
|
|
||||||
|
|
||||||
|
def update_category():
|
||||||
|
if args.manage == 'manage' or args.cat_update == 'cat_update':
|
||||||
|
num_cat = 0
|
||||||
|
for torrent in torrent_list:
|
||||||
|
if torrent.category == '':
|
||||||
|
new_cat = get_category(torrent.save_path)
|
||||||
|
if args.dry_run == 'dry_run':
|
||||||
|
logger.info('DRY-RUN: Torrent Name: %s', torrent.name)
|
||||||
|
logger.info('DRY-RUN: - New Category: %s', new_cat)
|
||||||
|
num_cat += 1
|
||||||
|
else:
|
||||||
|
logger.info('Torrent Name: %s', torrent.name)
|
||||||
|
logger.info(' - New Category: %s', new_cat)
|
||||||
|
torrent.set_category(category=new_cat)
|
||||||
|
num_cat += 1
|
||||||
|
if args.dry_run == 'dry_run':
|
||||||
|
if num_cat >= 1:
|
||||||
|
logger.info('DRY-RUN: Did not update %s new categories.', num_cat)
|
||||||
|
else:
|
||||||
|
logger.info('DRY-RUN: No new torrents to categorize.')
|
||||||
|
else:
|
||||||
|
if num_cat >= 1:
|
||||||
|
logger.info('Updated %s new categories.', num_cat)
|
||||||
|
else:
|
||||||
|
logger.info('No new torrents to categorize.')
|
||||||
|
|
||||||
|
|
||||||
def update_tags():
|
def update_tags():
|
||||||
|
<<<<<<< Updated upstream
|
||||||
|
=======
|
||||||
|
<<<<<<< HEAD
|
||||||
|
if args.manage == 'manage' or args.tag_update == 'tag_update':
|
||||||
|
num_tags = 0
|
||||||
|
for torrent in torrent_list:
|
||||||
|
if torrent.tags == '':
|
||||||
|
for x in torrent.trackers:
|
||||||
|
if x.url.startswith('http'):
|
||||||
|
new_tag = get_tags(x.url)
|
||||||
|
if args.dry_run == 'dry_run':
|
||||||
|
logger.info('DRY-RUN: Torrent Name: %s', torrent.name)
|
||||||
|
logger.info('DRY-RUN: - New Tag: %s', new_tag)
|
||||||
|
num_tags += 1
|
||||||
|
else:
|
||||||
|
logger.info('Torrent Name: %s', torrent.name)
|
||||||
|
logger.info(' - New Tag: %s', new_tag)
|
||||||
|
torrent.add_tags(tags=new_tag)
|
||||||
|
num_tags += 1
|
||||||
|
if args.dry_run == 'dry_run':
|
||||||
|
if num_tags >= 1:
|
||||||
|
logger.info('DRY-RUN: Did not update %s new tags.', num_tags)
|
||||||
|
else:
|
||||||
|
logger.info('DRY-RUN: No new torrents to tag.')
|
||||||
|
else:
|
||||||
|
if num_tags >= 1:
|
||||||
|
logger.info('Updated %s new tags.', num_tags)
|
||||||
|
else:
|
||||||
|
logger.info('No new torrents to tag.')
|
||||||
|
|
||||||
|
|
||||||
|
def rem_unregistered():
|
||||||
|
if args.manage == 'manage' or args.rem_unregistered == 'rem_unregistered':
|
||||||
|
rem_unr = 0
|
||||||
|
for torrent in torrent_list:
|
||||||
|
for status in torrent.trackers:
|
||||||
|
if 'Unregistered torrent' in status.msg:
|
||||||
|
if args.dry_run == 'dry_run':
|
||||||
|
logger.info('DRY-RUN: %s -> %s', torrent.name, status.msg)
|
||||||
|
logger.info('DRY-RUN: - NOT Deleted')
|
||||||
|
rem_unr += 1
|
||||||
|
else:
|
||||||
|
logger.info('%s -> %s', torrent.name, status.msg)
|
||||||
|
logger.info(' - Deleted')
|
||||||
|
torrent.delete(hash=torrent.hash,
|
||||||
|
delete_files=True)
|
||||||
|
rem_unr += 1
|
||||||
|
if args.dry_run == 'dry_run':
|
||||||
|
if rem_unr >= 1:
|
||||||
|
logger.info('DRY-RUN: Did not delete %s torrents.', rem_unr)
|
||||||
|
else:
|
||||||
|
logger.info('DRY-RUN: No unregistered torrents found.')
|
||||||
|
else:
|
||||||
|
if rem_unr >= 1:
|
||||||
|
logger.info('Deleted %s torrents.', rem_unr)
|
||||||
|
else:
|
||||||
|
logger.info('No unregistered torrents found.')
|
||||||
|
=======
|
||||||
|
>>>>>>> Stashed changes
|
||||||
num_tags = 0
|
num_tags = 0
|
||||||
for torrent in torrent_list:
|
for torrent in torrent_list:
|
||||||
if torrent.tags == '':
|
if torrent.tags == '':
|
||||||
|
@ -94,23 +241,13 @@ def rem_unregistered():
|
||||||
print('Deleted ', rem_unr, 'torrents.')
|
print('Deleted ', rem_unr, 'torrents.')
|
||||||
else:
|
else:
|
||||||
print('No unregistered torrents found.')
|
print('No unregistered torrents found.')
|
||||||
|
>>>>>>> e45a3f1064b6e19c352a0230194ff0293d13f548
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if args.command == 'manage':
|
update_category()
|
||||||
update_category()
|
update_tags()
|
||||||
update_tags()
|
rem_unregistered()
|
||||||
rem_unregistered()
|
|
||||||
elif args.command == 'tag-update':
|
|
||||||
update_tags()
|
|
||||||
elif args.command == 'cat-update':
|
|
||||||
update_category()
|
|
||||||
elif args.command == 'rem-unregistered':
|
|
||||||
rem_unregistered()
|
|
||||||
else:
|
|
||||||
print("The script requires an argument. See below:")
|
|
||||||
print("")
|
|
||||||
parser.print_help()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
5
requirements.txt
Normal file
5
requirements.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
PyYAML
|
||||||
|
qbittorrent-api
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue