Refactored torrent search

This commit is contained in:
Fabio Manganiello 2019-04-24 23:25:22 +02:00
parent 9f3f6c9caa
commit 2bcc22e0fc

View file

@ -25,6 +25,7 @@ class TorrentPlugin(Plugin):
""" """
default_torrent_ports = [6881, 6891] default_torrent_ports = [6881, 6891]
supported_categories = ['movies', 'tv', 'anime']
torrent_state = {} torrent_state = {}
transfers = {} transfers = {}
@ -46,25 +47,35 @@ class TorrentPlugin(Plugin):
self.download_dir = os.path.abspath(os.path.expanduser(download_dir)) self.download_dir = os.path.abspath(os.path.expanduser(download_dir))
@action @action
def search(self, query): def search(self, query, category='movies', language=None):
""" """
Perform a search of video torrents. Perform a search of video torrents.
:param query: Query string, video name or partial name :param query: Query string, video name or partial name
:type query: str :type query: str
:param category: Category to search. Supported types: "movies", "tv", "anime".
Default: "movies"
:type category: str
:param language: Language code for the results - example: "en" (default: None, no filter)
:type language: str
""" """
self.logger.info('Searching matching torrents for "{}"'.format(query)) if category not in self.supported_categories:
raise RuntimeError('Unsupported category {}. Supported category: {}'.
format(category, self.supported_categories))
self.logger.info('Searching {} torrents for "{}"'.format(category, query))
url = 'https://{category}-v2.api-fetch.website/{category}/1'.format(category=category)
request = urllib.request.urlopen(urllib.request.Request( request = urllib.request.urlopen(urllib.request.Request(
'https://api.apidomain.info/list?' + urllib.parse.urlencode({ url + '?' + urllib.parse.urlencode({
'sort': 'relevance', 'sort': 'relevance',
'quality': '720p,1080p,3d',
'page': 1,
'keywords': query, 'keywords': query,
}), }),
headers = { headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' '(KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}) })
) )
@ -72,24 +83,26 @@ class TorrentPlugin(Plugin):
if isinstance(response, bytes): if isinstance(response, bytes):
response = response.decode('utf-8') response = response.decode('utf-8')
results = [] return sorted([
{
for result in json.loads(response).get('MovieList', []): 'imdb_id': result.get('imdb_id'),
torrent = sorted(result['items'], key=lambda _: 'title': '{title} [{language}][{quality}]'.format(
_['torrent_seeds'] + _['torrent_peers'])[-1] title=result.get('title'), language=lang, quality=quality),
results.append({ 'year': result.get('year'),
'title': result['title'], 'synopsis': result.get('synopsis'),
'url': torrent['torrent_magnet'], 'trailer': result.get('trailer'),
'file': torrent['file'], 'language': lang,
'size': torrent['size_bytes'], 'quality': quality,
'language': torrent.get('language'), 'size': item.get('size'),
'quality': torrent.get('quality'), 'seeds': item.get('seed'),
'torrent_url': torrent['torrent_url'], 'peers': item.get('peer'),
'torrent_seeds': torrent['torrent_seeds'], 'url': item.get('url'),
'torrent_peers': torrent['torrent_peers'], }
}) for result in (json.loads(response) or [])
for (lang, items) in result.get('torrents', {}).items()
return results if not language or language == lang
for (quality, item) in items.items()
], key=lambda _: _.get('seeds'), reverse=True)
@action @action
def download(self, torrent, download_dir=None): def download(self, torrent, download_dir=None):