Skip to content

Commit

Permalink
- Re-implemented support for related videos, without using the YouTub…
Browse files Browse the repository at this point in the history
…e API.

As the API "relatedToVideoId" parameter was officially removed as of August 7, 2023.

See also:

	https://web.archive.org/web/20230714040516/https://developers.google.com/youtube/v3/docs/search/list
  • Loading branch information
trizen committed Aug 29, 2023
1 parent 67cb8bd commit 5c8afe4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 15 deletions.
22 changes: 11 additions & 11 deletions lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ sub set_lwp_useragent {
};

$agent->ssl_opts(Timeout => $self->get_timeout);
$agent->default_header('Accept-Encoding' => $accepted_encodings);
$agent->default_header('Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
$agent->default_header('Accept-Language' => 'en-US,en;q=0.5');
$agent->default_header('Connection' => 'keep-alive');
$agent->default_header('Accept-Encoding' => $accepted_encodings);
$agent->default_header('Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
$agent->default_header('Accept-Language' => 'en-US,en;q=0.5');
$agent->default_header('Connection' => 'keep-alive');
$agent->default_header('Upgrade-Insecure-Requests' => '1');
$agent->conn_cache($cache);
$agent->proxy(['http', 'https'], $self->get_http_proxy) if defined($self->get_http_proxy);
Expand Down Expand Up @@ -1095,12 +1095,12 @@ sub _get_youtubei_content {
"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,
}
"hl" => "en",
"gl" => "US",
"clientName" => "MWEB",
"clientVersion" => sprintf("2.%s.03.00", Time::Piece->new(time)->strftime("%Y%m%d")),
%args,
}
},
);
}
Expand Down Expand Up @@ -1271,7 +1271,7 @@ sub get_streaming_urls {
hl => "en",
);

%info = (player_response => $self->lwp_get($proxy_url, simple => 1));
%info = (player_response => $self->lwp_get($proxy_url, simple => 1) // undef);
};
}

Expand Down
122 changes: 119 additions & 3 deletions lib/WWW/YoutubeViewer/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sub _make_search_url {
),

%opts,
);
);

}

Expand Down Expand Up @@ -152,8 +152,124 @@ be set to a YouTube video ID.
=cut

sub related_to_videoID {
my ($self, $id) = @_;
return $self->search_for('video', undef, {relatedToVideoId => $id});
my ($self, $videoID) = @_;

# Feature deprecated and removed in August 2023
# 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 @results;

foreach my $entry (@$related) {

my $info = $entry->{compactVideoRenderer} // next;
my $title = $info->{title}{simpleText} // next;

my $viewCount = 0;

if (($info->{viewCountText}{simpleText} // '') =~ /^([\d,]+) views/) {
$viewCount = ($1 =~ tr/,//dr);
}
elsif (($info->{viewCountText}{simpleText} // '') =~ /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;
}

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);

my %details = (

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

id => $info->{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,
},

thumbnails => {
map {
(
medium => scalar {
quality => 'medium',
url => ($_->{url} =~ s{/hqdefault\.jpg}{/mqdefault.jpg}r),
width => $_->{width},
height => $_->{height},
}
)
} @{$info->{thumbnail}{thumbnails}}
},
}
);

push @results, \%details;
}

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

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

=head1 AUTHOR
Expand Down
2 changes: 1 addition & 1 deletion utils/auto_perltidy.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

alias perltidy='perltidy -utf8 -l=127 -f -kbl=1 -bbb -bbc -bbs -b -ple -bt=2 -pt=2 -sbt=2 -bvt=0 -sbvt=1 -cti=1 -bar -lp -anl';
alias perltidy='perltidy -utf8 -l=160 -f -kbl=1 -bbb -bbc -bbs -b -ple -bt=2 -pt=2 -sbt=2 -bvt=0 -sbvt=1 -cti=1 -bar -lp -anl';
which perltidy;
cd ..;
for i in $(git status | grep '^[[:cntrl:]]*modified:' | grep -E 'bin/|\.(pm|t)$' | perl -nE 'say +(split)[-1]'); do echo $i; perltidy -b $i; done

0 comments on commit 5c8afe4

Please sign in to comment.