From 575cf2705ae122ab10c6b21deabdafe4870ac5ba Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Fri, 8 Jul 2022 13:06:02 -0600 Subject: [PATCH] Added network checking so videos don't vanish into the ether when I lose wifi --- ytbrowser.pl | 58 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/ytbrowser.pl b/ytbrowser.pl index 403ccac..bdcf309 100755 --- a/ytbrowser.pl +++ b/ytbrowser.pl @@ -13,7 +13,7 @@ use Curses::UI; #$Data::Dumper::Maxdepth = 1; my $homeDir = "/home/bluesaxman/"; -my $cookiesFile = $homeDir.".scripts/cookies.txt"; +my $cookiesFile = $homeDir."cookies.txt"; my $cachedFile = $homeDir.".yt_cache"; my $playlistFile = $homeDir.".yt_play"; my $debugFile = $homeDir.".yt_last"; @@ -166,17 +166,17 @@ open(CACHING, ">", $cachedFile); open(SAVE, ">", $playlistFile); sub sync2files { -truncate(DEBUG,0); -truncate(CACHING,0); -truncate(SAVE,0); + truncate(DEBUG,0); + truncate(CACHING,0); + truncate(SAVE,0); -seek(DEBUG,0,0); -seek(CACHING,0,0); -seek(SAVE,0,0); + seek(DEBUG,0,0); + seek(CACHING,0,0); + seek(SAVE,0,0); -if ($rawFile) { print DEBUG $rawFile; } -print CACHING encode_json(\@videos); -print SAVE encode_json(\@watchlist); + if ($rawFile) { print DEBUG $rawFile; } + print CACHING encode_json(\@videos); + print SAVE encode_json(\@watchlist); } #################################################### @@ -198,22 +198,32 @@ $suggestions->set_binding( sub { $wlist->draw(); }, KEY_ENTER); $suggestions->set_binding( sub { - my $video = shift(@watchlist); - $cui->status("Playing Video, controls will resume once video is compelte\n Currently Playing: ".shift(@watchArray)); - `mpv --ytdl-raw-options=cookies=$cookiesFile,mark-watched= --terminal=no https://www.youtube.com/watch?v=$video->{id}`; - sync2files(); - $win->draw(); - $suggestions->draw(); - $wlist->draw(); + my $video = $watchlist[0]; + $cui->status("Playing Video, controls will resume once video is compelte\n Currently Playing: ".$watchArray[0]); + my $success=`mpv --script-opts=ytdl_hook-ytdl_path=yt-dlp --ytdl-raw-options=cookies=$cookiesFile,mark-watched=,buffer-size=200M,format='bestvideo[height<=720]+bestaudio/best[height<=720]' --terminal=no https://www.youtube.com/watch?v=$video->{id}`; + if($success) { + shift(@watchlist);} + sync2files(); + $win->draw(); + $suggestions->draw(); + $wlist->draw(); + } else { + $cui->status("PLAYBACK ERROR: Video not removed for safety."); + } }, "p"); $wlist->set_binding( sub { - my $video = splice(@watchlist,$wlist->get_active_id(),1); - $cui->status("Playing Video, controls will resume once video is compelte\n Currently Playing: ".splice(@watchArray,$wlist->get_active_id(),1)); - `mpv --ytdl-raw-options=cookies=$cookiesFile,mark-watched= --terminal=no https://www.youtube.com/watch?v=$video->{id}`; - sync2files(); - $win->draw(); - $suggestions->draw(); - $wlist->draw(); + my $video = $watchlist[$wlist->get_active_id()]; + $cui->status("Playing Video, controls will resume once video is compelte\n Currently Playing: ".$watchArray[$wlist->get_active_id()]); + my $success=`mpv --script-opts=ytdl_hook-ytdl_path=yt-dlp --ytdl-raw-options=cookies=$cookiesFile,mark-watched=,buffer-size=200M,format='bestvideo[height<=720]+bestaudio/best[height<=720]' --terminal=no https://www.youtube.com/watch?v=$video->{id}`; + if ($success) { + splice(@watchlist,$wlist->get_active_id(),1); + sync2files(); + $win->draw(); + $suggestions->draw(); + $wlist->draw(); + } else { + $cui->status("PLAYBACK ERROR: Video not removed for safety."); + } } , "p"); $suggestions->set_binding( sub { splice(@videos,0,scalar(@videos));