From 42a01921f79551a083d6f07256425c7fd18bcfeb Mon Sep 17 00:00:00 2001 From: Visorask <54461452+Visorask@users.noreply.github.com> Date: Fri, 19 Feb 2021 23:04:02 -0600 Subject: [PATCH] First push --- config.yml | 20 ++++++++++ qbit_scripts.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 config.yml create mode 100644 qbit_scripts.py diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..014c36d --- /dev/null +++ b/config.yml @@ -0,0 +1,20 @@ +qbt: + host: 'localhost:8080' + user: 'username' + pass: 'password' +cat: + # : + anime: anime + movies: movies + tv: tv +tags: + # : + awesome-hd: AHD + beyond-hd: Beyond-HD + animebytes.tv: AnimeBytes + avistaz: Avistaz + landof.tv: BroadcasTheNet + passthepopcorn: PassThePopcorn + + + diff --git a/qbit_scripts.py b/qbit_scripts.py new file mode 100644 index 0000000..dbcb634 --- /dev/null +++ b/qbit_scripts.py @@ -0,0 +1,97 @@ +#!/usr/bin/python3 + +from qbittorrentapi import Client +import yaml +import argparse + +parser = argparse.ArgumentParser("qBittorrent scripts.", + description='A mix of scripts combined for managing qBittorrent.') +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') +parser.add_argument('--cat-update', + dest='command', + action='store_const', + const='cat-update', + help='Use this if you would like to update your categories.') +parser.add_argument('--tag-update', + dest='command', + action='store_const', + const='tag-update', + help='Use this if you would like to update your tags.') +args = parser.parse_args() + +with open(args.config, "r") as cfg_file: + cfg = yaml.load(cfg_file, Loader=yaml.FullLoader) + +# Actual API call to connect to qbt. +client = Client(host=cfg["qbt"]["host"], username=cfg["qbt"]["user"], password=cfg["qbt"]["pass"]) +torrent_list = client.torrents.info() + + +def get_category(path): + cat_path = cfg["cat"] + for p, c in cat_path.items(): + if p in path: + category = c + return category + else: + category = '' + return category + + +# Main command that does the work. +def update_category(): + num_cat = 0 + for torrent in torrent_list: + if torrent.category == '': + new_cat = get_category(torrent.save_path) + print('Torrent Name: ' + torrent.name) + print(' - New Category: ' + new_cat) + torrent.set_category(category=new_cat) + num_cat += 1 + if num_cat >= 1: + print('Updated ', num_cat, ' new categories') + else: + print('No new torrents to categorize.') + + +def get_tags(url): + tag_path = cfg["tags"] + for t, n in tag_path.items(): + if t in url: + tag = n + return tag + else: + tag = '' + return tag + + +def update_tags(): + 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) + print('Torrent Name: ' + torrent.name) + print(' - New Tag: ' + new_tag) + torrent.add_tags(tags=new_tag) + num_tags += 1 + if num_tags >= 1: + print('Updated ', num_tags, ' new tags') + else: + print('No new torrents to tag.') + + +def main(): + if args.command == 'cat-update': + update_category() + elif args.command == 'tag-update': + update_tags() + + +if __name__ == "__main__": + main()