Skip to content

Commit 852f83f

Browse files
committed
gtk3: added "Watch history" support. (enabled by default) (#322)
The watched videos can be listed from "Menu -> Watched videos" or from the "Watch history" button from the "My panel" tab. This feature can be disabled by setting "watch_history => 0," in the config-file. cli: renamed config-option "remember_watched" to "watch_history" and enabled it by default. cli: renamed config-option "watched_file" to "watch_history_file".
1 parent e65d305 commit 852f83f

File tree

3 files changed

+928
-843
lines changed

3 files changed

+928
-843
lines changed

bin/gtk3-youtube-viewer

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#-------------------------------------------------------
1616
# GTK YouTube Viewer
1717
# Created on: 12 September 2010
18-
# Latest edit on: 10 October 2020
18+
# Latest edit on: 14 October 2020
1919
# https://github.com/trizen/youtube-viewer
2020
#-------------------------------------------------------
2121

@@ -86,6 +86,7 @@ my $config_file = catfile($config_dir, "gtk-youtube-viewer.conf");
8686
my $youtube_users_file = catfile($config_dir, 'youtube_users.txt');
8787
my $history_file = catfile($config_dir, 'gtk-history.txt');
8888
my $session_file = catfile($config_dir, 'session.dat');
89+
my $watch_history_file = catfile($config_dir, 'watched.txt');
8990
my $authentication_file = catfile($config_dir, 'reg.dat');
9091
my $api_file = catfile($config_dir, 'api.json');
9192
my $default_api_file = '/etc/youtube-viewer/api.json';
@@ -102,6 +103,9 @@ foreach my $dir ($config_dir) {
102103
# Video queue for the enqueue feature
103104
my @VIDEO_QUEUE;
104105

106+
# Keep track of watched videos
107+
my %WATCHED_VIDEOS;
108+
105109
sub which_command {
106110
my ($cmd) = @_;
107111

@@ -249,6 +253,10 @@ my %CONFIG = (
249253
remember_session_max => 10,
250254
save_titles_to_history => 0,
251255
entry_completion_limit => 10,
256+
257+
# Watch history
258+
watch_history => 1,
259+
watch_history_file => $watch_history_file,
252260
);
253261

254262
{
@@ -560,10 +568,12 @@ foreach my $path ($CONFIG{cache_dir}) {
560568
my $word = $words[$i];
561569

562570
for (my $j = $i ; $j <= $end_p ; ++$j) {
563-
my $part = $parts[$j];
564571

565572
my $matched;
566573
my $continue = 1;
574+
575+
my $part = $parts[$j];
576+
567577
while ($part eq $word) {
568578
$order_score += 1 - 1 / (length($word) + 1)**2;
569579
$matched ||= 1;
@@ -572,8 +582,9 @@ foreach my $path ($CONFIG{cache_dir}) {
572582
}
573583

574584
if ($matched) {
575-
$order_score += 1 - 1 / (length($word) + 1)
576-
if ($continue and index($part, $word) == 0);
585+
if ($continue and index($part, $word) == 0) {
586+
$order_score += 1 - 1 / (length($word) + 1);
587+
}
577588
last;
578589
}
579590
elsif (index($part, $word) == 0) {
@@ -697,7 +708,7 @@ foreach my $path ($CONFIG{cache_dir}) {
697708
search();
698709
}
699710
);
700-
$item->set_property(tooltip_text => $text);
711+
$item->set_property(tooltip_text => "Search for „${text}");
701712
$item->set_image('Gtk3::Image'->new_from_icon_name("history-view", q{menu}));
702713
$item->show;
703714
$history_menu->append($item);
@@ -2438,12 +2449,23 @@ sub get_code {
24382449

24392450
my $type = $liststore->get($iter, 7);
24402451

2441-
$type eq 'playlist' ? list_playlist($code)
2442-
: ($type eq 'channel' || $type eq 'subscription') ? uploads('channel', $code)
2443-
: $type eq 'next_page' && $code ne '' ? do {
2452+
if ($type eq 'playlist') {
2453+
list_playlist($code);
2454+
}
2455+
elsif ($type eq 'channel' or $type eq 'subscription') {
2456+
uploads('channel', $code);
2457+
}
2458+
elsif ($type eq 'next_page' and $code ne '') {
24442459

2460+
my $results;
24452461
my $next_page_token = $liststore->get($iter, 5);
2446-
my $results = $yv_obj->from_page_token($code, $next_page_token);
2462+
2463+
if ($next_page_token =~ /^watched (\d+)/) {
2464+
$results = get_watched_videos_from_file($1);
2465+
}
2466+
else {
2467+
$results = $yv_obj->from_page_token($code, $next_page_token);
2468+
}
24472469

24482470
if ($yv_utils->has_entries($results)) {
24492471
my $label = '<big><b>' . ('=' x 20) . '</b></big>';
@@ -2455,13 +2477,12 @@ sub get_code {
24552477
}
24562478

24572479
display_results($results);
2458-
}
2459-
: $type eq 'video' ? (
2460-
$CONFIG{audio_only}
2461-
? execute_cli_youtube_viewer("--id=$code")
2462-
: play_video($yv_obj->parse_json_string($liststore->get($iter, 8)))
2463-
)
2464-
: ();
2480+
}
2481+
elsif ($type eq 'video') {
2482+
$CONFIG{audio_only}
2483+
? execute_cli_youtube_viewer("--id=$code")
2484+
: play_video($yv_obj->parse_json_string($liststore->get($iter, 8)));
2485+
}
24652486

24662487
return 0;
24672488
},
@@ -2591,6 +2612,54 @@ sub get_pixbuf_thumbnail_from_entry {
25912612
return $pixbuf;
25922613
}
25932614

2615+
sub get_watched_videos_from_file {
2616+
my ($page) = @_;
2617+
2618+
$page //= $yv_obj->get_page;
2619+
2620+
my @ids;
2621+
2622+
if ($CONFIG{watch_history} and open(my $fh, '<', $CONFIG{watch_history_file})) {
2623+
chomp(@ids = <$fh>);
2624+
close $fh;
2625+
}
2626+
else {
2627+
@ids = keys %WATCHED_VIDEOS;
2628+
}
2629+
2630+
my %seen;
2631+
2632+
# Keep the most recent ones
2633+
@ids = reverse(@ids);
2634+
@ids = grep { !$seen{$_}++ } @ids;
2635+
2636+
my $maxResults = $yv_obj->get_maxResults;
2637+
2638+
if ($page >= 1 and scalar(@ids) >= $maxResults) {
2639+
@ids = grep { defined } @ids[($page - 1) * $maxResults .. $page * $maxResults - 1];
2640+
}
2641+
2642+
my %results;
2643+
my @videos;
2644+
2645+
foreach my $id (@ids) {
2646+
push @videos, {id => {kind => "youtube#video", videoId => $id},};
2647+
}
2648+
2649+
#<<<
2650+
$results{items} = \@videos;
2651+
$results{pageInfo} = {resultsPerPage => scalar(@videos), totalResults => scalar(@videos)};
2652+
$results{nextPageToken} = sprintf("%s %d", 'watched', $page+1);
2653+
#>>>
2654+
2655+
scalar {results => \%results, url => "watched videos"};
2656+
}
2657+
2658+
sub display_watched_videos {
2659+
$liststore->clear if $CONFIG{clear_search_list};
2660+
display_results(get_watched_videos_from_file(1));
2661+
}
2662+
25942663
sub display_results {
25952664
my ($results, $from_history) = @_;
25962665

@@ -3153,6 +3222,20 @@ sub get_player_command {
31533222
$has_video ? $cmd : join(' ', $cmd, quotemeta($streaming->{streaming}{url}));
31543223
}
31553224

3225+
sub save_watched_video {
3226+
my ($video_id) = @_;
3227+
3228+
$WATCHED_VIDEOS{$video_id} = 1;
3229+
3230+
if ($CONFIG{watch_history}) {
3231+
open(my $fh, '>>', $CONFIG{watch_history_file}) or return;
3232+
say {$fh} $video_id;
3233+
close $fh;
3234+
}
3235+
3236+
return 1;
3237+
}
3238+
31563239
sub play_video {
31573240
my ($video) = @_;
31583241

@@ -3181,7 +3264,13 @@ sub play_video {
31813264
}
31823265

31833266
my $code = execute_external_program($command);
3184-
warn "[!] Can't play this video -- player exited with code: $code\n" if $code != 0;
3267+
3268+
if ($code == 0) {
3269+
save_watched_video($video_id);
3270+
}
3271+
else {
3272+
warn "[!] Can't play this video -- player exited with code: $code\n";
3273+
}
31853274

31863275
return 1;
31873276
}

0 commit comments

Comments
 (0)