Skip to content

Commit

Permalink
- Fixed the "400 Bad Request" error.
Browse files Browse the repository at this point in the history
By updating the Android version in the user-agent.

Fix from yt-dlp: https://github.com/yt-dlp/yt-dlp/blob/413d3675804599bc8fe419c19e36490fd8f0b30f/yt_dlp/extractor/youtube.py#L119

- bin/youtube-viewer: show the executed `yt-dlp` command with `--debug`.
  • Loading branch information
trizen committed Feb 29, 2024
1 parent 2d266b4 commit 762e49a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions bin/youtube-viewer
Expand Up @@ -4038,8 +4038,14 @@ sub download_from_url {

# Download with yt-dlp / youtube-dl
if ($opt{ytdl} and $opt{download_with_ytdl} and defined($info->{_youtube_url}) and defined($info->{itag})) {

my $cmd =
join(' ', $opt{ytdl_cmd}, ($info->{wkad} ? () : ('-f', $info->{itag})), quotemeta($info->{_youtube_url}), '-o', quotemeta("$output_filename.part"));

if ($yv_obj->get_debug) {
say "-> Command: $cmd";
}

$yv_obj->proxy_system($cmd);
return if $?;
rename("$output_filename.part", $output_filename) or return undef;
Expand Down
16 changes: 11 additions & 5 deletions lib/WWW/YoutubeViewer.pm
Expand Up @@ -4,10 +4,10 @@ use utf8;
use 5.016;
use warnings;

use Memoize;
use Memoize::Expire;
use Memoize qw(memoize);

#<<<
#~ use Memoize::Expire;
#~ tie my %youtubei_cache => 'Memoize::Expire',
#~ LIFETIME => 600, # in seconds
#~ NUM_USES => 2;
Expand Down Expand Up @@ -1056,7 +1056,7 @@ sub _get_youtubei_content {

require Time::Piece;

my $android_useragent = 'com.google.android.youtube/17.31.35 (Linux; U; Android 11) gzip';
my $android_useragent = 'com.google.android.youtube/18.11.34 (Linux; U; Android 11) gzip';

my %android = (
"videoId" => $videoID,
Expand All @@ -1065,7 +1065,7 @@ sub _get_youtubei_content {
'hl' => 'en',
'gl' => 'US',
'clientName' => 'ANDROID',
'clientVersion' => '17.31.35',
'clientVersion' => '18.11.34',
'androidSdkVersion' => 30,
'userAgent' => $android_useragent,
%args,
Expand Down Expand Up @@ -1114,7 +1114,13 @@ sub _get_youtubei_content {
}

local $self->{access_token} = undef;
my $content = $self->post_as_json($url, $endpoint eq 'next' ? \%mweb : \%android);

my $content;
for (1 .. 3) {
$content = $self->post_as_json($url, $endpoint eq 'next' ? \%mweb : \%android);
last if defined $content;
}

$self->{lwp}->agent($agent);
return $content;
}
Expand Down

0 comments on commit 762e49a

Please sign in to comment.