Commit a915b3c3 authored by echel0n's avatar echel0n
Browse files

Added new code for converting magnet links to torrent files via SR API

parent bb8a5b8a
# Changelog
- * 54e8976 - 2017-12-15: Fix unicode issues for Newpct torrent provider
- * 5b32791 - 2017-12-15: Added new code for converting magnet links to torrent files via SR API
- * bb8a5b8 - 2017-12-15: Fix unicode issues for Newpct torrent provider
- * c0f1698 - 2017-12-15: Added custom url setting to torrent9 provider
- * 032e9ed - 2017-12-15: Release v9.2.64
- * 92337a7 - 2017-12-15: Added code to cleanup cached provider results with UNKNOWN quality Added code to prevent parsed search results with UNKNOWN quality from being cached
......
......@@ -76,3 +76,6 @@ class API(object):
'v1/providers/cache/results/{}/indexerids/{}'.format(provider, indexerid))[indexerid is not None]
return self._request('GET', query)
def magnet2torrent(self, magnet):
return self._request('POST', 'v1/mag2tor', data=dict({'magnet':magnet}))
......@@ -18,6 +18,7 @@
from __future__ import unicode_literals
import base64
import datetime
import importlib
import io
......@@ -632,36 +633,36 @@ class TorrentProvider(GenericProvider):
return result
def get_content(self, url):
urls = [url]
bt_cache_urls = [
'https://itorrents.org/torrent/{info_hash}.torrent',
'http://reflektor.karmorra.info/torrent/{info_hash}.torrent',
'https://torrent.cd/torrents/download/{info_hash}.torrent',
'https://asnet.pw/download/{info_hash}/',
'http://p2pdl.com/download/{info_hash}',
]
result = None
def verify_torrent(content):
try:
if bencode.bdecode(content).get('info'):
return content
except Exception:
pass
if url.startswith('magnet'):
# try iTorrents
info_hash = str(re.findall(r'urn:btih:([\w]{32,40})', url)[0]).upper()
if len(info_hash) == 32:
info_hash = b16encode(b32decode(info_hash)).upper()
if not info_hash:
sickrage.app.log.error("Unable to extract torrent hash from magnet: " + url)
return urls
if info_hash:
torrent_url = "https://itorrents.org/torrent/{info_hash}.torrent".format(info_hash=info_hash)
result = verify_torrent(super(TorrentProvider, self).get_content(torrent_url))
urls = [x.format(info_hash=info_hash) for x in bt_cache_urls]
# try api
if not result:
try:
result = verify_torrent(base64.b64decode(sickrage.app.api.magnet2torrent(url)['message']).strip())
except Exception:
pass
random.shuffle(urls)
if not result:
result = verify_torrent(super(TorrentProvider, self).get_content(url))
for u in urls:
try:
content = super(TorrentProvider, self).get_content(u)
if bencode.bdecode(content).get('info'):
return content
except Exception:
pass
return result
def _get_title_and_url(self, item):
title, download_url = '', ''
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment