admin-scripts/.archived/python/add_scihub_torrents.py

70 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""add_scihub_torrents.py
Description:
Choose from a list of paginated Sci Hub torrent URL files and add each
URL to a qBittorrent instance.
This script expects a ~/scihub-torrent-urls directory to exist and be
populated by running the following commands from the justfile in this
repository:
$ just scihub-setup
Usage:
add_scihub_torrents.py (HOSTNAME) (USERNAME) (PASSWORD)
add_scihub_torrents.py -h
Examples:
add_scihub_torrents.py "http://localhost:8080" "admin" "adminadmin"
add_scihub_torrents.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "pw"
Options:
-h, --help show this help message and exit
"""
import os
import subprocess
from pathlib import Path
from docopt import docopt
from qbittorrent import Client
if __name__ == "__main__":
args = docopt(__doc__)
# Initialize client and login
qb = Client(args["HOSTNAME"])
qb.login(username=args["USERNAME"], password=args["PASSWORD"])
# Get scihub torrent- files and display them with gum
scihub_torrent_dir = Path.home().joinpath("scihub-torrent-urls")
torrent_files = [x.name for x in scihub_torrent_dir.iterdir()]
subproc_arg = [
"gum",
"choose",
"--header='Select Sci Hub torrents'",
"--height=13",
"--no-limit",
]
subproc_arg.extend(torrent_files)
torrent_selection = (
subprocess.run(subproc_arg, stdout=subprocess.PIPE, text=True)
.stdout.strip()
.splitlines()
)
# Read the contents of each file and put lines (which are URLs) into a list
torrent_urls = []
for item in torrent_selection:
with open(scihub_torrent_dir.joinpath(item), "r") as tf:
urls = tf.readlines()
torrent_urls.extend(urls)
torrent_urls = [x.strip("\n") for x in torrent_urls]
# Add urls to qBittorrent instance
for url in torrent_urls:
qb.download_from_link(url, category="scihub")
print(f"Added {os.path.basename(url)}")