| 1 | =head1 NAME | ||
| 2 | |||
| 3 | StreamFinder::Rcast - Fetch actual raw streamable URLs from radio-station websites on rcast.net | ||
| 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::Rcast; | ||
| 26 | |||
| 27 | die "..usage: $0 ID|URL\n" unless ($ARGV[0]); | ||
| 28 | |||
| 29 | my $station = new StreamFinder::Rcast($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::Rcast accepts a valid radio station ID or URL on | ||
| 72 | rcast.net and returns the actual stream URL(s), title, and icon 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 | rcast.net 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 rcast.net station ID or URL and creates and returns a new | ||
| 92 | station object, or I<undef> if the URL is not a valid rcast.net | ||
| 93 | station or no streams are found. The URL can be the full URL, | ||
| 94 | ie. https://dir.rcast.net/radio/B<station-id>, or just I<station-id>. | ||
| 95 | |||
| 96 | The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>). | ||
| 97 | If 1 then only secure ("https://") streams will be returned. NOTE: rcast.net | ||
| 98 | currently only returns https (secure) stream URLs, as the stream URL is a | ||
| 99 | reformat of the station's url. | ||
| 100 | |||
| 101 | DEFAULT I<-secure> is 0 (false) - return all streams (http and https). | ||
| 102 | |||
| 103 | Additional options: | ||
| 104 | |||
| 105 | I<-log> => "I<logfile>" | ||
| 106 | |||
| 107 | Specify path to a log file. If a valid and writable file is specified, A line | ||
| 108 | will be appended to this file every time one or more streams is successfully | ||
| 109 | fetched for a url. | ||
| 110 | |||
| 111 | DEFAULT I<-none-> (no logging). | ||
| 112 | |||
| 113 | I<-logfmt> specifies a format string for lines written to the log file. | ||
| 114 | |||
| 115 | DEFAULT "I<[time] [url] - [site]: [title] ([total])>". | ||
| 116 | |||
| 117 | The valid field I<[variables]> are: [stream]: The url of the first/best | ||
| 118 | stream found. [site]: The site name (Rcast). [url]: The url searched for | ||
| 119 | streams. [time]: Perl timestamp when the line was logged. [title], [artist], | ||
| 120 | [album], [description], [year], [genre], [total], [albumartist]: | ||
| 121 | The corresponding field data returned (or "I<-na->", if no value). | ||
| 122 | |||
| 123 | =item $station->B<get>() | ||
| 124 | |||
| 125 | Returns an array of strings representing all stream URLs found. | ||
| 126 | |||
| 127 | =item $station->B<getURL>([I<options>]) | ||
| 128 | |||
| 129 | Similar to B<get>() except it only returns a single stream representing | ||
| 130 | the first valid stream found. | ||
| 131 | |||
| 132 | Current options are: I<"random">, I<"nopls">, and I<"noplaylists">. | ||
| 133 | By default, the first ("best"?) stream is returned. If I<"random"> is | ||
| 134 | specified, then a random one is selected from the list of streams found. | ||
| 135 | If I<"nopls"> is specified, and the stream to be returned is a ".pls" playlist, | ||
| 136 | it is first fetched and the first entry (or a random entry if I<"random"> is | ||
| 137 | specified) is returned. This is needed by Fauxdacious Mediaplayer. | ||
| 138 | If I<"noplaylists"> is specified, and the stream to be returned is a | ||
| 139 | "playlist" (either .pls or .m3u? extension), it is first fetched and the first | ||
| 140 | entry (or a random entry if I<"random"> is specified) in the playlist | ||
| 141 | is returned. | ||
| 142 | |||
| 143 | NOTE: Most, if not all streams returned by Rcast ARE "playlists" | ||
| 144 | (.pls, and a few .m3u), and thus using "nopls" may be needed to get to the | ||
| 145 | specific (playable) stream. | ||
| 146 | |||
| 147 | =item $station->B<count>() | ||
| 148 | |||
| 149 | Returns the number of streams found for the station. | ||
| 150 | |||
| 151 | =item $station->B<getID>() | ||
| 152 | |||
| 153 | Returns the station's Radio.net ID (alphanumeric). | ||
| 154 | |||
| 155 | =item $station->B<getTitle>(['desc']) | ||
| 156 | |||
| 157 | Returns the station's title, or (long description). | ||
| 158 | |||
| 159 | NOTE: rcast.net does NOT currently provide a long description for | ||
| 160 | any of their stations, so "description" will always be the "title". | ||
| 161 | |||
| 162 | =item $station->B<getIconURL>() | ||
| 163 | |||
| 164 | Returns the URL for the station's "cover art" icon image, if any. | ||
| 165 | |||
| 166 | =item $station->B<getIconData>() | ||
| 167 | |||
| 168 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 169 | "gif", "jpeg", etc.) and the actual icon image (binary data), if any. | ||
| 170 | |||
| 171 | =item $station->B<getImageURL>() | ||
| 172 | |||
| 173 | Returns the URL for the station's "cover art" (usually larger) | ||
| 174 | banner image. | ||
| 175 | |||
| 176 | NOTE: rcast.net only returns (at most) a single image for each station. | ||
| 177 | Therefore, this will be the same image as the I<icon-url>. | ||
| 178 | |||
| 179 | =item $station->B<getImageData>() | ||
| 180 | |||
| 181 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 182 | "gif", "jpeg", etc.) and the actual station's banner image (binary data). | ||
| 183 | |||
| 184 | =item $station->B<getType>() | ||
| 185 | |||
| 186 | Returns the station's type ("Rcast"). | ||
| 187 | |||
| 188 | =back | ||
| 189 | |||
| 190 | =head1 CONFIGURATION FILES | ||
| 191 | |||
| 192 | The default root location directory for StreamFinder configuration files | ||
| 193 | is "~/.config/StreamFinder". To use an alternate location directory, | ||
| 194 | specify it in the "I<STREAMFINDER>" environment variable, ie.: | ||
| 195 | B<$ENV{STREAMFINDER} = "/etc/StreamFinder">. | ||
| 196 | |||
| 197 | =over 4 | ||
| 198 | |||
| 199 | =item ~/.config/StreamFinder/Rcast/config | ||
| 200 | |||
| 201 | Optional text file for specifying various configuration options | ||
| 202 | for a specific site (submodule). Each option is specified on a | ||
| 203 | separate line in the format below: | ||
| 204 | NOTE: Do not follow the lines with a semicolon, comma, or any other | ||
| 205 | separator. Non-numeric I<values> should be surrounded with quotes, either | ||
| 206 | single or double. Blank lines and lines beginning with a "#" sign as | ||
| 207 | their first non-blank character are ignored as comments. | ||
| 208 | |||
| 209 | 'option' => 'value' [,] | ||
| 210 | |||
| 211 | and the options are loaded into a hash used only by the specific | ||
| 212 | (submodule) specified. Valid options include | ||
| 213 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 214 | |||
| 215 | Options specified here override any specified in I<~/.config/StreamFinder/config>. | ||
| 216 | |||
| 217 | =item ~/.config/StreamFinder/config | ||
| 218 | |||
| 219 | Optional text file for specifying various configuration options. | ||
| 220 | Each option is specified on a separate line in the format below: | ||
| 221 | |||
| 222 | 'option' => 'value' [,] | ||
| 223 | |||
| 224 | and the options are loaded into a hash used by all sites | ||
| 225 | (submodules) that support them. Valid options include | ||
| 226 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 227 | |||
| 228 | =back | ||
| 229 | |||
| 230 | NOTE: Options specified in the options parameter list of the I<new()> | ||
| 231 | function will override those corresponding options specified in these files. | ||
| 232 | |||
| 233 | =head1 KEYWORDS | ||
| 234 | |||
| 235 | Rcast | ||
| 236 | |||
| 237 | =head1 DEPENDENCIES | ||
| 238 | |||
| 239 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent> | ||
| 240 | |||
| 241 | =head1 RECCOMENDS | ||
| 242 | |||
| 243 | wget | ||
| 244 | |||
| 245 | =head1 BUGS | ||
| 246 | |||
| 247 | Please report any bugs or feature requests to C<bug-streamFinder-Rcast at rt.cpan.org>, or through | ||
| 248 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-Rcast>. I will be notified, and then you'll | ||
| 249 | automatically be notified of progress on your bug as I make changes. | ||
| 250 | |||
| 251 | =head1 SUPPORT | ||
| 252 | |||
| 253 | You can find documentation for this module with the perldoc command. | ||
| 254 | |||
| 255 | perldoc StreamFinder::Rcast | ||
| 256 | |||
| 257 | You can also look for information at: | ||
| 258 | |||
| 259 | =over 4 | ||
| 260 | |||
| 261 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 262 | |||
| 263 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-Rcast> | ||
| 264 | |||
| 265 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 266 | |||
| 267 | L<http://annocpan.org/dist/StreamFinder-Rcast> | ||
| 268 | |||
| 269 | =item * Search CPAN | ||
| 270 | |||
| 271 | L<http://search.cpan.org/dist/StreamFinder-Rcast/> | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | =head1 LICENSE AND COPYRIGHT | ||
| 276 | |||
| 277 | Copyright 2023-2026 Jim Turner. | ||
| 278 | |||
| 279 | This program is free software; you can redistribute it and/or modify it | ||
| 280 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 281 | copy of the full license at: | ||
| 282 | |||
| 283 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 284 | |||
| 285 | Any use, modification, and distribution of the Standard or Modified | ||
| 286 | Versions is governed by this Artistic License. By using, modifying or | ||
| 287 | distributing the Package, you accept this license. Do not use, modify, | ||
| 288 | or distribute the Package, if you do not accept this license. | ||
| 289 | |||
| 290 | If your Modified Version has been derived from a Modified Version made | ||
| 291 | by someone other than you, you are nevertheless required to ensure that | ||
| 292 | your Modified Version complies with the requirements of this license. | ||
| 293 | |||
| 294 | This license does not grant you the right to use any trademark, service | ||
| 295 | mark, tradename, or logo of the Copyright Holder. | ||
| 296 | |||
| 297 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 298 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 299 | otherwise transfer the Package with respect to any patent claims | ||
| 300 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 301 | Package. If you institute patent litigation (including a cross-claim or | ||
| 302 | counterclaim) against any party alleging that the Package constitutes | ||
| 303 | direct or contributory patent infringement, then this Artistic License | ||
| 304 | to you shall terminate on the date that such litigation is filed. | ||
| 305 | |||
| 306 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 307 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 308 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 309 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 310 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 311 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 312 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 313 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 314 | |||
| 315 | =cut | ||
| 316 | |||
| 317 | package StreamFinder::Rcast; | ||
| 318 | |||
| 319 | use strict; | ||
| 320 | use warnings; | ||
| 321 | use URI::Escape; | ||
| 322 | use HTML::Entities (); | ||
| 323 | use LWP::UserAgent (); | ||
| 324 | use parent 'StreamFinder::_Class'; | ||
| 325 | |||
| 326 | my $DEBUG = 0; | ||
| 327 | |||
| 328 | sub new | ||
| 329 | { | ||
| 330 | my $class = shift; | ||
| 331 | my $url = shift; | ||
| 332 | |||
| 333 | return undef unless ($url); | ||
| 334 | |||
| 335 | my $self = $class->SUPER::new('Rcast', @_); | ||
| 336 | $DEBUG = $self->{'debug'} if (defined $self->{'debug'}); | ||
| 337 | |||
| 338 | my $url2fetch = $url; | ||
| 339 | if ($url =~ /^https?\:/) { | ||
| 340 | $self->{'id'} = $1 if ($url2fetch =~ m#\/([0-9]+)#); | ||
| 341 | } else { | ||
| 342 | $self->{'id'} = $url; | ||
| 343 | $url2fetch = 'https://dir.rcast.net/radio/' . $url; | ||
| 344 | } | ||
| 345 | $url2fetch .= '/' unless ($url2fetch =~ m#\/$#); | ||
| 346 | my $html = ''; | ||
| 347 | print STDERR "-0(Rcast): URL=$url2fetch= TIMEOUT=".$self->{'timeout'}."= AGENTOPS=".join('|',@{$self->{'_userAgentOps'}})."=\n" if ($DEBUG); | ||
| 348 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 349 | $ua->timeout($self->{'timeout'}); | ||
| 350 | $ua->cookie_jar({}); | ||
| 351 | $ua->env_proxy; | ||
| 352 | my $response = $ua->get($url2fetch); | ||
| 353 | if ($response->is_success) { | ||
| 354 | $html = $response->decoded_content; | ||
| 355 | } else { | ||
| 356 | my $server_error = $response->status_line; | ||
| 357 | print STDERR "f:${server_error}\n" if ($DEBUG); | ||
| 358 | } | ||
| 359 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 360 | return undef unless ($html); #STEP 1 FAILED, INVALID STATION URL, PUNT! | ||
| 361 | |||
| 362 | $self->{'title'} = $1 if ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:title\"\s+content\=\"([^\"]+)\"#s); | ||
| 363 | $self->{'title'} ||= $1 if ($html =~ m#\<title\>(.+?)\<\/title\>#i); | ||
| 364 | $self->{'iconurl'} = $1 if ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:image\"\s+content\=\"(https?\:\/\/[^\"]+)\"#s); | ||
| 365 | if ($html =~ m#\<label\>Website\<\/label\>(.+?)\<label\>#si) { | ||
| 366 | my $goodpart = $1; | ||
| 367 | $self->{'albumartist'} = $1 if ($goodpart =~ m#(https?\:[^\<]+)#s); | ||
| 368 | } | ||
| 369 | $self->{'albumartist'} ||= $1 if ($html =~ m#\<meta\s+property\=\"(?:og|twitter)\:url\"\s+content\=\"(https?\:\/\/[^\"]+)\"#s); | ||
| 370 | $self->{'genre'} = ''; | ||
| 371 | while ($html =~ s#\<span\s+class\=\"badge\s+text\-bg\-secondary\"\>([^\<]+)##s) { | ||
| 372 | $self->{'genre'} .= "$1, "; | ||
| 373 | } | ||
| 374 | $self->{'genre'} =~ s/\, $// if ($self->{'genre'}); | ||
| 375 | $self->{'imageurl'} = $self->{'iconurl'}; | ||
| 376 | |||
| 377 | #GET THE STREAM: | ||
| 378 | while ($html =~ s#\bdata\-stream\=\"([^\"]+)\"##s) { | ||
| 379 | my $s = $1; | ||
| 380 | unless ($self->{'secure'} && $s !~ /^https/) { | ||
| 381 | push @{$self->{'streams'}}, $s; | ||
| 382 | print STDERR "--STATION STREAM FOUND=$s=!\n" if ($DEBUG); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | #FALLBACK STREAM: SO FAR, THIS ONE SEEMS TO ALWAYS? WORK: | ||
| 386 | push @{$self->{'streams'}}, "https://stream.rcast.net/$$self{'id'}"; | ||
| 387 | $self->{'cnt'} = scalar @{$self->{'streams'}}; | ||
| 388 | foreach my $field (qw(description genre)) { | ||
| 389 | $self->{$field} = HTML::Entities::decode_entities($self->{$field}); | ||
| 390 | $self->{$field} = uri_unescape($self->{$field}); | ||
| 391 | $self->{$field} =~ s/(?:\%|\\[ux\%]?00|\bu00)([0-9A-Fa-f]{2})/chr(hex($1))/egs; | ||
| 392 | } | ||
| 393 | $self->{'description'} = $self->{'title'}; #rcast.net DOES NOT PROVIDE LONG DESCRIPTIONS! | ||
| 394 | $self->{'title'} =~ s#\s*\-\s*RCAST.NET##i; | ||
| 395 | |||
| 396 | print STDERR "-2: ID=".$self->{'id'}."=\ntitle=".$self->{'title'}."=\ngenre=" | ||
| 397 | .$self->{'genre'}."=\ndesc=".$self->{'description'}."=\nalbum_artist=" | ||
| 398 | .$self->{'albumartist'}."=\n" if ($DEBUG); | ||
| 399 | $self->{'total'} = $self->{'cnt'}; | ||
| 400 | $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : ''; | ||
| 401 | if ($DEBUG) { | ||
| 402 | foreach my $f (sort keys %{$self}) { | ||
| 403 | print STDERR "--KEY=$f= VAL=$$self{$f}=\n"; | ||
| 404 | } | ||
| 405 | print STDERR "-SUCCESS: 1st stream=".$self->{'Url'}."=\n" if ($self->{'cnt'} > 0); | ||
| 406 | } | ||
| 407 | $self->_log($url); | ||
| 408 | |||
| 409 | bless $self, $class; #BLESS IT! | ||
| 410 | |||
| 411 | return $self; | ||
| 412 | } | ||
| 413 | |||
| 414 | 1 |