StreamFinder/PragerU.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 260 CC 43 SCAR 74.1
CC
43
Cov
0%
new
1892.0
BEGIN
2.0
BEGIN
2.0
CRAP 1660.6
1 =head1 NAME
2  
3 StreamFinder::PragerU - Fetch actual raw streamable video URLs on www.prageru.com
4  
5 =head1 AUTHOR
6  
7 This module is Copyright (C) 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::PragerU;
26  
27 die "..usage: $0 ID|URL\n" unless ($ARGV[0]);
28  
29 my $video = new StreamFinder::PragerU($ARGV[0]);
30  
31 die "Invalid URL or no streams found!\n" unless ($video);
32  
33 my $firstStream = $video->get();
34  
35 print "First Stream URL=$firstStream\n";
36  
37 my $url = $video->getURL();
38  
39 print "Stream URL=$url\n";
40  
41 my $videoTitle = $video->getTitle();
42
43 print "Title=$videoTitle\n";
44
45 my $videoDescription = $video->getTitle('desc');
46
47 print "Description=$videoDescription\n";
48
49 my $videoID = $video->getID();
50  
51 print "Video ID=$videoID\n";
52
53 my $artist = $video->{'artist'};
54  
55 print "Artist=$artist\n" if ($artist);
56
57 my $genre = $video->{'genre'};
58  
59 print "Genre=$genre\n" if ($genre);
60
61 my $icon_url = $video->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) = $video->getIconData();
68  
69 if ($icon_image && open IMGOUT, ">/tmp/${videoID}.$image_ext") {
70  
71 binmode IMGOUT;
72  
73 print IMGOUT $icon_image;
74  
75 close IMGOUT;
76  
77 print "...Icon image downloaded to (/tmp/${videoID}.$image_ext)\n";
78  
79 }
80  
81 }
82  
83 my $stream_count = $video->count();
84  
85 print "--Stream count=$stream_count=\n";
86  
87 my @streams = $video->get();
88  
89 foreach my $s (@streams) {
90  
91 print "------ stream URL=$s=\n";
92  
93 }
94  
95 =head1 DESCRIPTION
96  
97 StreamFinder::PragerU accepts a valid video ID or URL on
98 www.prageru.com and returns the actual stream URL(s), title, and cover
99 art icon. The purpose is that one needs one of these URLs in order to have
100 the option to stream the video in one's own choice of media player software
101 rather than using their web browser and accepting any / all flash, ads,
102 javascript, cookies, trackers, web-bugs, and other crapware that can come with
103 that method of play. The author uses his own custom all-purpose media player
104 called "fauxdacious" (his custom hacked version of the open-source "audacious"
105 audio player). "fauxdacious" can incorporate this module to decode and play
106 www.prageru.com streams.
107  
108 One stream URL can be returned for each video.
109  
110 =head1 SUBROUTINES/METHODS
111  
112 =over 4
113  
114 =item B<new>(I<ID>|I<url> [, I<-secure> [ => 0|1 ]] [, I<-debug> [ => 0|1|2 ]])
115  
116 Accepts a www.prageru.com video ID or URL and creates and
117 returns a a new video object, or I<undef> if the URL is not a valid video, or
118 no streams are found. The URL can be the full URL, ie.
119 https://www.prageru.com/videos/video-id>, or just
120 B<video-id>.
121  
122 The optional I<-keep> argument can be either a comma-separated string or an
123 array reference ([...]) of stream types (extensions) to keep (include) and
124 returned in order specified (type1, type2...). Each "type" (extension) can be
125 one of: "mp3", "m4a", "mp4", "pls" (playlist), etc. NOTE: Since these are
126 actual extensions used to identify streams, there is NO
127 "any/all/stream/playlist" catch-all options as used by some of the other
128 (more specific) StreamFinder-supported sites! Streams will be returned sorted
129 by extension in the order specified in this list.
130  
131 DEFAULT I<-keep> list is: "m3u8,mp4,m4a,mpd,mp3,ogg,flac,aac", meaning
132 that all m3u8 (hls) streams found (if any), followed by all "ogg" streams, etc.
133  
134 NOTE: Most PragerU videos are now in .m3u8 (hls) format!
135  
136 The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>).
137 If 1 then only secure ("https://") streams will be returned. Currently,
138 all PragerU streaming URLs are believed to be secure (https).
139  
140 DEFAULT I<-secure> is 0 (false) - return all streams (http and https).
141  
142 Additional options:
143  
144 I<-log> => "I<logfile>"
145  
146 Specify path to a log file. If a valid and writable file is specified, A line
147 will be appended to this file every time one or more streams is successfully
148 fetched for a url.
149  
150 DEFAULT I<-none-> (no logging).
151  
152 I<-logfmt> specifies a format string for lines written to the log file.
153  
154 DEFAULT "I<[time] [url] - [site]: [title] ([total])>".
155  
156 The valid field I<[variables]> are: [stream]: The url of the first/best
157 stream found.
158 [site]: The site name (PragerU). [url]: The url searched for streams.
159 [time]: Perl timestamp when the line was logged. [title], [artist], [album],
160 [description], [year], [genre], [total], [albumartist]: The corresponding
161 field data returned (or "I<-na->", if no value).
162  
163 =item $video->B<get>(['playlist'])
164  
165 Playlists are not currently supported.
166  
167 =item $video->B<getURL>([I<options>])
168  
169 Similar to B<get>() except it only returns a single stream representing
170 the first valid stream found. There currently are no valid I<options>.
171  
172 =item $video->B<count>()
173  
174 Returns the number of streams found for the video (will nearly always be 1).
175  
176 =item $video->B<getID>()
177  
178 Returns the video's PragerU ID (default). PragerU video IDs
179 consist of combinations of lower-case letters, numbers and hyphens.
180  
181 =item $video->B<getTitle>(['desc'])
182  
183 Returns the video's title, or (long description). Videos
184 on PragerU can have separate descriptions, but for videos,
185 it is always the video's title.
186  
187 Note: PragerU video descriptions are usually incomplete, ending with a "..",
188 since on the page itself there's a "Read More" button to view the rest, and
189 "the rest" can only be obtained with Javascript.
190  
191 =item $video->B<getIconURL>(['artist'])
192  
193 Returns the URL for the video's "cover art" icon image, if any.
194 If B<'artist'> is specified, the channel artist's icon url is returned,
195 if any.
196  
197 =item $video->B<getIconData>(['artist'])
198  
199 Returns a two-element array consisting of the extension (ie. "png",
200 "gif", "jpeg", etc.) and the actual icon image (binary data), if any.
201 If B<'artist'> is specified, the channel artist's icon data is returned,
202 if any.
203  
204 =item $video->B<getImageURL>(['artist'])
205  
206 Returns the URL for the video's "cover art" (usually larger)
207 banner image. If B<'artist'> is specified, the channel artist's image URL
208 is returned, if any. PragerU does not use separate banner images, so the
209 icon (or artist's icon) url is returned.
210  
211 =item $video->B<getImageData>(['artist'])
212  
213 Returns a two-element array consisting of the extension (ie. "png",
214 "gif", "jpeg", etc.) and the actual video's banner image (binary data).
215 If B<'artist'> is specified, the channel artist's image data is returned,
216 if any.. PragerU does not use separate banner images, so the
217 icon (or artist's icon) data is returned.
218  
219 =item $video->B<getType>()
220  
221 Returns the video's type ("PragerU").
222  
223 =back
224  
225 =head1 CONFIGURATION FILES
226  
227 The default root location directory for StreamFinder configuration files
228 is "~/.config/StreamFinder". To use an alternate location directory,
229 specify it in the "I<STREAMFINDER>" environment variable, ie.:
230 B<$ENV{STREAMFINDER} = "/etc/StreamFinder">.
231  
232 =over 4
233  
234 =item ~/.config/StreamFinder/PragerU/config
235  
236 Optional text file for specifying various configuration options
237 for a specific site (submodule). Each option is specified on a
238 separate line in the format below:
239 NOTE: Do not follow the lines with a semicolon, comma, or any other
240 separator. Non-numeric I<values> should be surrounded with quotes, either
241 single or double. Blank lines and lines beginning with a "#" sign as
242 their first non-blank character are ignored as comments.
243  
244 'option' => 'value' [,]
245  
246 and the options are loaded into a hash used only by the specific
247 (submodule) specified. Valid options include
248 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
249  
250 Options specified here override any specified in I<~/.config/StreamFinder/config>.
251  
252 =item ~/.config/StreamFinder/config
253  
254 Optional text file for specifying various configuration options.
255 Each option is specified on a separate line in the format below:
256  
257 'option' => 'value' [,]
258  
259 and the options are loaded into a hash used by all sites
260 (submodules) that support them. Valid options include
261 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
262  
263 =back
264  
265 NOTE: Options specified in the options parameter list of the I<new()>
266 function will override those corresponding options specified in these files.
267  
268 =head1 KEYWORDS
269  
270 prageru
271  
272 =head1 DEPENDENCIES
273  
274 L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>
275  
276 =head1 RECCOMENDS
277  
278 wget
279  
280 =head1 BUGS
281  
282 Please report any bugs or feature requests to C<bug-streamFinder-iheartradio at rt.cpan.org>, or through
283 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-PragerU>. I will be notified, and then you'll
284 automatically be notified of progress on your bug as I make changes.
285  
286 =head1 SUPPORT
287  
288 You can find documentation for this module with the perldoc command.
289  
290 perldoc StreamFinder::PragerU
291  
292 You can also look for information at:
293  
294 =over 4
295  
296 =item * RT: CPAN's request tracker (report bugs here)
297  
298 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-PragerU>
299  
300 =item * AnnoCPAN: Annotated CPAN documentation
301  
302 L<http://annocpan.org/dist/StreamFinder-PragerU>
303  
304 =item * Search CPAN
305  
306 L<http://search.cpan.org/dist/StreamFinder-PragerU/>
307  
308 =back
309  
310 =head1 LICENSE AND COPYRIGHT
311  
312 Copyright 2022 Jim Turner.
313  
314 This program is free software; you can redistribute it and/or modify it
315 under the terms of the the Artistic License (2.0). You may obtain a
316 copy of the full license at:
317  
318 L<http://www.perlfoundation.org/artistic_license_2_0>
319  
320 Any use, modification, and distribution of the Standard or Modified
321 Versions is governed by this Artistic License. By using, modifying or
322 distributing the Package, you accept this license. Do not use, modify,
323 or distribute the Package, if you do not accept this license.
324  
325 If your Modified Version has been derived from a Modified Version made
326 by someone other than you, you are nevertheless required to ensure that
327 your Modified Version complies with the requirements of this license.
328  
329 This license does not grant you the right to use any trademark, service
330 mark, tradename, or logo of the Copyright Holder.
331  
332 This license includes the non-exclusive, worldwide, free-of-charge
333 patent license to make, have made, use, offer to sell, sell, import and
334 otherwise transfer the Package with respect to any patent claims
335 licensable by the Copyright Holder that are necessarily infringed by the
336 Package. If you institute patent litigation (including a cross-claim or
337 counterclaim) against any party alleging that the Package constitutes
338 direct or contributory patent infringement, then this Artistic License
339 to you shall terminate on the date that such litigation is filed.
340  
341 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
342 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
343 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
344 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
345 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
346 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
347 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
348 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
349  
350 =cut
351  
352 package StreamFinder::PragerU;
353  
354 use strict;
355 use warnings;
356 use URI::Escape;
357 use HTML::Entities ();
358 use LWP::UserAgent ();
359 use parent 'StreamFinder::_Class';
360  
361 my $DEBUG = 0;
362  
363 sub new
364 {
365 my $class = shift;
366 my $url = shift;
367  
368 return undef unless ($url);
369  
370 my $self = $class->SUPER::new('PragerU', @_);
371 $DEBUG = $self->{'debug'} if (defined $self->{'debug'});
372  
373 my $baseURL = 'https://www.prageru.com';
374 my @okStreams;
375 $self->{'id'} = '';
376 while (@_) {
377 if ($_[0] =~ /^\-?keep$/o) {
378 shift;
379 if (defined $_[0]) {
380 my $keeporder = shift;
381 @okStreams = (ref($keeporder) =~ /ARRAY/) ? @{$keeporder} : split(/\,\s*/, $keeporder);
382 }
383 } else {
384 shift;
385 }
386 }
387 if (!defined($okStreams[0]) && defined($self->{'keep'})) {
388 @okStreams = (ref($self->{'keep'}) =~ /ARRAY/) ? @{$self->{'keep'}} : split(/\,\s*/, $self->{'keep'});
389 }
390 @okStreams = (qw(m3u8 mp4 m4a mpd mp3 ogg flac aac)) unless (defined $okStreams[0]);
391 my $streamRegex = join('|',@okStreams);
392  
393 (my $url2fetch = $url) =~ s/\?.*$//; #WE DO NOT CURRENTLY SUPPORT PLAYLISTS, ETC.
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  
401 $html = '';
402 if ($url2fetch =~ m#^(https?\:\/\/[^\/]+)#) {
403 $baseURL = $1;
404 $self->{'id'} = $1 if ($url =~ m#\/([a-z\-\d]+)\/?$#);
405 } else {
406 $self->{'id'} = $url;
407 $url2fetch = $baseURL . '/videos/' . $url;
408 }
409  
410 print STDERR "-0(PragerU): FETCHING URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG);
411 $response = $ua->get($url2fetch);
412 if ($response->is_success) {
413 $html = $response->decoded_content;
414 } else {
415 print STDERR $response->status_line if ($DEBUG);
416 }
417 print STDERR "-1: html=$html=\n" if ($DEBUG > 1);
418  
419 return undef unless ($html); #STEP 1 FAILED, INVALID PragerU URL, PUNT!
420  
421 my $protocol = $self->{'secure'} ? '' : '?';
422 $html =~ s/\\\\//gs;
423 $html =~ s/\\\"/\"/gs;
424 $self->{'_playbackID'} = ($html =~ m#\"playbackId\":\"([^\"]+)#s) ? $1 : '';
425 $self->{'title'} = $1 if ($html =~ /\<h1[^\>]*\>([^\<]+)/is);
426 $self->{'title'} ||= $1 if ($html =~ /\<title\>([^\<]+)/is);
427 $self->{'title'} =~ s/\s+\|\s+PragerU\s*$//;
428 $self->{'description'} = $1 if ($html =~ /\<p\s+class\=\"richText\"\>([^\<]+)/is);
429 $self->{'description'} ||= $1 if ($html =~ m#\<meta\s+name\=\"description\"\s+content\=\"(.+?)\"\/\>#is);
430 $self->{'iconurl'} = $1 if ($html =~ s#\"thumbnailUrl\"\:\[\"([^\"]+)\"\]\,?##is);
431 $self->{'iconurl'} =~ s/\?.*$//;
432 $self->{'imageurl'} = $self->{'iconurl'};
433 if ($html =~ s#\"uploadDate\"\:\"([^\"]+)\"\,?##is) {
434 $self->{'created'} = $1;
435 $self->{'year'} = $1 if ($self->{'created'} =~ /(\d\d\d\d)/);
436 }
437 unless ($self->{'year'}) {
438 if ($html =~ m#\,\"publishedAt\"\:\"([^\"]+)\"\,\"transcript\"#s) {
439 $self->{'created'} = $1;
440 $self->{'year'} = $1 if ($self->{'created'} =~ /(\d\d\d\d)/);
441 }
442 }
443 $self->{'artist'} = $1 if ($html =~ s#\"author\"\:\"([^\"]+)\"\,?##is);
444 if ($html =~ m# href\=\"(\/\@[^\"]+)\"[^\>]*\>\<img\s+.+?srcSet\=\"([^\s]+)[\s]+1x#s) {
445 $self->{'albumartist'} = $baseURL . $1;
446 $self->{'articonurl'} = $2;
447 $self->{'articonurl'} = $1 if ($self->{'articonurl'} =~ m#\;image\=(.+)$#);
448 $self->{'articonurl'} = uri_unescape($self->{'articonurl'});
449 $self->{'artimageurl'} = $self->{'articonurl'};
450 }
451 if ($html =~ m#\"topics\"\:\[?\{([^\}]+)#is) {
452 my $one = $1;
453 $self->{'genre'} = $1 if ($one =~ m#\"name\"\:\"([^\"]+)#);
454 }
455 $self->{'genre'} ||= 'Video';
456 if ($self->{'description'} =~ /\w/) {
457 $self->{'description'} =~ s/\s+$//;
458 } else {
459 $self->{'description'} = $self->{'title'};
460 }
461 foreach my $i (qw(title artist description)) {
462 $self->{$i} = HTML::Entities::decode_entities($self->{$i});
463 $self->{$i} = uri_unescape($self->{$i});
464 $self->{$i} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs;
465 }
466  
467 #NOW FIND THE STREAM(S):
468 my %streams;
469 while ($html =~ s#\"url\"\:\"(https$protocol\:\/\/[^\"]+)\"\,?##o) {
470 (my $url = $1) =~ s/\?.*$//o;
471 print STDERR "---FOUND STREAM URL=$url=\n" if ($DEBUG && $url =~ m#\.(?:$streamRegex)$#);
472 push (@{$streams{$1}}, $url) if ($url =~ m#\.($streamRegex)$#);
473 }
474 #PACK STREAMS IN "-keep" ORDER:
475 foreach my $ext (@okStreams) {
476 if (defined $streams{$ext}) {
477 foreach my $s (@{$streams{$ext}}) {
478 push (@{$self->{'streams'}}, $s);
479 ++$self->{'cnt'};
480 }
481 }
482 }
483 if ($self->{'cnt'} == 0 && $self->{'_playbackID'} && $streamRegex =~ /\bm3u8\b/) {
484 #NO STREAMS FOUND, TRY FALLBACK STREAM:
485 push (@{$self->{'streams'}}, ('https://stream.media.prageru.com/'
486 . $self->{'_playbackID'} . '.m3u8'));
487 ++$self->{'cnt'};
488 }
489  
490 $self->{'total'} = $self->{'cnt'};
491 $self->{'imageurl'} = $self->{'iconurl'};
492 $self->{'Url'} = ($self->{'cnt'} > 0) ? $self->{'streams'}->[0] : '';
493 print STDERR "-count=".$self->{'cnt'}."= iconurl=".$self->{'iconurl'}."=\n" if ($DEBUG);
494 print STDERR "--SUCCESS: 1st stream=".$self->{'Url'}."= total=".$self->{'total'}."=\n"
495 if ($DEBUG && $self->{'cnt'} > 0);
496 if ($DEBUG) {
497 foreach my $i (sort keys %{$self}) {
498 print STDERR "--KEY=$i= VAL=".$self->{$i}."=\n";
499 }
500 }
501  
502 return undef unless ($self->{'cnt'} > 0);
503  
504 $self->_log($url); #LOG IT.
505  
506 bless $self, $class; #BLESS IT!
507  
508 return $self;
509 }
510  
511 1