| 1 | =head1 NAME | ||
| 2 | |||
| 3 | StreamFinder::Apple - Fetch actual raw streamable URLs from Apple | ||
| 4 | podcasts on podcasts.apple.com | ||
| 5 | |||
| 6 | =head1 AUTHOR | ||
| 7 | |||
| 8 | This module is Copyright (C) 2017-2026 by | ||
| 9 | |||
| 10 | Jim Turner, C<< <turnerjw784 at yahoo.com> >> | ||
| 11 | |||
| 12 | Email: turnerjw784@yahoo.com | ||
| 13 | |||
| 14 | All rights reserved. | ||
| 15 | |||
| 16 | You may distribute this module under the terms of either the GNU General | ||
| 17 | Public License or the Artistic License, as specified in the Perl README | ||
| 18 | file. | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | #!/usr/bin/perl | ||
| 23 | |||
| 24 | use strict; | ||
| 25 | |||
| 26 | use StreamFinder::Apple; | ||
| 27 | |||
| 28 | die "..usage: $0 ID|URL\n" unless ($ARGV[0]); | ||
| 29 | |||
| 30 | my $podcast = new StreamFinder::Apple($ARGV[0]); | ||
| 31 | |||
| 32 | die "Invalid URL or no streams found!\n" unless ($podcast); | ||
| 33 | |||
| 34 | my $firstStream = $podcast->get(); | ||
| 35 | |||
| 36 | print "First Stream URL=$firstStream\n"; | ||
| 37 | |||
| 38 | my $url = $podcast->getURL(); | ||
| 39 | |||
| 40 | print "Stream URL=$url\n"; | ||
| 41 | |||
| 42 | my $podcastTitle = $podcast->getTitle(); | ||
| 43 | |||
| 44 | print "Title=$podcastTitle\n"; | ||
| 45 | |||
| 46 | my $podcastDescription = $podcast->getTitle('desc'); | ||
| 47 | |||
| 48 | print "Description=$podcastDescription\n"; | ||
| 49 | |||
| 50 | my $podcastID = $podcast->getID(); | ||
| 51 | |||
| 52 | print "PODCAST ID=$podcastID\n"; | ||
| 53 | |||
| 54 | my $artist = $podcast->{'artist'}; | ||
| 55 | |||
| 56 | print "Artist=$artist\n" if ($artist); | ||
| 57 | |||
| 58 | my $genre = $podcast->{'genre'}; | ||
| 59 | |||
| 60 | print "Genre=$genre\n" if ($genre); | ||
| 61 | |||
| 62 | my $icon_url = $podcast->getIconURL(); | ||
| 63 | |||
| 64 | if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE: | ||
| 65 | |||
| 66 | my ($image_ext, $icon_image) = $podcast->getIconData(); | ||
| 67 | |||
| 68 | if ($icon_image && open IMGOUT, ">/tmp/${podcastID}.$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 = $podcast->count(); | ||
| 81 | |||
| 82 | print "--Stream count=$stream_count=\n"; | ||
| 83 | |||
| 84 | my @streams = $podcast->get(); | ||
| 85 | |||
| 86 | foreach my $s (@streams) { | ||
| 87 | |||
| 88 | print "------ stream URL=$s=\n"; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | =head1 DESCRIPTION | ||
| 93 | |||
| 94 | StreamFinder::Apple accepts a valid podcast or episode URL on | ||
| 95 | podcasts.apple.com, and returns the actual stream URL(s), title, and cover | ||
| 96 | art icon for that podcast. The purpose is that one needs one of these URLs | ||
| 97 | 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 any / | ||
| 99 | all flash, ads, javascript, cookies, trackers, web-bugs, and other crapware | ||
| 100 | that can come with that method of play. The author uses his own custom | ||
| 101 | all-purpose media player called "fauxdacious" (his custom hacked version of | ||
| 102 | the open-source "audacious" audio player. "fauxdacious" incorporates this | ||
| 103 | module to decode and play podcasts.apple.com streams. | ||
| 104 | |||
| 105 | NOTE: The URL must be either a podcast site, (format: | ||
| 106 | https://podcasts.apple.com/I<country>/podcast/idB<podcast#> | ||
| 107 | (returns stream(s) for the first (latest?) "episode" for that site); OR, a | ||
| 108 | specific podcast episode page site, (format: | ||
| 109 | https://podcasts.apple.com/I<country>/podcast/idB<podcast#>?i=B<episode#> | ||
| 110 | (returns stream(s) for that specific podcast episode). | ||
| 111 | |||
| 112 | Note: Apple no longer returns a playlist of episodes for podcast pages, as | ||
| 113 | the stream urls are no longer in the podcast pages. | ||
| 114 | |||
| 115 | =head1 SUBROUTINES/METHODS | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item B<new>(I<ID>|I<url> [I<-secure> [ => 0|1 ]] [, I<-debug> [ => 0|1|2 ]]) | ||
| 120 | |||
| 121 | Accepts a podcasts.apple.com podcast or episode ID or URL and creates and | ||
| 122 | returns a new podcast object, or I<undef> if the URL is not a valid podcast, | ||
| 123 | album, etc. or no streams are found. The URL can be the full URL, | ||
| 124 | ie. I<https://podcasts.apple.com/podcast/id>B<podcast-id>, | ||
| 125 | I<https://podcasts.apple.com/podcast/id>B<podcast-id>I<?i=>B<episode-id>, | ||
| 126 | or just I<podcast-id>, or I<podcast-id>/I<episode-id>. | ||
| 127 | |||
| 128 | The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>). If 1 | ||
| 129 | then only secure ("https://") streams will be returned. | ||
| 130 | |||
| 131 | DEFAULT I<-secure> is 0 (false) - return all streams (http and https). | ||
| 132 | |||
| 133 | Additional options: | ||
| 134 | |||
| 135 | I<-log> => "I<logfile>" | ||
| 136 | |||
| 137 | Specify path to a log file. If a valid and writable file is specified, A line will be | ||
| 138 | appended to this file every time one or more streams is successfully fetched for a url. | ||
| 139 | |||
| 140 | DEFAULT I<-none-> (no logging). | ||
| 141 | |||
| 142 | I<-logfmt> specifies a format string for lines written to the log file. | ||
| 143 | |||
| 144 | DEFAULT "I<[time] [url] - [site]: [title] ([total])>". | ||
| 145 | |||
| 146 | The valid field I<[variables]> are: [stream]: The url of the first/best stream found. | ||
| 147 | [site]: The site name (Apple). [url]: The url searched for streams. | ||
| 148 | [time]: Perl timestamp when the line was logged. [title], [artist], [album], | ||
| 149 | [description], [year], [genre], [total], [albumartist]: The corresponding field data | ||
| 150 | returned (or "I<-na->", if no value). | ||
| 151 | |||
| 152 | =item $podcast->B<get>() | ||
| 153 | |||
| 154 | Returns an array of strings representing all stream urls found. | ||
| 155 | |||
| 156 | =item $podcast->B<getURL>([I<options>]) | ||
| 157 | |||
| 158 | Similar to B<get>() except it only returns a single stream representing | ||
| 159 | the first valid stream found. | ||
| 160 | |||
| 161 | Current options are: I<"random">, I<"nopls">, and I<"noplaylists">. | ||
| 162 | By default, the first ("best"?) stream is returned. If I<"random"> is | ||
| 163 | specified, then a random one is selected from the list of streams found. | ||
| 164 | If I<"nopls"> is specified, and the stream to be returned is a ".pls" playlist, | ||
| 165 | it is first fetched and the first entry (or a random entry if I<"random"> is | ||
| 166 | specified) is returned. This is needed by Fauxdacious Mediaplayer. | ||
| 167 | If I<"noplaylists"> is specified, and the stream to be returned is a | ||
| 168 | "playlist" (either .pls or .m3u? extension), it is first fetched and the first | ||
| 169 | entry (or a random entry if I<"random"> is specified) in the playlist | ||
| 170 | is returned. | ||
| 171 | |||
| 172 | =item $podcast->B<count>() | ||
| 173 | |||
| 174 | Returns the number of streams found for the podcast. | ||
| 175 | |||
| 176 | =item $podcast->B<getID>() | ||
| 177 | |||
| 178 | Returns the station's Apple ID (numeric). For podcasts and albums, this | ||
| 179 | is a single numeric value. For episodes and songs, it's two numbers | ||
| 180 | separated by a slash ("/"). | ||
| 181 | |||
| 182 | =item $podcast->B<getTitle>(['desc']) | ||
| 183 | |||
| 184 | Returns the podcast's, album's, episode's or song clip's title, | ||
| 185 | or (long description). | ||
| 186 | |||
| 187 | =item $podcast->B<getIconURL>(['artist']) | ||
| 188 | |||
| 189 | Returns the url for the podcast's / album's "cover art" icon image, | ||
| 190 | if any. | ||
| 191 | If B<'artist'> is specified, the channel artist's icon url is returned, | ||
| 192 | if any. Note: This requires StreamFinder::Apple to also fetch the | ||
| 193 | channel artist's (AlbumArtist) url as this icon is currently not included | ||
| 194 | on the podcast's page. | ||
| 195 | |||
| 196 | =item $podcast->B<getIconData>(['artist']) | ||
| 197 | |||
| 198 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 199 | "gif", "jpeg", etc.) and the actual icon image (binary data), if any. | ||
| 200 | If B<'artist'> is specified, the channel artist's icon data is returned, | ||
| 201 | if any. | ||
| 202 | |||
| 203 | =item $podcast->B<getImageURL>() | ||
| 204 | |||
| 205 | Returns the url for the podcast's / album's "cover art" banner image, | ||
| 206 | which for Apple is always the icon image, as Apple does not support | ||
| 207 | a separate banner image at this time. | ||
| 208 | |||
| 209 | =item $podcast->B<getImageData>() | ||
| 210 | |||
| 211 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 212 | "gif", "jpeg", etc.) and the actual station's banner image (binary data). | ||
| 213 | |||
| 214 | =item $podcast->B<getType>() | ||
| 215 | |||
| 216 | Returns the station's type ("Apple"). | ||
| 217 | |||
| 218 | =back | ||
| 219 | |||
| 220 | =head1 CONFIGURATION FILES | ||
| 221 | |||
| 222 | The default root location directory for StreamFinder configuration files | ||
| 223 | is "~/.config/StreamFinder". To use an alternate location directory, | ||
| 224 | specify it in the "I<STREAMFINDER>" environment variable, ie.: | ||
| 225 | B<$ENV{STREAMFINDER} = "/etc/StreamFinder">. | ||
| 226 | |||
| 227 | =over 4 | ||
| 228 | |||
| 229 | =item ~/.config/StreamFinder/Apple/config | ||
| 230 | |||
| 231 | Optional text file for specifying various configuration options | ||
| 232 | for a specific site (submodule). Each option is specified on a | ||
| 233 | separate line in the format below: | ||
| 234 | NOTE: Do not follow the lines with a semicolon, comma, or any other | ||
| 235 | separator. Non-numeric I<values> should be surrounded with quotes, either | ||
| 236 | single or double. Blank lines and lines beginning with a "#" sign as | ||
| 237 | their first non-blank character are ignored as comments. | ||
| 238 | |||
| 239 | 'option' => 'value' [,] | ||
| 240 | |||
| 241 | and the options are loaded into a hash used only by the specific | ||
| 242 | (submodule) specified. Valid options include | ||
| 243 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 244 | Blank lines and lines starting with a "#" sign are ignored. | ||
| 245 | |||
| 246 | Options specified here override any specified in I<~/.config/StreamFinder/config>. | ||
| 247 | |||
| 248 | =item ~/.config/StreamFinder/config | ||
| 249 | |||
| 250 | Optional text file for specifying various configuration options. | ||
| 251 | Each option is specified on a separate line in the format below: | ||
| 252 | |||
| 253 | 'option' => 'value' [,] | ||
| 254 | |||
| 255 | and the options are loaded into a hash used by all sites | ||
| 256 | (submodules) that support them. Valid options include | ||
| 257 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 258 | |||
| 259 | =back | ||
| 260 | |||
| 261 | NOTE: Options specified in the options parameter list of the I<new()> | ||
| 262 | function will override those corresponding options specified in these files. | ||
| 263 | |||
| 264 | =head1 KEYWORDS | ||
| 265 | |||
| 266 | apple podcasts | ||
| 267 | |||
| 268 | =head1 DEPENDENCIES | ||
| 269 | |||
| 270 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent> | ||
| 271 | |||
| 272 | =head1 RECCOMENDS | ||
| 273 | |||
| 274 | wget | ||
| 275 | |||
| 276 | =head1 BUGS | ||
| 277 | |||
| 278 | Please report any bugs or feature requests to C<bug-streamFinder-apple at rt.cpan.org>, or through | ||
| 279 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-Apple>. I will be notified, and then you'll | ||
| 280 | automatically be notified of progress on your bug as I make changes. | ||
| 281 | |||
| 282 | =head1 SUPPORT | ||
| 283 | |||
| 284 | You can find documentation for this module with the perldoc command. | ||
| 285 | |||
| 286 | perldoc StreamFinder::Apple | ||
| 287 | |||
| 288 | You can also look for information at: | ||
| 289 | |||
| 290 | =over 4 | ||
| 291 | |||
| 292 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 293 | |||
| 294 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-Apple> | ||
| 295 | |||
| 296 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 297 | |||
| 298 | L<http://annocpan.org/dist/StreamFinder-Apple> | ||
| 299 | |||
| 300 | =item * Search CPAN | ||
| 301 | |||
| 302 | L<http://search.cpan.org/dist/StreamFinder-Apple/> | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | =head1 LICENSE AND COPYRIGHT | ||
| 307 | |||
| 308 | Copyright 2017-2026 Jim Turner. | ||
| 309 | |||
| 310 | This program is free software; you can redistribute it and/or modify it | ||
| 311 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 312 | copy of the full license at: | ||
| 313 | |||
| 314 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 315 | |||
| 316 | Any use, modification, and distribution of the Standard or Modified | ||
| 317 | Versions is governed by this Artistic License. By using, modifying or | ||
| 318 | distributing the Package, you accept this license. Do not use, modify, | ||
| 319 | or distribute the Package, if you do not accept this license. | ||
| 320 | |||
| 321 | If your Modified Version has been derived from a Modified Version made | ||
| 322 | by someone other than you, you are nevertheless required to ensure that | ||
| 323 | your Modified Version complies with the requirements of this license. | ||
| 324 | |||
| 325 | This license does not grant you the right to use any trademark, service | ||
| 326 | mark, tradename, or logo of the Copyright Holder. | ||
| 327 | |||
| 328 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 329 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 330 | otherwise transfer the Package with respect to any patent claims | ||
| 331 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 332 | Package. If you institute patent litigation (including a cross-claim or | ||
| 333 | counterclaim) against any party alleging that the Package constitutes | ||
| 334 | direct or contributory patent infringement, then this Artistic License | ||
| 335 | to you shall terminate on the date that such litigation is filed. | ||
| 336 | |||
| 337 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 338 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 339 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 340 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 341 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 342 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 343 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 344 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 345 | |||
| 346 | =cut | ||
| 347 | |||
| 348 | package StreamFinder::Apple; | ||
| 349 | |||
| 350 | use strict; | ||
| 351 | use warnings; | ||
| 352 | use URI::Escape; | ||
| 353 | use HTML::Entities (); | ||
| 354 | use LWP::UserAgent (); | ||
| 355 | use parent 'StreamFinder::_Class'; | ||
| 356 | |||
| 357 | my $DEBUG = 0; | ||
| 358 | |||
| 359 | sub new | ||
| 360 | { | ||
| 361 | my $class = shift; | ||
| 362 | my $url = shift; | ||
| 363 | |||
| 364 | return undef unless ($url); | ||
| 365 | |||
| 366 | my $self = $class->SUPER::new('Apple', @_); | ||
| 367 | $DEBUG = $self->{'debug'} if (defined $self->{'debug'}); | ||
| 368 | |||
| 369 | $self->{'id'} = ''; | ||
| 370 | (my $url2fetch = $url); | ||
| 371 | if ($url2fetch =~ m#^https?\:\/\/(?:embed\.)?podcasts\.apple\.#) { | ||
| 372 | #EXAMPLE1:my $url = 'https://podcasts.apple.com/us/podcast/wnbc-sec-shorts-josh-snead/id1440412195?i=1000448441439'; | ||
| 373 | #EXAMPLE2:my $url = 'https://podcasts.apple.com/us/podcast/good-bull-hunting-for-texas-a-m-fans/id1440412195'; | ||
| 374 | $self->{'id'} = ($url =~ m#\/(?:id)?(\d\d\d\d\d+)(?:\?i\=(\d+))?\/?#) ? $1 : ''; | ||
| 375 | $self->{'id'} .= '/'. $2 if (defined $2); | ||
| 376 | } elsif ($url2fetch !~ m#^https?\:\/\/#) { | ||
| 377 | my ($id, $podcastid) = split(m#\/#, $url2fetch); | ||
| 378 | $self->{'id'} = $id; | ||
| 379 | $url2fetch = 'https://podcasts.apple.com/podcast/id' . $id; | ||
| 380 | $url2fetch .= '?i=' . $podcastid if ($podcastid); | ||
| 381 | } | ||
| 382 | |||
| 383 | print STDERR "--URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 384 | return undef unless ($self->{'id'}); | ||
| 385 | |||
| 386 | my $html = ''; | ||
| 387 | print STDERR "-0(Apple): ID=".$self->{'id'}."= AGENT=".join('|',@{$self->{'_userAgentOps'}})."=\n" if ($DEBUG); | ||
| 388 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 389 | $ua->timeout($self->{'timeout'}); | ||
| 390 | $ua->cookie_jar({}); | ||
| 391 | $ua->env_proxy; | ||
| 392 | my $response; | ||
| 393 | # $self->{'albumartist'} = $url2fetch; | ||
| 394 | |||
| 395 | if ($self->{'id'} !~ m#\/#) { #PAGE (multiple episodes): | ||
| 396 | print STDERR "i:FETCHING PAGE URL ($url2fetch)...\n" if ($DEBUG); | ||
| 397 | $response = $ua->get($url2fetch); | ||
| 398 | if ($response->is_success) { | ||
| 399 | $html = $response->decoded_content; | ||
| 400 | } else { | ||
| 401 | print STDERR $response->status_line if ($DEBUG); | ||
| 402 | my $no_wget = system('wget','-V'); | ||
| 403 | unless ($no_wget) { | ||
| 404 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 405 | $html = `wget -t 2 -T 20 -O- -o /dev/null "$url2fetch" 2>/dev/null `; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 409 | |||
| 410 | return undef unless ($html); | ||
| 411 | |||
| 412 | if ($url2fetch =~ s#\/\/embed.podcast#\/\/podcast#) { #HANDLE "EMBEDDED PODCAST URLS: | ||
| 413 | print STDERR "--2a: EMBEDDED PODCAST, take 5, then fetch podcast page ($url2fetch)...\n" if ($DEBUG); | ||
| 414 | sleep 5; #AVOID HITTING 'EM TOO QUICK IN SUCCESSION (AVOID DOS SUSPICION): | ||
| 415 | $response = $ua->get($url2fetch); | ||
| 416 | if ($response->is_success) { #JETCH PODCAST PAGE: | ||
| 417 | $html = $response->decoded_content; | ||
| 418 | } else { | ||
| 419 | print STDERR $response->status_line if ($DEBUG); | ||
| 420 | my $no_wget = system('wget','-V'); | ||
| 421 | unless ($no_wget) { | ||
| 422 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 423 | $html = `wget -t 2 -T 20 -O- -o /dev/null "$url2fetch" 2>/dev/null `; | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 428 | return undef unless ($html); | ||
| 429 | |||
| 430 | if ($html =~ m#${url2fetch}\?i\=(\d+)#s) { | ||
| 431 | $self->{'id'} = $1; | ||
| 432 | $url2fetch .= '?i=' . $1; | ||
| 433 | print STDERR "--3: EMBEDDED EPISODE FOUND (id=$1): URL=$url2fetch)!\n" if ($DEBUG); | ||
| 434 | } else { | ||
| 435 | print STDERR "f:Could not find embedded episode in ($url2fetch), aborting!\n"; | ||
| 436 | return undef; | ||
| 437 | } | ||
| 438 | } else { | ||
| 439 | $url2fetch = ($html =~ m#\,\"uploadDate\"\:\"[^\"]+\"\,\"url\"\:\"([^\"]+)#) | ||
| 440 | ? $1 : ''; | ||
| 441 | return undef unless ($url2fetch); | ||
| 442 | $self->{'id'} = ($url2fetch =~ m#\/(?:id)?(\d\d\d\d\d+)(?:\?i\=(\d+))?\/?#) ? $1 : ''; | ||
| 443 | $self->{'id'} .= '/'. $2 if (defined $2); | ||
| 444 | print "--FETCH EPISODE ID=$$self{'id'}= URL=$url2fetch=\n" if ($DEBUG); | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | #FETCH EPISODE: | ||
| 449 | |||
| 450 | print STDERR "i:FETCHING EPISODE URL ($url2fetch)...\n" if ($DEBUG); | ||
| 451 | $response = $ua->get($url2fetch); | ||
| 452 | if ($response->is_success) { | ||
| 453 | $html = $response->decoded_content; | ||
| 454 | } else { | ||
| 455 | print STDERR $response->status_line if ($DEBUG); | ||
| 456 | my $no_wget = system('wget','-V'); | ||
| 457 | unless ($no_wget) { | ||
| 458 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 459 | $html = `wget -t 2 -T 20 -O- -o /dev/null "$url2fetch" 2>/dev/null `; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | |||
| 463 | print STDERR "-2: html=$html=\n" if ($DEBUG > 1); | ||
| 464 | return undef unless ($html); | ||
| 465 | |||
| 466 | $self->{'iconurl'} = ($html =~ /\bsrcset\=\"([^\"\s]+)/s) ? $1 : ''; | ||
| 467 | $self->{'iconurl'} = ($html =~ m#\"thumbnailUrl\"\:\"([^\"]+)#s) ? $1 : '' | ||
| 468 | if (!$self->{'iconurl'} || $self->{'iconurl'} !~ /^http/); | ||
| 469 | $self->{'imageurl'} = $1 if ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:image\:secure\_url\"\s+content\=\"([^\"\s]+)#s); | ||
| 470 | $self->{'imageurl'} ||= $1 if ($html =~ m#\<meta\s+(?:property|name)\=\"(?:og|twitter)\:image\"\s+content\=\"([^\"\s]+)#s); | ||
| 471 | $self->{'imageurl'} = $self->{'iconurl'} | ||
| 472 | if (!$self->{'imageurl'} || $self->{'imageurl'} !~ /^http/); | ||
| 473 | $self->{'iconurl'} ||= $self->{'imageurl'}; | ||
| 474 | if ($html =~ m#\:\{\"\@type\"\:\"CreativeWorkSeries\"([^\}]+)#s) { | ||
| 475 | my $artistdata = $1; | ||
| 476 | $self->{'artist'} = $1 if ($artistdata =~ m#\"name\"\:\"([^\"]+)\"#s); | ||
| 477 | $self->{'albumartist'} = $1 if ($artistdata =~ m#\"url\"\:\"([^\"]+)\"#s); | ||
| 478 | } | ||
| 479 | if ($html =~ m#\<h1(.+?)\<\/h1\>#si) { | ||
| 480 | my $titlestuff = $1; | ||
| 481 | if ($titlestuff =~ m#\s+aria\-label\=\"([^\"]+)#s) { | ||
| 482 | $self->{'title'} = $1; | ||
| 483 | } elsif ($titlestuff =~ m#\>(.+?)\<\/span\>#s) { | ||
| 484 | $self->{'title'} = $1; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | $self->{'title'} ||= $1 if ($html =~ s#\"(?:name|itunesTitle)\\?\"\:\\?\"(.+?)\\?\"\,##so); | ||
| 488 | $self->{'title'} =~ s#\\##g; | ||
| 489 | $self->{'title'} =~ s#\<[^\>]+\>##g; | ||
| 490 | $self->{'description'} = $1 | ||
| 491 | if ($html =~ m#\>\<\!\-\- HTML\_TAG\_START \-\-\>(.+?)\<\!\-\- HTML\_TAG\_END \-\-\>#s); | ||
| 492 | #JWT:I DO NOT FULLY TRUST THE REGEX JUST ABOVE!: | ||
| 493 | my $shorterDesc = ($html =~ m#\"description\"\:\"([^\\\"]+)#s) ? $1 : ''; | ||
| 494 | $shorterDesc ||= $1 if ($html =~ m#\<meta\s+name\=\"description\"\s+content\=\"([^\"]+)\"#s); | ||
| 495 | if ($shorterDesc && $shorterDesc =~ /\w\w+/) { | ||
| 496 | $shorterDesc =~ s/^\s+//s; | ||
| 497 | $self->{'description'} = $shorterDesc | ||
| 498 | if (length($shorterDesc) > length($self->{'description'})); | ||
| 499 | } | ||
| 500 | $self->{'created'} = $1 if ($html =~ m#\"datePublished\"\:\"([^\"]+)#s); | ||
| 501 | my %streamHash = (); | ||
| 502 | while ($html =~ s#\"streamUrl\"\:\"([^\\\"]+)##s) { | ||
| 503 | my $stream = $1; | ||
| 504 | $stream =~ s#www.podtrac.com/pts/redirect.mp3/pdst.fm/e/pscrb.fm/rss/p/claritaspod.com/measure/traffic(.megaphone.fm\/[A-Z0-9]+\.mp3).*$#dcs-spotify$1#; | ||
| 505 | $streamHash{$stream} = 1; | ||
| 506 | } | ||
| 507 | foreach my $s (keys %streamHash) { | ||
| 508 | print STDERR "--FOUND STREAM=$s=\n" if ($DEBUG); | ||
| 509 | push (@{$self->{'streams'}}, $s) unless ($self->{'secure'} && $s !~ /^https/o); | ||
| 510 | } | ||
| 511 | $self->{'year'} = $1 if ($self->{'created'} =~ /(\d\d\d\d)/); | ||
| 512 | if ($html =~ m#\,\"genre\"\:\[([^\]]+)#s) { | ||
| 513 | my $genredata = $1; | ||
| 514 | while ($genredata =~ s#\"([^\"]+)\"##) { | ||
| 515 | $self->{'genre'} .= "$1, "; | ||
| 516 | } | ||
| 517 | $self->{'genre'} =~ s/\,\s+$// if ($self->{'genre'}); | ||
| 518 | } | ||
| 519 | $self->{'genre'} ||= 'Podcast'; | ||
| 520 | if ($self->{'description'} =~ /\w/) { | ||
| 521 | $self->{'description'} =~ s/\s+$//; | ||
| 522 | $self->{'description'} =~ s/^\s+//; | ||
| 523 | } else { | ||
| 524 | $self->{'description'} = $self->{'title'}; | ||
| 525 | } | ||
| 526 | foreach my $i (qw(title artist album description genre)) { | ||
| 527 | $self->{$i} = HTML::Entities::decode_entities($self->{$i}); | ||
| 528 | $self->{$i} = uri_unescape($self->{$i}); | ||
| 529 | $self->{$i} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs; | ||
| 530 | } | ||
| 531 | $self->{'cnt'} = scalar @{$self->{'streams'}}; | ||
| 532 | $self->{'total'} = $self->{'cnt'}; | ||
| 533 | |||
| 534 | if ($self->{'cnt'} > 0) { #MANY APPLE PODCASTS DO NOT PROVIDE LENGTH IN STREAMS, SO CALCULATE IT!: | ||
| 535 | if ($html =~ m#Length\<\/div\>\s+\<div[^\>]+\>([^\<]+)#s) { | ||
| 536 | my $lengthdata = $1; | ||
| 537 | my $len = 0; | ||
| 538 | $len += ($1 * 3600) if ($lengthdata =~ /(\d+)h/i); | ||
| 539 | $len += ($1 * 60) if ($lengthdata =~ /(\d+)m/i); | ||
| 540 | $len += $1 if ($lengthdata =~ /(\d+)s/); | ||
| 541 | $len *= 1000; #NEED IT IN MILLISECS! | ||
| 542 | $self->{'length'} = $len; | ||
| 543 | } | ||
| 544 | $self->{'Url'} = $self->{'streams'}->[0]; | ||
| 545 | $self->{'playlist_cnt'} = 1; | ||
| 546 | $self->{'playlist'} = "#EXTM3U\n"; | ||
| 547 | $self->{'playlist'} .= "#EXTINF:-1, " . $self->{'title'} . "\n"; | ||
| 548 | $self->{'playlist'} .= "#EXTART:" . $self->{'artist'} . "\n" | ||
| 549 | if ($self->{'artist'}); | ||
| 550 | $self->{'playlist'} .= "#EXTALB:" . $self->{'albumartist'} . "\n" | ||
| 551 | if ($self->{'albumartist'}); | ||
| 552 | $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n" | ||
| 553 | if ($self->{'genre'}); | ||
| 554 | $self->{'playlist'} .= $self->{'Url'} . "\n"; | ||
| 555 | } | ||
| 556 | if ($DEBUG) { | ||
| 557 | foreach my $f (sort keys %{$self}) { | ||
| 558 | print "--KEY=$f= VAL=$$self{$f}=\n"; | ||
| 559 | } | ||
| 560 | print STDERR "-SUCCESS: 1st stream=".$self->{'Url'}."=\n" if ($self->{'cnt'} > 0); | ||
| 561 | } | ||
| 562 | |||
| 563 | $self->_log($url); | ||
| 564 | |||
| 565 | bless $self, $class; #BLESS IT! | ||
| 566 | |||
| 567 | return $self; | ||
| 568 | } | ||
| 569 | |||
| 570 | sub getIconURL | ||
| 571 | { | ||
| 572 | my $self = shift; | ||
| 573 | return $self->{'iconurl'} unless (defined($_[0]) && $_[0] =~ /^\-?artist/i); | ||
| 574 | |||
| 575 | unless ($self->{'articonurl'}) { | ||
| 576 | my $html = ''; | ||
| 577 | return '' unless ($self->{'albumartist'} && $self->{'albumartist'} =~ m#^https?\:\/\/#); | ||
| 578 | |||
| 579 | my $url2fetch = $self->{'albumartist'}; | ||
| 580 | print STDERR "-0(Fetch Apple Channel for alt. icon from $url2fetch): \n" if ($DEBUG); | ||
| 581 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 582 | $ua->timeout($self->{'timeout'}); | ||
| 583 | $ua->cookie_jar({}); | ||
| 584 | $ua->env_proxy; | ||
| 585 | my $response = $ua->get($url2fetch); | ||
| 586 | $html = $response->decoded_content if ($response->is_success); | ||
| 587 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 588 | return '' unless ($html); | ||
| 589 | |||
| 590 | $self->{'articonurl'} = ($html =~ /\bsrcset\=\"([^\"\s]+)/s) ? $1 : ''; | ||
| 591 | $self->{'artimageurl'} = $1 if ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:image\:secure\_url\"\s+content\=\"([^\"\s]+)#s); | ||
| 592 | $self->{'artimageurl'} ||= $1 if ($html =~ m#\<meta\s+(?:property|name)\=\"(?:og|twitter)\:image\"\s+content\=\"([^\"\s]+)#s); | ||
| 593 | $self->{'artimageurl'} = ($html =~ m#\"thumbnailUrl\"\:\"([^\"]+)#s) ? $1 : '' | ||
| 594 | if (!$self->{'artimageurl'} || $self->{'artimageurl'} !~ /^http/); | ||
| 595 | $self->{'articonurl'} ||= $self->{'artimageurl'}; | ||
| 596 | $self->{'articonurl'} = $self->{'artimageurl'} | ||
| 597 | if (!$self->{'articonurl'} || $self->{'articonurl'} !~ /^http/); | ||
| 598 | $self->{'artimageurl'} ||= $self->{'articonurl'}; | ||
| 599 | print STDERR "--ART ICON URL=".$self->{'articonurl'}."=\n" if ($DEBUG); | ||
| 600 | } | ||
| 601 | return $self->{'articonurl'}; | ||
| 602 | } | ||
| 603 | |||
| 604 | sub getImageURL | ||
| 605 | { | ||
| 606 | my $self = shift; | ||
| 607 | return $self->{'imageurl'} unless (defined($_[0]) && $_[0] =~ /^\-?artist/i); | ||
| 608 | |||
| 609 | $self->getIconURL(@_) unless ($self->{'articonurl'}); | ||
| 610 | return $self->{'artimageurl'}; | ||
| 611 | } | ||
| 612 | |||
| 613 | 1 |