From dcb055557150314759b0ca483b6080f42ce740f9 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 5 Mar 2019 00:10:06 +0100 Subject: [PATCH] Returing the best option for torrents based on max peers/seeds and returning more metadata info on torrent.search --- platypush/plugins/torrent.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/platypush/plugins/torrent.py b/platypush/plugins/torrent.py index 007955a3d..1ffd9a5cd 100644 --- a/platypush/plugins/torrent.py +++ b/platypush/plugins/torrent.py @@ -72,14 +72,24 @@ class TorrentPlugin(Plugin): if isinstance(response, bytes): response = response.decode('utf-8') - return [ - { - 'url': _['items'][0]['torrent_magnet'], - 'title': _['title'], - } + results = [] - for _ in json.loads(response).get('MovieList', []) - ] + for result in json.loads(response).get('MovieList', []): + torrent = sorted(result['items'], key=lambda _: + _['torrent_seeds'] + _['torrent_peers'])[-1] + results.append({ + 'title': result['title'], + 'url': torrent['torrent_magnet'], + 'file': torrent['file'], + 'size': torrent['size_bytes'], + 'language': torrent.get('language'), + 'quality': torrent.get('quality'), + 'torrent_url': torrent['torrent_url'], + 'torrent_seeds': torrent['torrent_seeds'], + 'torrent_peers': torrent['torrent_peers'], + }) + + return results @action def download(self, torrent, download_dir=None):