sane-bt-add: accept https:// urls and extract actual torrents from them
This commit is contained in:
@@ -46,7 +46,7 @@ let
|
|||||||
bt-add = static-nix-shell.mkPython3Bin {
|
bt-add = static-nix-shell.mkPython3Bin {
|
||||||
pname = "sane-bt-add";
|
pname = "sane-bt-add";
|
||||||
src = ./src;
|
src = ./src;
|
||||||
pyPkgs = [ "sane-lib.bt" ];
|
pyPkgs = [ "requests" "sane-lib.bt" ];
|
||||||
pkgs = [ "sane-scripts.lib.bt.propagatedBuildInputs" ];
|
pkgs = [ "sane-scripts.lib.bt.propagatedBuildInputs" ];
|
||||||
};
|
};
|
||||||
bt-rm = static-nix-shell.mkPython3Bin {
|
bt-rm = static-nix-shell.mkPython3Bin {
|
||||||
|
@@ -1,12 +1,35 @@
|
|||||||
#!/usr/bin/env nix-shell
|
#!/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 :
|
# vim: set filetype=python :
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
import requests
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from sane_bt import MediaMeta, TransmissionApi
|
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():
|
def main():
|
||||||
logging.basicConfig()
|
logging.basicConfig()
|
||||||
@@ -15,13 +38,14 @@ def main():
|
|||||||
parser = argparse.ArgumentParser(description="instruct servo to download some torrent")
|
parser = argparse.ArgumentParser(description="instruct servo to download some torrent")
|
||||||
TransmissionApi.add_arguments(parser)
|
TransmissionApi.add_arguments(parser)
|
||||||
MediaMeta.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()
|
args = parser.parse_args()
|
||||||
meta = MediaMeta.from_arguments(args)
|
meta = MediaMeta.from_arguments(args)
|
||||||
bt_api = TransmissionApi.from_arguments(args)
|
bt_api = TransmissionApi.from_arguments(args)
|
||||||
torrent = args.torrent
|
torrent = args.torrent
|
||||||
|
|
||||||
|
torrent = resolve_torrent(torrent)
|
||||||
bt_api.add_or_move_torrent(meta, torrent)
|
bt_api.add_or_move_torrent(meta, torrent)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Reference in New Issue
Block a user