StreamFinder/RadioNet.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 223 CC 32 SCAR 67.9
CC
32
Cov
0%
new
1056.0
BEGIN
2.0
BEGIN
2.0
CRAP 889.6
1 =head1 NAME
2  
3 StreamFinder::RadioNet - Fetch actual raw streamable URLs from radio-stations and podcasts on radio.net
4  
5 =head1 AUTHOR
6  
7 This module is Copyright (C) 2017-2026 by
8  
9 Jim Turner, C<< <turnerjw784 at yahoo.com> >>
10
11 Email: turnerjw784@yahoo.com
12  
13 All rights reserved.
14  
15 You may distribute this module under the terms of either the GNU General
16 Public License or the Artistic License, as specified in the Perl README
17 file.
18  
19 =head1 SYNOPSIS
20  
21 #!/usr/bin/perl
22  
23 use strict;
24  
25 use StreamFinder::RadioNet;
26  
27 die "..usage: $0 ID|URL\n" unless ($ARGV[0]);
28  
29 my $station = new StreamFinder::RadioNet($ARGV[0]);
30  
31 #Note: $station may be either a radio-station or podcast!
32  
33 die "Invalid URL or no streams found!\n" unless ($station);
34  
35 my $firstStream = $station->get();
36  
37 print "First Stream URL=$firstStream\n";
38  
39 my $url = $station->getURL();
40  
41 print "Stream URL=$url\n";
42  
43 my $playlist = $station->getURL('playlist');
44  
45 print "Extended m3u episodes playlist =$playlist\n"
46  
47 if ($playlist); #IF PODCAST PAGE!
48  
49 my $stationTitle = $station->getTitle();
50
51 print "Title=$stationTitle\n";
52
53 my $stationDescription = $station->getTitle('desc');
54
55 print "Title=$stationDescription\n";
56
57 my $stationID = $station->getID();
58  
59 print "Station ID=$stationID\n";
60
61 my $genre = $station->{'genre'};
62  
63 print "Genre=$genre\n" if ($genre);
64
65 my $icon_url = $station->getIconURL();
66  
67 if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE:
68  
69 my ($image_ext, $icon_image) = $station->getIconData();
70  
71 if ($icon_image && open IMGOUT, ">/tmp/${stationID}.$image_ext") {
72  
73 binmode IMGOUT;
74  
75 print IMGOUT $icon_image;
76  
77 close IMGOUT;
78  
79 }
80  
81 }
82  
83 my $stream_count = $station->count();
84  
85 print "--Stream count=$stream_count=\n";
86  
87 my @streams = $station->get();
88  
89 foreach my $s (@streams) {
90  
91 print "------ stream URL=$s=\n";
92  
93 }
94  
95 =head1 DESCRIPTION
96  
97 StreamFinder::RadioNet accepts a valid radio station or podcast ID or URL on
98 Radio.net and returns the actual stream URL(s), title, and cover art icon for
99 that station or podcast. The purpose is that one needs one of these URLs in
100 order to have the option to stream the station in one's own choice of media
101 player software rather than using their web browser and accepting any / all
102 flash, ads, javascript, cookies, trackers, web-bugs, and other crapware that
103 can come with that method of play. The author uses his own custom all-purpose
104 media player called "fauxdacious" (his custom hacked version of the open-source
105 "audacious" audio player). "fauxdacious" can incorporate this module to decode
106 and play Radio.net streams.
107  
108 One or more streams can be returned for each station.
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 Radio.net station or podcast ID or URL, and creates and
117 returns a new station (or podcast) object, or I<undef> if the URL is not a
118 valid Radio.net station or no streams are found. The URL can be the full URL,
119 ie. I<https://www.radio.net/s/>B<station-id>, or just I<station-id>, or
120 I<https://www.radio.net/podcast/>B<podcast-id> or just I<p/podcast-id>.
121  
122 The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>).
123 If 1 then only secure ("https://") streams will be returned.
124  
125 DEFAULT I<-secure> is 0 (false) - return all streams (http and https).
126  
127 Additional options:
128  
129 I<-log> => "I<logfile>"
130  
131 Specify path to a log file. If a valid and writable file is specified, A line
132 will be appended to this file every time one or more streams is successfully
133 fetched for a url.
134  
135 DEFAULT I<-none-> (no logging).
136  
137 I<-logfmt> specifies a format string for lines written to the log file.
138  
139 DEFAULT "I<[time] [url] - [site]: [title] ([total])>".
140  
141 The valid field I<[variables]> are: [stream]: The url of the first/best
142 stream found. [site]: The site name (RadioNet). [url]: The url searched
143 for streams. [time]: Perl timestamp when the line was logged. [title],
144 [artist], [album], [description], [year], [genre], [total], [albumartist]:
145 The corresponding field data returned (or "I<-na->", if no value).
146  
147 =item $station->B<get>(I<['playlist']>)
148  
149 Returns an array of strings representing all stream URLs found.
150 If I<"playlist"> is specified, and the URL is a podcast page, then an extended
151 m3u playlist is returned instead of stream url(s), Otherwise, the array will
152 likely contain a stream for each episode found in order of latest to oldest,
153 as displayed on the podcast's web-page.
154  
155 NOTE: Radio.net does not provide web-pages for individual podcast episodes.
156 Therefore, if a podcast page url is given, rather than a radio-station page,
157 get() returns the first (latest?) podcast episode found, and get("playlist")
158 returns an extended m3u playlist containing the urls, titles, etc. for all the
159 podcast episodes found on that page url from latest to oldest. One must fetch
160 the I<playlist> in order to get specific episodes other than the first one.
161  
162 =item $station->B<getURL>([I<options>])
163  
164 Similar to B<get>() except it only returns a single stream representing
165 the first valid stream found. For podcast pages, this will be the stream
166 for the first (usually latest) episode. Radio.net does not provide web-pages
167 for individual podcast episodes.
168  
169 Current options are: I<"random">, I<"nopls">, and I<"noplaylists">.
170 By default, the first ("best"?) stream is returned. If I<"random"> is
171 specified, then a random one is selected from the list of streams found.
172 If I<"nopls"> is specified, and the stream to be returned is a ".pls" playlist,
173 it is first fetched and the first entry (or a random entry if I<"random"> is
174 specified) is returned. This is needed by Fauxdacious Mediaplayer.
175 If I<"noplaylists"> is specified, and the stream to be returned is a
176 "playlist" (either .pls or .m3u? extension), it is first fetched and the first
177 entry (or a random entry if I<"random"> is specified) in the playlist
178 is returned.
179  
180 =item $station->B<count>()
181  
182 Returns the number of streams found for the station.
183  
184 =item $station->B<getID>()
185  
186 Returns the station's or podcast's Radio.net ID (alphanumeric).
187  
188 =item $station->B<getTitle>(['desc'])
189  
190 Returns the station's or podcast's title, or (long description).
191  
192 =item $station->B<getIconURL>()
193  
194 Returns the URL for the station's or podcast's "cover art" icon image, if any.
195  
196 =item $station->B<getIconData>()
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  
201 =item $station->B<getImageURL>()
202  
203 Returns the URL for the station's or podcast's "cover art" (usually larger)
204 banner image.
205  
206 =item $station->B<getImageData>()
207  
208 Returns a two-element array consisting of the extension (ie. "png",
209 "gif", "jpeg", etc.) and the actual station's banner image (binary data).
210  
211 =item $station->B<getType>()
212  
213 Returns the station's type ("RadioNet").
214  
215 =back
216  
217 =head1 CONFIGURATION FILES
218  
219 The default root location directory for StreamFinder configuration files
220 is "~/.config/StreamFinder". To use an alternate location directory,
221 specify it in the "I<STREAMFINDER>" environment variable, ie.:
222 B<$ENV{STREAMFINDER} = "/etc/StreamFinder">.
223  
224 =over 4
225  
226 =item ~/.config/StreamFinder/RadioNet/config
227  
228 Optional text file for specifying various configuration options
229 for a specific site (submodule). Each option is specified on a
230 separate line in the format below:
231 NOTE: Do not follow the lines with a semicolon, comma, or any other
232 separator. Non-numeric I<values> should be surrounded with quotes, either
233 single or double. Blank lines and lines beginning with a "#" sign as
234 their first non-blank character are ignored as comments.
235  
236 'option' => 'value' [,]
237  
238 and the options are loaded into a hash used only by the specific
239 (submodule) specified. Valid options include
240 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
241  
242 Options specified here override any specified in
243 I<~/.config/StreamFinder/config>.
244  
245 =item ~/.config/StreamFinder/config
246  
247 Optional text file for specifying various configuration options.
248 Each option is specified on a separate line in the format below:
249  
250 'option' => 'value' [,]
251  
252 and the options are loaded into a hash used by all sites
253 (submodules) that support them. Valid options include
254 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
255  
256 =back
257  
258 NOTE: Options specified in the options parameter list of the I<new()>
259 function will override those corresponding options specified in these files.
260  
261 =head1 KEYWORDS
262  
263 RadioNet
264  
265 =head1 DEPENDENCIES
266  
267 L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>
268  
269 =head1 RECCOMENDS
270  
271 wget
272  
273 =head1 BUGS
274  
275 Please report any bugs or feature requests to C<bug-streamFinder-RadioNet at rt.cpan.org>, or through
276 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-RadioNet>. I will be notified, and then you'll
277 automatically be notified of progress on your bug as I make changes.
278  
279 =head1 SUPPORT
280  
281 You can find documentation for this module with the perldoc command.
282  
283 perldoc StreamFinder::RadioNet
284  
285 You can also look for information at:
286  
287 =over 4
288  
289 =item * RT: CPAN's request tracker (report bugs here)
290  
291 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-RadioNet>
292  
293 =item * AnnoCPAN: Annotated CPAN documentation
294  
295 L<http://annocpan.org/dist/StreamFinder-RadioNet>
296  
297 =item * Search CPAN
298  
299 L<http://search.cpan.org/dist/StreamFinder-RadioNet/>
300  
301 =back
302  
303 =head1 LICENSE AND COPYRIGHT
304  
305 Copyright 2017-2026 Jim Turner.
306  
307 This program is free software; you can redistribute it and/or modify it
308 under the terms of the the Artistic License (2.0). You may obtain a
309 copy of the full license at:
310  
311 L<http://www.perlfoundation.org/artistic_license_2_0>
312  
313 Any use, modification, and distribution of the Standard or Modified
314 Versions is governed by this Artistic License. By using, modifying or
315 distributing the Package, you accept this license. Do not use, modify,
316 or distribute the Package, if you do not accept this license.
317  
318 If your Modified Version has been derived from a Modified Version made
319 by someone other than you, you are nevertheless required to ensure that
320 your Modified Version complies with the requirements of this license.
321  
322 This license does not grant you the right to use any trademark, service
323 mark, tradename, or logo of the Copyright Holder.
324  
325 This license includes the non-exclusive, worldwide, free-of-charge
326 patent license to make, have made, use, offer to sell, sell, import and
327 otherwise transfer the Package with respect to any patent claims
328 licensable by the Copyright Holder that are necessarily infringed by the
329 Package. If you institute patent litigation (including a cross-claim or
330 counterclaim) against any party alleging that the Package constitutes
331 direct or contributory patent infringement, then this Artistic License
332 to you shall terminate on the date that such litigation is filed.
333  
334 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
335 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
336 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
337 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
338 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
339 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
340 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
341 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
342  
343 =cut
344  
345 package StreamFinder::RadioNet;
346  
347 use strict;
348 use warnings;
349 use URI::Escape;
350 use HTML::Entities ();
351 use LWP::UserAgent ();
352 use parent 'StreamFinder::_Class';
353  
354 my $DEBUG = 0;
355  
356 sub new
357 {
358 my $class = shift;
359 my $url = shift;
360  
361 return undef unless ($url);
362  
363 my $self = $class->SUPER::new('RadioNet', @_);
364 $DEBUG = $self->{'debug'} if (defined $self->{'debug'});
365  
366 (my $url2fetch = $url);
367 if ($url =~ /^https?\:/) {
368 $self->{'id'} = $1 if ($url2fetch =~ m#\/([^\/]+)\/?$#);
369 } else {
370 my ($type, $id) = split(m#\/#, $url);
371 if (defined $id) {
372 $self->{'id'} = $id;
373 $type = 'podcast' if ($type eq 'p');
374 $url2fetch = "https://www.radio.net/${type}/$id";
375 } else {
376 $url2fetch = 'https://www.radio.net/s/' . $url;
377 }
378 }
379 my $html = '';
380 print STDERR "-0(RadioNet): URL=$url2fetch=\n" if ($DEBUG);
381 my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}});
382 $ua->timeout($self->{'timeout'});
383 $ua->cookie_jar({});
384 $ua->env_proxy;
385 my $response = $ua->get($url2fetch);
386 if ($response->is_success) {
387 $html = $response->decoded_content;
388 } else {
389 print STDERR $response->status_line if ($DEBUG);
390 }
391 print STDERR "-1: html=$html=\n" if ($DEBUG > 1);
392 return undef unless ($html); #STEP 1 FAILED, INVALID STATION URL, PUNT!
393 my @epiTitles = ();
394 my @epiStreams = ();
395 my $epiCnt = 0;
396 $self->{'iconurl'} = ($html =~ m#\"og\:image\"\s+content\=\"([^\"]+)\"#s) ? $1 : '';
397 my $logoSz = 0;
398 while ($html =~ s#\\\"logo(\d+)x\d+\\\"\:\\\"([^\\]+)##s) {
399 if ($1 > $logoSz) {
400 $logoSz = $1;
401 $self->{'imageurl'} = $2;
402 }
403 }
404 $self->{'_isPodcast'} = 0;
405 $self->{'imageurl'} ||= $self->{'iconurl'};
406 $self->{'title'} = ($html =~ m#\"og\:title\"\s+content\=\"([^\"]+)\"#s) ? $1 : '';
407 $self->{'description'} = ($html =~ m#\b(?:name\=\"twitter|property\=\"og)\:description\"\s+content\=\"([^\"]+)\"#s) ? $1 : $self->{'title'};
408 $self->{'title'} = HTML::Entities::decode_entities($self->{'title'});
409 $self->{'title'} = uri_unescape($self->{'title'});
410 $self->{'title'} =~ s/\s+[\|\-]\s+Listen.*$//; #TIDY UP TITLE A BIT.
411 $self->{'title'} =~ s/\s+radio stream$//i; #TIDY UP TITLE A BIT.
412 $self->{'description'} = HTML::Entities::decode_entities($self->{'description'});
413 $self->{'description'} = uri_unescape($self->{'description'});
414 $self->{'genre'} = $1 if ($html =~ m#\"genres\"\:\s*\[\"([^\"]+)\"#s);
415 if ($self->{'genre'}) {
416 $self->{'genre'} = HTML::Entities::decode_entities($self->{'genre'});
417 $self->{'genre'} = uri_unescape($self->{'genre'});
418 }
419 $self->{'albumartist'} = ($html =~ s# href\=\"([^\"]+)\"\>Station website\<##s) ? $1 : '';
420 print STDERR "-2: icon=".$self->{'iconurl'}."= title=".$self->{'title'}."= image=".$self->{'imageurl'}."=\n" if ($DEBUG);
421 $self->{'cnt'} = 0;
422 while ($html =~ s#\\\"streams\\\"\:\[([^\]]+)\]##s) {
423 my $streamdata = $1;
424 if ($streamdata =~ m#\\\"url\\\"\:\\\"([^\\]+)#) {
425 my $s = $1;
426 unless ($self->{'secure'} && $s !~ /^https/) {
427 push @{$self->{'streams'}}, $s;
428 print STDERR "----STATION STREAM FOUND=$s=!\n" if ($DEBUG);
429 $self->{'cnt'}++;
430 }
431 }
432 }
433 while ($html =~ s#\\\"title\\\"\:\\\"([^\\]+)\\\"\,\\\"url\\\"\:\\\"([^\\]+)\\\"##s) {
434 my $title = $1;
435 my $stream = $2;
436 print "----EPISODE STREAM FOUND=$stream= ($title)!\n" if ($DEBUG);
437 unless ($self->{'secure'} && $stream !~ /^https/o) {
438 push @{$self->{'streams'}}, $stream;
439 $self->{'cnt'}++;
440 $epiTitles[$epiCnt] = $title;
441 $epiStreams[$epiCnt++] = $stream;
442 }
443 $self->{'_isPodcast'} = 1;
444 }
445 $self->{'total'} = $self->{'cnt'};
446 $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : '';
447 foreach my $f (qw(iconurl imageurl)) {
448 $self->{$f} =~ s/\?.*$//;
449 }
450  
451 if ($self->{'_isPodcast'}) {
452 if ($html =~ m#\"PodcastSeries\"\,\"name\"\:\"([^\"]+)\"\,\"author\"\:\{([^\}]+)#s) {
453 $self->{'title'} = $1; #OVERRIDE (BETTER TITLE)
454 my $artistData = $2;
455 $self->{'artist'} = $1 if ($artistData =~ m#\"name\"\:\"([^\"]+)#);
456 }
457 $self->{'genre'} ||= 'Podcast';
458 if ($epiCnt > 0) {
459 my $artist = $self->{'artist'} || $self->{'title'};
460 $self->{'playlist'} = "#EXTM3U\n";
461 $self->{'playlist_cnt'} = 0;
462 for (my $i=0;$i<$epiCnt;$i++) {
463 $self->{'playlist'} .= "#EXTINF:-1, $epiTitles[$i]\n";
464 $self->{'playlist'} .= "#EXTART:$artist\n";
465 $self->{'playlist'} .= "#EXTALB:$url\n";
466 $self->{'playlist'} .= "#EXTGENRE:" . $self->{'genre'} . "\n";
467 $self->{'playlist'} .= "$epiStreams[$i]\n";
468 ++$self->{'playlist_cnt'};
469 }
470 }
471 } else {
472 $self->{'genre'} ||= 'Radio';
473 }
474 if ($DEBUG) {
475 foreach my $f (sort keys %{$self}) {
476 print STDERR "--KEY=$f= VAL=$$self{$f}=\n";
477 }
478 print STDERR "-SUCCESS: 1st stream=".$self->{'Url'}."=\n" if ($self->{'cnt'} > 0);;
479 }
480 $self->_log($url);
481  
482 bless $self, $class; #BLESS IT!
483  
484 return $self;
485 }
486  
487 1