StreamFinder/OnlineRadiobox.pm untested

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