diff options
Diffstat (limited to 'lib/WWW/FairViewer.pm')
-rw-r--r-- | lib/WWW/FairViewer.pm | 104 |
1 files changed, 46 insertions, 58 deletions
diff --git a/lib/WWW/FairViewer.pm b/lib/WWW/FairViewer.pm index 76e96d8..71a1389 100644 --- a/lib/WWW/FairViewer.pm +++ b/lib/WWW/FairViewer.pm @@ -419,7 +419,7 @@ sub lwp_get { $opt{depth} ||= 0; # Try again on 500+ HTTP errors - if ( $opt{depth} < 3 + if ( $opt{depth} <= 3 and $response->code() >= 500 and $response->status_line() =~ /(?:Temporary|Server) Error|Timeout|Service Unavailable/i) { return $self->lwp_get($url, %opt, depth => $opt{depth} + 1); @@ -614,7 +614,9 @@ sub _fallback_extract_urls { my @formats; + # Use hypervideo if ($self->_ytdl_is_available) { + if ($self->get_debug) { say STDERR ":: Using hypervideo to extract the streaming URLs..."; } @@ -623,10 +625,10 @@ sub _fallback_extract_urls { if ($self->get_debug) { my $count = scalar(@formats); - say STDERR ":: Found $count streaming URLs..."; + say STDERR ":: hypervideo: found $count streaming URLs..."; } - return @formats; + @formats && return @formats; } # Use the API of invidio.us @@ -637,7 +639,8 @@ sub _fallback_extract_urls { push @formats, $self->_extract_from_invidious($videoID); if ($self->get_debug) { - say STDERR ":: Found ", scalar(@formats), " streaming URLs."; + my $count = scalar(@formats); + say STDERR ":: invidious: found $count streaming URLs..."; } return @formats; @@ -705,6 +708,43 @@ sub _group_keys_with_values { return @hashes; } +sub _check_streaming_urls { + my ($self, $videoID, $results) = @_; + + foreach my $video (@$results) { + + if ( exists $video->{s} + or exists $video->{signatureCipher} + or exists $video->{cipher}) { # has an encrypted signature :( + + if ($self->get_debug) { + say STDERR ":: Detected an encrypted signature..."; + } + + my @formats = $self->_fallback_extract_urls($videoID); + + foreach my $format (@formats) { + foreach my $ref (@$results) { + if (defined($ref->{itag}) and ($ref->{itag} eq $format->{itag})) { + $ref->{url} = $format->{url}; + last; + } + } + } + + last; + } + } + + foreach my $video (@$results) { + if (exists $video->{mimeType}) { + $video->{type} = $video->{mimeType}; + } + } + + return 1; +} + sub _old_extract_streaming_urls { my ($self, $info, $videoID) = @_; @@ -726,27 +766,7 @@ sub _old_extract_streaming_urls { push @results, $self->_group_keys_with_values(%stream_map); push @results, $self->_group_keys_with_values(%adaptive_fmts); - foreach my $video (@results) { - if (exists $video->{s}) { # has an encrypted signature :( - - if ($self->get_debug) { - say STDERR ":: Detected an encrypted signature..."; - } - - my @formats = $self->_fallback_extract_urls($videoID); - - foreach my $format (@formats) { - foreach my $ref (@results) { - if (defined($ref->{itag}) and ($ref->{itag} eq $format->{itag})) { - $ref->{url} = $format->{url}; - last; - } - } - } - - last; - } - } + $self->_check_streaming_urls($videoID, \@results); if ($info->{livestream} or $info->{live_playback}) { @@ -767,11 +787,6 @@ sub _old_extract_streaming_urls { } } - if ($self->get_debug) { - my $count = scalar(@results); - say STDERR ":: Found $count streaming URLs..."; - } - return @results; } @@ -809,29 +824,7 @@ sub _extract_streaming_urls { } } - foreach my $item (@results) { - - if (exists $item->{cipher} and not exists $item->{url}) { - - my %data = $self->parse_query_string($item->{cipher}); - - $item->{url} = $data{url} if defined($data{url}); - - if (defined($data{s})) { # unclear how this can be decrypted... - require URI::Escape; - my $sig = $data{s}; - $sig = URI::Escape::uri_escape($sig); - $item->{url} .= "&sig=$sig"; - } - } - - if (exists $item->{mimeType}) { - $item->{type} = $item->{mimeType}; - } - } - - # Cipher streaming URLs are currently unsupported, so let's filter them out. - @results = grep { not exists $_->{cipher} } @results; + $self->_check_streaming_urls($videoID, \@results); # Keep only streams with contentLength > 0. @results = grep { $_->{itag} == 22 or (exists($_->{contentLength}) and $_->{contentLength} > 0) } @results; @@ -855,11 +848,6 @@ sub _extract_streaming_urls { } } - if ($self->get_debug) { - my $count = scalar(@results); - say STDERR ":: Found $count streaming URLs..."; - } - return @results; } |