From ec48e1066dd9eb73ca0d9a1a0585231a4633796a Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Fri, 5 Jun 2020 12:44:51 +0200 Subject: [PATCH] ytv: Deal with files that are renamed while running. --- ytv/ytv | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/ytv/ytv b/ytv/ytv index b935251..fffe5c3 100755 --- a/ytv/ytv +++ b/ytv/ytv @@ -95,37 +95,48 @@ sub playfiles { my @files = @_; if($opt::kodi) { - my @existing = - uniq( - grep { -f $_ } - map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @files - ); - for (my $i = 0; $i <= $#existing; $i++) { - my $answer; - $_ = $existing[$i]; - do { + # Playlist includes partial files + my @plist = + uniq(map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @files); + my $i = 0; + while(1) { + $_ = $plist[$i]; + if(-e $_) { + # Only try playing if the file exists print "KODI playing $_\n"; if(not fork()) { exec("idok", $_); } print "Press (r)etry, (d)elete, (p)revious, (n)ext\n"; - $answer = undef; + my $answer; while(not defined $answer) { open(my $tty_fh, "<", "/dev/tty") || ::die_bug("interactive-tty"); $answer = <$tty_fh>; close $tty_fh; - if($answer =~ /^d$/i) { - unlink $_; - } - if($answer =~ /^p$/i) { - $i -= 2; - } } - } while($answer =~ /^r?$/i); - } - if($answer =~ m,http.?://,) { - play($answer); + if($answer =~ /^d$/i) { + unlink $_; + } + if($answer =~ /^p$/i) { + # Find previous existing file + do { + $i--; + } until (-e $plist[$i] or $i == 0); + } + if($answer =~ /^n$/i) { + # Find next existing file + do { + $i++; + } until (-e $plist[$i] or $i > $#plist); + } + } else { + # Find the first existing file + do { + $i++; + } until (-e $plist[$i] or $i > $#plist); + } + if($i > $#plist) { last; } } } else { system("vlc", map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @files);