Skip to content

Commit

Permalink
Added range function to play: 'play [show] [[episode][-][episode-to-p…
Browse files Browse the repository at this point in the history
…lay-to]]'
  • Loading branch information
Joaqim committed Apr 22, 2020
1 parent 0a5170c commit 830c185
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
40 changes: 31 additions & 9 deletions trackma/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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]

Expand All @@ -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**.
Expand All @@ -865,28 +867,48 @@ 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))
if len(eps) > 1:
if eps[0] != '':
playep = eps[0]
else:
playep = 0
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
if not playep:
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:
with open(os.devnull, 'wb') as DEVNULL:
subprocess.Popen(arg_list, stdout=DEVNULL, stderr=DEVNULL)
Expand Down
8 changes: 6 additions & 2 deletions trackma/ui/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 <show index or title> [episode number]
"""
try:
Expand All @@ -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)

Expand Down

0 comments on commit 830c185

Please sign in to comment.