Assorted shell and Python scripts
1#!/usr/bin/env -S uv run --script
2# /// script
3# dependencies = [
4# "qbittorrent-api",
5# "requests",
6# "bs4",
7# "docopt"
8# ]
9# ///
10
11"""seed_armbian_torrents.py
12
13Description:
14Armbian torrents seed script
15
16This script will scrape https://mirrors.jevincanders.net/armbian/dl/ for
17torrent files and add them to a qBittorrent instance. If there are already
18Armbian torrents in the qBittorrent instance, they will be removed, and new
19ones will be added in their place. This script is intended to be run under
20/etc/cron.weekly or used in a systemd timer.
21
22Usage:
23 seed_armbian_torrents.py (HOSTNAME) (USERNAME) (PASSWORD)
24 seed_armbian_torrents.py -h
25
26Examples:
27 seed_armbian_torrents.py "http://localhost:8080" "admin" "adminadmin"
28 seed_armbian_torrents.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "pw"
29
30Options:
31 -h, --help show this help message and exit.
32"""
33
34import os
35
36import qbittorrentapi
37import requests
38from bs4 import BeautifulSoup
39from docopt import docopt
40
41
42def add_torrents(args: dict):
43 base_url = "https://mirrors.jevincanders.net/armbian/dl"
44 ignore_dirs = ["/armbian/", "_patch/", "_toolchain/"]
45 archive_dir_urls = []
46
47 page = requests.get(base_url).text
48 soup = BeautifulSoup(page, "html.parser")
49 for node in soup.find_all("a"):
50 if node.get("href") is not None:
51 if node.get("href").endswith("/") and node.get("href") not in ignore_dirs:
52 archive_dir_urls.append(f"{base_url}/{node.get("href")}archive/")
53
54 torrent_urls = []
55 for url in archive_dir_urls:
56 response = requests.get(url, timeout=60)
57 soup = BeautifulSoup(response.content, "html.parser")
58 links = soup.find_all("a")
59 for link in links:
60 if link.text.endswith(".torrent"):
61 torrent_urls.append(url + link.text)
62
63 try:
64 qbt_client = qbittorrentapi.Client(
65 host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
66 )
67 qbt_client.auth_log_in()
68
69 torrent_count = 0
70 for url in torrent_urls:
71 torrent_count = torrent_count + 1
72
73 print(f"There are {torrent_count} torrents to add. This gonna take a while...")
74
75 for url in torrent_urls:
76 qbt_client.torrents_add(url, category="distro")
77 print(f"Added {os.path.basename(url)}")
78 qbt_client.auth_log_out()
79 except qbittorrentapi.LoginFailed as e:
80 print(e)
81
82
83def remove_torrents(args: dict):
84 try:
85 qbt_client = qbittorrentapi.Client(
86 host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
87 )
88 qbt_client.auth_log_in()
89
90 for torrent in qbt_client.torrents_info():
91 if torrent.name.startswith("Armbian"):
92 torrent.delete(delete_files=True)
93 print(f"Removed {torrent.name}")
94 qbt_client.auth_log_out()
95 except qbittorrentapi.LoginFailed as e:
96 print(e)
97
98
99if __name__ == "__main__":
100 args = docopt(__doc__) # type: ignore
101 remove_torrents(args)
102 add_torrents(args)
103
104# vim: ts=4 sts=4 sw=4 et ai ft=python