# /// script
# dependencies = [
#   "requests",
#   "docopt",
# ]
# ///

"""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)