From e5ef81ed12b6aab2de8a09537a56d2afbbf1fc16 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Mon, 1 Mar 2021 15:59:23 -0700 Subject: [PATCH] Changed watchlist to store metadata --- ytbrowser.pl | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/ytbrowser.pl b/ytbrowser.pl index e35a5a5..e294683 100755 --- a/ytbrowser.pl +++ b/ytbrowser.pl @@ -20,6 +20,7 @@ my $debugFile = $homeDir.".yt_last"; my @watchlist = (); my @suggestArray = (); +my @watchArray = (); my $continuation; my $visitorID; my $pageID; @@ -43,14 +44,12 @@ $cookieCrisp =~ m/\.youtube\.com[^\n]*SAPISID\s([^\n]*)/; $sapisidhash = (gettimeofday())[0]."_".sha1_hex((gettimeofday())[0]." ".$1." https://www.youtube.com"); open(WATCH, $playlistFile) or goto SKIPWATCHLOAD; -while() { - my $item = $_; - chomp $item; - push(@watchlist, $item); -} +my $readFile = ""; +while() { $readFile .= $_; } close(WATCH); +@watchlist = @{decode_json($readFile)}; SKIPWATCHLOAD: -my %tests = map { (split("=",$_))[1] => 1 } @watchlist; +my %tests = map { $_->{id} => 1 } @watchlist; # Currently we read from a file that was rerived from # curl -b ~/.scripts/cookies.txt https://www.youtube.com @@ -142,10 +141,16 @@ sub loadSugs { push(@suggestArray, sprintf('%3.3s.',$counter).sprintf('%8.8s ',$playtime).$title); $counter ++; } + for (@watchlist) { + my $title = $_->{title}; + my $playtime = $_->{runtime}; + unless($playtime) { $playtime = "Live"; } + push(@watchArray, sprintf('%8.8s ',$playtime).$title); + } } open(CACHED, $cachedFile) or goto SKIPCACHED; -my $readFile = ""; +$readFile = ""; while () { $readFile .= $_; } close(CACHED); @videos = @{decode_json($readFile)}; @@ -169,7 +174,7 @@ seek(SAVE,0,0); if ($rawFile) { print DEBUG $rawFile; } print CACHING encode_json(\@videos); -for (@watchlist) { print SAVE $_."\n"; } +print SAVE encode_json(\@watchlist); } #################################################### @@ -179,29 +184,27 @@ for (@watchlist) { print SAVE $_."\n"; } my $cui = new Curses::UI( -clear_on_exit => 1, -color_support => 1 ); my $win = $cui->add( 'main', 'Window'); my $suggestions = $win->add( 'suggestions', 'Listbox', -pad => 1, -ipad => 1, -border => 1, -title => "Suggestions", -fg => "blue", -bg => "white", -height => $win->height() / 2 , -values => \@suggestArray); -my $wlist = $win->add( 'watchlist', 'Listbox', -pad => 1, -ipad => 1, -border => 1, -title => "Watch list", -fg => "green", -bg => "black", -height => $win->height() / 2, -y => $suggestions->height(), -values => \@watchlist); +my $wlist = $win->add( 'watchlist', 'Listbox', -pad => 1, -ipad => 1, -border => 1, -title => "Watch list", -fg => "green", -bg => "black", -height => $win->height() / 2, -y => $suggestions->height(), -values => \@watchArray); loadSugs(); $cui->set_binding( sub { $cui->mainloopExit(); }, "q" ); $suggestions->set_binding( sub { - my $vid = splice(@videos,$suggestions->get_active_id(),1); - splice(@suggestArray,$suggestions->get_active_id(),1); - push(@watchlist, "https://www.youtube.com/watch?v=".$vid->{id}); -# $cui->status("https://www.youtube.com/watch?v=".$vid->{id}."\nhas been added to the watchlist. Watchlist is ".(scalar(@watchlist) + 1)." long."); + push(@watchArray, (split(". " splice(@suggestArray,$suggestions->get_active_id(),1),2))[1] ); + push(@watchlist, splice(@videos,$suggestions->get_active_id(),1)); sync2files(); $suggestions->draw(); $wlist->draw(); }, KEY_ENTER); -$cui->set_binding( sub { +$suggestions->set_binding( sub { my $video = shift(@watchlist); - $cui->status("Playing Video, controls will resume once video is compelte"); - `mpv --ytdl-raw-options=cookies=$cookiesFile,mark-watched= --terminal=no $video`; + $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(); }, "p"); -$cui->set_binding( sub { +$suggestions->set_binding( sub { splice(@videos,0,scalar(@videos)); splice(@suggestArray,0,scalar(@suggestArray)); $cui->status("Refreshing from Youtube, please wait..."); @@ -215,7 +218,7 @@ $cui->set_binding( sub { $suggestions->draw(); $wlist->draw(); }, "r"); -$cui->set_binding( sub { +$suggestions->set_binding( sub { my $vid = $videos[$suggestions->get_active_id()]; $cui->status("Video: ".$vid->{title}."\nChannel: ".$vid->{channel}."\nRuntime: ".$vid->{runtime}."\nPublished: ".$vid->{published}."\nURL: https://www.youtube.com/watch?v=".$vid->{id}); }, "i");