| 1 | =head1 NAME | ||
| 2 | |||
| 3 | StreamFinder::IHeartRadio - Fetch actual raw streamable URLs from radio-station websites on IHeart.com | ||
| 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 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | #!/usr/bin/perl | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | |||
| 25 | use StreamFinder::IHeartRadio; | ||
| 26 | |||
| 27 | die "..usage: $0 ID|URL\n" unless ($ARGV[0]); | ||
| 28 | |||
| 29 | my $station = new StreamFinder::IHeartRadio($ARGV[0], -keep => | ||
| 30 | {'secure_shoutcast', 'secure', 'any'}, -skip => 'rtmp'); | ||
| 31 | |||
| 32 | die "Invalid URL or no streams found!\n" unless ($station); | ||
| 33 | |||
| 34 | my $firstStream = $station->get(); | ||
| 35 | |||
| 36 | print "First Stream URL=$firstStream\n"; | ||
| 37 | |||
| 38 | my $url = $station->getURL(); | ||
| 39 | |||
| 40 | print "Stream URL=$url\n"; | ||
| 41 | |||
| 42 | my $stationTitle = $station->getTitle(); | ||
| 43 | |||
| 44 | print "Title=$stationTitle\n"; | ||
| 45 | |||
| 46 | my $stationDescription = $station->getTitle('desc'); | ||
| 47 | |||
| 48 | print "Description=$stationDescription\n"; | ||
| 49 | |||
| 50 | my $stationID = $station->getID(); | ||
| 51 | |||
| 52 | print "Station ID=$stationID\n"; | ||
| 53 | |||
| 54 | my $artist = $station->{'artist'}; | ||
| 55 | |||
| 56 | print "Artist=$artist\n" if ($artist); | ||
| 57 | |||
| 58 | my $genre = $station->{'genre'}; | ||
| 59 | |||
| 60 | print "Genre=$genre\n" if ($genre); | ||
| 61 | |||
| 62 | my $icon_url = $station->getIconURL(); | ||
| 63 | |||
| 64 | if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE: | ||
| 65 | |||
| 66 | my ($image_ext, $icon_image) = $station->getIconData(); | ||
| 67 | |||
| 68 | if ($icon_image && open IMGOUT, ">/tmp/${stationID}.$image_ext") { | ||
| 69 | |||
| 70 | binmode IMGOUT; | ||
| 71 | |||
| 72 | print IMGOUT $icon_image; | ||
| 73 | |||
| 74 | close IMGOUT; | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | my $stream_count = $station->count(); | ||
| 81 | |||
| 82 | print "--Stream count=$stream_count=\n"; | ||
| 83 | |||
| 84 | my @streams = $station->get(); | ||
| 85 | |||
| 86 | foreach my $s (@streams) { | ||
| 87 | |||
| 88 | print "------ stream URL=$s=\n"; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | =head1 DESCRIPTION | ||
| 93 | |||
| 94 | StreamFinder::IHeartRadio accepts a valid radio station or podcast ID or URL on | ||
| 95 | IHeart.com (formerly known as IHeartRadio.com) and returns the actual stream | ||
| 96 | URL(s), title, and cover art icon. The purpose is that one needs one of these | ||
| 97 | URLs in order to have the option to stream the station in one's own choice of | ||
| 98 | media player software rather than using their web browser and accepting | ||
| 99 | any / all flash, ads, javascript, cookies, trackers, web-bugs, and other | ||
| 100 | crapware that can come with that method of play. The author uses his own | ||
| 101 | custom all-purpose media player called "fauxdacious" (his custom hacked version | ||
| 102 | of the open-source "audacious" audio player). "fauxdacious" can incorporate | ||
| 103 | this module to decode and play IHeart.com streams. | ||
| 104 | |||
| 105 | One or more stream URLs can be returned for each station or podcast. | ||
| 106 | |||
| 107 | =head1 SUBROUTINES/METHODS | ||
| 108 | |||
| 109 | =over 4 | ||
| 110 | |||
| 111 | =item B<new>(I<ID>|I<url> [, I<-keep|-skip> => I<streamtypes>] | ||
| 112 | [, I<-secure> [ => 0|1 ]] [, I<-debug> [ => 0|1|2 ] ... ]) | ||
| 113 | |||
| 114 | Accepts an iheart.com station / podcast ID or URL and creates and returns | ||
| 115 | a new station (or podcast) object, or I<undef> if the URL is not a valid IHeart | ||
| 116 | station or podcast, or no streams are found. The URL can be the full URL, | ||
| 117 | ie. https://www.iheart.com/live/B<station-id>, | ||
| 118 | https://B<station-id>.iheart.com, | ||
| 119 | https://www.iheart.com/podcast/B<podcast-id>/episode/B<episode-id>, | ||
| 120 | https://www.iheart.com/podcast/B<podcast-id>, or just | ||
| 121 | B<station-id>, or B<podcast-id/episode-id>. NOTE: For podcasts, you must | ||
| 122 | include the I<episode-id> if not specifying a full URL, otherwise, the | ||
| 123 | I<podcast-id> will be interpreted as a I<station-id> (and you likely won't get | ||
| 124 | any streams), but if specifying a full URL and no I<episode-id> is specified, | ||
| 125 | the first (latest) episode for the podcast channel is returned. | ||
| 126 | |||
| 127 | I<-keep> and I<-skip> specify a list of one or more I<streamtypes> to either | ||
| 128 | include or skip respectively. The list for each can be either a | ||
| 129 | comma-separated string or an array reference ([...]) of stream types, in the | ||
| 130 | order they should be returned. Each stream type in the list can be one of: | ||
| 131 | any, secure, secure_pls, pls, secure_hls, hls, secure_shortcast, shortcast, | ||
| 132 | secure_rtmp, rtmp, (I<ext>, ie. mp4) etc. NOTE: If you specify the I<-secure> | ||
| 133 | option, described below, as 1 (true), and only non-secure* options above for | ||
| 134 | I<-keep>, you won't get any streams! | ||
| 135 | |||
| 136 | DEFAULT I<-keep> list is 'secure_shoutcast, shoutcast, secure, any', meaning | ||
| 137 | that all secure_shoutcast (https:) streams followed by any other shoutcast | ||
| 138 | streams, then all other secure (https:) streams, followed by any remaining | ||
| 139 | (http:) streams (.m3u8, etc.). More than one value can be specified to | ||
| 140 | control order of search. | ||
| 141 | |||
| 142 | The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>). | ||
| 143 | If 1 then only secure ("https://") streams will be returned. | ||
| 144 | |||
| 145 | DEFAULT I<-secure> is 0 (false) - return all streams (http and https). | ||
| 146 | |||
| 147 | Additional options: | ||
| 148 | |||
| 149 | I<-log> => "I<logfile>" | ||
| 150 | |||
| 151 | Specify path to a log file. If a valid and writable file is specified, A line | ||
| 152 | will be appended to this file every time one or more streams is successfully | ||
| 153 | fetched for a url. | ||
| 154 | |||
| 155 | DEFAULT I<-none-> (no logging). | ||
| 156 | |||
| 157 | I<-logfmt> specifies a format string for lines written to the log file. | ||
| 158 | |||
| 159 | DEFAULT "I<[time] [url] - [site]: [title] ([total])>". | ||
| 160 | |||
| 161 | The valid field I<[variables]> are: [stream]: The url of the first/best | ||
| 162 | stream found. [site]: The site name (IHeartRadio). [url]: The url | ||
| 163 | searched for streams. [time]: Perl timestamp when the line was logged. | ||
| 164 | [title], [artist], [album], [description], [year], [genre], [total], | ||
| 165 | [albumartist]: The corresponding field data returned (or "I<-na->", | ||
| 166 | if no value). | ||
| 167 | |||
| 168 | =item $station->B<get>() | ||
| 169 | |||
| 170 | Returns an array of strings representing all stream URLs found. | ||
| 171 | |||
| 172 | =item $station->B<getURL>([I<options>]) | ||
| 173 | |||
| 174 | Similar to B<get>() except it only returns a single stream representing | ||
| 175 | the first valid stream found. | ||
| 176 | |||
| 177 | Current options are: I<"random">, I<"nopls">, and I<"noplaylists">. | ||
| 178 | By default, the first ("best"?) stream is returned. If I<"random"> is | ||
| 179 | specified, then a random one is selected from the list of streams found. | ||
| 180 | If I<"nopls"> is specified, and the stream to be returned is a ".pls" playlist, | ||
| 181 | it is first fetched and the first entry (or a random entry if I<"random"> is | ||
| 182 | specified) is returned. This is needed by Fauxdacious Mediaplayer. | ||
| 183 | If I<"noplaylists"> is specified, and the stream to be returned is a | ||
| 184 | "playlist" (either .pls or .m3u? extension), it is first fetched and the first | ||
| 185 | entry (or a random entry if I<"random"> is specified) in the playlist | ||
| 186 | is returned. | ||
| 187 | |||
| 188 | =item $station->B<count>() | ||
| 189 | |||
| 190 | Returns the number of streams found for the station / podcast episode. | ||
| 191 | |||
| 192 | =item $station->B<getID>(['fccid']) | ||
| 193 | |||
| 194 | Returns the station's IHeartRadio ID (default) or station's FCC | ||
| 195 | call-letters ("fccid"). For stations, the IHeartRadio ID is a single value. | ||
| 196 | For individual podcast episodes it's two values separated by a slash ("/"). | ||
| 197 | |||
| 198 | =item $station->B<getTitle>(['desc']) | ||
| 199 | |||
| 200 | Returns the station's title, or (long description). Podcasts | ||
| 201 | on IHeartRadio can have separate descriptions, but for stations, | ||
| 202 | it is always the station's title. | ||
| 203 | |||
| 204 | =item $station->B<getIconURL>(['artist']) | ||
| 205 | |||
| 206 | Returns the URL for the station's "cover art" icon image, if any. | ||
| 207 | If B<'artist'> is specified, the podcast channel artist's icon url | ||
| 208 | is returned, if any. NOTE: The B<'artist'> option will return an | ||
| 209 | empty string unless the station is a podcast. | ||
| 210 | |||
| 211 | =item $station->B<getIconData>(['artist']) | ||
| 212 | |||
| 213 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 214 | "gif", "jpeg", etc.) and the actual icon image (binary data), if any. | ||
| 215 | If B<'artist'> is specified, the podcast channel artist's icon data | ||
| 216 | is returned, if any. NOTE: The B<'artist'> option will return an | ||
| 217 | empty string unless the station is a podcast. | ||
| 218 | |||
| 219 | =item $station->B<getImageURL>(['artist']) | ||
| 220 | |||
| 221 | Returns the URL for the station's "cover art" (usually larger) | ||
| 222 | banner image. NOTE: For IHeart podcasts, this will be the same as the | ||
| 223 | icon url. | ||
| 224 | If B<'artist'> is specified, the podcast channel artist's image url | ||
| 225 | is returned, if any. NOTE: The B<'artist'> option will return an | ||
| 226 | empty string unless the station is a podcast. | ||
| 227 | |||
| 228 | =item $station->B<getImageData>(['artist']) | ||
| 229 | |||
| 230 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 231 | "gif", "jpeg", etc.) and the actual station's banner image (binary data). | ||
| 232 | NOTE: For IHeart podcasts, this will be the same as the icon url. | ||
| 233 | If B<'artist'> is specified, the podcast channel artist's image data | ||
| 234 | is returned, if any. NOTE: The B<'artist'> option will return an | ||
| 235 | empty string unless the station is a podcast. | ||
| 236 | |||
| 237 | =item $station->B<getType>() | ||
| 238 | |||
| 239 | Returns the station's type ("IHeartRadio"). | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | =head1 CONFIGURATION FILES | ||
| 244 | |||
| 245 | The default root location directory for StreamFinder configuration files | ||
| 246 | is "~/.config/StreamFinder". To use an alternate location directory, | ||
| 247 | specify it in the "I<STREAMFINDER>" environment variable, ie.: | ||
| 248 | B<$ENV{STREAMFINDER} = "/etc/StreamFinder">. | ||
| 249 | |||
| 250 | =over 4 | ||
| 251 | |||
| 252 | =item ~/.config/StreamFinder/IHeartRadio/config | ||
| 253 | |||
| 254 | Optional text file for specifying various configuration options | ||
| 255 | for a specific site (submodule). Each option is specified on a | ||
| 256 | separate line in the format below: | ||
| 257 | NOTE: Do not follow the lines with a semicolon, comma, or any other | ||
| 258 | separator. Non-numeric I<values> should be surrounded with quotes, either | ||
| 259 | single or double. Blank lines and lines beginning with a "#" sign as | ||
| 260 | their first non-blank character are ignored as comments. | ||
| 261 | |||
| 262 | 'option' => 'value' [,] | ||
| 263 | |||
| 264 | and the options are loaded into a hash used only by the specific | ||
| 265 | (submodule) specified. Valid options include | ||
| 266 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 267 | |||
| 268 | Options specified here override any specified in | ||
| 269 | I<~/.config/StreamFinder/config>. | ||
| 270 | |||
| 271 | Among options valid for IHeartRadio streams are the I<-keep> and | ||
| 272 | I<-skip> options described in the B<new()> function. | ||
| 273 | |||
| 274 | =item ~/.config/StreamFinder/config | ||
| 275 | |||
| 276 | Optional text file for specifying various configuration options. | ||
| 277 | Each option is specified on a separate line in the format below: | ||
| 278 | |||
| 279 | 'option' => 'value' [,] | ||
| 280 | |||
| 281 | and the options are loaded into a hash used by all sites | ||
| 282 | (submodules) that support them. Valid options include | ||
| 283 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 284 | |||
| 285 | =back | ||
| 286 | |||
| 287 | NOTE: Options specified in the options parameter list of the I<new()> | ||
| 288 | function will override those corresponding options specified in these files. | ||
| 289 | |||
| 290 | =head1 KEYWORDS | ||
| 291 | |||
| 292 | iheartradio | ||
| 293 | |||
| 294 | =head1 DEPENDENCIES | ||
| 295 | |||
| 296 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent> | ||
| 297 | |||
| 298 | =head1 RECCOMENDS | ||
| 299 | |||
| 300 | wget | ||
| 301 | |||
| 302 | =head1 BUGS | ||
| 303 | |||
| 304 | Please report any bugs or feature requests to | ||
| 305 | C<bug-streamFinder-iheartradio at rt.cpan.org>, or through the web interface at | ||
| 306 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-IHeartRadio>. | ||
| 307 | I will be notified, and then you'll automatically be notified of progress on | ||
| 308 | your bug as I make changes. | ||
| 309 | |||
| 310 | =head1 SUPPORT | ||
| 311 | |||
| 312 | You can find documentation for this module with the perldoc command. | ||
| 313 | |||
| 314 | perldoc StreamFinder::IHeartRadio | ||
| 315 | |||
| 316 | You can also look for information at: | ||
| 317 | |||
| 318 | =over 4 | ||
| 319 | |||
| 320 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 321 | |||
| 322 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-IHeartRadio> | ||
| 323 | |||
| 324 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 325 | |||
| 326 | L<http://annocpan.org/dist/StreamFinder-IHeartRadio> | ||
| 327 | |||
| 328 | =item * Search CPAN | ||
| 329 | |||
| 330 | L<http://search.cpan.org/dist/StreamFinder-IHeartRadio/> | ||
| 331 | |||
| 332 | =back | ||
| 333 | |||
| 334 | =head1 LICENSE AND COPYRIGHT | ||
| 335 | |||
| 336 | Copyright 2017-2026 Jim Turner. | ||
| 337 | |||
| 338 | This program is free software; you can redistribute it and/or modify it | ||
| 339 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 340 | copy of the full license at: | ||
| 341 | |||
| 342 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 343 | |||
| 344 | Any use, modification, and distribution of the Standard or Modified | ||
| 345 | Versions is governed by this Artistic License. By using, modifying or | ||
| 346 | distributing the Package, you accept this license. Do not use, modify, | ||
| 347 | or distribute the Package, if you do not accept this license. | ||
| 348 | |||
| 349 | If your Modified Version has been derived from a Modified Version made | ||
| 350 | by someone other than you, you are nevertheless required to ensure that | ||
| 351 | your Modified Version complies with the requirements of this license. | ||
| 352 | |||
| 353 | This license does not grant you the right to use any trademark, service | ||
| 354 | mark, tradename, or logo of the Copyright Holder. | ||
| 355 | |||
| 356 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 357 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 358 | otherwise transfer the Package with respect to any patent claims | ||
| 359 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 360 | Package. If you institute patent litigation (including a cross-claim or | ||
| 361 | counterclaim) against any party alleging that the Package constitutes | ||
| 362 | direct or contributory patent infringement, then this Artistic License | ||
| 363 | to you shall terminate on the date that such litigation is filed. | ||
| 364 | |||
| 365 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 366 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 367 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 368 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 369 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 370 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 371 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 372 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 373 | |||
| 374 | =cut | ||
| 375 | |||
| 376 | package StreamFinder::IHeartRadio; | ||
| 377 | |||
| 378 | use strict; | ||
| 379 | use warnings; | ||
| 380 | use URI::Escape; | ||
| 381 | use HTML::Entities (); | ||
| 382 | use LWP::UserAgent (); | ||
| 383 | use parent 'StreamFinder::_Class'; | ||
| 384 | |||
| 385 | my $DEBUG = 0; | ||
| 386 | |||
| 387 | sub new | ||
| 388 | { | ||
| 389 | my $class = shift; | ||
| 390 | my $url = shift; | ||
| 391 | |||
| 392 | return undef unless ($url); | ||
| 393 | |||
| 394 | my $self = $class->SUPER::new('IHeartRadio', @_); | ||
| 395 | $DEBUG = $self->{'debug'} if (defined $self->{'debug'}); | ||
| 396 | |||
| 397 | my (@okStreams, @skipStreams); | ||
| 398 | while (@_) { | ||
| 399 | if ($_[0] =~ /^\-?keep$/o) { | ||
| 400 | shift; | ||
| 401 | if (defined $_[0]) { | ||
| 402 | my $keeporder = shift; | ||
| 403 | @okStreams = (ref($keeporder) =~ /ARRAY/) ? @{$keeporder} : split(/\,\s*/, $keeporder); | ||
| 404 | } | ||
| 405 | } elsif ($_[0] =~ /^\-?skip$/o) { | ||
| 406 | shift; | ||
| 407 | if (defined $_[0]) { | ||
| 408 | my $skiporder = shift; | ||
| 409 | @skipStreams = (ref($skiporder) =~ /ARRAY/) ? @{$skiporder} : split(/\,\s*/, $skiporder); | ||
| 410 | } | ||
| 411 | } else { | ||
| 412 | shift; | ||
| 413 | } | ||
| 414 | } | ||
| 415 | if (!defined($okStreams[0]) && defined($self->{'keep'})) { | ||
| 416 | @okStreams = (ref($self->{'keep'}) =~ /ARRAY/) ? @{$self->{'keep'}} : split(/\,\s*/, $self->{'keep'}); | ||
| 417 | } | ||
| 418 | if (!defined($skipStreams[0]) && defined($self->{'skip'})) { | ||
| 419 | @skipStreams = (ref($self->{'skip'}) =~ /ARRAY/) ? @{$self->{'skip'}} : split(/\,\s*/, $self->{'skip'}); | ||
| 420 | } | ||
| 421 | @okStreams = (qw(secure_shoutcast shoutcast secure any)) unless (defined $okStreams[0]); # one of: {secure_pls | pls | stw} | ||
| 422 | |||
| 423 | print STDERR "-0(IHeartRadio): URL=$url= KEEP=(",join('|',@okStreams).") SKIP=(",join('|',@skipStreams).")\n" if ($DEBUG); | ||
| 424 | |||
| 425 | my $url2fetch = $url; | ||
| 426 | if ($url =~ /^https?\:/) { | ||
| 427 | $self->{'id'} = $1 if ($url2fetch =~ m#\/([^\/]+)\/?$#); | ||
| 428 | if ($url2fetch =~ m#\/episode\/#) { | ||
| 429 | my $id = $1 if ($url2fetch =~ m#([^\/]+)\/episode\/#); | ||
| 430 | $self->{'id'} = $id . '/' . $self->{'id'} if ($id); | ||
| 431 | } | ||
| 432 | } else { | ||
| 433 | $self->{'id'} = $url2fetch; | ||
| 434 | my ($id, $podcastid) = split(m#\/#, $url2fetch); | ||
| 435 | $url2fetch = $podcastid ? "https://www.iheart.com/podcast/$id" : "https://${id}.iheart.com/"; | ||
| 436 | $url2fetch .= '/episode/' . $podcastid if ($podcastid); | ||
| 437 | } | ||
| 438 | return undef unless ($self->{'id'}); #STEP 1 FAILED, INVALID STATION URL, PUNT! | ||
| 439 | |||
| 440 | my $html = ''; | ||
| 441 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 442 | my $response; | ||
| 443 | |||
| 444 | $ua->timeout($self->{'timeout'}); | ||
| 445 | $ua->cookie_jar({}); | ||
| 446 | $ua->env_proxy; | ||
| 447 | print STDERR "-1 FETCHING URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 448 | if ($url2fetch =~ m#\.iheart\.com\/?$#) { #URL FORMAT: https://station.iheart.com (SEE CPAN BUG# 133982): | ||
| 449 | $response = $ua->get($url2fetch); | ||
| 450 | if ($response->is_success) { | ||
| 451 | $html = $response->decoded_content; | ||
| 452 | } else { | ||
| 453 | print STDERR $response->status_line if ($DEBUG); | ||
| 454 | } | ||
| 455 | $url2fetch = ''; | ||
| 456 | print STDERR "-1a: html=$html=\n" if ($DEBUG > 1); | ||
| 457 | if ($html && $html =~ m#\<div\s+class\=\"play\-icon\"\>([^\>]+)#s) { | ||
| 458 | my $playHtml = $1; | ||
| 459 | if ($playHtml =~ m#href\=\"([^\"]+)#s) { | ||
| 460 | my $playlink = $1; | ||
| 461 | if ($playlink =~ m#\/live\/([a-z0-9\-]+)#) { | ||
| 462 | $self->{'id'} = $1; | ||
| 463 | $url2fetch = $playlink; | ||
| 464 | print STDERR "-1a FETCHING REDIRECTED URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | } | ||
| 468 | #IF NO REDIRECT FOUND, RESET TO CONVENTIONAL "live" URL AND TRY THAT: | ||
| 469 | $url2fetch ||= 'https://www.iheart.com/live/' . $self->{'id'}; | ||
| 470 | } | ||
| 471 | |||
| 472 | print STDERR "-1: FINAL FETCH URL=$url2fetch=\n" if ($DEBUG); | ||
| 473 | my $tryit = 0; | ||
| 474 | TRYIT: | ||
| 475 | $response = $ua->get($url2fetch); | ||
| 476 | if ($response->is_success) { | ||
| 477 | $html = $response->decoded_content; | ||
| 478 | } else { | ||
| 479 | print STDERR $response->status_line if ($DEBUG); | ||
| 480 | } | ||
| 481 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 482 | return undef unless ($html && $self->{'id'}); #STEP 1 FAILED, INVALID STATION URL, PUNT! | ||
| 483 | |||
| 484 | my $html2 = ''; | ||
| 485 | my $streamhtml0 = ($html =~ /\"streams\"\s*\:\s*\{([^\}]+)\}/) ? $1 : ''; | ||
| 486 | print STDERR "-2: streamhtml=$streamhtml0=\n" if ($DEBUG); | ||
| 487 | unless ($streamhtml0) { #NO STREAMS (PODCAST?) - LOOK FOR MEDIAURL: | ||
| 488 | while ($html =~ s#\"mediaUrl\"\:\"([^\"]+)\"##gso) { | ||
| 489 | my $one = $1; | ||
| 490 | unless ($self->{'secure'} && $one !~ /^https/o) { | ||
| 491 | push @{$self->{'streams'}}, $one; | ||
| 492 | $self->{'cnt'}++; | ||
| 493 | } | ||
| 494 | } | ||
| 495 | unless ($tryit || $self->{'cnt'} > 0) { | ||
| 496 | print "--no streams found, ID=".$self->{'id'}."= url=$url2fetch= PODCAST PAGE, MAYBE?\n" if ($DEBUG); | ||
| 497 | my $episodeID = ''; | ||
| 498 | my $episodeList = ''; | ||
| 499 | my $firstEpisode = ($html =~ m#\"episodes\"\:\{\"([\d]+)\"\:#s) ? $1 : ''; | ||
| 500 | if ($html =~ m#\"\,\"url\"\:\"\/podcast\/$self->{'id'}\/?\"\,\"episodeIds\"\:\[([^\]]+)\]#s) { | ||
| 501 | $episodeList = $1; | ||
| 502 | if ($firstEpisode && $episodeList =~ /\b$firstEpisode$/) { #THE EPISODE LIST IS IN REVERSE ORDER!: | ||
| 503 | ($episodeID = $episodeList) =~ s/\D.*$//; #WE WANT THE FIRST ONE. | ||
| 504 | } elsif ($episodeList =~ /(\d+)$/) { | ||
| 505 | $episodeID = $1; | ||
| 506 | } | ||
| 507 | } elsif ($firstEpisode) { | ||
| 508 | while ($html =~ s#\"episodeIds\"\:\[([^\]]+)\]##s) { | ||
| 509 | $episodeList = $1; | ||
| 510 | if ($episodeList =~ /\b$firstEpisode$/) { #THE EPISODE LIST IS IN REVERSE ORDER!: | ||
| 511 | ($episodeID = $episodeList) =~ s/\D.*$//; #WE WANT THE FIRST ONE. | ||
| 512 | last; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | } | ||
| 516 | $episodeID ||= $firstEpisode; | ||
| 517 | if ($episodeID) { | ||
| 518 | if ($html =~ m#\<h1[^\>]*\>([^\<]+)#si) { | ||
| 519 | $self->{'artist'} = $1; | ||
| 520 | while ($html =~ s#\<img([^\>]+)\>##si) { | ||
| 521 | my $imgdata = $1; | ||
| 522 | if ($imgdata =~ m#\balt\=\"$self->{'artist'}\"# | ||
| 523 | && $imgdata =~ m#\bsrc\=\"([^\"]+)#) { | ||
| 524 | $self->{'artimageurl'} = $self->{'articonurl'} = $1; | ||
| 525 | $self->{'articonurl'} =~ s/\?.*$//; | ||
| 526 | last; | ||
| 527 | } | ||
| 528 | } | ||
| 529 | } | ||
| 530 | $url2fetch =~ s#\/$##; | ||
| 531 | $url2fetch .= "/episode/$episodeID"; | ||
| 532 | $self->{'id'} .= "/$episodeID"; | ||
| 533 | print STDERR "----TRY AGAIN w/($url2fetch) ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 534 | ++$tryit; | ||
| 535 | goto TRYIT; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | return undef unless ($self->{'cnt'} > 0); | ||
| 539 | |||
| 540 | unless ($self->{'articonurl'}) { | ||
| 541 | if ($html =~ m#\"image\"\:\"([^\"]+)\"\,\"url\"\:#s) { | ||
| 542 | $self->{'artimageurl'} = $self->{'articonurl'} = $1; | ||
| 543 | $self->{'articonurl'} =~ s/\?.*$//; | ||
| 544 | print STDERR "--found channel art url=".$self->{'articonurl'}."=\n" if ($DEBUG); | ||
| 545 | } | ||
| 546 | } | ||
| 547 | my $id = $url; | ||
| 548 | $id =~ s#\/$##; | ||
| 549 | $id = $1 if ($id =~ m#([^\/]+)\/episode\/#); | ||
| 550 | my $seedID = ($id =~ /(\d+)$/) ? $1 : ''; #NUMERIC PART | ||
| 551 | $self->{'title'} = $1 if ($html =~ s# rel\=\"alternate\"\s+title\=\"([^\"]+)\"##s); | ||
| 552 | $self->{'title'} ||= ($html =~ s#\<title[^\>]+\>([^\<]+)\<\/title\>##s) ? $1 : ''; | ||
| 553 | if ($html =~ m#\bitemProp\=\"podcastDescription\"\>(.+?)\<\/div\>#s) { | ||
| 554 | $self->{'description'} = $1; | ||
| 555 | } | ||
| 556 | $self->{'description'} ||= $1 if ($html =~ s#\"description\"\:\"([^\"]+)##s); | ||
| 557 | $self->{'description'} ||= $1 if ($html =~ s#\"podcastDescription\"\>\<p\>(.+?)\<\/p\>##s); | ||
| 558 | # $self->{'description'} ||= ($html =~ s#\"podcastDescription\"\>(?:\<[^\>]+\>)?([^\<]+)##s) ? $1 : $self->{'title'}; | ||
| 559 | $self->{'description'} ||= $self->{'title'}; | ||
| 560 | $self->{'description'} = HTML::Entities::decode_entities($self->{'description'}); | ||
| 561 | $self->{'description'} = uri_unescape($self->{'description'}); | ||
| 562 | ($self->{'albumartist'} = $url) =~ s#\/episode.+$##; | ||
| 563 | if ($seedID) { | ||
| 564 | my $albumHtml = $1 if ($html =~ m#\"seedShowId\"\:$seedID([^\}]+)#); | ||
| 565 | if ($albumHtml) { | ||
| 566 | $self->{'artist'} = $1 if ($albumHtml =~ m#\"title\"\:\"([^\"]+)#); | ||
| 567 | } | ||
| 568 | } | ||
| 569 | if ($html =~ m#\<span class\=\"css\-\w+ \w+\"\>\<span\>([^\<]+)\<\/span\>\<\/span\>\<\/a\>\</div\>\<span\>\<div class\=\"css\-\w+ \w+\"\>([^\<]+)\<\!\-\- \-\-\>#) { | ||
| 570 | $self->{'title'} = $1; | ||
| 571 | $self->{'created'} = $2; | ||
| 572 | $self->{'year'} = ($self->{'created'} =~ /(\d\d\d\d)/) ? $1 : ''; | ||
| 573 | print STDERR "i:Found better title (".$self->{'title'}."), year (".$self->{'year'}.").\n" if ($DEBUG); | ||
| 574 | } | ||
| 575 | $self->{'title'} = HTML::Entities::decode_entities($self->{'title'}); | ||
| 576 | $self->{'title'} = uri_unescape($self->{'title'}); | ||
| 577 | if ($html =~ m#\<h1[^\>]*\>([^\<]+)#s) { | ||
| 578 | $self->{'artist'} = $1; | ||
| 579 | $self->{'artist'} = HTML::Entities::decode_entities($self->{'artist'}); | ||
| 580 | $self->{'artist'} = uri_unescape($self->{'artist'}); | ||
| 581 | print STDERR "i:Found artist name (".$self->{'artist'}.").\n" if ($DEBUG); | ||
| 582 | } | ||
| 583 | $self->{'year'} = ($html =~ m#\<p\>.*?\xa9\s*(\d\d\d\d)#s) ? $1 : ''; | ||
| 584 | $self->{'year'} ||= $1 if ($html =~ m#(\d\d\d\d)\<\!\-\-#s); | ||
| 585 | $self->{'genre'} = 'Podcast'; | ||
| 586 | $self->{'imageurl'} = ($html =~ s#\"imageUrl\"\:\"([^\"]+)\"##s) ? $1 : ''; | ||
| 587 | $self->{'iconurl'} = $self->{'imageurl'}; | ||
| 588 | $self->{'total'} = $self->{'cnt'}; | ||
| 589 | $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : ''; | ||
| 590 | if ($DEBUG) { | ||
| 591 | foreach my $i (sort keys %{$self}) { | ||
| 592 | print STDERR "--KEY=$i= VAL=".$self->{$i}."=\n"; | ||
| 593 | } | ||
| 594 | print STDERR "-SUCCESS2: 1st stream=".$self->{'Url'}."=\n"; | ||
| 595 | } | ||
| 596 | |||
| 597 | $self->_log($url); | ||
| 598 | |||
| 599 | bless $self, $class; #BLESS IT! | ||
| 600 | |||
| 601 | return $self; | ||
| 602 | } | ||
| 603 | |||
| 604 | $self->{'fccid'} = ($html =~ m#\"callLetters\"\s*\:\s*\"([^\"]+)\"#is) ? $1 : ''; | ||
| 605 | $self->{'title'} = ($html =~ m#\"stationName\"\s*\:\s*\"([^\"]+)\"#s) ? $1 : $url; | ||
| 606 | $self->{'title'} = $1 if ($html =~ m#\"broadcastDisplayName\"\s*\:\s*\"([^\"]+)\"#s); | ||
| 607 | $self->{'title'} =~ s#http[s]?\:\/\/www\.iheart\.com\/live\/##; | ||
| 608 | $self->{'description'} = ($html =~ m#\"description\"\s*\:\s*\"([^\"]+)\"#s) ? $1 : $self->{'title'}; | ||
| 609 | $self->{'title'} = HTML::Entities::decode_entities($self->{'title'}); | ||
| 610 | $self->{'title'} = uri_unescape($self->{'title'}); | ||
| 611 | $self->{'description'} = HTML::Entities::decode_entities($self->{'description'}); | ||
| 612 | $self->{'description'} = uri_unescape($self->{'description'}); | ||
| 613 | $self->{'imageurl'} = ($html =~ m#\"image_src\"\s+href=\"([^\"]+)\"#s) ? $1 : ''; | ||
| 614 | $self->{'iconurl'} = ($html =~ m#\,\"logo\"\:\"([^\"]+)\"\,\"freq\"\:#s) ? $1 : ''; | ||
| 615 | $self->{'imageurl'} ||= $self->{'iconurl'}; | ||
| 616 | $self->{'genre'} = $1 if ($html =~ m#\"Genre\"\s+name\=\"twitter\:label1\"\/\>\<meta\s+data\-react\-helmet\=\"[^\"]*\"\s+content\=\"([^\"]+)\"#s); | ||
| 617 | if ($self->{'genre'}) { | ||
| 618 | $self->{'genre'} = HTML::Entities::decode_entities($self->{'genre'}); | ||
| 619 | $self->{'genre'} = uri_unescape($self->{'genre'}); | ||
| 620 | } | ||
| 621 | |||
| 622 | my ($streamhtml, $streampattern); | ||
| 623 | my %streams = (); | ||
| 624 | my $weight = 999; | ||
| 625 | |||
| 626 | # OUTTERMOST LOOP TO TRY EACH STREAM-TYPE: (CONSTRAINED IF USER SPECIFIES STREAMTYPES AS EXTRA ARGS TO new()) | ||
| 627 | foreach my $streamtype (@okStreams) { | ||
| 628 | print STDERR "--OUTTER: type=$streamtype=\n" if ($DEBUG); | ||
| 629 | $streamhtml = $streamhtml0; | ||
| 630 | $streampattern = $streamtype; | ||
| 631 | if ($streamtype eq 'secure') { | ||
| 632 | $streampattern = '\"secure_\w+'; | ||
| 633 | } elsif ($streamtype eq 'any') { | ||
| 634 | $streampattern = '\"\w+'; | ||
| 635 | } else { | ||
| 636 | $streampattern = '\"' . $streamtype; | ||
| 637 | } | ||
| 638 | $self->{'cnt'} = 0; | ||
| 639 | # INNER LOOP: MATCH STREAM URLS BY TYPE PATTEREN REGEX UNTIL WE FIND ONE THAT'S ACCEPTABLE (NOT EXCLUDED TYPE): | ||
| 640 | print STDERR "-3: PATTERN=${streampattern}_stream=\n" if ($DEBUG); | ||
| 641 | INNER: while ($streamhtml =~ s#(${streampattern}_stream)\"\s*\:\s*\"([^\"]+)\"##) | ||
| 642 | { | ||
| 643 | print STDERR "----INNER: type=$streampattern=\n" if ($DEBUG); | ||
| 644 | $self->{'streamtype'} = substr($1, 1); | ||
| 645 | $self->{'streamurl'} = $2; | ||
| 646 | foreach my $xp (@skipStreams) { | ||
| 647 | next INNER if ($self->{'streamtype'} =~ /$xp/); #REJECTED STREAM-TYPE. | ||
| 648 | } | ||
| 649 | |||
| 650 | # WE NOW HAVE A STREAM THAT MATCHES OUR CONSTRAINTS: | ||
| 651 | # IF IT'S A ".pls" (PLAYLIST) STREAM, WE NEED TO FETCH THE LIST OF ACTUAL STREAMS: | ||
| 652 | # streamurl WILL STILL CONTAIN THE PLAYLIST STREAM ITSELF! | ||
| 653 | if ($self->{'streamurl'} && $self->{'streamtype'} =~ /pls/) { | ||
| 654 | $self->{'plsid'} = $1 if ($self->{'streamurl'} =~ m#\/([^\/]+)\.pls$#i); | ||
| 655 | print STDERR "---4: PLS stream id=".$self->{'plsid'}."= URL=".$self->{'streamurl'}."\n" if ($DEBUG); | ||
| 656 | $response = $ua->get($self->{'streamurl'}); | ||
| 657 | if ($response->is_success) { | ||
| 658 | $html = $response->decoded_content; | ||
| 659 | } else { | ||
| 660 | $html = ''; | ||
| 661 | print STDERR $response->status_line if ($DEBUG); | ||
| 662 | } | ||
| 663 | while ($html =~ s#File\d+\=(\S+)##) { | ||
| 664 | $streams{$1} = $weight unless (defined $streams{$1}); | ||
| 665 | print STDERR "-----5: Adding PLS stream ($1) ($weight)!\n" if ($DEBUG); | ||
| 666 | ++$self->{'cnt'}; | ||
| 667 | --$weight; | ||
| 668 | } | ||
| 669 | } | ||
| 670 | else #NON-pls STREAM, WE'LL HAVE A LIST CONTAINING A SINGLE STREAM: | ||
| 671 | { | ||
| 672 | $streams{$self->{'streamurl'}} = $weight unless (defined $streams{$self->{'streamurl'}}); | ||
| 673 | print STDERR "-----6: Adding ".$self->{'streamtype'}." stream (".$self->{'streamurl'}.") ($weight)!\n" if ($DEBUG); | ||
| 674 | ++$self->{'cnt'}; | ||
| 675 | --$weight | ||
| 676 | } | ||
| 677 | } | ||
| 678 | last if ($streamtype eq 'any'); #"any" SHOULD ALWAYS BE THE LAST ONE TO TRY! | ||
| 679 | $weight -= 100; | ||
| 680 | } | ||
| 681 | return undef unless ($self->{'cnt'}); #STEP 2 FAILED - NO PLAYABLE STREAMS FOUND, PUNT! | ||
| 682 | |||
| 683 | #$self->{'streams'} = \@streams; #WE'LL HAVE A LIST OF 'EM TO RANDOMLY CHOOSE ONE FROM: | ||
| 684 | #$self->{'total'} = $self->{'cnt'}; | ||
| 685 | $self->{'total'} = 0; | ||
| 686 | foreach my $s (sort {$streams{$b} <=> $streams{$a}} keys %streams) { | ||
| 687 | unless ($self->{'secure'} && $s !~ /^https/o) { | ||
| 688 | push @{$self->{'streams'}}, $s; | ||
| 689 | print STDERR "++++ ADDING STREAM(".$streams{$s}."): $s\n" if ($DEBUG); | ||
| 690 | ++$self->{'total'}; | ||
| 691 | } | ||
| 692 | } | ||
| 693 | $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : ''; | ||
| 694 | if ($DEBUG) { | ||
| 695 | foreach my $i (sort keys %{$self}) { | ||
| 696 | print STDERR "--KEY=$i= VAL=".$self->{$i}."=\n"; | ||
| 697 | } | ||
| 698 | print STDERR "-SUCCESS: 1st stream=".$self->{'Url'}."=\n"; | ||
| 699 | } | ||
| 700 | |||
| 701 | $self->_log($url); | ||
| 702 | |||
| 703 | bless $self, $class; #BLESS IT! | ||
| 704 | |||
| 705 | return $self; | ||
| 706 | } | ||
| 707 | |||
| 708 | 1 |