···
1
+
#!/usr/bin/env -S uv run --script
11
+
"""seed_armbian_torrents.py
14
+
Armbian torrents seed script
16
+
This script will scrape https://mirrors.jevincanders.net/armbian/dl/ for
17
+
torrent files and add them to a qBittorrent instance. If there are already
18
+
Armbian torrents in the qBittorrent instance, they will be removed, and new
19
+
ones will be added in their place. This script is intended to be run under
20
+
/etc/cron.weekly or used in a systemd timer.
23
+
seed_armbian_torrents.py (HOSTNAME) (USERNAME) (PASSWORD)
24
+
seed_armbian_torrents.py -h
27
+
seed_armbian_torrents.py "http://localhost:8080" "admin" "adminadmin"
28
+
seed_armbian_torrents.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "pw"
31
+
-h, --help show this help message and exit.
36
+
import qbittorrentapi
38
+
from bs4 import BeautifulSoup
39
+
from docopt import docopt
42
+
def add_torrents(args: dict):
43
+
base_url = "https://mirrors.jevincanders.net/armbian/dl"
44
+
ignore_dirs = ["/armbian/", "_patch/", "_toolchain/"]
45
+
archive_dir_urls = []
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/")
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")
60
+
if link.text.endswith(".torrent"):
61
+
torrent_urls.append(url + link.text)
64
+
qbt_client = qbittorrentapi.Client(
65
+
host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
67
+
qbt_client.auth_log_in()
70
+
for url in torrent_urls:
71
+
torrent_count = torrent_count + 1
73
+
print(f"There are {torrent_count} torrents to add. This gonna take a while...")
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:
83
+
def remove_torrents(args: dict):
85
+
qbt_client = qbittorrentapi.Client(
86
+
host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
88
+
qbt_client.auth_log_in()
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:
99
+
if __name__ == "__main__":
100
+
args = docopt(__doc__) # type: ignore
101
+
remove_torrents(args)
104
+
# vim: ts=4 sts=4 sw=4 et ai ft=python