diff --git a/platypush/plugins/video/torrentcast.py b/platypush/plugins/video/torrentcast.py index 40c3a37f..c64b4df2 100644 --- a/platypush/plugins/video/torrentcast.py +++ b/platypush/plugins/video/torrentcast.py @@ -1,5 +1,7 @@ +import json import urllib3 import urllib.request +import urllib.parse from platypush.message.response import Response @@ -9,6 +11,7 @@ class VideoTorrentcastPlugin(Plugin): def __init__(self, server='localhost', port=9090, *args, **kwargs): self.server = server self.port = port + self.playing = False def play(self, url): request = urllib.request.urlopen( @@ -18,6 +21,7 @@ class VideoTorrentcastPlugin(Plugin): }) ) + self.playing = True return Response(output=request.read()) def pause(self): @@ -32,8 +36,39 @@ class VideoTorrentcastPlugin(Plugin): request = http.request('POST', 'http://{}:{}/stop/'.format(self.server, self.port)) + self.playing = False return Response(output=request.read()) + def search(self, query): + request = urllib.request.urlopen(urllib.request.Request( + 'https://api.apidomain.info/list?' + urllib.parse.urlencode({ + 'sort': 'seeds', + 'quality': '720p,1080p,3d', + 'page': 1, + 'keywords': query, + }), + headers = { + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' + + '(KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' + }) + ) + + results = json.loads(request.read()) + return Response(output=results) + + def search_and_play(self, query): + response = self.search(query) + if not response.output['MovieList']: + logging.info('No torrent results found for {}'.format(query)) + return Response() + + item = response.output['MovieList'][0] + magnet = item['items'][0]['torrent_magnet'] + logging.info('Playing torrent "{}" from {}' + .format(item['title'], magnet)) + + return self.play(magnet) + def voldown(self): return Response(output='Unsupported method') def volup(self): return Response(output='Unsupported method') def back(self): return Response(output='Unsupported method')