|
15 | 15 | #------------------------------------------------------- |
16 | 16 | # youtube-viewer |
17 | 17 | # Created on: 02 June 2010 |
18 | | -# Latest edit on: 11 September 2019 |
| 18 | +# Latest edit on: 04 October 2019 |
19 | 19 | # https://github.com/trizen/youtube-viewer |
20 | 20 | #------------------------------------------------------- |
21 | 21 | # |
@@ -204,6 +204,7 @@ my $config_dir = catdir($xdg_config_home, $execname); |
204 | 204 | my $config_file = catfile($config_dir, "$execname.conf"); |
205 | 205 | my $authentication_file = catfile($config_dir, 'reg.dat'); |
206 | 206 | my $history_file = catfile($config_dir, 'history.txt'); |
| 207 | +my $watched_file = catfile($config_dir, 'watched.txt'); |
207 | 208 |
|
208 | 209 | if (not -d $config_dir) { |
209 | 210 | require File::Path; |
@@ -324,6 +325,9 @@ my %CONFIG = ( |
324 | 325 | keep_original_video => 0, |
325 | 326 | download_and_play => 0, |
326 | 327 | autohide_watched => 0, |
| 328 | + skip_watched => 0, |
| 329 | + remember_watched => 0, |
| 330 | + watched_file => $watched_file, |
327 | 331 | highlight_watched => 1, |
328 | 332 | highlight_color => 'bold', |
329 | 333 | remove_played_file => 0, |
@@ -578,6 +582,19 @@ if (not -d $CONFIG{cache_dir}) { |
578 | 582 |
|
579 | 583 | @opt{keys %CONFIG} = values(%CONFIG); |
580 | 584 |
|
| 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 | + |
581 | 598 | if ($opt{history}) { |
582 | 599 |
|
583 | 600 | # Create the history file. |
@@ -777,6 +794,7 @@ usage: $execname [options] ([url] | [keywords]) |
777 | 794 | --min-seconds=i : ignore videos shorter than i seconds |
778 | 795 | --get-term-width! : allow $execname to read your terminal width |
779 | 796 | --autohide! : automatically hide watched videos |
| 797 | + --skip-watched! : don't play already watched videos |
780 | 798 | --highlight! : remember and highlight selected videos |
781 | 799 | --confirm! : show a confirmation message after each play |
782 | 800 | --prefer-mp4! : prefer videos in MP4 format, instead of WEBM |
@@ -1501,6 +1519,7 @@ sub parse_arguments { |
1501 | 1519 | 'novideo|no-video|n!' => \$opt{novideo}, |
1502 | 1520 | 'autohide!' => \$opt{autohide_watched}, |
1503 | 1521 | 'highlight!' => \$opt{highlight_watched}, |
| 1522 | + 'skip-watched!' => \$opt{skip_watched}, |
1504 | 1523 | 'results=i' => \$opt{maxResults}, |
1505 | 1524 | 'shuffle|s!' => \$opt{shuffle}, |
1506 | 1525 | 'more|m!' => \$opt{more_results}, |
@@ -3098,6 +3117,19 @@ sub download_video { |
3098 | 3117 | return 1; |
3099 | 3118 | } |
3100 | 3119 |
|
| 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 | + |
3101 | 3133 | sub get_player_command { |
3102 | 3134 | my ($streaming, $video) = @_; |
3103 | 3135 |
|
@@ -3175,11 +3207,15 @@ sub play_videos { |
3175 | 3207 | next; |
3176 | 3208 | } |
3177 | 3209 |
|
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; |
3181 | 3214 | } |
3182 | 3215 |
|
| 3216 | + # It may be downloaded, but that's OK... |
| 3217 | + save_watched_video($video_id); |
| 3218 | + |
3183 | 3219 | if (defined($opt{max_seconds}) and $opt{max_seconds} >= 0) { |
3184 | 3220 | next if $yv_utils->get_duration($video) > $opt{max_seconds}; |
3185 | 3221 | } |
|
0 commit comments