Skip to content

Commit

Permalink
youtube-viewer: added the '--highlight` command-line option (close #119)
Browse files Browse the repository at this point in the history
Additionally, two more config options has been added:

 * highlight_watched  -- controls the `highlight` feature
 * highlight_color    -- specifies a color for the highlight feature

Any color supported by Term::ANSIColor is valid.

Example:

	highlight_color => 'red on_black',	# red text on black background
	highlight_color => 'bold italic',   	# italic bold text
  • Loading branch information
trizen committed May 16, 2016
1 parent 598be61 commit e7aff75
Showing 1 changed file with 71 additions and 40 deletions.
111 changes: 71 additions & 40 deletions bin/youtube-viewer
Expand Up @@ -18,13 +18,14 @@
#-------------------------------------------------------
# Appname: youtube-viewer
# Created on: 02 June 2010
# Latest edit on: 27 February 2016
# Latest edit on: 16 May 2016
# Website: https://github.com/trizen/youtube-viewer
#-------------------------------------------------------
#
# youtube-viewer is a command line utility for viewing youtube-videos in MPlayer.
# youtube-viewer is a command line utility for streaming YouTube videos in mpv/vlc/mplayer.
#
# [CHANGELOG]
# - Added the `--highlight` option to remember and highlight watched videos in a session. - NEW (v3.2.2)
# - Added the `--channel-videos=s` command-line option to display videos from a channel ID. - NEW (v3.2.1)
# - Added the `i..` range, which plays all the videos, starting with video `i`. (#114) - NEW (v3.2.1)
# - Added support for downloading multiple videos in parallel (--dl-parallel) - NEW (v3.1.4)
Expand Down Expand Up @@ -164,6 +165,9 @@ sub EXTRA_VIDEO_PART () { join(',', 'snippet', VIDEO_PART()) }
my %opt;
my $term_width = 80;

# Keep track of watched videos by their ID
my %watched_videos;

# Unchangeable data goes here
my %constant = (win32 => $^O eq 'MSWin32'); # doh

Expand Down Expand Up @@ -312,6 +316,8 @@ my %CONFIG = (
keep_original_video => 0,
download_and_play => 0,
autohide_watched => 0,
highlight_watched => 0,
highlight_color => 'bold',
remove_played_file => 0,
history => 0,
history_limit => 10_000,
Expand Down Expand Up @@ -734,6 +740,7 @@ usage: $execname [options] ([url] | [keywords])
--combine-multi! : combine multiple videos into one play instance
--get-term-width! : allow $execname to read your terminal width
--autohide! : automatically hide watched videos
--highlight! : remember and highlight selected videos
--confirm! : show a confirmation message after each play
* Closed-captions
Expand Down Expand Up @@ -1134,12 +1141,12 @@ sub apply_configuration {
$opt{_colors} = $opt->{colors};
if (delete $opt->{colors}) {
state $x = require Term::ANSIColor;
*colored = \&Term::ANSIColor::colored;
*colored = \&Term::ANSIColor::colored;
*colorstrip = \&Term::ANSIColor::colorstrip;
}
else {
*colored = sub {
return $_[0];
};
*colored = sub { $_[0] };
*colorstrip = sub { $_[0] };
}
}

Expand Down Expand Up @@ -1467,6 +1474,7 @@ sub parse_arguments {
'page=i' => \$opt{page},
'novideo|n!' => \$opt{novideo},
'autohide!' => \$opt{autohide_watched},
'highlight!' => \$opt{highlight_watched},
'results=i' => \$opt{maxResults},
'shuffle|s!' => \$opt{shuffle},
'more|m!' => \$opt{more_results},
Expand Down Expand Up @@ -3373,6 +3381,11 @@ sub play_videos {

my $video_id = $yv_utils->get_video_id($video);

# It may be downloaded, but that's OK...
if ($opt{highlight_watched}) {
$watched_videos{$video_id} = 1;
}

if (defined($opt{max_seconds}) and $opt{max_seconds} >= 0) {
next if $yv_utils->get_duration($video) > $opt{max_seconds};
}
Expand Down Expand Up @@ -3563,37 +3576,40 @@ sub print_videos {
$results->{has_extra_info} = 1;
}

my @formatted;

foreach my $i (0 .. $#{$videos}) {
my $video = $videos->[$i];

if ($opt{results_with_details}) {
printf(
"\n%s. %s\n" . " %s: %-16s %s: %-13s %s: %s\n" . " %s: %-12s %s: %-10s %s: %s\n%s\n",
colored(sprintf('%2d', $i + 1), 'bold') => colored($yv_utils->get_title($video), 'bold blue'),
colored('Views' => 'bold') => $yv_utils->set_thousands($yv_utils->get_views($video)),
colored('Likes' => 'bold') => $yv_utils->set_thousands($yv_utils->get_likes($video)),
colored('Dislikes' => 'bold') => $yv_utils->set_thousands($yv_utils->get_dislikes($video)),
colored('Published' => 'bold') => $yv_utils->get_publication_date($video),
colored('Duration' => 'bold') => $yv_utils->format_time($yv_utils->get_duration($video)),
colored('Author' => 'bold') => $yv_utils->get_channel_title($video),
wrap_text(
i_tab => q{ } x 4,
s_tab => q{ } x 4,
text => [$yv_utils->get_description($video) || 'No description available...']
),
);
push @formatted,
($i == 0 ? '' : "\n")
. sprintf(
"%s. %s\n" . " %s: %-16s %s: %-13s %s: %s\n" . " %s: %-12s %s: %-10s %s: %s\n%s\n",
colored(sprintf('%2d', $i + 1), 'bold') => colored($yv_utils->get_title($video), 'bold blue'),
colored('Views' => 'bold') => $yv_utils->set_thousands($yv_utils->get_views($video)),
colored('Likes' => 'bold') => $yv_utils->set_thousands($yv_utils->get_likes($video)),
colored('Dislikes' => 'bold') => $yv_utils->set_thousands($yv_utils->get_dislikes($video)),
colored('Published' => 'bold') => $yv_utils->get_publication_date($video),
colored('Duration' => 'bold') => $yv_utils->format_time($yv_utils->get_duration($video)),
colored('Author' => 'bold') => $yv_utils->get_channel_title($video),
wrap_text(
i_tab => q{ } x 4,
s_tab => q{ } x 4,
text => [$yv_utils->get_description($video) || 'No description available...']
),
);
}
elsif ($opt{results_with_colors}) {
print "\n" if $i == 0;

my $definition = $yv_utils->get_definition($video);
printf(
"%s. %s (%s) [%s]\n",
colored(sprintf('%2d', $i + 1), 'bold'),
colored($yv_utils->get_title($video), 'bold green'),
colored("by " . $yv_utils->get_channel_title($video), 'bold yellow'),
colored($yv_utils->format_time($yv_utils->get_duration($video)), 'bold bright_blue'),
);
push @formatted,
sprintf(
"%s. %s (%s) [%s]\n",
colored(sprintf('%2d', $i + 1), 'bold'),
colored($yv_utils->get_title($video), 'bold green'),
colored("by " . $yv_utils->get_channel_title($video), 'bold yellow'),
colored($yv_utils->format_time($yv_utils->get_duration($video)), 'bold bright_blue'),
);
}
elsif ($opt{results_fixed_width}) {

Expand All @@ -3606,26 +3622,41 @@ sub print_videos {
my $time_width = List::Util::first(sub { $_ >= 3600 }, @durations) ? 8 : 6;
my $title_length = $term_width - ($author_width + $time_width + 3 + 2 + 1);

print "\n";
foreach my $i (0 .. $#{$videos}) {
my $video = $videos->[$i];
printf "%s. %s %*s %*s\n", colored(sprintf('%2d', $i + 1), 'bold'),
chop_title($yv_utils->get_title($video), $title_length),
$author_width, $yv_utils->get_channel_title($video), $time_width, $yv_utils->format_time($durations[$i]);
push @formatted,
sprintf(
"%s. %s %*s %*s\n",
colored(sprintf('%2d', $i + 1), 'bold'), chop_title($yv_utils->get_title($video), $title_length),
$author_width, $yv_utils->get_channel_title($video),
$time_width, $yv_utils->format_time($durations[$i])
);
}
last;
}
else {
print "\n" if $i == 0;
push @formatted,
sprintf(
"%s. %s (by %s) [%s]\n",
colored(sprintf('%2d', $i + 1), 'bold'), $yv_utils->get_title($video),
$yv_utils->get_channel_title($video), $yv_utils->format_time($yv_utils->get_duration($video)),
);
}
}

printf(
"%s. %s (by %s) [%s]\n",
colored(sprintf('%2d', $i + 1), 'bold'), $yv_utils->get_title($video),
$yv_utils->get_channel_title($video), $yv_utils->format_time($yv_utils->get_duration($video)),
);
if ($opt{highlight_watched}) {
foreach my $i (0 .. $#{$videos}) {
my $video = $videos->[$i];
if (exists($watched_videos{$yv_utils->get_video_id($video)})) {
$formatted[$i] = colored(colorstrip($formatted[$i]), $opt{highlight_color});
}
}
}

if (@formatted) {
print "\n" . join("", @formatted);
}

if ($opt{play_all} || $opt{play_backwards}) {
if (@{$videos}) {
if (
Expand Down

0 comments on commit e7aff75

Please sign in to comment.