StreamFinder/Podchaser.pm untested

filter: Statementstmt 0.0% 0 / 239 Branchbran 0.0% 0 / 172 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 494 CC 87 SCAR 88.8
CC
87
Cov
0%
new
7656.0
BEGIN
2.0
BEGIN
2.0
CRAP 7162.2
1 =head1 NAME
2  
3 StreamFinder::Podchaser - Fetch actual raw streamable URLs on podchaser.com
4  
5 =head1 AUTHOR
6  
7 This module is Copyright (C) 2023-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::Podchaser;
26  
27 die "..usage: $0 ID|URL\n" unless ($ARGV[0]);
28  
29 my $podcast = new StreamFinder::Podchaser($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 $artist = $podcast->{'artist'};
54  
55 print "Artist=$artist\n" if ($artist);
56
57 my $genre = $podcast->{'genre'};
58  
59 print "Genre=$genre\n" if ($genre);
60
61 my $icon_url = $podcast->getIconURL();
62  
63 if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE:
64  
65 print "Icon URL=$icon_url=\n";
66  
67 my ($image_ext, $icon_image) = $podcast->getIconData();
68  
69 if ($icon_image && open IMGOUT, ">/tmp/${podcastID}.$image_ext") {
70  
71 binmode IMGOUT;
72  
73 print IMGOUT $icon_image;
74  
75 close IMGOUT;
76  
77 print "...Icon image downloaded to (/tmp/${podcastID}.$image_ext)\n";
78  
79 }
80  
81 }
82  
83 my $stream_count = $podcast->count();
84  
85 print "--Stream count=$stream_count=\n";
86  
87 my @streams = $podcast->get();
88  
89 foreach my $s (@streams) {
90  
91 print "------ stream URL=$s=\n";
92  
93 }
94  
95 =head1 DESCRIPTION
96  
97 StreamFinder::Podchaser accepts a valid podcast or episode ID or URL on
98 Podchaser.com and returns the actual stream URL(s), title, and cover art icon.
99 The purpose is that one needs one of these URLs in order to have the option to
100 stream the podcast in one's own choice of media player software rather than
101 using their web browser and accepting any / all flash, ads, javascript,
102 cookies, trackers, web-bugs, and other crapware that can come with that method
103 of play. The author uses his own custom all-purpose media player called
104 "fauxdacious" (his custom hacked version of the open-source "audacious"
105 audio player). "fauxdacious" can incorporate this module to decode and play
106 Podchaser.com streams.
107  
108 One or more stream URLs can be returned for each podcast.
109  
110 =head1 SUBROUTINES/METHODS
111  
112 =over 4
113  
114 =item B<new>(I<ID>|I<url> [, I<-notrim> [ => 0|1 ]] [, I<-secure> [ => 0|1 ]]
115 [, I<-debug> [ => 0|1|2 ]])
116  
117 Accepts a podchaser.com podcast-ID, episode-ID or URL and creates and returns a
118 a new podcast object, or I<undef> if the URL is not a valid podcast, or no
119 streams are found. The URL can be the full URL, ie.
120 I<https://www.podchaser.com/podcasts/>B<podcast-id>I</episodes/>B<episode-id>,
121 I<https://www.podchaser.com/podcasts/>B<podcast-id>, or just
122 B<podcast-id/episode-id> or B<podcast-id>. I do not (yet) know how to fetch
123 a podchaser.com episode with just the I<episode-ID>, though it IS legal to just
124 supply the numeric part of episode-IDs
125 (as long as you also have the podcast-ID).
126  
127 Some other URL formats also seem to work well, such as:
128 I<https://www.podchaser.com/creators/>B<creator-id> and
129 I<https://www.podchaser.com/creators/>B<creator-id>I</appearances: (both
130 formats are podcast pages (multiple episodes).
131  
132 The optional I<-notrim> argument can be either 0 or 1 (I<false> or I<true>).
133 If 0 (I<false>) then stream URLs are trimmed of excess "ad" parameters
134 (everything after the first "?" character, ie. "?ads.cust_params=premium" is
135 removed, including the "?". Otherwise, the stream URLs are returned as-is.
136  
137 DEFAULT I<-notrim> (if not given) is 0 (I<false>) and URLs are trimmed. If
138 I<-notrim> is specified without argument, the default is 1 (I<true>). Try
139 using I<-notrim> if stream will not play without the extra arguments.
140  
141 The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>).
142 If 1 then only secure ("https://") streams will be returned.
143  
144 DEFAULT I<-secure> is 0 (false) - return all streams (http and https).
145  
146 Additional options:
147  
148 I<-log> => "I<logfile>"
149  
150 Specify path to a log file. If a valid and writable file is specified, A line
151 will be appended to this file every time one or more streams is successfully
152 fetched for a url.
153  
154 DEFAULT I<-none-> (no logging).
155  
156 I<-logfmt> specifies a format string for lines written to the log file.
157  
158 DEFAULT "I<[time] [url] - [site]: [title] ([total])>".
159  
160 The valid field I<[variables]> are: [stream]: The url of the first/best
161 stream found. [site]: The site name (Podchaser). [url]: The url searched
162 for streams. [time]: Perl timestamp when the line was logged. [title],
163 [artist], [album], [description], [year], [genre], [total], [albumartist]:
164 The corresponding field data returned (or "I<-na->", if no value).
165  
166 =item $podcast->B<get>(['playlist'])
167  
168 Returns an array of strings representing all stream URLs found.
169 If I<"playlist"> is specified, then an extended m3u playlist is returned
170 instead of stream url. NOTE: If an author / channel page url is given,
171 rather than an individual podcast episode's url, get() returns the first
172 (latest?) podcast episode found, and get("playlist") returns an extended
173 m3u playlist containing the urls, titles, etc. for all the podcast
174 episodes found on that page url.
175  
176 =item $podcast->B<getURL>([I<options>])
177  
178 Similar to B<get>() except it only returns a single stream representing
179 the first valid stream found. There currently are no valid I<options>.
180  
181 =item $podcast->B<count>(['playlist'])
182  
183 Returns the number of streams found for the podcast.
184 If I<"playlist"> is specified, the number of episodes returned in the
185 playlist is returned (the playlist can have more than one item if a
186 podcast page URL is specified).
187  
188 =item $podcast->B<getID>()
189  
190 Returns the podcast's Episode-ID (default). For podcaster pages, the
191 Episode-ID is that of the most recent episode.
192  
193 =item $podcast->B<getTitle>(['desc'])
194  
195 Returns the podcast's title, or (long description).
196  
197 =item $podcast->B<getIconURL>(['artist'])
198  
199 Returns the URL for the podcast's "cover art" icon image, if any.
200 If B<'artist'> is specified, the channel/podcast artist's icon url
201 is returned, if any.
202  
203 =item $podcast->B<getIconData>(['artist'])
204  
205 Returns a two-element array consisting of the extension (ie. "png",
206 "gif", "jpeg", etc.) and the actual icon image (binary data), if any.
207 If B<'artist'> is specified, the channel/podcast artist's icon data
208 is returned, if any.
209  
210 =item $podcast->B<getImageURL>()
211  
212 Returns the URL for the podcast's "cover art" (usually larger)
213 banner image. Podchaser podcasts do not have a banner image, so the
214 Icon URL (if any) will be returned.
215  
216 =item $podcast->B<getImageData>()
217  
218 Returns a two-element array consisting of the extension (ie. "png",
219 "gif", "jpeg", etc.) and the actual podcast's banner image (binary data).
220  
221 =item $podcast->B<getType>()
222  
223 Returns the podcast's type ("Podchaser").
224  
225 =back
226  
227 =head1 CONFIGURATION FILES
228  
229 The default root location directory for StreamFinder configuration files
230 is "~/.config/StreamFinder". To use an alternate location directory,
231 specify it in the "I<STREAMFINDER>" environment variable, ie.:
232 B<$ENV{STREAMFINDER} = "/etc/StreamFinder">.
233  
234 =over 4
235  
236 =item ~/.config/StreamFinder/Podchaser/config
237  
238 Optional text file for specifying various configuration options
239 for a specific site (submodule). Each option is specified on a
240 separate line in the format below:
241 NOTE: Do not follow the lines with a semicolon, comma, or any other
242 separator. Non-numeric I<values> should be surrounded with quotes, either
243 single or double. Blank lines and lines beginning with a "#" sign as
244 their first non-blank character are ignored as comments.
245  
246 'option' => 'value' [,]
247  
248 and the options are loaded into a hash used only by the specific
249 (submodule) specified. Valid options include
250 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
251  
252 Options specified here override any specified in I<~/.config/StreamFinder/config>.
253  
254 Among options valid for Podchaser streams is the I<-notrim> described in the
255 B<new()> function.
256  
257 =item ~/.config/StreamFinder/config
258  
259 Optional text file for specifying various configuration options.
260 Each option is specified on a separate line in the format below:
261  
262 'option' => 'value' [,]
263  
264 and the options are loaded into a hash used by all sites
265 (submodules) that support them. Valid options include
266 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
267  
268 =back
269  
270 NOTE: Options specified in the options parameter list of the I<new()>
271 function will override those corresponding options specified in these files.
272  
273 =head1 KEYWORDS
274  
275 podchaser
276  
277 =head1 DEPENDENCIES
278  
279 L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>
280  
281 =head1 RECCOMENDS
282  
283 wget
284  
285 =head1 BUGS
286  
287 Please report any bugs or feature requests to C<bug-streamFinder-iheartradio at rt.cpan.org>, or through
288 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-Podchaser>. I will be notified, and then you'll
289 automatically be notified of progress on your bug as I make changes.
290  
291 =head1 SUPPORT
292  
293 You can find documentation for this module with the perldoc command.
294  
295 perldoc StreamFinder::Podchaser
296  
297 You can also look for information at:
298  
299 =over 4
300  
301 =item * RT: CPAN's request tracker (report bugs here)
302  
303 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-Podchaser>
304  
305 =item * AnnoCPAN: Annotated CPAN documentation
306  
307 L<http://annocpan.org/dist/StreamFinder-Podchaser>
308  
309 =item * Search CPAN
310  
311 L<http://search.cpan.org/dist/StreamFinder-Podchaser/>
312  
313 =back
314  
315 =head1 LICENSE AND COPYRIGHT
316  
317 Copyright 2023-2026 Jim Turner.
318  
319 This program is free software; you can redistribute it and/or modify it
320 under the terms of the the Artistic License (2.0). You may obtain a
321 copy of the full license at:
322  
323 L<http://www.perlfoundation.org/artistic_license_2_0>
324  
325 Any use, modification, and distribution of the Standard or Modified
326 Versions is governed by this Artistic License. By using, modifying or
327 distributing the Package, you accept this license. Do not use, modify,
328 or distribute the Package, if you do not accept this license.
329  
330 If your Modified Version has been derived from a Modified Version made
331 by someone other than you, you are nevertheless required to ensure that
332 your Modified Version complies with the requirements of this license.
333  
334 This license does not grant you the right to use any trademark, service
335 mark, tradename, or logo of the Copyright Holder.
336  
337 This license includes the non-exclusive, worldwide, free-of-charge
338 patent license to make, have made, use, offer to sell, sell, import and
339 otherwise transfer the Package with respect to any patent claims
340 licensable by the Copyright Holder that are necessarily infringed by the
341 Package. If you institute patent litigation (including a cross-claim or
342 counterclaim) against any party alleging that the Package constitutes
343 direct or contributory patent infringement, then this Artistic License
344 to you shall terminate on the date that such litigation is filed.
345  
346 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
347 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
348 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
349 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
350 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
351 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
352 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
353 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
354  
355 =cut
356  
357 package StreamFinder::Podchaser;
358  
359 use strict;
360 use warnings;
361 use URI::Escape;
362 use HTML::Entities ();
363 use LWP::UserAgent ();
364 use parent 'StreamFinder::_Class';
365  
366 my $DEBUG = 0;
367  
368 sub new
369 {
370 my $class = shift;
371 my $url = shift;
372  
373 return undef unless ($url);
374  
375 my $self = $class->SUPER::new('Podchaser', @_);
376 $DEBUG = $self->{'debug'} if (defined $self->{'debug'});
377 $self->{'notrim'} = 0;
378 while (@_) {
379 if ($_[0] =~ /^\-?notrim$/o) {
380 shift;
381 $self->{'notrim'} = (defined $_[0]) ? shift : 1;
382 } else {
383 shift;
384 }
385 }
386  
387 my $baseURL = 'https://www.podchaser.com';
388 $self->{'id'} = '';
389 $self->{'_podcast_id'} = '';
390 (my $url2fetch = $url) =~ s#\/$##;
391 my $tried = 0;
392 my @epiTitles = ();
393 my @epiStreams = ();
394 my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}});
395 $ua->timeout($self->{'timeout'});
396 $ua->cookie_jar({});
397 $ua->env_proxy;
398 my $html = '';
399 my $response;
400 my $isEpisode;
401  
402 TRYIT:
403 if ($url2fetch =~ m#^(https?\:\/\/[^\/]+)#) {
404 $baseURL = $1;
405 $url2fetch =~ s#\/episodes\/recent$##; #"recent" IS SPECIAL CASE, TREAT AS PODCAST PAGE!:
406 if ($url2fetch =~ m#\/episodes?\/(.+)$#) {
407 ($self->{'id'} = $1) =~ s#^[^\d]+##;
408 ($self->{'_podcast_id'} = $1) =~ s#^[^\d]+##
409 if ($url2fetch =~ m#\/podcasts?\/([^\/]+)#);
410 $isEpisode = 1;
411 } elsif ($url2fetch =~ m#\/([^\/]+)$#) {
412 $self->{'id'} = $self->{'_podcast_id'} = $1;
413 $isEpisode = 0;
414 $url2fetch .= '/episodes/recent'; #NOW REQUIRED TO GET ANYTHING FROM PODCAST PAGES:
415 }
416 } else {
417 $self->{'id'} = $url2fetch;
418 $isEpisode = ($url2fetch =~ m#\/#) ? 1 : 0;
419 if ($isEpisode) {
420 my ($podcastID, $episodeID) = split(m#\/#, $self->{'id'});
421 $url2fetch = "$baseURL/podcasts/${podcastID}/episodes/${episodeID}";
422 ($self->{'id'} = $episodeID) =~ s#^[^\d]+##;
423 ($self->{'_podcast_id'} = $podcastID) =~ s#^[^\d]+##;
424 } else {
425 $url2fetch = "$baseURL/podcasts/$$self{'id'}/episodes/recent";
426 $self->{'id'} =~ s#^[^\d]+##;
427 }
428 }
429 print STDERR "--ID=$$self{'id'}= fetchURL=$url2fetch= isEP=$isEpisode=\n" if ($DEBUG);
430 return undef unless ($self->{'id'}); #INVALID ID/URL!
431  
432 $html = '';
433 print STDERR "-0(Podchaser): ($tried) FETCHING URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG);
434 $response = $ua->get($url2fetch);
435 if ($response->is_success) {
436 $html = $response->decoded_content;
437 } else {
438 print STDERR $response->status_line if ($DEBUG);
439 }
440 print STDERR "-1: html=$html=\n" if ($DEBUG > 1);
441 return undef unless ($html); #STEP 1 FAILED, INVALID PODCAST URL, PUNT!
442  
443 $self->{'genre'} = 'Podcast'; #DEFAULT.
444 print STDERR "---ID=".$self->{'id'}."= tried=$tried=\n" if ($DEBUG);
445 if ($isEpisode) { #EPISODE PAGE (NOW GET THE DETAILED EPISODE METADATA & WE'RE DONE):
446 print STDERR "-----WE'RE AN EPISODE PAGE: ID=".$self->{'id'}."!\n" if ($DEBUG);
447 $html =~ s#\\\"description\_sanitized\\\"\:##s; #SKIP PODCAST DESCRIPTION!
448 ($self->{'description'} = $1) =~ s/[\\]+\"/\"/gs
449 if ($html =~ m#\\\"description\_sanitized\\\"\:\\\"(.+?)\\\"\,\\\"\w#);
450
451 $html =~ s/\\u00([0-9A-Fa-f]{2})/chr(hex($1))/egs;
452 $html =~ s#\\##gs;
453  
454 $self->{'title'} ||= $1 if ($html =~ m#\<h1\s+class\=\"[^\"]*\"\s+title\=\"[^\>]+\>([^\<]+)#s);
455 $self->{'iconurl'} = $1 if ($html =~ m# (?:name|itemProp|property)\=\"image\"\s+content\=\"([^\"]+)#s);
456 $self->{'iconurl'} ||= $1 if ($html =~ m# property\=\"(?:ogg|twitter)\:image(?:\:src)?\"\s+content\=\"([^\"]+)#s);
457 $self->{'iconurl'} ||= $1 if ($html =~ m#\"image\"\:\"([^\"]+)#s);
458 $self->{'imageurl'} = $self->{'iconurl'}; #Podchaser.com DOES NOT CURRENTLY PROVIDE BANNER IMAGES.
459 $self->{'genre'} = $1 if ($html =~ m#\"genre\"\:\"([^\"]+)#s);
460 ($self->{'albumartist'} = $url2fetch) =~ s#\/$$self{'id'}## unless ($self->{'albumartist'});
461 if ($html =~ m#\"PodcastSeries\"\,\"\@id\"\:\"([^\"]+)\"\,\"name\"\:\"([^\"]+)#s) {
462 $self->{'albumartist'} = $1;
463 $self->{'artist'} = $2;
464 } elsif ($html =~ m#\<span\s+class\=\"[^\"]*\"\>([^\<]+)\<\/span\>\<\/a\>\<\/h4\>#s) {
465 $self->{'artist'} = $1;
466 }
467 #SOME PODCASTS HAVE A SEPARATE PODCAST-ARTIST NAME, IF SO, MOVE THE PODCAST NAME TO THE ALBUM FIELD:
468 #(FOR REFERENCE, NORMALLY THE "ARTIST" IS THE PODCAST'S ARTIST'S NAME, AND ALBUM IS THE
469 #PODCAST'S NAME (AN ARTIST CAN HAVE MULTIPLE PODCASTS & A PODCAST CAN HAVE MULT. EPISODES)!)
470 if ($html =~ m#\"itunes_owner_name\"\:\"([^\"]+)#s) {
471 $self->{'album'} = $self->{'artist'} if ($self->{'artist'});
472 $self->{'artist'} = $1;
473 }
474 $self->{'articonurl'} ||= $1 if ($html =~ m#\,\"image_url\":\"([^\"]+)#s);
475 $self->{'articonurl'} ||= $self->{'iconurl'};
476 if ($html =~ m#\,\"air_date\"\:\"([^\"]+)#s) {
477 $self->{'created'} = $1;
478 $self->{'year'} = ($self->{'created'} =~ /(\d\d\d\d)/) ? $1 : '';
479 }
480 unless ($self->{'description'}) {
481 #last resort for description:
482 $self->{'description'} = $1 if ($html =~ m# (?:name|itemProp|property)\=\"description\"\s+content\=\"([^\"]+)#s);
483 $self->{'description'} =~ s/\x{2026}$/\.\.\.)/;
484 $self->{'description'} ||= $self->{'title'};
485 }
486  
487 #NOW FETCH THE STREAM(S) (Podchaser.com PODCASTS NORMALLY ONLY HAVE ONE):
488 my $stream = '';
489 $stream = $1 if ($html =~ m#\>Download\s+Audio\s+File\<\/span\>\<span\s+(?:class\=\"[^\"]*\"\s+)?title\=\"([^\"]+)#s);
490 $stream =~ s/\.(mp3|m3u8|pls)\?.*$/\.$1/ unless ($self->{'notrim'}); #STRIP OFF EXTRA GARBAGE PARMS, COMMENT OUT IF STARTS FAILING!
491 my $protocol = $self->{'secure'} ? '' : '?';
492
493 @{$self->{'streams'}} = ($stream) if ($stream && $stream =~ m#^https${protocol}\:#);
494 #MIGHT AS WELL ANTICIPATE & TRY TO EXTRAPOLATE POSSIBLE VIDEO!:
495 $stream = ($html =~ m#\"video_url\"\:\"([^\"]+)#s) ? $1 : '';
496 $stream ||= $1 if ($html =~ m#\>Download\s+Video\s+File\<\/span\>\<span\s+(class\=\"[^\"]*\"\s+)?title\=\"([^\"]+)#s);
497 if ($stream && $stream =~ m#^https${protocol}\:#) {
498 $stream =~ s/\.(mp3|m3u8|pls)\?.*$/\.$1/ unless ($self->{'notrim'}); #STRIP OFF EXTRA GARBAGE PARMS, COMMENT OUT IF STARTS FAILING!
499 unshift(@{$self->{'streams'}}, $stream);
500 }
501 } else { #PODCAST PAGE:
502 print STDERR "-----WE'RE A PODCAST PAGE: ID=".$self->{'id'}."!\n" if ($DEBUG);
503  
504 #FETCH PODCAST-WIDE METADATA HERE!:
505 $self->{'albumartist'} = $url2fetch;
506 $self->{'articonurl'} = $1 if ($html =~ m#\"\s+name\=\"image\"\s+content\=\"([^\"]+)#s);
507 $self->{'articonurl'} ||= $1 if ($html =~ m#\s+property\=\"(?:og|twiter)\:image\"\s+content\=\"([^\"]+)#s);
508 $self->{'artist'} = $1 if ($html =~ m#\s+property\=\"title\"\s+content\=\"([^\"]+)#s);
509 if ($html =~ m#\"itunes_owner_name\"\:\"([^\"]+)#s) {
510 $self->{'album'} = $self->{'artist'} if ($self->{'artist'});
511 $self->{'artist'} = $1;
512 }
513 $self->{'album'} = $1 if ($html =~ m# property\=\"(?:og\:|twitter\:?)title\"\s+content\=\"([^\"]+)#s);
514  
515 #WE NEED TO EXTRACT 1ST EPISODE ID, BUT WHILST AT IT, GO AHEAD AND FETCH PLAYLIST DATA HERE TOO!:
516 my %epiHash = ();
517 my %titleHash = ();
518  
519 #Podchaser.com PODCAST PAGES HAVE THE LATEST AND/OR FEATURED EPISODE AT THE TOP, BUT OFTEN
520 #(BUT NOT ALWAYS) REPEATED AGAIN IN THE EPISODE LIST:
521 #THE "LATEST EPISODE" MAY NOT ALWAYS BE THE LATEST EITHER, GO FIGURE! :/
522 my $firstEp = 0;
523 foreach my $special (qw(latest featured)) {
524 if ($html =~ s#\"${special}\_episode\\\"\:\{\\\"id\\\"\:[^\d\-\,]*(\d+)\,\\\"podcast\_id\\\"\:[^\d\-\,]*(\d+)##s) {
525 $firstEp = $1;
526 $self->{'_podcast_id'} = $2;
527 print STDERR "----FOUND $special EPISODE ID=$$self{'_podcast_id'}/$firstEp=!\n"
528 if ($DEBUG);
529 last;
530 }
531 }
532 #NOTE: Podchaser.com HAS AN ABBREVIATED LIST OF MOST RECENT EPISODES SORTED IN REVERSE ORDER
533 #(LATEST|FEATURED, THEN OLDEST TO NEWEST):
534 while ($html =~ s#\\\"(\d+)\\\"\:\{\\\"air\_date\\\"\:(.+?)\,\\\"user\_data\\\"##so) {
535 my $epid = $1;
536 next if ($epid == $firstEp); #WILL ADD LATER!
537  
538 my $epihtml = $2;
539 #SCRAPE THE STREAM(S) FOR EACH EPISODE (WE DON'T KNOW WHICH ONE IS LATEST YET):
540 my @streams = ();
541 foreach my $av (qw(video audio)) {
542 while ($epihtml =~ s#\\\"${av}_url\\\"\:\\\"(.+?)\\\"##s) {
543 my $streamURL = uri_unescape($1);
544 $streamURL =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs;
545 next if ($self->{'secure'} && $streamURL !~ /^https/o);
546  
547 $streamURL =~ s/\.(mp3|m3u8|pls)\?.*$/\.$1/ unless ($self->{'notrim'}); #STRIP OFF EXTRA GARBAGE PARMS, COMMENT OUT IF STARTS FAILING!
548 push @streams, $streamURL;
549 }
550 }
551 next unless ($#streams >= 0); #SKIP EPISODE IF NO STREAMS.
552  
553 #SCRAPE THE DETAILED METADATA FOR EACH EPISODE (WE DON'T KNOW WHICH ONE IS LATEST YET):
554 my $temp;
555 $temp->{'_podcast_id'} = ($epihtml =~ s#\,\\\"podcast_id\\\"\:\\\"(\d+)##o)
556 ? $1 : $self->{'_podcast_id'};
557 $temp->{'artist'} = ($epihtml =~ s#\,\\\"title\\\"\:\\\"(.+?)\\\"##o)
558 ? $1 : $self->{'artist'};
559 $temp->{'id'} = $epid;
560 if ($epihtml =~ s#\,\\\"title\\\"\:\\\"(.+?)\\\"##o) {
561 $temp->{'title'} = $1;
562 next if ($temp->{'title'} =~ /Date Aired/o); #HACK!
563  
564 foreach my $field (qw(genre created year)) {
565 $temp->{$field} = ''; #INIT 'EM TO AVOID PERL NANNY WARNINGS.
566 }
567 if ($epihtml =~ m#^\\\"(.+?)\\\"#so) {
568 $temp->{'created'} = $1;
569 $temp->{'year'} = ($temp->{'created'} =~ /(\d\d\d\d)/o) ? $1 : '';
570 }
571 $temp->{'genre'} = ($epihtml =~ m#\,\\\"text\\\"\:\\\"(.+?)\\\"#so)
572 ? $1 : $self->{'genre'};
573 @{$temp->{'streams'}} = @streams;
574  
575 #STORE SCRAPED METADATA IN EPISODE HASH FOR LATER SORTING:
576 my $epikey = "$$temp{'created'}|$$temp{'title'}";
577 print STDERR "---EPIKEY=$epikey=\n" if ($DEBUG);
578 $epiHash{$epikey} = "id=$epid";
579 foreach my $field (qw(id _podcast_id genre created year)) {
580 $epiHash{$epikey} .= "\x02${field}=$$temp{$field}";
581 }
582 $epiHash{$epikey} .= "\x02_epiurl=" . $1
583 if ($epihtml =~ m#\"url\"\:\"([^\"]+)#s);
584 $epiHash{$epikey} .= "\x02streamstr=";
585 foreach my $s (@{$temp->{'streams'}}) {
586 $epiHash{$epikey} .= $s . '|';
587 }
588 $epiHash{$epikey} =~ s#\|$##o;
589 }
590 }
591  
592 #NOW SET UP THE PLAYLIST AND SORT EPISODES BY AIRING DATA/TIME,TITLE (AVOIDING DUPLICATE TITLES):
593 my $first = 1;
594 my ($name, $value);
595 foreach my $epikey (sort { $b cmp $a } keys %epiHash) {
596 my @data = split(/\x02/o, $epiHash{$epikey});
597 my (undef, $epiTitle) = split(/\|/o, $epikey);
598 next if ($titleHash{$epiTitle});
599  
600 foreach my $f (@data) {
601 ($name, $value) = split(/\=/o, $f);
602 if ($name eq 'streamstr') {
603 my ($epiStream) = split(/\|/o, $value);
604 push @epiTitles, $epiTitle;
605 push @epiStreams, $epiStream;
606 $titleHash{$epiTitle} = 1;
607 } elsif ($name eq 'id') {
608 $firstEp ||= $value;
609 } elsif ($name eq '_epiurl') {
610 ($url2fetch = $value) =~ s/\\u00([0-9A-Fa-f]{2})/chr(hex($1))/e;
611 print STDERR "--WILL TRY EPI URL=$url2fetch!=\n" if ($DEBUG);
612 }
613 }
614 }
615 if ($firstEp && $tried < 1) { #EMPTY EP. LIST, BUT HAVE LATEST|FEATURED (INCOMPLETE DATA, MUST FETCH EP. PAGE)!:
616 ++$tried;
617 $url2fetch =~ s#\/episodes\/recent.*$##;
618 $url2fetch .= '/episodes/' . $firstEp
619 unless ($url2fetch =~ m#\/episodes\/#);
620  
621 print STDERR "======loopback & FETCH 1ST EPISODE ($url2fetch)\n\n"
622 if ($DEBUG);
623 goto TRYIT;
624 }
625  
626 }
627 $self->{'imageurl'} ||= $self->{'iconurl'};
628 $self->{'artimageurl'} ||= $self->{'articonurl'};
629 $self->{'cnt'} = scalar(@{$self->{'streams'}});
630 $self->{'total'} = $self->{'cnt'};
631 $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : '';
632 foreach my $i (qw(title artist album description genre)) {
633 $self->{$i} = HTML::Entities::decode_entities($self->{$i});
634 $self->{$i} = uri_unescape($self->{$i});
635 $self->{$i} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs;
636 }
637  
638 #GENERATE EXTENDED-M3U PLAYLIST (NOTE: MAY NOT BE ABLE TO UNTIL USER CALLS $podcast->get('playlist')!):
639 if ($self->{'cnt'} > 0) {
640 $self->{'playlist'} = "#EXTM3U\n";
641 $self->{'playlist'} .= "#EXTINF:-1, " . $self->{'title'} . "\n";
642 $self->{'playlist'} .= "#EXTART:" . $self->{'artist'} . "\n"
643 if ($self->{'artist'});
644 $self->{'playlist'} .= "#EXTALB:" . $self->{'album'} . "\n"
645 if ($self->{'album'});
646 $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n"
647 if ($self->{'genre'});
648 $self->{'playlist'} .= ${$self->{'streams'}}[0] . "\n";
649 $self->{'playlist_cnt'} = 1;
650 if ($#epiStreams >= 0) {
651 $self->{'playlist_cnt'} += scalar @epiStreams;
652 for (my $i=0;$i<=$#epiStreams;$i++) {
653 last if ($i > $#epiTitles);
654 $self->{'playlist'} .= "#EXTINF:-1, " . $epiTitles[$i] . "\n";
655 $self->{'playlist'} .= "#EXTART:" . $self->{'artist'} . "\n"
656 if ($self->{'artist'});
657 $self->{'playlist'} .= "#EXTALB:" . $self->{'album'} . "\n"
658 if ($self->{'album'});
659 $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n"
660 if ($self->{'genre'});
661 $self->{'playlist'} .= $epiStreams[$i] . "\n";
662 }
663 }
664 }
665  
666 if ($DEBUG) {
667 foreach my $i (sort keys %{$self}) {
668 print STDERR "--KEY=$i= VAL=".$self->{$i}."=\n";
669 }
670 print STDERR "-SUCCESS: 1st stream=".$self->{'Url'}."=\n"
671 if ($self->{'total'} > 0);
672 }
673  
674 $self->_log($url);
675  
676 bless $self, $class; #BLESS IT!
677  
678 return $self;
679 }
680  
681 1