StreamFinder/InternetRadio.pm untested

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