Changed watchlist to store metadata

This commit is contained in:
bluesaxman 2021-03-01 15:59:23 -07:00
parent efc26aaa9c
commit e5ef81ed12

View File

@ -20,6 +20,7 @@ my $debugFile = $homeDir.".yt_last";
my @watchlist = (); my @watchlist = ();
my @suggestArray = (); my @suggestArray = ();
my @watchArray = ();
my $continuation; my $continuation;
my $visitorID; my $visitorID;
my $pageID; 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"); $sapisidhash = (gettimeofday())[0]."_".sha1_hex((gettimeofday())[0]." ".$1." https://www.youtube.com");
open(WATCH, $playlistFile) or goto SKIPWATCHLOAD; open(WATCH, $playlistFile) or goto SKIPWATCHLOAD;
while(<WATCH>) { my $readFile = "";
my $item = $_; while(<WATCH>) { $readFile .= $_; }
chomp $item;
push(@watchlist, $item);
}
close(WATCH); close(WATCH);
@watchlist = @{decode_json($readFile)};
SKIPWATCHLOAD: SKIPWATCHLOAD:
my %tests = map { (split("=",$_))[1] => 1 } @watchlist; my %tests = map { $_->{id} => 1 } @watchlist;
# Currently we read from a file that was rerived from # Currently we read from a file that was rerived from
# curl -b ~/.scripts/cookies.txt https://www.youtube.com # 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); push(@suggestArray, sprintf('%3.3s.',$counter).sprintf('%8.8s ',$playtime).$title);
$counter ++; $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; open(CACHED, $cachedFile) or goto SKIPCACHED;
my $readFile = ""; $readFile = "";
while (<CACHED>) { $readFile .= $_; } while (<CACHED>) { $readFile .= $_; }
close(CACHED); close(CACHED);
@videos = @{decode_json($readFile)}; @videos = @{decode_json($readFile)};
@ -169,7 +174,7 @@ seek(SAVE,0,0);
if ($rawFile) { print DEBUG $rawFile; } if ($rawFile) { print DEBUG $rawFile; }
print CACHING encode_json(\@videos); 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 $cui = new Curses::UI( -clear_on_exit => 1, -color_support => 1 );
my $win = $cui->add( 'main', 'Window'); 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 $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(); loadSugs();
$cui->set_binding( sub { $cui->mainloopExit(); }, "q" ); $cui->set_binding( sub { $cui->mainloopExit(); }, "q" );
$suggestions->set_binding( sub { $suggestions->set_binding( sub {
my $vid = splice(@videos,$suggestions->get_active_id(),1); push(@watchArray, (split(". " splice(@suggestArray,$suggestions->get_active_id(),1),2))[1] );
splice(@suggestArray,$suggestions->get_active_id(),1); push(@watchlist, splice(@videos,$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.");
sync2files(); sync2files();
$suggestions->draw(); $suggestions->draw();
$wlist->draw(); $wlist->draw();
}, KEY_ENTER); }, KEY_ENTER);
$cui->set_binding( sub { $suggestions->set_binding( sub {
my $video = shift(@watchlist); my $video = shift(@watchlist);
$cui->status("Playing Video, controls will resume once video is compelte"); $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 $video`; `mpv --ytdl-raw-options=cookies=$cookiesFile,mark-watched= --terminal=no https://www.youtube.com/watch?v=$video->{id}`;
sync2files(); sync2files();
$win->draw(); $win->draw();
$suggestions->draw(); $suggestions->draw();
$wlist->draw(); $wlist->draw();
}, "p"); }, "p");
$cui->set_binding( sub { $suggestions->set_binding( sub {
splice(@videos,0,scalar(@videos)); splice(@videos,0,scalar(@videos));
splice(@suggestArray,0,scalar(@suggestArray)); splice(@suggestArray,0,scalar(@suggestArray));
$cui->status("Refreshing from Youtube, please wait..."); $cui->status("Refreshing from Youtube, please wait...");
@ -215,7 +218,7 @@ $cui->set_binding( sub {
$suggestions->draw(); $suggestions->draw();
$wlist->draw(); $wlist->draw();
}, "r"); }, "r");
$cui->set_binding( sub { $suggestions->set_binding( sub {
my $vid = $videos[$suggestions->get_active_id()]; 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}); $cui->status("Video: ".$vid->{title}."\nChannel: ".$vid->{channel}."\nRuntime: ".$vid->{runtime}."\nPublished: ".$vid->{published}."\nURL: https://www.youtube.com/watch?v=".$vid->{id});
}, "i"); }, "i");