Skip to content

Commit

Permalink
Fixed the support for related videos.
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Feb 2, 2024
1 parent aff37eb commit c061f1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 53 deletions.
19 changes: 16 additions & 3 deletions lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ my %valid_options = (

#<<<
# LWP user agent
user_agent => {valid => qr/^.{5}/, default => 'Mozilla/5.0 (Android 11; Tablet; rv:83.0) Gecko/83.0 Firefox/83.0,gzip(gfe)'},
user_agent => {valid => qr/^.{5}/, default => 'Mozilla/5.0 (Android 14; Tablet; rv:109.0) Gecko/122.0 Firefox/122.0,gzip(gfe)'},
#>>>
);

Expand Down Expand Up @@ -1086,12 +1086,25 @@ sub _get_youtubei_content {
"hl" => "en",
"gl" => "US",
"clientName" => "WEB",
"clientVersion" => sprintf("2.%s.05.00", Time::Piece->new(time)->strftime("%Y%m%d")),
"clientVersion" => sprintf("2.%s.00.00", Time::Piece->new(time)->strftime("%Y%m%d")),
%args,
},
},
);

my %mweb = (
"videoId" => $videoID,
"context" => {
"client" => {
"hl" => "en",
"gl" => "US",
"clientName" => "MWEB",
"clientVersion" => sprintf("2.%s.00.00", Time::Piece->new(time)->strftime("%Y%m%d")),
%args,
},
},
);

if (0) {
%android = (
"videoId" => $videoID,
Expand All @@ -1108,7 +1121,7 @@ sub _get_youtubei_content {
}

local $self->{access_token} = undef;
my $content = $self->post_as_json($url, $endpoint eq 'next' ? \%web : \%android);
my $content = $self->post_as_json($url, $endpoint eq 'next' ? \%mweb : \%android);
$self->{lwp}->agent($agent);
return $content;
}
Expand Down
58 changes: 8 additions & 50 deletions lib/WWW/YoutubeViewer/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,22 @@ sub related_to_videoID {
# return $self->search_for('video', undef, {relatedToVideoId => $id});

my $watch_next_response = $self->parse_json_string($self->_get_video_next_info($videoID) // return {results => {}});
my $related = eval { $watch_next_response->{contents}{twoColumnWatchNextResults}{secondaryResults}{secondaryResults}{results} } // return {results => {}};
my $related = eval { $watch_next_response->{contents}{singleColumnWatchNextResults}{results}{results}{contents} } // return {results => {}};

my @results;

foreach my $entry (@$related) {
foreach my $entry (map { @{$_->{itemSectionRenderer}{contents} // []} } @$related) {

my $info = $entry->{compactVideoRenderer} // next;
my $title = $info->{title}{simpleText} // next;
my $info = $entry->{videoWithContextRenderer} // next;
my $title = $info->{headline}{runs}[0]{text} // next;

my $viewCount = 0;

if (($info->{viewCountText}{simpleText} // '') =~ /^([\d,]+) views/) {
$viewCount = ($1 =~ tr/,//dr);
}
elsif (($info->{viewCountText}{simpleText} // '') =~ /Recommended for you/i) {
if (($info->{viewCountText}{simpleText} // '') =~ /Recommended for you/i) {
next; # filter out recommended videos from related videos
}

my $lengthSeconds = 0;

if (($info->{lengthText}{simpleText} // '') =~ /([\d:]+)/) {
if (($info->{lengthText}{runs}[0]{text} // '') =~ /([\d:]+)/) {
my $time = $1;
my @fields = split(/:/, $time);

Expand All @@ -189,32 +184,6 @@ sub related_to_videoID {
$lengthSeconds = 3600 * $hours + 60 * $minutes + $seconds;
}

my $published = 0;
if (exists $info->{publishedTimeText} and $info->{publishedTimeText}{simpleText} =~ /(\d+)\s+(\w+)\s+ago/) {

my $quantity = $1;
my $period = $2;

$period =~ s/s\z//; # make it singural

my %table = (
year => 31556952, # seconds in a year
month => 2629743.83, # seconds in a month
week => 604800, # seconds in a week
day => 86400, # seconds in a day
hour => 3600, # seconds in a hour
minute => 60, # seconds in a minute
second => 1, # seconds in a second
);

if (exists $table{$period}) {
$published = int(time - $quantity * $table{$period});
}
else {
warn "BUG: cannot parse: <<$quantity $period>>";
}
}

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

Expand All @@ -231,19 +200,8 @@ sub related_to_videoID {
kind => "youtube#video",

snippet => {
channelId => $info->{longBylineText}{runs}[0]{navigationEndpoint}{browseEndpoint}{browseId},
channelTitle => $info->{longBylineText}{runs}[0]{text},
description => $info->{accessibility}{accessibilityData}{label},
title => $title,

liveBroadcastContent => (($lengthSeconds == 0) ? 'live' : 'no'),

published => $published,
publishedText => $info->{publishedTimeText}{simpleText},

statistics => {
viewCount => $viewCount,
},
description => $info->{accessibility}{accessibilityData}{label},
title => $title,

thumbnails => {
map {
Expand Down

0 comments on commit c061f1c

Please sign in to comment.