| 1 | =head1 NAME | ||
| 2 | |||
| 3 | StreamFinder::Spreaker - Fetch actual raw streamable URLs on widget.spreaker.com | ||
| 4 | |||
| 5 | =head1 AUTHOR | ||
| 6 | |||
| 7 | This module is Copyright (C) 2021-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::Spreaker; | ||
| 26 | |||
| 27 | die "..usage: $0 ID|URL\n" unless ($ARGV[0]); | ||
| 28 | |||
| 29 | my $podcast = new StreamFinder::Spreaker($ARGV[0]); | ||
| 30 | |||
| 31 | die "Invalid URL or no streams found!\n" unless ($podcast); | ||
| 32 | |||
| 33 | my $firstStream = $podcast->get(); | ||
| 34 | |||
| 35 | print "First Stream URL=$firstStream\n"; | ||
| 36 | |||
| 37 | my $url = $podcast->getURL(); | ||
| 38 | |||
| 39 | print "Stream URL=$url\n"; | ||
| 40 | |||
| 41 | my $podcastTitle = $podcast->getTitle(); | ||
| 42 | |||
| 43 | print "Title=$podcastTitle\n"; | ||
| 44 | |||
| 45 | my $podcastDescription = $podcast->getTitle('desc'); | ||
| 46 | |||
| 47 | print "Description=$podcastDescription\n"; | ||
| 48 | |||
| 49 | my $podcastID = $podcast->getID(); | ||
| 50 | |||
| 51 | print "Podcast ID=$podcastID\n"; | ||
| 52 | |||
| 53 | my $icon_url = $podcast->getIconURL(); | ||
| 54 | |||
| 55 | if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE: | ||
| 56 | |||
| 57 | print "Icon URL=$icon_url=\n"; | ||
| 58 | |||
| 59 | my ($image_ext, $icon_image) = $podcast->getIconData(); | ||
| 60 | |||
| 61 | if ($icon_image && open IMGOUT, ">/tmp/${podcastID}.$image_ext") { | ||
| 62 | |||
| 63 | binmode IMGOUT; | ||
| 64 | |||
| 65 | print IMGOUT $icon_image; | ||
| 66 | |||
| 67 | close IMGOUT; | ||
| 68 | |||
| 69 | print "...Icon image downloaded to (/tmp/${podcastID}.$image_ext)\n"; | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | my $stream_count = $podcast->count(); | ||
| 76 | |||
| 77 | print "--Stream count=$stream_count=\n"; | ||
| 78 | |||
| 79 | my @streams = $podcast->get(); | ||
| 80 | |||
| 81 | foreach my $s (@streams) { | ||
| 82 | |||
| 83 | print "------ stream URL=$s=\n"; | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | =head1 DESCRIPTION | ||
| 88 | |||
| 89 | StreamFinder::Spreaker accepts a valid podcast ID or URL on | ||
| 90 | Spreaker.com and returns the actual stream URL(s), title, and cover art icon. | ||
| 91 | The purpose is that one needs one of these URLs in order to have the option to | ||
| 92 | stream the podcast in one's own choice of media player software rather than | ||
| 93 | using their web browser and accepting any / all flash, ads, javascript, | ||
| 94 | cookies, trackers, web-bugs, and other crapware that can come with that method | ||
| 95 | of play. The author uses his own custom all-purpose media player called | ||
| 96 | "fauxdacious" (his custom hacked version of the open-source "audacious" | ||
| 97 | audio player). "fauxdacious" can incorporate this module to decode and play | ||
| 98 | Spreaker.com streams. | ||
| 99 | |||
| 100 | One or more stream URLs can be returned for each podcast. | ||
| 101 | |||
| 102 | =head1 SUBROUTINES/METHODS | ||
| 103 | |||
| 104 | =over 4 | ||
| 105 | |||
| 106 | =item B<new>(I<ID>|I<url> [, I<-secure> [ => 0|1 ]] [, I<-debug> [ => 0|1|2 ]]) | ||
| 107 | |||
| 108 | Accepts a spreaker.com podcast ID or URL and creates and returns a new podcast | ||
| 109 | object, or I<undef> if the URL is not a valid podcast, or no streams | ||
| 110 | are found. The URL can be the full URL, ie. | ||
| 111 | I<https://widget.spreaker.com/player?episode_id=>B<episode-id>, | ||
| 112 | I<https://www.spreaker.com/user/B<user-id>/>B<podcast-id-string>, or just | ||
| 113 | I<podcast-id>. Due to multiple Spreaker URL combinations, it is advised to | ||
| 114 | provide a full Spreaker URL, if possible. | ||
| 115 | |||
| 116 | The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>). | ||
| 117 | If 1 then only secure ("https://") streams will be returned. | ||
| 118 | |||
| 119 | DEFAULT I<-secure> is 0 (false) - return all streams (http and https). | ||
| 120 | |||
| 121 | Additional options: | ||
| 122 | |||
| 123 | I<-log> => "I<logfile>" | ||
| 124 | |||
| 125 | Specify path to a log file. If a valid and writable file is specified, A line | ||
| 126 | will be appended to this file every time one or more streams is successfully | ||
| 127 | fetched for a url. | ||
| 128 | |||
| 129 | DEFAULT I<-none-> (no logging). | ||
| 130 | |||
| 131 | I<-logfmt> specifies a format string for lines written to the log file. | ||
| 132 | |||
| 133 | DEFAULT "I<[time] [url] - [site]: [title] ([total])>". | ||
| 134 | |||
| 135 | The valid field I<[variables]> are: [stream]: The url of the first/best | ||
| 136 | stream found. [site]: The site name (Spreaker). [url]: The url searched | ||
| 137 | for streams. [time]: Perl timestamp when the line was logged. [title], | ||
| 138 | [artist], [album], [description], [year], [genre], [total], [albumartist]: | ||
| 139 | The corresponding field data returned (or "I<-na->", if no value). | ||
| 140 | |||
| 141 | =item $podcast->B<get>(['playlist']) | ||
| 142 | |||
| 143 | Returns an array of strings representing all stream URLs found. | ||
| 144 | If I<"playlist"> is specified, then an extended m3u playlist is returned | ||
| 145 | instead of stream url. NOTE: If an author / channel page url is given, | ||
| 146 | rather than an individual podcast episode's url, get() returns the first | ||
| 147 | (latest?) podcast episode found, and get("playlist") returns an extended | ||
| 148 | m3u playlist containing the urls, titles, etc. for all the podcast | ||
| 149 | episodes found on that page url. | ||
| 150 | |||
| 151 | =item $podcast->B<getURL>([I<options>]) | ||
| 152 | |||
| 153 | Similar to B<get>() except it only returns a single stream representing | ||
| 154 | the first valid stream found. There currently are no valid I<options>. | ||
| 155 | |||
| 156 | =item $podcast->B<count>(['playlist']) | ||
| 157 | |||
| 158 | Returns the number of streams found for the podcast. | ||
| 159 | If I<"playlist"> is specified, the number of episodes returned in the | ||
| 160 | playlist is returned (the playlist can have more than one item if a | ||
| 161 | podcast page URL is specified). | ||
| 162 | |||
| 163 | =item $podcast->B<getID>() | ||
| 164 | |||
| 165 | Returns the podcast's Spreaker ID (default). For podcasts, the Spreaker ID | ||
| 166 | is a single value. For individual podcast episodes it's two values | ||
| 167 | separated by a slash ("/"). | ||
| 168 | |||
| 169 | =item $podcast->B<getTitle>(['desc']) | ||
| 170 | |||
| 171 | Returns the podcast's title, or (long description). Podcasts | ||
| 172 | on Spreaker can have separate descriptions, but for podcasts, | ||
| 173 | it is always the podcast's title. | ||
| 174 | |||
| 175 | =item $podcast->B<getIconURL>(['artist']) | ||
| 176 | |||
| 177 | Returns the URL for the podcast's "cover art" icon image, if any. | ||
| 178 | If B<'artist'> is specified, the channel artist's icon url is returned, | ||
| 179 | if any. | ||
| 180 | |||
| 181 | =item $podcast->B<getIconData>(['artist']) | ||
| 182 | |||
| 183 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 184 | "gif", "jpeg", etc.) and the actual icon image (binary data), if any. | ||
| 185 | If B<'artist'> is specified, the channel artist's icon data is returned, | ||
| 186 | if any. | ||
| 187 | |||
| 188 | =item $podcast->B<getImageURL>() | ||
| 189 | |||
| 190 | Returns the URL for the podcast's "cover art" (usually larger) | ||
| 191 | banner image. | ||
| 192 | |||
| 193 | =item $podcast->B<getImageData>() | ||
| 194 | |||
| 195 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 196 | "gif", "jpeg", etc.) and the actual podcast's banner image (binary data). | ||
| 197 | |||
| 198 | =item $podcast->B<getType>() | ||
| 199 | |||
| 200 | Returns the podcast's type ("Spreaker"). | ||
| 201 | |||
| 202 | =back | ||
| 203 | |||
| 204 | =head1 CONFIGURATION FILES | ||
| 205 | |||
| 206 | The default root location directory for StreamFinder configuration files | ||
| 207 | is "~/.config/StreamFinder". To use an alternate location directory, | ||
| 208 | specify it in the "I<STREAMFINDER>" environment variable, ie.: | ||
| 209 | B<$ENV{STREAMFINDER} = "/etc/StreamFinder">. | ||
| 210 | |||
| 211 | =over 4 | ||
| 212 | |||
| 213 | =item ~/.config/StreamFinder/Spreaker/config | ||
| 214 | |||
| 215 | Optional text file for specifying various configuration options | ||
| 216 | for a specific site (submodule). Each option is specified on a | ||
| 217 | separate line in the format below: | ||
| 218 | NOTE: Do not follow the lines with a semicolon, comma, or any other | ||
| 219 | separator. Non-numeric I<values> should be surrounded with quotes, either | ||
| 220 | single or double. Blank lines and lines beginning with a "#" sign as | ||
| 221 | their first non-blank character are ignored as comments. | ||
| 222 | |||
| 223 | 'option' => 'value' [,] | ||
| 224 | |||
| 225 | and the options are loaded into a hash used only by the specific | ||
| 226 | (submodule) specified. Valid options include | ||
| 227 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 228 | |||
| 229 | Options specified here override any specified in | ||
| 230 | I<~/.config/StreamFinder/config>. | ||
| 231 | |||
| 232 | =item ~/.config/StreamFinder/config | ||
| 233 | |||
| 234 | Optional text file for specifying various configuration options. | ||
| 235 | Each option is specified on a separate line in the format below: | ||
| 236 | |||
| 237 | 'option' => 'value' [,] | ||
| 238 | |||
| 239 | and the options are loaded into a hash used by all sites | ||
| 240 | (submodules) that support them. Valid options include | ||
| 241 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 242 | |||
| 243 | =back | ||
| 244 | |||
| 245 | NOTE: Options specified in the options parameter list of the I<new()> | ||
| 246 | function will override those corresponding options specified in these files. | ||
| 247 | |||
| 248 | =head1 KEYWORDS | ||
| 249 | |||
| 250 | spreaker | ||
| 251 | |||
| 252 | =head1 DEPENDENCIES | ||
| 253 | |||
| 254 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent> | ||
| 255 | |||
| 256 | =head1 RECCOMENDS | ||
| 257 | |||
| 258 | =head1 BUGS | ||
| 259 | |||
| 260 | Please report any bugs or feature requests to C<bug-streamFinder-iheartradio at rt.cpan.org>, or through | ||
| 261 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-Spreaker>. I will be notified, and then you'll | ||
| 262 | automatically be notified of progress on your bug as I make changes. | ||
| 263 | |||
| 264 | =head1 SUPPORT | ||
| 265 | |||
| 266 | You can find documentation for this module with the perldoc command. | ||
| 267 | |||
| 268 | perldoc StreamFinder::Spreaker | ||
| 269 | |||
| 270 | You can also look for information at: | ||
| 271 | |||
| 272 | =over 4 | ||
| 273 | |||
| 274 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 275 | |||
| 276 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-Spreaker> | ||
| 277 | |||
| 278 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 279 | |||
| 280 | L<http://annocpan.org/dist/StreamFinder-Spreaker> | ||
| 281 | |||
| 282 | =item * Search CPAN | ||
| 283 | |||
| 284 | L<http://search.cpan.org/dist/StreamFinder-Spreaker/> | ||
| 285 | |||
| 286 | =back | ||
| 287 | |||
| 288 | =head1 LICENSE AND COPYRIGHT | ||
| 289 | |||
| 290 | Copyright 2021-2026 Jim Turner. | ||
| 291 | |||
| 292 | This program is free software; you can redistribute it and/or modify it | ||
| 293 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 294 | copy of the full license at: | ||
| 295 | |||
| 296 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 297 | |||
| 298 | Any use, modification, and distribution of the Standard or Modified | ||
| 299 | Versions is governed by this Artistic License. By using, modifying or | ||
| 300 | distributing the Package, you accept this license. Do not use, modify, | ||
| 301 | or distribute the Package, if you do not accept this license. | ||
| 302 | |||
| 303 | If your Modified Version has been derived from a Modified Version made | ||
| 304 | by someone other than you, you are nevertheless required to ensure that | ||
| 305 | your Modified Version complies with the requirements of this license. | ||
| 306 | |||
| 307 | This license does not grant you the right to use any trademark, service | ||
| 308 | mark, tradename, or logo of the Copyright Holder. | ||
| 309 | |||
| 310 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 311 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 312 | otherwise transfer the Package with respect to any patent claims | ||
| 313 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 314 | Package. If you institute patent litigation (including a cross-claim or | ||
| 315 | counterclaim) against any party alleging that the Package constitutes | ||
| 316 | direct or contributory patent infringement, then this Artistic License | ||
| 317 | to you shall terminate on the date that such litigation is filed. | ||
| 318 | |||
| 319 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 320 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 321 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 322 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 323 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 324 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 325 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 326 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 327 | |||
| 328 | =cut | ||
| 329 | |||
| 330 | package StreamFinder::Spreaker; | ||
| 331 | |||
| 332 | use strict; | ||
| 333 | use warnings; | ||
| 334 | use URI::Escape; | ||
| 335 | use HTML::Entities (); | ||
| 336 | use LWP::UserAgent (); | ||
| 337 | use parent 'StreamFinder::_Class'; | ||
| 338 | |||
| 339 | my $DEBUG = 0; | ||
| 340 | |||
| 341 | sub new | ||
| 342 | { | ||
| 343 | my $class = shift; | ||
| 344 | my $url = shift; | ||
| 345 | |||
| 346 | return undef unless ($url); | ||
| 347 | |||
| 348 | my $self = $class->SUPER::new('Spreaker', @_); | ||
| 349 | $DEBUG = $self->{'debug'} if (defined $self->{'debug'}); | ||
| 350 | $self->{'id'} = ''; | ||
| 351 | $self->{'_podcast_id'} = ''; | ||
| 352 | (my $url2fetch = $url); | ||
| 353 | my $tried = 0; | ||
| 354 | my @epiTitles = (); | ||
| 355 | my @epiStreams = (); | ||
| 356 | my $episodeID = ''; | ||
| 357 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 358 | $ua->timeout($self->{'timeout'}); | ||
| 359 | $ua->cookie_jar({}); | ||
| 360 | $ua->env_proxy; | ||
| 361 | my $html = ''; | ||
| 362 | my $response; | ||
| 363 | |||
| 364 | if ($url2fetch =~ m#^(https?\:\/\/([^\/]+))#) { | ||
| 365 | if ($url2fetch =~ m#\?\w+?\_id\=([\d]+)#) { | ||
| 366 | $self->{'id'} = $1; | ||
| 367 | } else { #WE'RE A FULL URL, IE. "https://www.spreaker.com/user/<user-id>/<podcast-id-string>" | ||
| 368 | #OR https://www.spreaker.com/show/<podcast-id> | ||
| 369 | $self->{'id'} = ($url2fetch =~ m#([^\/]+)$#) ? $1 : ''; | ||
| 370 | # $self->{'id'} =~ s/^[^\d]+(\d+)$/$1/; | ||
| 371 | } | ||
| 372 | } else { | ||
| 373 | $self->{'id'} = $url2fetch; | ||
| 374 | } | ||
| 375 | if ($self->{'id'} =~ m#\/#) { | ||
| 376 | $self->{'_podcast_id'} = $1 if ($self->{'id'} =~ m#^(\d+)\/#); | ||
| 377 | $self->{'id'} =~ s#^.*\/##; | ||
| 378 | $episodeID = $self->{'id'}; | ||
| 379 | } else { | ||
| 380 | # $episodeID = ($self->{'id'} =~ /(\d\d\d\d\d\d\d\d)$/) ? $1 : $self->{'id'} | ||
| 381 | $episodeID = ($self->{'id'} =~ /(\d\d\d\d\d\d\d\d)$/) ? $1 : ''; | ||
| 382 | } | ||
| 383 | if ($episodeID) { | ||
| 384 | $url2fetch = "https://api.spreaker.com/episode/$episodeID"; | ||
| 385 | } elsif ($self->{'id'} =~ /^\d+$/) { | ||
| 386 | if (length($self->{'id'}) < 8) { | ||
| 387 | $url2fetch = "https://www.spreaker.com/show/$self->{'id'}/episodes/feed"; | ||
| 388 | } else { | ||
| 389 | $url2fetch = "https://api.spreaker.com/episode/$self->{'id'}"; | ||
| 390 | $episodeID = $self->{'id'}; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | $html = ''; | ||
| 395 | print STDERR "-0(Spreaker): ($tried) FETCHING URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 396 | print STDERR "-!!!-ID=$$self{'id'}= episodeID=$episodeID= fetch=$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 | } | ||
| 403 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 404 | return undef unless ($html && $self->{'id'}); #STEP 1 FAILED, INVALID PODCAST URL, PUNT! | ||
| 405 | |||
| 406 | $self->{'genre'} = 'Podcast'; | ||
| 407 | $self->{'albumartist'} = $url2fetch; | ||
| 408 | print STDERR "---ID=".$self->{'id'}."= tried=$tried=\n" if ($DEBUG); | ||
| 409 | if ($self->{'id'} =~ /^\d+$/) { #NUMERIC ID: ** DEPRECIATED? ** | ||
| 410 | if (length($self->{'id'}) < 8) { #PODCAST PAGE ID (FETCH XML PAGE): | ||
| 411 | print STDERR "-----WE'RE AN XML PODCAST PAGE!\n" if ($DEBUG); | ||
| 412 | my $ep1id = ''; | ||
| 413 | #WE NEED TO EXTRACT 1ST EPISODE ID, BUT WHILST AT IT, GO AHEAD AND FETCH PLAYLIST DATA HERE TOO!: | ||
| 414 | while ($html =~ s#\<item\>(.+?)\<\/item\>##si) { | ||
| 415 | my $item = $1; | ||
| 416 | if ($item =~ s#\<enclosure\s+url\=\"([^\"]+)##si) { | ||
| 417 | my $stream = $1; | ||
| 418 | next if ($self->{'secure'} && $stream !~ /^https/o); | ||
| 419 | if ($item =~ s#\<title\>([^\<]+)\<\/title\>##si) { | ||
| 420 | my $title = $1; | ||
| 421 | push @epiStreams, $stream; | ||
| 422 | push @epiTitles, $title; | ||
| 423 | $ep1id ||= $1 if ($stream =~ m#episode\/(\d\d\d\d\d\d\d\d)\b#o); #EXTRACT 1ST EPISODE ID! | ||
| 424 | } | ||
| 425 | } | ||
| 426 | } | ||
| 427 | if ($ep1id) { #WE FOUND AN EPISODE, SO RETRY (TO FETCH THE EPISODE PAGE): | ||
| 428 | ++$tried; | ||
| 429 | $url2fetch = $ep1id; | ||
| 430 | print STDERR "-!!!!- RETRY w/XML EPISODE ID2=$url2fetch=\n" if ($DEBUG); | ||
| 431 | goto TRYIT; | ||
| 432 | } | ||
| 433 | } else { #EPISODE PAGE ID (NOW GET THE DETAILED EPISODE METADATA & WE'RE DONE): | ||
| 434 | my %fh = (qw(title title published_at year download_url Url large_url iconurl | ||
| 435 | big_url imageurl description description site_url albumartist | ||
| 436 | name genre fullname artist)); | ||
| 437 | foreach my $f (keys %fh) { | ||
| 438 | ($self->{$fh{$f}} = $1) =~ s#\\##g if ($html =~ s#\"$f\"\:\"([^\"]+)\"##); | ||
| 439 | } | ||
| 440 | %fh = (qw(large_url articonurl site_url albumartist title album)); | ||
| 441 | foreach my $f (keys %fh) { | ||
| 442 | ($self->{$fh{$f}} = $1) =~ s#\\##g if ($html =~ s#\"$f\"\:\"([^\"]+)\"##); | ||
| 443 | } | ||
| 444 | $self->{'created'} = $self->{'year'}; | ||
| 445 | $self->{'year'} =~ s#^(\d\d\d\d).+$#$1#; | ||
| 446 | if ($self->{'Url'} =~ /^http/) { | ||
| 447 | push @{$self->{'streams'}}, $self->{'Url'}; | ||
| 448 | $self->{'cnt'}++; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | } elsif ($episodeID) { #EXTRACT EPISODE DATA FROM api.spreaker.com: | ||
| 452 | $html = HTML::Entities::decode_entities($html); | ||
| 453 | $html = uri_unescape($html); | ||
| 454 | $html =~ s#\\\/#\/#gs; | ||
| 455 | $self->{'title'} = ($html =~ s#\"title\"\:\"([^\"]+)##s) ? $1 : ''; | ||
| 456 | $self->{'description'} = ($html =~ m#\"description\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 457 | $self->{'description'} = HTML::Entities::decode_entities($self->{'description'}); | ||
| 458 | #x $self->{'description'} =~ s/(?:\%|\\?u?00)([0-9A-Fa-f]{2})/chr(hex($1))/egs; | ||
| 459 | $self->{'description'} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs; | ||
| 460 | #FIX QUOTES: | ||
| 461 | $self->{'description'} =~ s/\\u201([89cd])/($1 =~ m#[89]#o) ? "'" : '"'/egs; | ||
| 462 | $self->{'description'} =~ s#\\$##gs; | ||
| 463 | $self->{'album'} = ($html =~ m#\"title\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 464 | $self->{'iconurl'} = ($html =~ s#\"medium\_url\"\:\"([^\"]+)##s) ? $1 : ''; | ||
| 465 | $self->{'articonurl'} = ($html =~ m#\"medium\_url\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 466 | $self->{'imageurl'} = ($html =~ m#\"(?:image\_original|big)\_url\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 467 | $self->{'artimageurl'} = ($html =~ m#\"cover\_url\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 468 | $self->{'artist'} = ($html =~ m#\"fullname\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 469 | $self->{'genre'} = ($html =~ m#\"category\_id\"\:\d+\,\"name\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 470 | $self->{'created'} = ($html =~ m#\"published\_at\"\:\"([^\"]+)\"#s) ? $1 : ''; | ||
| 471 | $self->{'year'} = $1 if ($self->{'created'} =~ /(\d\d\d\d)/o); | ||
| 472 | my $stream = ($html =~ m#\"download\_url\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 473 | if ($stream) { | ||
| 474 | unless ($self->{'secure'} && $stream !~ /^https/o) { | ||
| 475 | push @epiTitles, $self->{'title'}; | ||
| 476 | push @epiStreams, $stream; | ||
| 477 | push @{$self->{'streams'}}, $stream; | ||
| 478 | $self->{'cnt'}++; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | } else { #EXTRACT PODCAST PAGE DATA FROM EMBEDDED, ENCODED JSON: | ||
| 482 | $html = HTML::Entities::decode_entities($html); | ||
| 483 | $html = uri_unescape($html); | ||
| 484 | $html =~ s#\\\/#\/#gs; | ||
| 485 | $self->{'album'} = ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:title\"\s+content\=\"([^\"]+)\"\s*\/\>#s) ? $1 : ''; | ||
| 486 | $self->{'articonurl'} = ($html =~ m#\<link\s+rel\=\"image\_src\"\s+href\=\"([^\"]+)#s) ? $1 : ''; | ||
| 487 | $self->{'articonurl'} ||= ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:image\"\s+content\=\"([^\"]+)\"\s*\/\>#s) ? $1 : ''; | ||
| 488 | $self->{'artist'} = ($html =~ m#\"fullname\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 489 | $self->{'genre'} = ($html =~ m#\"category\_id\"\:\d+\,\"name\"\:\"([^\"]+)#s) ? $1 : ''; | ||
| 490 | |||
| 491 | my $episodes = $1 if ($html =~ m#\"episodes\"\:\[(.+)$#s); | ||
| 492 | |||
| 493 | #EXTRACT PLAYLIST DATA FOR ALL EPISODES & ALL DATA FOR THE FIRST/LATEST EPISODE: | ||
| 494 | my $haveit = 0; #TRUE WHEN WE HAVE DETAILED DATA FOR THE LATEST EPISODE. | ||
| 495 | while ($episodes =~ s#^.+?\{\"miniplayer_data\"\:##s) { | ||
| 496 | (my $ephtml = $episodes) =~ s/\{\"miniplayer_data\"\:.*$//so; | ||
| 497 | next unless ($ephtml =~ /\"episode_id\"\:(\d+)/so); | ||
| 498 | my $epiID = $1; | ||
| 499 | my $stream = ($ephtml =~ /\"playback\_url\"\:\"([^\"]+)/so) ? $1 : ''; | ||
| 500 | my $title = ($stream && $ephtml =~ /\"title\"\:\"([^\"]+)/so) ? $1 : ''; | ||
| 501 | if ($title) { | ||
| 502 | unless ($self->{'secure'} && $stream !~ /^https/o) { | ||
| 503 | push @epiTitles, $title; #ADD ALL EPISODES TO THE PLAYLIST: | ||
| 504 | push @epiStreams, $stream; | ||
| 505 | print STDERR "--haveit=$haveit= epiID=$epiID= episodeID=$episodeID=\n" if ($DEBUG); | ||
| 506 | if (!$haveit && (!$episodeID || $episodeID eq $epiID)) { #GRAB 1ST/LATEST EPISODE DETAILS: | ||
| 507 | $episodeID = $epiID; | ||
| 508 | $haveit = 1; | ||
| 509 | $self->{'title'} = $title; | ||
| 510 | print STDERR "---GRABBED EPI=$epiID= title=$title=\n" if ($DEBUG); | ||
| 511 | $self->{'description'} = $1 if ($ephtml =~ m#\"description\"\:\"([^\"]+)\"#so); | ||
| 512 | $self->{'description'} = HTML::Entities::decode_entities($self->{'description'}); | ||
| 513 | # $self->{'description'} =~ s/(?:\%|\\?u?00)([0-9A-Fa-f]{2})/chr(hex($1))/egs; | ||
| 514 | $self->{'description'} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs; | ||
| 515 | #FIX QUOTES: | ||
| 516 | $self->{'description'} =~ s/\\u201([89cd])/($1 =~ m#[89]#o) ? "'" : '"'/egs; | ||
| 517 | $self->{'description'} =~ s#\\$##gs; | ||
| 518 | $self->{'iconurl'} = $1 if ($ephtml =~ m#\"image\_url\"\:\"([^\"]+)\"#so); | ||
| 519 | $self->{'imageurl'} = $1 if ($ephtml =~ m#\"image\_original\_url\"\:\"([^\"]+)\"#so); | ||
| 520 | $self->{'iconurl'} ||= $self->{'imageurl'}; | ||
| 521 | $self->{'imageurl'} ||= $self->{'iconurl'}; | ||
| 522 | $self->{'created'} = $1 if ($ephtml =~ m#\"published\_at\"\:\"([^\"]+)\"#so); | ||
| 523 | $self->{'year'} = $1 if ($self->{'created'} =~ /(\d\d\d\d)/o); | ||
| 524 | push @{$self->{'streams'}}, $stream; | ||
| 525 | $self->{'cnt'}++; | ||
| 526 | } | ||
| 527 | } | ||
| 528 | } | ||
| 529 | } | ||
| 530 | } | ||
| 531 | |||
| 532 | $self->{'total'} = $self->{'cnt'}; | ||
| 533 | $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : ''; | ||
| 534 | |||
| 535 | #GENERATE EXTENDED-M3U PLAYLIST: | ||
| 536 | |||
| 537 | if ($self->{'cnt'} > 0) { | ||
| 538 | $self->{'playlist'} = "#EXTM3U\n"; | ||
| 539 | if ($#epiStreams >= 0) { #PLAYLIST PAGE SPECIFIED, ADD ALL EPISODES TO PLAYLIST: | ||
| 540 | $self->{'playlist_cnt'} = scalar @epiStreams; | ||
| 541 | for (my $i=0;$i<=$#epiStreams;$i++) { | ||
| 542 | last if ($i > $#epiTitles); | ||
| 543 | $self->{'playlist'} .= "#EXTINF:-1, " . $epiTitles[$i] . "\n"; | ||
| 544 | $self->{'playlist'} .= "#EXTART:" . $self->{'artist'} . "\n" | ||
| 545 | if ($self->{'artist'}); | ||
| 546 | $self->{'playlist'} .= "#EXTALB:" . $self->{'album'} . "\n" | ||
| 547 | if ($self->{'album'}); | ||
| 548 | $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n" | ||
| 549 | if ($self->{'genre'}); | ||
| 550 | $self->{'playlist'} .= $epiStreams[$i] . "\n"; | ||
| 551 | } | ||
| 552 | } else { #EPISODE PAGE SPECIFIED, ONLY THAT EPISODE ADDED TO PLAYLIST: | ||
| 553 | $self->{'playlist_cnt'} = 1; | ||
| 554 | $self->{'playlist'} .= "#EXTINF:-1, " . $self->{'title'} . "\n"; | ||
| 555 | $self->{'playlist'} .= "#EXTART:" . $self->{'artist'} . "\n" | ||
| 556 | if ($self->{'artist'}); | ||
| 557 | $self->{'playlist'} .= "#EXTALB:" . $self->{'album'} . "\n" | ||
| 558 | if ($self->{'album'}); | ||
| 559 | $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n" | ||
| 560 | if ($self->{'genre'}); | ||
| 561 | $self->{'playlist'} .= ${$self->{'streams'}}[0] . "\n"; | ||
| 562 | } | ||
| 563 | } | ||
| 564 | |||
| 565 | if ($DEBUG) { | ||
| 566 | foreach my $i (sort keys %{$self}) { | ||
| 567 | print STDERR "--KEY=$i= VAL=".$self->{$i}."=\n"; | ||
| 568 | } | ||
| 569 | print STDERR "-SUCCESS: 1st stream=".$self->{'Url'}."=\n" if ($self->{'cnt'} > 0); | ||
| 570 | } | ||
| 571 | |||
| 572 | $self->_log($url); | ||
| 573 | |||
| 574 | bless $self, $class; #BLESS IT! | ||
| 575 | |||
| 576 | return $self; | ||
| 577 | } | ||
| 578 | |||
| 579 | #IF WE WERE GIVEN A PODCAST PAGE W/O A NUMERIC PODCAST-ID, WE WAIT UNTIL HERE (ASKED BY USER) TO | ||
| 580 | #FETCH THE XML PODCAST PAGE (REQUIRES NUMERIC PODCAST-ID) | ||
| 581 | #OTHERWISE, WE ALREADY HAVE THE PLAYLIST DATA, SO WE RETURN THAT: | ||
| 582 | sub get ## DEPRECIATED? ## | ||
| 583 | { | ||
| 584 | my $self = shift; | ||
| 585 | |||
| 586 | if (defined($_[0]) && $_[0] =~ /playlist/i) { | ||
| 587 | print STDERR "---GET PLAYLIST!--- CNT=".$self->{'playlist_cnt'}."= PCID=".$self->{'_podcast_id'}."=\n" if ($DEBUG); | ||
| 588 | return $self->{'playlist'} if ($self->{'playlist_cnt'} > 1); #HAVE ALREADY FETCHED IT BEFORE! | ||
| 589 | |||
| 590 | if ($self->{'_podcast_id'}) { #FETCH UNFETCHED PLAYLIST DATA (NOW THAT USER HAS ASKED FOR IT): | ||
| 591 | my @epiTitles = (); | ||
| 592 | my @epiStreams = (); | ||
| 593 | my $url2fetch = "https://www.spreaker.com/show/$self->{'_podcast_id'}/episodes/feed"; | ||
| 594 | print STDERR "--GET: FETCHING URL=$url2fetch=\n" if ($DEBUG); | ||
| 595 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 596 | $ua->timeout($self->{'timeout'}); | ||
| 597 | $ua->cookie_jar({}); | ||
| 598 | $ua->env_proxy; | ||
| 599 | my $xml = ''; | ||
| 600 | my $response = $ua->get($url2fetch); | ||
| 601 | if ($response->is_success) { | ||
| 602 | $xml = $response->decoded_content; | ||
| 603 | } else { | ||
| 604 | print STDERR $response->status_line if ($DEBUG); | ||
| 605 | } | ||
| 606 | |||
| 607 | while ($xml =~ s#\<item\>(.+?)\<\/item\>##si) { | ||
| 608 | my $item = $1; | ||
| 609 | if ($item =~ s#\<enclosure\s+url\=\"([^\"]+)##si) { | ||
| 610 | my $stream = $1; | ||
| 611 | next if ($self->{'secure'} && $stream !~ /^https/o); | ||
| 612 | if ($item =~ s#\<title\>([^\<]+)\<\/title\>##si) { | ||
| 613 | my $title = $1; | ||
| 614 | push @epiStreams, $stream; | ||
| 615 | push @epiTitles, $title; | ||
| 616 | } | ||
| 617 | } | ||
| 618 | } | ||
| 619 | $self->{'playlist'} = "#EXTM3U\n"; | ||
| 620 | if ($#epiStreams >= 0) { | ||
| 621 | $self->{'playlist_cnt'} = scalar @epiStreams; | ||
| 622 | for (my $i=0;$i<=$#epiStreams;$i++) { | ||
| 623 | last if ($i > $#epiTitles); | ||
| 624 | $self->{'playlist'} .= "#EXTINF:-1, " . $epiTitles[$i] . "\n"; | ||
| 625 | $self->{'playlist'} .= "#EXTART:" . $self->{'artist'} . "\n" | ||
| 626 | if ($self->{'artist'}); | ||
| 627 | $self->{'playlist'} .= "#EXTALB:" . $self->{'album'} . "\n" | ||
| 628 | if ($self->{'album'}); | ||
| 629 | $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n" | ||
| 630 | if ($self->{'genre'}); | ||
| 631 | $self->{'playlist'} .= $epiStreams[$i] . "\n"; | ||
| 632 | } | ||
| 633 | } else { | ||
| 634 | $self->{'playlist_cnt'} = 1; | ||
| 635 | $self->{'playlist'} .= "#EXTINF:-1, " . $self->{'title'} . "\n"; | ||
| 636 | $self->{'playlist'} .= "#EXTART:" . $self->{'artist'} . "\n" | ||
| 637 | if ($self->{'artist'}); | ||
| 638 | $self->{'playlist'} .= "#EXTALB:" . $self->{'album'} . "\n" | ||
| 639 | if ($self->{'album'}); | ||
| 640 | $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n" | ||
| 641 | if ($self->{'genre'}); | ||
| 642 | $self->{'playlist'} .= ${$self->{'streams'}}[0] . "\n"; | ||
| 643 | } | ||
| 644 | } | ||
| 645 | return $self->{'playlist'}; | ||
| 646 | } | ||
| 647 | return wantarray ? @{$self->{'streams'}} : ${$self->{'streams'}}[0]; | ||
| 648 | } | ||
| 649 | |||
| 650 | 1 |