StreamFinder
view release on metacpan or search on metacpan
lib/StreamFinder/Anystream.pm view on Meta::CPAN
my @okStreams;
$self->{'maxstreams'} = 20;
while (@_) {
if ($_[0] =~ /^\-?keep$/o) {
shift;
if (defined $_[0]) {
my $keeporder = shift;
@okStreams = (ref($keeporder) =~ /ARRAY/) ? @{$keeporder} : split(/\,\s*/, $keeporder);
}
} elsif ($_[0] =~ /^\-?maxstreams$/o) {
shift;
$self->{'maxstreams'} = (defined $_[0]) ? shift : 20;
} else {
shift;
}
}
if (!defined($okStreams[0]) && defined($self->{'keep'})) {
@okStreams = (ref($self->{'keep'}) =~ /ARRAY/) ? @{$self->{'keep'}} : split(/\,\s*/, $self->{'keep'});
}
@okStreams = (qw(mp3 ogg flac mp4 m4a mpd aac m3u8 m3u pls)) unless (defined $okStreams[0]);
my $url2fetch = $url;
my $urlPrefix = '';
my $domainName = '';
my $baseURL = '';
$url2fetch = 'https://' . $url unless ($url =~ m#^https?\:\/\/#);
my $t = $url2fetch;
if ($t =~ s#(https?\:\/\/)([^\/\?\&\#]+).?##) {
$urlPrefix = $1;
$domainName = $2;
$baseURL = $urlPrefix . $domainName;
$self->{'id'} = $domainName;
$self->{'id'} .= "/$1" if ($t =~ s#^([^\/\?\&\#\.\=]+)##);
} else {
print STDERR "u:Anystream - Invalid URL ($url2fetch) t=$t=\n" if ($DEBUG);
#---PUNT!---
return undef;
}
$self->{'id'} =~ s#[^a-z0-9\-\_\.\/]##gi;
$url2fetch =~ s#\/$##;
print STDERR "-url2fetch=$url2fetch= ID=".$self->{'id'}."= PFX=$urlPrefix= DN=$domainName= BASE=$baseURL=\n" if ($DEBUG);
$self->{'albumartist'} = $url2fetch;
my $html = '';
print STDERR "-0(Anystream): URL=$url2fetch=\n" if ($DEBUG);
my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}});
$ua->timeout($self->{'timeout'});
$ua->max_size(2048); #LIMIT FETCH-SIZE TO AVOID INFINITELY DOWNLOADING A STREAM!
$ua->cookie_jar({});
$ua->env_proxy;
my $response = $ua->get($url2fetch);
if ($response->is_success) {
$html = $response->decoded_content;
} else {
print STDERR $response->status_line if ($DEBUG);
}
unless ($html) { #STEP 1 FAILED, INVALID URL, PUNT!
print STDERR "-!!!- COULD NOT FETCH URL=$url2fetch=\n" if ($DEBUG);
return undef;
}
my $isHLSpage = ($html =~ /#EXTM3U/) ? 1 : 0; #SEE AUDACIOUS ISSUE#1169 FOR EMBEDDED HLS IN HTML PAGE EXAMPLE USED FOR THIS CODE:
my $streamExts = join('|',@okStreams);
print STDERR "-1: HLS=$isHLSpage=\n----html=$html=\n" if ($DEBUG > 1);
return undef unless ($html =~ /\<\!DOCTYPE\s+(?:html|text)/i
|| ($isHLSpage && $streamExts =~ /(?:m3u8|any|all)/i)); #STEP 1 FAILED, INVALID STATION URL, PUNT!
print STDERR "-1: GOT SOME(<=2048 BYTES) HTML!\n" if ($DEBUG);
$ua->requests_redirectable([]) if ($isHLSpage);
unless ($isHLSpage) {
print STDERR "-1a: NOW RE-FETCH FULL PAGE!\n" if ($DEBUG);
$ua->max_size(undef); #(NOW OK TO FETCH THE WHOLE DOCUMENT)
$response = $ua->get($url2fetch);
if ($response->is_success) {
$html = $response->decoded_content;
} else {
print STDERR $response->status_line if ($DEBUG);
}
unless ($html) { #STEP 1 FAILED, INVALID URL, PUNT!
print STDERR "-!!!- COULD NOT FETCH URL=$url2fetch=\n" if ($DEBUG);
return undef;
}
}
$self->{'_havehls'} = 0;
if ($isHLSpage) { #JWT:NOTE:MAY END UP NEEDING TO REMOVE TRAILING "$"!:
$self->{'title'} = $self->{'id'} || $url;
$self->{'description'} = $url2fetch;
if ($self->{'hls_bandwidth'} > 0 && $url2fetch =~ /\.m3u8$/) { #JWT:NOTE:MAY END UP NEEDING TO REMOVE TRAILING "$"!:
my $location = $response->header('location');
($baseURL = $url2fetch) =~ s#\/[^\/]+$##;
print STDERR "-!!!- WE'RE AN HLS PAGE!: HLS BASE URL=$baseURL= locn=$location\n" if ($DEBUG);
my @lines = split(/\r?\n/, $html);
my @plentries = ();
my $firstTitle = '';
my $plidx = ($arglist =~ /\b\-?random\b/) ? 1 : 0;
my $line = 1;
(my $urlpath = $firstStream) =~ s#[^\/]+$##;
my $highestBW = 0;
my %streamsByBandwidth;
while ($line <= $#lines) { #FIND HIGHEST BANDWIDTH STREAM (WITHIN ANY USER-SET BANDWIDTH):
if ($lines[$line] =~ /\s*\#EXT\-X\-STREAM\-INF\:(?:.*?)BANDWIDTH\=(\d+)/o) {
my $bw = $1;
if ($lines[$line] =~ /\,\s*URI\=\"([^\"]+)/o) { #SOMETIMES THEY STICK IT ON THE END OF CURRENT LINE!:
$lines[$line] = $1;
} else {
$line++;
}
if ($line <= $#lines) {
$bw =~ s/^\d*x//o;
if ($lines[$line] =~ m#\.m3u8#o && ($bw <= $self->{'hls_bandwidth'})) {
my $url = $lines[$line];
if ($lines[$line] =~ m#^https?\:\/\/#o) {
$streamsByBandwidth{$bw} = $lines[$line];
} elsif ($lines[$line] =~ m#^\/\/#o) {
my $protocol = $1 if ($url2fetch =~ /^(https?)/);
$streamsByBandwidth{$bw} = $protocol . ':' . $lines[$line];
} elsif ($lines[$line] =~ m#^\/#) {
$streamsByBandwidth{$bw} = $baseURL . $lines[$line];
} else {
$streamsByBandwidth{$bw} = $baseURL . '/' . $lines[$line];
}
( run in 1.557 second using v1.01-cache-2.11-cpan-71847e10f99 )