sane-bt-add: handle https:// URIs which forward to magnet:

This commit is contained in:
Colin 2024-01-20 21:18:58 +00:00
parent 59187a0ec0
commit a31fe44624

View File

@ -7,6 +7,8 @@ import logging
import requests import requests
import tempfile import tempfile
from requests.exceptions import InvalidSchema
from sane_bt import MediaMeta, TransmissionApi from sane_bt import MediaMeta, TransmissionApi
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -31,12 +33,26 @@ def resolve_torrent(uri: str) -> str:
""" """
if uri.startswith("http://") or uri.startswith("https://"): if uri.startswith("http://") or uri.startswith("https://"):
logger.info(f"downloading remote torrent data: {uri}") logger.info(f"downloading remote torrent data: {uri}")
response = requests.get(uri) try:
# N.B.: without `delete=False`, file gets deleted when the function returns. response = requests.get(uri)
# could work around this, but it's nice to keep the file around anyway for troubleshooting except InvalidSchema as e:
f = tempfile.NamedTemporaryFile(suffix=".torrent", delete=False) # sometimes the URI resolves to a magnet: link (popular with KAT)
f.write(response.content) # that's *FINE*, unfortunately `requests` library gives us no way to access that link except by parsing the error message.
uri = f.name # alternatively i could install some special requests adapter that handles the magnet: schema.
logger.debug(f"URL resolution error: {e}")
if len(e.args) == 1 and "magnet:" in e.args[0]:
uri = e.args[0].replace("No connection adapters were found for ", "")
uri = uri.strip("'")
if not uri.startswith("magnet:"): raise
logger.info(f"resolved torrent to {uri}")
else:
raise
else:
# 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")): if not (uri.startswith("magnet:") or uri.endswith(".torrent")):
logger.warn(f"unknown type for torrent URI: {uri}") logger.warn(f"unknown type for torrent URI: {uri}")