Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
Improve URL resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
fugkco committed May 28, 2020
1 parent 322047e commit 6f61bd9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from urlparse import urljoin
from xml.etree import ElementTree

import requests
from elementum.provider import log
from requests_toolbelt import sessions

Expand Down Expand Up @@ -259,6 +260,23 @@ def _parse_item(self, item):

result[json] = val

# if we didn't get a magnet uri, attempt to resolve the magnet uri.
# todo for some reason Elementum cannot resolve the link that gets proxied through Jackett.
# So we will resolve it manually for Elementum for now.
# In actuality, this should be fixed within Elementum
if result["uri"] is None:
link = item.find('link')
jackett_uri = ""
if link is not None:
jackett_uri = link.text
else:
enclosure = item.find('enclosure')
if enclosure is not None:
jackett_uri = enclosure.attrib['url']

if jackett_uri != "":
result["uri"] = get_magnet_from_jackett(jackett_uri)

if result["name"] is None or result["uri"] is None:
log.warning("Could not parse item; name = %s; uri = %s", result["name"], result["uri"])
log.debug("Failed item is: %s", ElementTree.tostring(item, encoding='utf8'))
Expand All @@ -277,3 +295,19 @@ def _parse_item(self, item):
result["size"] = human_size(result["_size_bytes"])

return result


def get_magnet_from_jackett(uri):
magnet_prefix = 'magnet:'
original_uri = uri
while True:
response = requests.get(uri, allow_redirects=False)
if response.status_code >= httplib.MULTIPLE_CHOICES and response.status_code < httplib.BAD_REQUEST:
uri = response.headers['Location']
if len(uri) >= len(magnet_prefix) and uri[0:7] == magnet_prefix:
return uri
else:
log.warning("Could not get final redirect location for URI %s", original_uri)
break

return None
4 changes: 2 additions & 2 deletions src/jackett.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def validate_client():
def search(payload, method="general"):
payload = parse_payload(method, payload)

log.debug("Searching with payload (%s): %s" % (method, repr(payload)))
log.debug("Searching with payload (%s): %s", method, repr(payload))

p_dialog = xbmcgui.DialogProgressBG()
p_dialog.create('Elementum [COLOR FFFF6B00]Jackett[/COLOR]', utils.translation(32602))
Expand All @@ -68,7 +68,7 @@ def search(payload, method="general"):

log.debug("All results: %s" % repr(results))

log.info("Jackett returned %d results in %s seconds" % (len(results), request_time))
log.info("Jackett returned %d results in %s seconds", len(results), request_time)
finally:
p_dialog.close()
del p_dialog
Expand Down

0 comments on commit 6f61bd9

Please sign in to comment.