admin-scripts/bin/add_scihub_torrents.py

70 lines
2.0 KiB
Python
Raw Normal View History

2024-07-20 03:19:16 +02:00
#!/usr/bin/env python3
2024-07-29 08:48:44 +02:00
"""add_scihub_torrents.py
2024-07-20 03:19:16 +02:00
2024-07-29 08:48:44 +02:00
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
2024-07-20 03:19:16 +02:00
Usage:
2024-07-29 08:48:44 +02:00
add_scihub_torrents.py (HOSTNAME) (USERNAME) (PASSWORD)
add_scihub_torrents.py -h
2024-07-20 03:19:16 +02:00
Examples:
2024-07-29 08:48:44 +02:00
add_scihub_torrents.py "http://localhost:8080" "admin" "adminadmin"
add_scihub_torrents.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "pw"
2024-07-20 03:19:16 +02:00
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
2024-07-28 04:23:03 +02:00
torrent_urls = []
2024-07-20 03:19:16 +02:00
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)}")