From 651e9d9cfe5cb06d12cb79567ba72164f4072643 Mon Sep 17 00:00:00 2001 From: Joaqim Planstedt Date: Wed, 22 Apr 2020 13:15:13 +0200 Subject: [PATCH] Added range function to play: 'play [show] [[episode][-][episode-to-play-to]]' --- trackma/engine.py | 45 ++++++++++++++++++++++++++++++++++++--------- trackma/ui/cli.py | 8 ++++++-- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/trackma/engine.py b/trackma/engine.py index 81df0edd..ba0d3926 100644 --- a/trackma/engine.py +++ b/trackma/engine.py @@ -817,7 +817,7 @@ def _add_show_to_library(self, library, library_cache, rescan, fullpath, filenam return library, library_cache - def get_episode_path(self, show, episode): + def get_episode_path(self, show, episode, error_on_fail=True): """ This function returns the full path of the requested episode from the requested show. """ @@ -828,7 +828,9 @@ def get_episode_path(self, show, episode): if showid not in library: raise utils.EngineError('Show not in library.') if episode not in library[showid]: - raise utils.EngineError('Episode not in library.') + if error_on_fail: + raise utils.EngineError('Episode not in library.') + return return library[showid][episode] @@ -854,7 +856,7 @@ def play_random(self): ep = self.play_episode(show) return (show, ep) - def play_episode(self, show, playep=0): + def play_episode(self, show, playep=0, playto=0): """ Does a local search in the hard disk (in the folder specified by the config file) for the specified episode (**playep**) for the specified **show**. @@ -865,10 +867,25 @@ def play_episode(self, show, playep=0): if not self.mediainfo.get('can_play'): raise utils.EngineError('Operation not supported by current site or mediatype.') + eps = re.split('-', str(playep)) + print(len(eps)) + print(eps) + if len(eps) > 1: + if eps[0] != '': + playep = eps[0] + else: + playep = 0 + if len(eps) > 2: + if eps[1] != '': + playto = eps[1] + else: + playto = show['total'] + try: playep = int(playep) + playto = int(playto) except ValueError: - raise utils.EngineError('Episode must be numeric.') + raise utils.EngineError('Episode[s] must be numeric.') if show: playing_next = False @@ -876,18 +893,28 @@ def play_episode(self, show, playep=0): playep = show['my_progress'] + 1 playing_next = True - if show['total'] and playep > show['total']: - raise utils.EngineError('Episode beyond limits.') + if not playto or playto < playep: + playto = playep + + if show['total']: + if playep > show['total']: + raise utils.EngineError('Episode beyond limits.') + if playto > show['total']: + self.msg.info(self.name, "Play to %i is beyond limits of show %s. Defaulting to total episodes of %s" % (playto, show['title'], show['total'])) + playto = show['total'] self.msg.info(self.name, "Getting %s %s from library..." % (show['title'], playep)) - filename = self.get_episode_path(show, playep) endep = playep - if filename: + if self.get_episode_path(show, playep): self.msg.info(self.name, 'Found. Starting player...') arg_list = shlex.split(self.config['player']) - arg_list.append(filename) + for episode in range(playep, playto+1): + ep = self.get_episode_path(show, episode, error_on_fail=False) + if ep: + arg_list.append(ep) try: + print(arg_list) with open(os.devnull, 'wb') as DEVNULL: subprocess.Popen(arg_list, stdout=DEVNULL, stderr=DEVNULL) except OSError: diff --git a/trackma/ui/cli.py b/trackma/ui/cli.py index 5705d7df..4e6d8f30 100644 --- a/trackma/ui/cli.py +++ b/trackma/ui/cli.py @@ -78,7 +78,7 @@ class Trackma_cmd(cmd.Cmd): 'add': 1, 'del': 1, 'delete': 1, - 'play': (1, 2), + 'play': (1, 3), 'openfolder': 1, 'update': (1, 2), 'score': 2, @@ -498,6 +498,7 @@ def do_play(self, args): :param show Episode index or title. :optparam ep Episode number. Assume next if not specified. + :optparam playto Episode number to play to in range. :usage play [episode number] """ try: @@ -508,8 +509,11 @@ def do_play(self, args): # otherwise play the next episode not watched yet if len(args) > 1: episode = args[1] + playto = episode + if len(args) > 2: + playto = args[2] - self.engine.play_episode(show, episode) + self.engine.play_episode(show, episode, playto) except utils.TrackmaError as e: self.display_error(e)