From 1d13c79c75d5d15ac2e232155349b493ec1b28d8 Mon Sep 17 00:00:00 2001 From: mrlt8 <67088095+mrlt8@users.noreply.github.com> Date: Fri, 25 Sep 2020 15:21:44 -0600 Subject: [PATCH] Check for magnet link in 302 Check if url (e.g. Jackett) is redirecting to the magnet link. --- .../core/media/_base/providers/torrent/torrentpotato.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/couchpotato/core/media/_base/providers/torrent/torrentpotato.py b/couchpotato/core/media/_base/providers/torrent/torrentpotato.py index 5437f41..5dd78ab 100644 --- a/couchpotato/core/media/_base/providers/torrent/torrentpotato.py +++ b/couchpotato/core/media/_base/providers/torrent/torrentpotato.py @@ -1,6 +1,7 @@ from urlparse import urlparse import re import traceback +import requests from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.variable import splitString, tryInt, tryFloat @@ -42,9 +43,19 @@ class Base(TorrentProvider): log.error('%s: %s', (torrents.get('error'), host['host'])) elif torrents.get('results'): for torrent in torrents.get('results', []): + if re.match('^(http|https|ftp)://.*$', torrent.get('download_url')): + req = requests.get(torrent.get('download_url'), allow_redirects = False) + if req.status_code == 302 and re.match('^magnet:.*$', req.headers["Location"]): + torrent['download_url'] = req.headers["Location"] + proto = "torrent_magnet" + else: + proto = "transmission" + else: + proto = "torrent_magnet" + results.append({ 'id': torrent.get('torrent_id'), - 'protocol': 'torrent' if re.match('^(http|https|ftp)://.*$', torrent.get('download_url')) else 'torrent_magnet', + 'protocol' : proto, 'provider_extra': urlparse(host['host']).hostname or host['host'], 'name': toUnicode(torrent.get('release_name')), 'url': torrent.get('download_url'),