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() 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(): def _thread():
if not self._webtorrent_process: if not self._webtorrent_process:
return return
@ -191,7 +192,7 @@ class MediaWebtorrentPlugin(MediaPlugin):
if not output_dir: if not output_dir:
raise RuntimeError('Could not download torrent') 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: if not media_file:
self.logger.warning( self.logger.warning(
'The torrent does not contain any video files') 'The torrent does not contain any video files')
@ -203,34 +204,38 @@ class MediaWebtorrentPlugin(MediaPlugin):
except: pass except: pass
return return
# Then wait until we have enough chunks to start the player player = None
while True:
result = poll.poll(0)
if not result:
continue
if not self._is_process_alive(): if not download_only:
break # Wait until we have enough chunks to start the player
while True:
result = poll.poll(0)
if not result:
continue
try: if not self._is_process_alive():
if os.path.getsize(media_file) > \
self._download_size_before_streaming:
break break
except FileNotFoundError:
continue
player = get_plugin('media.' + player_type) if player_type \ try:
else self._media_plugin if os.path.getsize(media_file) > \
self._download_size_before_streaming:
break
except FileNotFoundError:
continue
media = media_file if player.is_local() else webtorrent_url player = get_plugin('media.' + player_type) if player_type \
else self._media_plugin
self.logger.info( media = media_file if player.is_local() else webtorrent_url
'Starting playback of {} to {} through {}'.format(
media_file, player.__class__.__name__, self.logger.info(
webtorrent_url)) 'Starting playback of {} to {} through {}'.format(
media_file, player.__class__.__name__,
webtorrent_url))
player.play(media, **player_args)
self.logger.info('Waiting for player to terminate')
player.play(media, **player_args)
self.logger.info('Waiting for player to terminate')
self._wait_for_player(player) self._wait_for_player(player)
self.logger.info('Torrent player terminated') self.logger.info('Torrent player terminated')
bus.post(TorrentDownloadCompletedEvent(resource=resource)) bus.post(TorrentDownloadCompletedEvent(resource=resource))
@ -242,16 +247,18 @@ class MediaWebtorrentPlugin(MediaPlugin):
return _thread return _thread
def _wait_for_player(self, player): def _wait_for_player(self, player):
media_cls = player.__class__.__name__
stop_evt = None stop_evt = None
if media_cls == 'MediaMplayerPlugin': if player:
stop_evt = player._mplayer_stopped_event media_cls = player.__class__.__name__
elif media_cls == 'MediaOmxplayerPlugin':
stop_evt = threading.Event() if media_cls == 'MediaMplayerPlugin':
def stop_callback(): stop_evt = player._mplayer_stopped_event
stop_evt.set() elif media_cls == 'MediaOmxplayerPlugin':
player.add_handler('stop', stop_callback) stop_evt = threading.Event()
def stop_callback():
stop_evt.set()
player.add_handler('stop', stop_callback)
if stop_evt: if stop_evt:
stop_evt.wait() stop_evt.wait()
@ -270,7 +277,7 @@ class MediaWebtorrentPlugin(MediaPlugin):
@action @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 Download and stream a torrent
@ -307,10 +314,16 @@ class MediaWebtorrentPlugin(MediaPlugin):
threading.Thread(target=self._process_monitor( threading.Thread(target=self._process_monitor(
resource=resource, download_dir=download_dir, 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 } return { 'resource': resource }
@action
def download(resource, *args, **kwargs):
return self.play(resource, download_only=True, *args, **kwargs)
@action @action
def stop(self): def stop(self):
""" Stop the playback """ """ Stop the playback """