admin-scripts/fetch_scihub_infohashes.py

48 lines
1.3 KiB
Python
Raw Normal View History

2024-11-09 00:18:47 +01:00
# /// script
# dependencies = [
# "requests",
# "docopt",
# ]
# ///
2024-08-05 00:18:24 +02:00
"""fetch_scihub_infohashes.py
Description:
This script fetches the infohashes of all Sci Hub torrents and writes them to a
plaintext file. The plaintext file is intended to be appended to a bittorrent
tracker whitelist. E.g., /etc/opentracker/whitelist.txt.
Optionally set the TORRENT_JSON_URL for the Sci Hub torrent health checker, or
run the script with no arguments to use the default.
Default health check URL:
https://zrthstr.github.io/libgen_torrent_cardiography/torrent.json
Usage:
fetch_scihub_infohashes.py [TORRENT_JSON_URL]
fetch_scihub_infohashes.py -h
Options:
-h, --help show this help message and exit.
"""
import json
from pathlib import Path
import requests
from docopt import docopt
if __name__ == "__main__":
args = docopt(__doc__) # type: ignore
url = (
args["TORRENT_JSON_URL"]
if args["TORRENT_JSON_URL"]
else "https://zrthstr.github.io/libgen_torrent_cardiography/torrent.json"
)
response = requests.get(url, timeout=60)
json_data = json.loads(response.text)
torrent_infohashes = [f"{x["infohash"]}\n" for x in json_data]
with open(Path.cwd().joinpath("scihub_torrent_infohashes.txt"), "w") as tf:
tf.writelines(torrent_infohashes)