Skip to content

Commit

Permalink
- Added basic support for playing videos when no API key is set.
Browse files Browse the repository at this point in the history
When no API key has been set, youtube-viewer no longer dies, but allows youtube videos to be played by URL.

Also fixes #336.
  • Loading branch information
trizen committed Sep 14, 2020
1 parent a35d56e commit c65f76f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 21 deletions.
41 changes: 23 additions & 18 deletions bin/youtube-viewer
Expand Up @@ -648,9 +648,9 @@ EOT
}

if (not defined $yv_obj->get_key) {
die join("\n",
"\nError: no API key has been set!\n\nFor creating a new YouTube API key, please see:",
"https://github.com/trizen/youtube-viewer#logging-in\n\n");
warn join("\n",
"\nError: no API key has been set!\n\nFor creating/setting an YouTube API key, please see:",
"https://github.com/trizen/youtube-viewer#logging-in\n\n");
}

$yv_obj->load_authentication_tokens();
Expand Down Expand Up @@ -3622,22 +3622,27 @@ sub print_video_info {

$rep = 0 if $rep < 0;

print "\n$hr\n", q{ } x $rep => (_bold_color("=>> $title <<=") . "\n\n"),
map(sprintf(q{-> } . "%-*s: %s\n", $opt{_colors} ? 18 : 10, _bold_color($_->[0]), $_->[1]),
print(
"\n$hr\n",
q{ } x $rep => (_bold_color("=>> $title <<=") . "\n\n"),
(
['Channel' => $yv_utils->get_channel_title($video)],
['ChannelID' => $yv_utils->get_channel_id($video)],
['VideoID' => $yv_utils->get_video_id($video)],
['Category' => $yv_utils->get_category_name($video)],
['Definition' => $yv_utils->get_definition($video)],
['Duration' => $yv_utils->get_time($video)],
['Likes' => $yv_utils->set_thousands($yv_utils->get_likes($video))],
['Dislikes' => $yv_utils->set_thousands($yv_utils->get_dislikes($video))],
['Comments' => $yv_utils->set_thousands($yv_utils->get_comments($video))],
['Views' => $yv_utils->set_thousands($yv_utils->get_views($video))],
['Published' => $yv_utils->get_publication_date($video)],
)),
"$hr\n";
map { sprintf(q{-> } . "%-*s: %s\n", $opt{_colors} ? 18 : 10, _bold_color($_->[0]), $_->[1]) }
grep { defined($_->[1]) } (
['Channel' => $yv_utils->get_channel_title($video)],
['ChannelID' => $yv_utils->get_channel_id($video)],
['VideoID' => $yv_utils->get_video_id($video)],
['Category' => $yv_utils->get_category_name($video)],
['Definition' => $yv_utils->get_definition($video)],
['Duration' => $yv_utils->get_time($video)],
['Likes' => $yv_utils->set_thousands($yv_utils->get_likes($video))],
['Dislikes' => $yv_utils->set_thousands($yv_utils->get_dislikes($video))],
['Comments' => $yv_utils->set_thousands($yv_utils->get_comments($video))],
['Views' => $yv_utils->set_thousands($yv_utils->get_views($video))],
['Published' => $yv_utils->get_publication_date($video)],
)
),
"$hr\n"
);

return 1;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/WWW/YoutubeViewer.pm
Expand Up @@ -374,6 +374,10 @@ sub lwp_get {
$url // return;
$self->{lwp} // $self->set_lwp_useragent();

if (not defined($self->get_key)) {
return undef if not $opt{simple};
}

my %lwp_header = ($opt{simple} ? () : $self->_auth_lwp_header);
my $response = $self->{lwp}->get($url, %lwp_header);

Expand Down
13 changes: 12 additions & 1 deletion lib/WWW/YoutubeViewer/Utils.pm
Expand Up @@ -133,6 +133,8 @@ Return string "04 May 2010" from "2010-05-04T00:25:55.000Z"
sub format_date {
my ($self, $date) = @_;

$date // return undef;

# 2010-05-04T00:25:55.000Z
# to: 04 May 2010

Expand Down Expand Up @@ -222,7 +224,16 @@ Returns true if a given result has entries.

sub has_entries {
my ($self, $result) = @_;
ref($result) eq 'HASH' and (($result->{results}{pageInfo}{totalResults} // 0) > 0);

ref($result) eq 'HASH' or return;

if (exists $result->{results}) {
$result = $result->{results};
}

ref($result) eq 'HASH' or return;

($result->{pageInfo}{totalResults} // 0) > 0;
}

=head2 normalize_video_title($title, $fat32safe)
Expand Down
78 changes: 76 additions & 2 deletions lib/WWW/YoutubeViewer/Videos.pm
Expand Up @@ -195,8 +195,82 @@ When C<$part> is C<undef>, it defaults to I<snippet>.
=cut

sub video_details {
my ($self, $id, $part) = @_;
return $self->_get_results($self->_make_videos_url(id => $id, part => $part // 'snippet'));
my ($self, $ids, $part) = @_;

my $info = $self->_get_results($self->_make_videos_url(id => $ids, part => $part // 'snippet'));

state $yv_utils = WWW::YoutubeViewer::Utils->new;

if ($yv_utils->has_entries($info)) {
return $info;
}

if ($self->get_debug) {
say STDERR ":: Extracting video info using the fallback method...";
}

my @items;

foreach my $id (split(/,/, $ids)) {

# Fallback using the `get_video_info` URL
my %video_info = $self->_get_video_info($id);
my $video = $self->parse_json_string($video_info{player_response} // next);

if (exists $video->{videoDetails}) {
$video = $video->{videoDetails};
}
else {
next;
}

my $length = $video->{lengthSeconds};
my $duration = sprintf("PT%dH%dM%dS", int($length / 3600), int($length / 60) % 60, $length % 60);

my %details = (

contentDetails => {
definition => "hd",
dimension => "2d",
duration => $duration,
projection => "rectangular",
},

id => $id,
kind => "youtube#video",

snippet => {
channelId => $video->{channelId},
channelTitle => $video->{author},
description => $video->{shortDescription},
title => $video->{title},
tags => $video->{keywords},

liveBroadcastContent => ($video->{isLiveContent} ? 'live' : 'no'),

thumbnails => [default => $video->{thumbnail}{thumbnails}[0],
medium => $video->{thumbnail}{thumbnails}[1],
standard => $video->{thumbnail}{thumbnails}[2],
high => $video->{thumbnail}{thumbnails}[3],
maxres => $video->{thumbnail}{thumbnails}[4],
],
},

statistics => {
viewCount => $video->{viewCount},
},
);

push @items, \%details;
}

my %results = (
items => \@items,
kind => "youtube#videoListResponse",
pageInfo => {resultsPerPage => scalar(@items), totalResults => scalar(@items)},
);

return scalar {results => \%results};
}

=head2 Return details
Expand Down

0 comments on commit c65f76f

Please sign in to comment.