Assorted shell and Python scripts

Add seed_armbian_torrents.py

-91
.archived/seed_armbian_torrents.py
···
-
#!/usr/bin/env -S uv run --script
-
# /// script
-
# dependencies = [
-
# "qbittorrent-api",
-
# "requests",
-
# "bs4",
-
# "docopt"
-
# ]
-
# ///
-
-
"""seed_armbian_torrents.py
-
-
Description:
-
Armbian torrents seed script
-
-
This script will scrape https://mirrors.jevincanders.net/armbian/dl/ for
-
torrent files and add them to a qBittorrent instance. If there are already
-
Armbian torrents in the qBittorrent instance, they will be removed, and new
-
ones will be added in their place. This script is intended to be run under
-
/etc/cron.weekly or used in a systemd timer.
-
-
Usage:
-
seed_armbian_torrents.py (HOSTNAME) (USERNAME) (PASSWORD)
-
seed_armbian_torrents.py -h
-
-
Examples:
-
seed_armbian_torrents.py "http://localhost:8080" "admin" "adminadmin"
-
seed_armbian_torrents.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "pw"
-
-
Options:
-
-h, --help show this help message and exit.
-
"""
-
-
import os
-
-
import qbittorrentapi
-
import requests
-
from bs4 import BeautifulSoup
-
from docopt import docopt
-
-
-
def add_torrents(args: dict):
-
archive_dir_urls = [
-
"https://mirrors.jevincanders.net/armbian/dl/orangepi5-plus/archive/",
-
"https://mirrors.jevincanders.net/armbian/dl/rockpro64/archive/",
-
"https://mirrors.jevincanders.net/armbian/dl/rpi4b/archive/",
-
]
-
-
torrent_urls = []
-
for url in archive_dir_urls:
-
response = requests.get(url, timeout=60)
-
soup = BeautifulSoup(response.content, "html.parser")
-
links = soup.find_all("a")
-
for link in links:
-
if link.text.endswith(".torrent"):
-
torrent_urls.append(url + link.text)
-
-
try:
-
qbt_client = qbittorrentapi.Client(
-
host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
-
)
-
qbt_client.auth_log_in()
-
-
for url in torrent_urls:
-
qbt_client.torrents_add(url, category="distro")
-
print(f"Added {os.path.basename(url)}")
-
qbt_client.auth_log_out()
-
except qbittorrentapi.LoginFailed as e:
-
print(e)
-
-
-
def remove_torrents(args: dict):
-
try:
-
qbt_client = qbittorrentapi.Client(
-
host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
-
)
-
qbt_client.auth_log_in()
-
-
for torrent in qbt_client.torrents_info():
-
if torrent.name.startswith("Armbian"):
-
torrent.delete(delete_files=True)
-
print(f"Removed {torrent.name}")
-
qbt_client.auth_log_out()
-
except qbittorrentapi.LoginFailed as e:
-
print(e)
-
-
-
if __name__ == "__main__":
-
args = docopt(__doc__) # type: ignore
-
remove_torrents(args)
-
add_torrents(args)
+104
seed_armbian_torrents.py
···
+
#!/usr/bin/env -S uv run --script
+
# /// script
+
# dependencies = [
+
# "qbittorrent-api",
+
# "requests",
+
# "bs4",
+
# "docopt"
+
# ]
+
# ///
+
+
"""seed_armbian_torrents.py
+
+
Description:
+
Armbian torrents seed script
+
+
This script will scrape https://mirrors.jevincanders.net/armbian/dl/ for
+
torrent files and add them to a qBittorrent instance. If there are already
+
Armbian torrents in the qBittorrent instance, they will be removed, and new
+
ones will be added in their place. This script is intended to be run under
+
/etc/cron.weekly or used in a systemd timer.
+
+
Usage:
+
seed_armbian_torrents.py (HOSTNAME) (USERNAME) (PASSWORD)
+
seed_armbian_torrents.py -h
+
+
Examples:
+
seed_armbian_torrents.py "http://localhost:8080" "admin" "adminadmin"
+
seed_armbian_torrents.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "pw"
+
+
Options:
+
-h, --help show this help message and exit.
+
"""
+
+
import os
+
+
import qbittorrentapi
+
import requests
+
from bs4 import BeautifulSoup
+
from docopt import docopt
+
+
+
def add_torrents(args: dict):
+
base_url = "https://mirrors.jevincanders.net/armbian/dl"
+
ignore_dirs = ["/armbian/", "_patch/", "_toolchain/"]
+
archive_dir_urls = []
+
+
page = requests.get(base_url).text
+
soup = BeautifulSoup(page, "html.parser")
+
for node in soup.find_all("a"):
+
if node.get("href") is not None:
+
if node.get("href").endswith("/") and node.get("href") not in ignore_dirs:
+
archive_dir_urls.append(f"{base_url}/{node.get("href")}archive/")
+
+
torrent_urls = []
+
for url in archive_dir_urls:
+
response = requests.get(url, timeout=60)
+
soup = BeautifulSoup(response.content, "html.parser")
+
links = soup.find_all("a")
+
for link in links:
+
if link.text.endswith(".torrent"):
+
torrent_urls.append(url + link.text)
+
+
try:
+
qbt_client = qbittorrentapi.Client(
+
host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
+
)
+
qbt_client.auth_log_in()
+
+
torrent_count = 0
+
for url in torrent_urls:
+
torrent_count = torrent_count + 1
+
+
print(f"There are {torrent_count} torrents to add. This gonna take a while...")
+
+
for url in torrent_urls:
+
qbt_client.torrents_add(url, category="distro")
+
print(f"Added {os.path.basename(url)}")
+
qbt_client.auth_log_out()
+
except qbittorrentapi.LoginFailed as e:
+
print(e)
+
+
+
def remove_torrents(args: dict):
+
try:
+
qbt_client = qbittorrentapi.Client(
+
host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
+
)
+
qbt_client.auth_log_in()
+
+
for torrent in qbt_client.torrents_info():
+
if torrent.name.startswith("Armbian"):
+
torrent.delete(delete_files=True)
+
print(f"Removed {torrent.name}")
+
qbt_client.auth_log_out()
+
except qbittorrentapi.LoginFailed as e:
+
print(e)
+
+
+
if __name__ == "__main__":
+
args = docopt(__doc__) # type: ignore
+
remove_torrents(args)
+
add_torrents(args)
+
+
# vim: ts=4 sts=4 sw=4 et ai ft=python