Assorted shell and Python scripts
1#!/usr/bin/env -S uv run --script
2# /// script
3# dependencies = [
4# "requests",
5# "docopt",
6# ]
7# ///
8
9"""fetch_scihub_infohashes.py
10
11Description:
12This script fetches the infohashes of all Sci Hub torrents and writes them to a
13plaintext file. The plaintext file is intended to be appended to a bittorrent
14tracker whitelist. E.g., /etc/opentracker/whitelist.txt.
15
16Optionally set the TORRENT_JSON_URL for the Sci Hub torrent health checker, or
17run the script with no arguments to use the default.
18
19Default health check URL:
20https://zrthstr.github.io/libgen_torrent_cardiography/torrent.json
21
22Usage:
23 fetch_scihub_infohashes.py [TORRENT_JSON_URL]
24 fetch_scihub_infohashes.py -h
25
26Options:
27 -h, --help show this help message and exit.
28"""
29
30import json
31from pathlib import Path
32
33import requests
34from docopt import docopt
35
36if __name__ == "__main__":
37 args = docopt(__doc__) # type: ignore
38 url = (
39 args["TORRENT_JSON_URL"]
40 if args["TORRENT_JSON_URL"]
41 else "https://zrthstr.github.io/libgen_torrent_cardiography/torrent.json"
42 )
43 response = requests.get(url, timeout=60)
44 json_data = json.loads(response.text)
45 torrent_infohashes = [f"{x["infohash"]}\n" for x in json_data]
46
47 with open(Path.cwd().joinpath("scihub_torrent_infohashes.txt"), "w") as tf:
48 tf.writelines(torrent_infohashes)