Skip to content

Commit

Permalink
Fixed the fallback method in Video#video_details. (fixes #421)
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Nov 1, 2022
1 parent 0f69457 commit cb6527f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
9 changes: 5 additions & 4 deletions bin/gtk-youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,7 @@ sub get_pixbuf_thumbnail_from_url {
sub get_pixbuf_thumbnail_from_entry {
my ($entry) = @_;

my $thumbnail_url = $yv_utils->get_thumbnail_url($entry, $CONFIG{thumbnail_type});
my $thumbnail_url = $yv_utils->get_thumbnail_url($entry, $CONFIG{thumbnail_type}) // return;
my $thumbnail_data = ($entry->{_thumbnail_data} ||= lwp_get($thumbnail_url));

# Don't cache thumbnails that failed to be retrieved.
Expand Down Expand Up @@ -3137,8 +3137,9 @@ sub set_thumbnail {
Glib::Idle->add(
sub {
my ($entry, $liststore, $iter) = @{$_[0]};
my $pixbuf = get_pixbuf_thumbnail_from_entry($entry);
$liststore->set($iter, [1], [$pixbuf]);
if (defined(my $pixbuf = get_pixbuf_thumbnail_from_entry($entry))) {
$liststore->set($iter, [1], [$pixbuf]);
}
return 0;
},
[$entry, $liststore, $iter],
Expand Down Expand Up @@ -3256,7 +3257,7 @@ sub add_video_entry {
my $info_label = reflow_text(
join(
"\n",
map { sprintf("%s %s", $_->[0], $_->[1]) } (
map { sprintf("%s %s", $_->[0], $_->[1]) } grep { defined($_->[1]) } (
[$symbols{play} => $yv_utils->get_time($video)],
[$symbols{type} => $yv_utils->get_definition($video)],
[$symbols{views} => $yv_utils->set_thousands($views)],
Expand Down
9 changes: 6 additions & 3 deletions lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,9 @@ sub get_streaming_urls {

my @caption_urls;

if ($self->get_debug and $json->{videoDetails}{videoId} ne $videoID) {
say STDERR ":: Different video ID detected: $json->{videoDetails}{videoId}";
my $new_video_id = $json->{videoDetails}{videoId};
if ($self->get_debug and defined($new_video_id) and $new_video_id ne $videoID) {
say STDERR ":: Different video ID detected: $new_video_id != $videoID";
}

if (not defined $json->{streamingData}) {
Expand Down Expand Up @@ -1313,11 +1314,13 @@ sub get_streaming_urls {
push @caption_urls, $self->_make_translated_captions(\@caption_urls);
}

$new_video_id = $json->{videoDetails}{videoId};

# Try again with yt-dlp / youtube-dl
if ( !@streaming_urls
or (($json->{playabilityStatus}{status} // '') =~ /fail|error|unavailable|not available/i)
or $self->get_force_fallback
or $json->{videoDetails}{videoId} ne $videoID) {
or (defined($new_video_id) and $new_video_id ne $videoID)) {

@streaming_urls = $self->_fallback_extract_urls($videoID);

Expand Down
6 changes: 5 additions & 1 deletion lib/WWW/YoutubeViewer/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,11 @@ Get thumbnail URL.

sub get_thumbnail_url {
my ($self, $info, $type) = @_;
$info->{snippet}{thumbnails}{$type}{url} // $info->{snippet}{thumbnails}{default}{url}

ref($info) eq 'HASH' or return;
ref($info->{snippet}) eq 'HASH' or return;

eval { $info->{snippet}{thumbnails}{$type}{url} } // $info->{snippet}{thumbnails}{default}{url}
// $info->{snippet}{thumbnails}{medium}{url} // $info->{snippet}{thumbnails}{high}{url};
}

Expand Down
34 changes: 32 additions & 2 deletions lib/WWW/YoutubeViewer/Videos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,35 @@ sub video_details {
my %video_info = $self->_get_video_info($id);
my $video = $self->parse_json_string($video_info{player_response} // next);

my $new_video_id = $video->{videoDetails}{videoId};

# Check if we succeeded in extracting video for the wanted video ID
if (defined($new_video_id) and $new_video_id ne $id) {

## TODO: maybe extract video info with yt-dlp or invidious.

say STDERR ":: Different video ID detected: $new_video_id != $id" if $self->get_debug;

my $proxy_url = "https://youtube-proxy.zerody.one/getPlayer?";

$proxy_url .= $self->list_to_url_arguments(
videoId => $id,
reason => "LOGIN_REQUIRED",
clientName => "ANDROID",
clientVersion => "16.20",
hl => "en",
);

$video = $self->parse_json_string($self->lwp_get($proxy_url, simple => 1) // next);

$new_video_id = $video->{videoDetails}{videoID};

if (defined($new_video_id) and $new_video_id ne $id) {
say STDERR ":: Different video ID detected: $new_video_id != $id" if $self->get_debug;
next; # give up
}
}

if (exists $video->{videoDetails}) {
$video = $video->{videoDetails};
}
Expand Down Expand Up @@ -256,12 +285,13 @@ sub video_details {

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

thumbnails => [default => $video->{thumbnail}{thumbnails}[0],
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 => {
Expand Down

0 comments on commit cb6527f

Please sign in to comment.