sane-bt-add: accept https:// urls and extract actual torrents from them

This commit is contained in:
2023-09-27 17:56:35 +00:00
parent aa3ee802d2
commit 10e6436c34
2 changed files with 27 additions and 3 deletions

View File

@@ -46,7 +46,7 @@ let
bt-add = static-nix-shell.mkPython3Bin {
pname = "sane-bt-add";
src = ./src;
pyPkgs = [ "sane-lib.bt" ];
pyPkgs = [ "requests" "sane-lib.bt" ];
pkgs = [ "sane-scripts.lib.bt.propagatedBuildInputs" ];
};
bt-rm = static-nix-shell.mkPython3Bin {

View File

@@ -1,12 +1,35 @@
#!/usr/bin/env nix-shell
#!nix-shell -i python3 -p "python3.withPackages (ps: [ ps.sane-lib.bt ])" -p sane-scripts.lib.bt.propagatedBuildInputs
#!nix-shell -i python3 -p "python3.withPackages (ps: [ ps.requests ps.sane-lib.bt ])" -p sane-scripts.lib.bt.propagatedBuildInputs
# vim: set filetype=python :
import argparse
import logging
import requests
import tempfile
from sane_bt import MediaMeta, TransmissionApi
logger = logging.getLogger(__name__)
def resolve_torrent(uri: str) -> str:
"""
given a URI, coerce it into something native to transmission
i.e. magnet: or .torrent.
"""
if uri.startswith("http://") or uri.startswith("https://"):
logger.info(f"downloading remote torrent data: {uri}")
response = requests.get(uri)
# N.B.: without `delete=False`, file gets deleted when the function returns.
# could work around this, but it's nice to keep the file around anyway for troubleshooting
f = tempfile.NamedTemporaryFile(suffix=".torrent", delete=False)
f.write(response.content)
uri = f.name
if not (uri.startswith("magnet:") or uri.endswith(".torrent")):
logger.warn(f"unknown type for torrent URI: {uri}")
return uri
def main():
logging.basicConfig()
@@ -15,13 +38,14 @@ def main():
parser = argparse.ArgumentParser(description="instruct servo to download some torrent")
TransmissionApi.add_arguments(parser)
MediaMeta.add_arguments(parser)
parser.add_argument("torrent", help="magnet: URI or file path to torrent")
parser.add_argument("torrent", help="file path or URI to torrent (magnet, https)")
args = parser.parse_args()
meta = MediaMeta.from_arguments(args)
bt_api = TransmissionApi.from_arguments(args)
torrent = args.torrent
torrent = resolve_torrent(torrent)
bt_api.add_or_move_torrent(meta, torrent)
if __name__ == "__main__":