Skip to content

Commit 02def28

Browse files
committed
- Added the remember_watched config-option (disabled by default). (implements and closes #272)
When enabled, it will store the watched video IDs in the "watched.txt" file. The "watched.txt" file can be changed by modifying the `watched_file` config-option. - Added the `skip_watched` config-option (disabled by default). When enabled, it will ignore already watched videos (i.e. will not play them again).
1 parent f23e956 commit 02def28

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

bin/youtube-viewer

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#-------------------------------------------------------
1616
# youtube-viewer
1717
# Created on: 02 June 2010
18-
# Latest edit on: 11 September 2019
18+
# Latest edit on: 04 October 2019
1919
# https://github.com/trizen/youtube-viewer
2020
#-------------------------------------------------------
2121
#
@@ -204,6 +204,7 @@ my $config_dir = catdir($xdg_config_home, $execname);
204204
my $config_file = catfile($config_dir, "$execname.conf");
205205
my $authentication_file = catfile($config_dir, 'reg.dat');
206206
my $history_file = catfile($config_dir, 'history.txt');
207+
my $watched_file = catfile($config_dir, 'watched.txt');
207208

208209
if (not -d $config_dir) {
209210
require File::Path;
@@ -324,6 +325,9 @@ my %CONFIG = (
324325
keep_original_video => 0,
325326
download_and_play => 0,
326327
autohide_watched => 0,
328+
skip_watched => 0,
329+
remember_watched => 0,
330+
watched_file => $watched_file,
327331
highlight_watched => 1,
328332
highlight_color => 'bold',
329333
remove_played_file => 0,
@@ -578,6 +582,19 @@ if (not -d $CONFIG{cache_dir}) {
578582

579583
@opt{keys %CONFIG} = values(%CONFIG);
580584

585+
if ($opt{remember_watched}) {
586+
if (-f $opt{watched_file}) {
587+
if (open my $fh, '<', $opt{watched_file}) {
588+
chomp(my @ids = <$fh>);
589+
@watched_videos{@ids} = ();
590+
close $fh;
591+
}
592+
else {
593+
warn "[!] Can't open the watched file `$opt{watched_file}' for reading: $!";
594+
}
595+
}
596+
}
597+
581598
if ($opt{history}) {
582599

583600
# Create the history file.
@@ -777,6 +794,7 @@ usage: $execname [options] ([url] | [keywords])
777794
--min-seconds=i : ignore videos shorter than i seconds
778795
--get-term-width! : allow $execname to read your terminal width
779796
--autohide! : automatically hide watched videos
797+
--skip-watched! : don't play already watched videos
780798
--highlight! : remember and highlight selected videos
781799
--confirm! : show a confirmation message after each play
782800
--prefer-mp4! : prefer videos in MP4 format, instead of WEBM
@@ -1501,6 +1519,7 @@ sub parse_arguments {
15011519
'novideo|no-video|n!' => \$opt{novideo},
15021520
'autohide!' => \$opt{autohide_watched},
15031521
'highlight!' => \$opt{highlight_watched},
1522+
'skip-watched!' => \$opt{skip_watched},
15041523
'results=i' => \$opt{maxResults},
15051524
'shuffle|s!' => \$opt{shuffle},
15061525
'more|m!' => \$opt{more_results},
@@ -3098,6 +3117,19 @@ sub download_video {
30983117
return 1;
30993118
}
31003119

3120+
sub save_watched_video {
3121+
my ($video_id) = @_;
3122+
3123+
if ($opt{remember_watched} and not exists($watched_videos{$video_id})) {
3124+
open my $fh, '>>', $opt{watched_file} or return;
3125+
say {$fh} $video_id;
3126+
close $fh;
3127+
}
3128+
3129+
$watched_videos{$video_id} = 1;
3130+
return 1;
3131+
}
3132+
31013133
sub get_player_command {
31023134
my ($streaming, $video) = @_;
31033135

@@ -3175,11 +3207,15 @@ sub play_videos {
31753207
next;
31763208
}
31773209

3178-
# It may be downloaded, but that's OK...
3179-
if ($opt{highlight_watched}) {
3180-
$watched_videos{$video_id} = 1;
3210+
# Ignore already watched videos
3211+
if (exists($watched_videos{$video_id}) and $opt{skip_watched}) {
3212+
say "[*] Already watched video (ID: $video_id)... Skipping...";
3213+
next;
31813214
}
31823215

3216+
# It may be downloaded, but that's OK...
3217+
save_watched_video($video_id);
3218+
31833219
if (defined($opt{max_seconds}) and $opt{max_seconds} >= 0) {
31843220
next if $yv_utils->get_duration($video) > $opt{max_seconds};
31853221
}

0 commit comments

Comments
 (0)