Support for download-only option in webtorrent plugin

This commit is contained in:
Fabio Manganiello 2019-01-20 15:10:14 +01:00
parent d297b5cc42
commit 3c0ed443a2
1 changed files with 46 additions and 33 deletions

View File

@ -119,7 +119,8 @@ class MediaWebtorrentPlugin(MediaPlugin):
return re.sub('\x1b\[((\d+m)|(.{1,2}))', '', line).strip()
def _process_monitor(self, resource, download_dir, player_type, player_args):
def _process_monitor(self, resource, download_dir, download_only,
player_type, player_args):
def _thread():
if not self._webtorrent_process:
return
@ -191,7 +192,7 @@ class MediaWebtorrentPlugin(MediaPlugin):
if not output_dir:
raise RuntimeError('Could not download torrent')
if not media_file or not webtorrent_url:
if not download_only and (not media_file or not webtorrent_url):
if not media_file:
self.logger.warning(
'The torrent does not contain any video files')
@ -203,7 +204,10 @@ class MediaWebtorrentPlugin(MediaPlugin):
except: pass
return
# Then wait until we have enough chunks to start the player
player = None
if not download_only:
# Wait until we have enough chunks to start the player
while True:
result = poll.poll(0)
if not result:
@ -231,6 +235,7 @@ class MediaWebtorrentPlugin(MediaPlugin):
player.play(media, **player_args)
self.logger.info('Waiting for player to terminate')
self._wait_for_player(player)
self.logger.info('Torrent player terminated')
bus.post(TorrentDownloadCompletedEvent(resource=resource))
@ -242,9 +247,11 @@ class MediaWebtorrentPlugin(MediaPlugin):
return _thread
def _wait_for_player(self, player):
media_cls = player.__class__.__name__
stop_evt = None
if player:
media_cls = player.__class__.__name__
if media_cls == 'MediaMplayerPlugin':
stop_evt = player._mplayer_stopped_event
elif media_cls == 'MediaOmxplayerPlugin':
@ -270,7 +277,7 @@ class MediaWebtorrentPlugin(MediaPlugin):
@action
def play(self, resource, player=None, **player_args):
def play(self, resource, player=None, download_only=False, **player_args):
"""
Download and stream a torrent
@ -307,10 +314,16 @@ class MediaWebtorrentPlugin(MediaPlugin):
threading.Thread(target=self._process_monitor(
resource=resource, download_dir=download_dir,
player_type=player, player_args=player_args)).start()
player_type=player, player_args=player_args,
download_only=download_only)).start()
return { 'resource': resource }
@action
def download(resource, *args, **kwargs):
return self.play(resource, download_only=True, *args, **kwargs)
@action
def stop(self):
""" Stop the playback """