Skip to content

Commit

Permalink
Fixed 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 50a0a7b commit dd9b904
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
32 changes: 18 additions & 14 deletions lib/WWW/PipeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1145,35 +1145,39 @@ sub _get_youtubei_content {
$self->{lwp}->agent($android_useragent);
}

my $client_version = sprintf("2.%s.00.00", Time::Piece->new(time)->strftime("%Y%m%d"));

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

my %mweb = (
"videoId" => $videoID,
"context" => {
"client" => {
"hl" => "en",
"gl" => "US",
"clientName" => "MWEB",
"clientVersion" => $client_version,
%args,
},
},
);

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

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
18 changes: 9 additions & 9 deletions lib/WWW/PipeViewer/InitialData.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ sub yt_playlist_next_page {

my @results =
$self->_parse_itemSection(eval { $hash->{continuationContents}{playlistVideoListContinuation} }
// eval { $hash->{continuationContents}{itemSectionContinuation} },
// eval { $hash->{continuationContents}{itemSectionContinuation} },
%args);

if (!@results) {
Expand All @@ -1180,10 +1180,10 @@ sub yt_browse_request {
context => {
client => {
browserName => "Firefox",
browserVersion => "83.0",
browserVersion => "122.0",
clientFormFactor => "LARGE_FORM_FACTOR",
clientName => "MWEB",
clientVersion => "2.20221013.07.00",
clientVersion => "2.20240201.00.00",
deviceMake => "Mozilla",
deviceModel => "Firefox for Android",
hl => "en",
Expand All @@ -1192,15 +1192,15 @@ sub yt_browse_request {
},
originalUrl => $url,
osName => "Android",
osVersion => "11",
osVersion => "14",
platform => "TABLET",
playerType => "UNIPLAYER",
screenDensityFloat => 1,
screenHeightPoints => 500,
screenPixelDensity => 1,
screenWidthPoints => 1800,
timeZone => "UTC",
userAgent => "Mozilla/5.0 (Android 11; Tablet; rv:83.0) Gecko/83.0 Firefox/83.0,gzip(gfe)",
userAgent => "Mozilla/5.0 (Android 14; Tablet; rv:109.0) Gecko/122.0 Firefox/122.0,gzip(gfe)",
userInterfaceTheme => "USER_INTERFACE_THEME_LIGHT",
utcOffsetMinutes => 0,
},
Expand Down Expand Up @@ -1257,10 +1257,10 @@ sub yt_search_next_page {
"context" => {
"client" => {
"browserName" => "Firefox",
"browserVersion" => "83.0",
"browserVersion" => "122.0",
"clientFormFactor" => "LARGE_FORM_FACTOR",
"clientName" => "MWEB",
"clientVersion" => "2.20221013.07.00",
"clientVersion" => "2.20240201.00.00",
"deviceMake" => "Mozilla",
"deviceModel" => "Firefox for Android",
"gl" => "US",
Expand All @@ -1269,14 +1269,14 @@ sub yt_search_next_page {
"graftUrl" => $url,
},
"osName" => "Android",
"osVersion" => "11",
"osVersion" => "14",
"platform" => "TABLET",
"playerType" => "UNIPLAYER",
"screenDensityFloat" => 1,
"screenHeightPoints" => 600,
"screenPixelDensity" => 1,
"screenWidthPoints" => 1800,
"userAgent" => "Mozilla/5.0 (Android 11; Tablet; rv:83.0) Gecko/83.0 Firefox/83.0,gzip(gfe)",
"userAgent" => "Mozilla/5.0 (Android 14; Tablet; rv:109.0) Gecko/122.0 Firefox/122.0,gzip(gfe)",
"userInterfaceTheme" => "USER_INTERFACE_THEME_LIGHT",
"utcOffsetMinutes" => 0,
},
Expand Down
34 changes: 13 additions & 21 deletions lib/WWW/PipeViewer/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -161,39 +161,33 @@ sub related_to_videoID {
my ($self, $videoID) = @_;

my $watch_next_response = 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);
if (($info->{shortViewCountText}{runs}[0]{text} // '') =~ /^(\S+) views/) {
$viewCount = WWW::PipeViewer::InitialData::_human_number_to_int($1);
}
elsif (($info->{viewCountText}{simpleText} // '') =~ /Recommended for you/i) {
elsif (($info->{shortViewCountText}{runs}[0]{text} // '') =~ /Recommended for you/i) {
next; # filter out recommended videos from related videos
}

my $lengthSeconds = 0;

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

my $seconds = pop(@fields) // 0;
my $minutes = pop(@fields) // 0;
my $hours = pop(@fields) // 0;

$lengthSeconds = 3600 * $hours + 60 * $minutes + $seconds;
if (($info->{lengthText}{runs}[0]{text} // '') =~ /([\d:]+)/) {
$lengthSeconds = WWW::PipeViewer::InitialData::_time_to_seconds($1);
}

my $published = 0;

# FIXME: this code no longer works
if (exists $info->{publishedTimeText} and $info->{publishedTimeText}{simpleText} =~ /(\d+)\s+(\w+)\s+ago/) {

my $quantity = $1;
Expand Down Expand Up @@ -223,10 +217,8 @@ sub related_to_videoID {
type => "video",
title => $title,
videoId => $info->{videoId},
author => $info->{longBylineText}{runs}[0]{text},
authorId => $info->{longBylineText}{runs}[0]{navigationEndpoint}{browseEndpoint}{browseId},

#authorUrl => $info->{longBylineText}{runs}[0]{navigationEndpoint}{browseEndpoint}{browseId},
author => $info->{shortBylineText}{runs}[0]{text},
authorId => $info->{shortBylineText}{runs}[0]{navigationEndpoint}{browseEndpoint}{browseId},

description => $info->{accessibility}{accessibilityData}{label},
descriptionHtml => undef,
Expand Down

0 comments on commit dd9b904

Please sign in to comment.