| 1 | =head1 NAME | ||
| 2 | |||
| 3 | StreamFinder::_Class - Base module containing default methods common to all StreamFinder submodules. | ||
| 4 | |||
| 5 | =head1 AUTHOR | ||
| 6 | |||
| 7 | This module is Copyright (C) 2017-2026 by | ||
| 8 | |||
| 9 | Jim Turner, C<< <turnerjw784 at yahoo.com> >> | ||
| 10 | |||
| 11 | Email: turnerjw784@yahoo.com | ||
| 12 | |||
| 13 | All rights reserved. | ||
| 14 | |||
| 15 | You may distribute this module under the terms of either the GNU General | ||
| 16 | Public License or the Artistic License, as specified in the Perl README | ||
| 17 | file. | ||
| 18 | |||
| 19 | NOTE: This module is for internal use only by the other StreamFinder modules | ||
| 20 | and should not be used directly. Please see the main module (L<StreamFinder>) | ||
| 21 | POD documentation for documentation for all the methods and how to use. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | package StreamFinder::_Class; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | use URI::Escape; | ||
| 30 | use HTML::Entities (); | ||
| 31 | use LWP::UserAgent (); | ||
| 32 | use vars qw(@ISA @EXPORT); | ||
| 33 | |||
| 34 | my $DEBUG = 0; | ||
| 35 | my $bummer = ($^O =~ /MSWin/); | ||
| 36 | my $AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"; | ||
| 37 | |||
| 38 | sub new | ||
| 39 | { | ||
| 40 | my $class = shift; | ||
| 41 | my $objname = shift; | ||
| 42 | |||
| 43 | my $self = {'_objname' => $objname}; | ||
| 44 | |||
| 45 | my $homedir = $bummer ? $ENV{'HOMEDRIVE'} . $ENV{'HOMEPATH'} : $ENV{'HOME'}; | ||
| 46 | $homedir ||= $ENV{'LOGDIR'} if ($bummer && $ENV{'LOGDIR'}); | ||
| 47 | $homedir =~ s#[\/\\]$##; | ||
| 48 | my $configdir = (defined $ENV{STREAMFINDER}) ? $ENV{STREAMFINDER} : "${homedir}/.config/StreamFinder"; | ||
| 49 | $configdir =~ s#[\/\\]$##; | ||
| 50 | @{$self->{'_userAgentOps'}} = (); | ||
| 51 | foreach my $p ("${configdir}/config", "${configdir}/${objname}/config") { | ||
| 52 | if (open IN, $p) { | ||
| 53 | my ($atr, $val); | ||
| 54 | while (<IN>) { | ||
| 55 | chomp; | ||
| 56 | next if (/^\s*\#/o); | ||
| 57 | ($atr, $val) = split(/\s*\=\>\s*/o, $_, 2); | ||
| 58 | eval "\$self->{'$atr'} = $val"; #CATCH JSON-LIKE ARGS, IE. "arg => {key => value, ...}" | ||
| 59 | eval "\$self->{'$atr'} = \"\Q$val\E\"" if ($@); #CATCH UNESCAPED ARGS, IE. "arg => str[with brackets,commas,etc.]" | ||
| 60 | } | ||
| 61 | close IN; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | foreach my $i (qw(agent from conn_cache default_headers local_address ssl_opts max_size | ||
| 65 | max_redirect parse_head protocols_allowed protocols_forbidden requests_redirectable | ||
| 66 | proxy no_proxy)) { | ||
| 67 | push @{$self->{'_userAgentOps'}}, $i, $self->{$i} if (defined $self->{$i}); | ||
| 68 | } | ||
| 69 | push (@{$self->{'_userAgentOps'}}, 'agent', $AGENT) | ||
| 70 | unless (defined $self->{'agent'}); | ||
| 71 | $self->{'timeout'} = 10 unless (defined $self->{'timeout'}); | ||
| 72 | $self->{'secure'} = 0 unless (defined $self->{'secure'}); | ||
| 73 | $self->{'hls_bandwidth'} = 0 unless (defined($self->{'hls_bandwidth'}) && $self->{'hls_bandwidth'} =~ /^\d+$/); | ||
| 74 | $self->{'log'} = ''; | ||
| 75 | $self->{'logfmt'} = '[time] [url] - [site]: [title] ([total])'; | ||
| 76 | |||
| 77 | while (@_) { | ||
| 78 | if ($_[0] =~ /^\-?debug$/o) { | ||
| 79 | shift; | ||
| 80 | $self->{'debug'} = (defined($_[0]) && $_[0] =~/^[0-9]$/) ? shift : 1; | ||
| 81 | } elsif ($_[0] =~ /^\-?secure$/o) { | ||
| 82 | shift; | ||
| 83 | $self->{'secure'} = (defined $_[0]) ? shift : 1; | ||
| 84 | } elsif ($_[0] =~ /^\-?log$/o) { | ||
| 85 | shift; | ||
| 86 | $self->{'log'} = (defined $_[0]) ? shift : ''; | ||
| 87 | } elsif ($_[0] =~ /^\-?logfmt$/o) { | ||
| 88 | shift; | ||
| 89 | $self->{'logfmt'} = shift if (defined $_[0]); | ||
| 90 | } else { | ||
| 91 | shift; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | $DEBUG = $self->{'debug'} if (defined $self->{'debug'}); | ||
| 95 | $self->{'title'} = ''; | ||
| 96 | $self->{'artist'} = ''; | ||
| 97 | $self->{'album'} = ''; | ||
| 98 | $self->{'description'} = ''; | ||
| 99 | $self->{'created'} = ''; | ||
| 100 | $self->{'year'} = ''; | ||
| 101 | $self->{'genre'} = ''; | ||
| 102 | $self->{'iconurl'} = ''; | ||
| 103 | $self->{'imageurl'} = ''; | ||
| 104 | $self->{'articonurl'} = ''; | ||
| 105 | $self->{'artimageurl'} = ''; | ||
| 106 | $self->{'streams'} = []; | ||
| 107 | $self->{'cnt'} = 0; | ||
| 108 | $self->{'playlist_cnt'} = 0; | ||
| 109 | $self->{'total'} = 0; | ||
| 110 | $self->{'Url'} = ''; | ||
| 111 | $self->{'playlist'} = ''; | ||
| 112 | $self->{'albumartist'} = ''; | ||
| 113 | |||
| 114 | bless $self, $class; #BLESS IT! | ||
| 115 | |||
| 116 | return $self; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub get | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | return wantarray ? ($self->{'playlist'}) : $self->{'playlist'} if (defined($_[0]) && $_[0] =~ /playlist/i); | ||
| 124 | return wantarray ? @{$self->{'streams'}} : ${$self->{'streams'}}[0]; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub getURL #LIKE GET, BUT ONLY RETURN THE SINGLE ONE W/BEST BANDWIDTH AND RELIABILITY: | ||
| 128 | { | ||
| 129 | my $self = shift; | ||
| 130 | my $arglist = (defined $_[0]) ? join('|',@_) : ''; | ||
| 131 | my $idx = ($arglist =~ /\b\-?random\b/) ? int rand scalar @{$self->{'streams'}} : 0; | ||
| 132 | my $firstStream = ${$self->{'streams'}}[$idx]; | ||
| 133 | |||
| 134 | return '' unless (defined $firstStream); | ||
| 135 | |||
| 136 | if (($arglist =~ /\b\-?nopls\b/ && $firstStream =~ /\.(pls|m3u)$/i) | ||
| 137 | || ($self->{'hls_bandwidth'} > 0 && $firstStream =~ /\.(m3u8)$/i) | ||
| 138 | || ($arglist =~ /\b\-?noplaylists\b/ && $firstStream =~ /\.(pls|m3u8?)$/i)) { | ||
| 139 | my $plType = $1; | ||
| 140 | print STDERR "-getURL($idx): -NOPLAYLISTS and (".$firstStream.") TP=$plType=\n" if ($DEBUG); | ||
| 141 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 142 | $ua->timeout($self->{'timeout'}); | ||
| 143 | $ua->cookie_jar({}); | ||
| 144 | $ua->env_proxy; | ||
| 145 | my $html = ''; | ||
| 146 | my $response = $ua->get($firstStream); | ||
| 147 | if ($response->is_success) { | ||
| 148 | $html = $response->decoded_content; | ||
| 149 | } else { | ||
| 150 | print STDERR $response->status_line if ($DEBUG); | ||
| 151 | my $no_wget = system('wget','-V'); | ||
| 152 | unless ($no_wget) { | ||
| 153 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 154 | $html = `wget -t 2 -T 20 -O- -o /dev/null "$firstStream" 2>/dev/null `; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | my @lines = split(/\r?\n/, $html); | ||
| 158 | my @plentries = (); | ||
| 159 | my $firstTitle = ''; | ||
| 160 | my $plidx = ($arglist =~ /\b\-?random\b/) ? 1 : 0; | ||
| 161 | if ($plType =~ /pls/i) { #PLS: | ||
| 162 | foreach my $line (@lines) { | ||
| 163 | if ($line =~ m#^\s*File\d+\=(.+)$#o) { | ||
| 164 | push (@plentries, $1); | ||
| 165 | } elsif ($line =~ m#^\s*Title\d+\=(.+)$#o) { | ||
| 166 | $firstTitle ||= $1; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | $self->{'title'} ||= $firstTitle; | ||
| 170 | $self->{'title'} = HTML::Entities::decode_entities($self->{'title'}); | ||
| 171 | $self->{'title'} = uri_unescape($self->{'title'}); | ||
| 172 | print STDERR "-getURL(PLS): title=$firstTitle= pl_idx=$plidx=\n" if ($DEBUG); | ||
| 173 | if ($plidx && $#plentries >= 0) { | ||
| 174 | $plidx = int rand scalar @plentries; | ||
| 175 | } else { | ||
| 176 | $plidx = 0; | ||
| 177 | } | ||
| 178 | $firstStream = $plentries[$plidx] | ||
| 179 | if (defined($plentries[$plidx]) && $plentries[$plidx]); | ||
| 180 | } elsif ($plType =~ /m3u8/i) { #HLS?: | ||
| 181 | my $line = 1; | ||
| 182 | (my $urlpath = $firstStream) =~ s#[^\/]+$##; | ||
| 183 | my $highestBW = 0; | ||
| 184 | my $bestStream = ''; | ||
| 185 | while ($line <= $#lines) { #FIND HIGHEST BANDWIDTH STREAM (WITHIN ANY USER-SET BANDWIDTH): | ||
| 186 | if ($lines[$line] =~ /\s*\#EXT\-X\-STREAM\-INF\:(?:.*?)BANDWIDTH\=(\d+)/o) { | ||
| 187 | $line++; | ||
| 188 | if ($line <= $#lines) { | ||
| 189 | (my $bw = $1) =~ s/^\d*x//o; | ||
| 190 | if ($bw > $highestBW && $lines[$line] =~ m#\.m3u8#o | ||
| 191 | && ($self->{'hls_bandwidth'} <= 0 || $bw <= $self->{'hls_bandwidth'})) { | ||
| 192 | my $url = $lines[$line]; | ||
| 193 | $highestBW = $bw; | ||
| 194 | if ($lines[$line] =~ m#^https?\:\/\/#o) { | ||
| 195 | $bestStream = $lines[$line]; | ||
| 196 | } else { | ||
| 197 | $lines[$line] =~ s#^\/##o; | ||
| 198 | $bestStream = $urlpath . $lines[$line]; | ||
| 199 | } | ||
| 200 | print STDERR "----($bw): found stream=$bestStream= bw=$bw=...\n" if ($DEBUG); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | $line++; | ||
| 205 | } | ||
| 206 | $firstStream = $bestStream if ($bestStream); | ||
| 207 | print STDERR "-getURL(m3u8/HLS) best=$bestStream=\n" if ($DEBUG); | ||
| 208 | } else { #m3u: | ||
| 209 | (my $urlpath = $firstStream) =~ s#[^\/]+$##; | ||
| 210 | foreach my $line (@lines) { | ||
| 211 | if ($line =~ m#^\s*([^\#].+)$#o) { | ||
| 212 | my $urlpart = $1; | ||
| 213 | $urlpart =~ s#^\s+##o; | ||
| 214 | $urlpart =~ s#^\/##o; | ||
| 215 | push (@plentries, ($urlpart =~ m#https?\:#) ? $urlpart : ($urlpath . $urlpart)); | ||
| 216 | last unless ($plidx); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | if ($plidx && $#plentries >= 0) { | ||
| 220 | $plidx = int rand scalar @plentries; | ||
| 221 | } else { | ||
| 222 | $plidx = 0; | ||
| 223 | } | ||
| 224 | $firstStream = $plentries[$plidx] | ||
| 225 | if (defined($plentries[$plidx]) && $plentries[$plidx]); | ||
| 226 | print STDERR "-getURL(m3u): pl_idx=$plidx=\n" if ($DEBUG); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | print STDERR "-getURL returning stream=$firstStream=\n" if ($DEBUG); | ||
| 231 | return $firstStream; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub count | ||
| 235 | { | ||
| 236 | my $self = shift; | ||
| 237 | return $self->{'playlist_cnt'} if (defined($_[0]) && $_[0] =~ /playlist/i); | ||
| 238 | return $self->{'total'}; #TOTAL NUMBER OF PLAYABLE STREAM URLS FOUND. | ||
| 239 | } | ||
| 240 | |||
| 241 | sub getType | ||
| 242 | { | ||
| 243 | my $self = shift; | ||
| 244 | return $self->{'_objname'}; #STATION TYPE (FOR PARENT StreamFinder MODULE). | ||
| 245 | } | ||
| 246 | |||
| 247 | sub getID | ||
| 248 | { | ||
| 249 | my $self = shift; | ||
| 250 | return $self->{'fccid'} if (defined($_[0]) && $_[0] =~ /fcc/i && defined($self->{'fccid'})); #STATION'S CALL LETTERS OR IHEARTRADIO-ID. | ||
| 251 | return $self->{'id'}; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub getTitle | ||
| 255 | { | ||
| 256 | my $self = shift; | ||
| 257 | return $self->{'description'} if (defined($_[0]) && $_[0] =~ /^\-?(?:long|desc)/i); | ||
| 258 | return $self->{'title'}; #STATION'S TITLE(DESCRIPTION), IF ANY. | ||
| 259 | } | ||
| 260 | |||
| 261 | sub getIconURL | ||
| 262 | { | ||
| 263 | my $self = shift; | ||
| 264 | return (defined($_[0]) && $_[0] =~ /^\-?artist/i) | ||
| 265 | ? $self->{'articonurl'} : $self->{'iconurl'}; #URL TO THE STATION'S THUMBNAIL ICON, IF ANY. | ||
| 266 | } | ||
| 267 | |||
| 268 | sub getIconData | ||
| 269 | { | ||
| 270 | my $self = shift; | ||
| 271 | |||
| 272 | my $whichurl = (defined($_[0]) && $_[0] =~ /^\-?artist/i) | ||
| 273 | ? 'articonurl' : 'iconurl'; | ||
| 274 | return () unless ($self->{$whichurl}); | ||
| 275 | |||
| 276 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 277 | $ua->timeout($self->{'timeout'}); | ||
| 278 | $ua->cookie_jar({}); | ||
| 279 | $ua->env_proxy; | ||
| 280 | my $art_image = ''; | ||
| 281 | my $response = $ua->get($self->{$whichurl}); | ||
| 282 | if ($response->is_success) { | ||
| 283 | $art_image = $response->decoded_content; | ||
| 284 | } else { | ||
| 285 | print STDERR $response->status_line if ($DEBUG); | ||
| 286 | my $no_wget = system('wget','-V'); | ||
| 287 | unless ($no_wget) { | ||
| 288 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 289 | my $iconUrl = $self->{$whichurl}; | ||
| 290 | $art_image = `wget -t 2 -T 20 -O- -o /dev/null "$iconUrl" 2>/dev/null `; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | return () unless ($art_image); | ||
| 294 | |||
| 295 | (my $image_ext = $self->{$whichurl}) =~ s/^.+\.//; | ||
| 296 | $image_ext =~ s/[^A-Za-z].*$//; | ||
| 297 | |||
| 298 | return ($image_ext, $art_image); | ||
| 299 | } | ||
| 300 | |||
| 301 | sub getImageURL | ||
| 302 | { | ||
| 303 | my $self = shift; | ||
| 304 | return $self->{'imageurl'}; #URL TO THE STATION'S|CHANNEL'S BANNER IMAGE, IF ANY. | ||
| 305 | return (defined($_[0]) && $_[0] =~ /^\-?artist/i) | ||
| 306 | ? $self->{'artimageurl'} : $self->{'imageurl'}; #URL TO THE STATION'S|CHANNEL'S BANNER IMAGE, IF ANY. | ||
| 307 | } | ||
| 308 | |||
| 309 | sub getImageData | ||
| 310 | { | ||
| 311 | my $self = shift; | ||
| 312 | |||
| 313 | my $whichurl = (defined($_[0]) && $_[0] =~ /^\-?artist/i) | ||
| 314 | ? 'artimageurl' : 'imageurl'; | ||
| 315 | return () unless ($self->{$whichurl}); | ||
| 316 | |||
| 317 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 318 | $ua->timeout($self->{'timeout'}); | ||
| 319 | $ua->cookie_jar({}); | ||
| 320 | $ua->env_proxy; | ||
| 321 | my $art_image = ''; | ||
| 322 | my $response = $ua->get($self->{$whichurl}); | ||
| 323 | if ($response->is_success) { | ||
| 324 | $art_image = $response->decoded_content; | ||
| 325 | } else { | ||
| 326 | print STDERR $response->status_line if ($DEBUG); | ||
| 327 | my $no_wget = system('wget','-V'); | ||
| 328 | unless ($no_wget) { | ||
| 329 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 330 | my $iconUrl = $self->{$whichurl}; | ||
| 331 | $art_image = `wget -t 2 -T 20 -O- -o /dev/null "$iconUrl" 2>/dev/null `; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | return () unless ($art_image); | ||
| 335 | my $image_ext = $self->{$whichurl}; | ||
| 336 | $image_ext = ($self->{$whichurl} =~ /\.(\w+)$/) ? $1 : 'png'; | ||
| 337 | $image_ext =~ s/[^A-Za-z].*$//; | ||
| 338 | return ($image_ext, $art_image); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub _log | ||
| 342 | { | ||
| 343 | my $self = shift; | ||
| 344 | my $inurl = shift; | ||
| 345 | |||
| 346 | if ($self->{'log'} && open(LOG, '>>'.$self->{'log'})) { | ||
| 347 | my $logline = $self->{'logfmt'}; | ||
| 348 | if ($logline =~ m/\[time\]/) { | ||
| 349 | my $time = time; | ||
| 350 | $logline =~ s/\[time\]/$time/; | ||
| 351 | } | ||
| 352 | $logline =~ s/\[stream\]/$self->{'total'} ? $$self{'Url'}: '-no streams!-'/e; | ||
| 353 | $logline =~ s/\[site\]/$$self{'_objname'}/; | ||
| 354 | $logline =~ s/\[url\]/$inurl/; | ||
| 355 | foreach my $f (qw(title artist album description created year genre iconurl total albumartist)) { | ||
| 356 | my $val = $self->{$f} || '-na-'; | ||
| 357 | $logline =~ s/\[$f\]/$val/; | ||
| 358 | } | ||
| 359 | my $sep = $bummer ? "\r\n" : "\n"; | ||
| 360 | print LOG $logline . $sep; | ||
| 361 | close LOG; | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | 1 |