| 1 | =head1 NAME | ||
| 2 | |||
| 3 | StreamFinder::SoundCloud - Fetch actual raw streamable URLs from song-entry websites on SoundCloud.com | ||
| 4 | |||
| 5 | =head1 AUTHOR | ||
| 6 | |||
| 7 | This module is Copyright (C) 2023 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::SoundCloud; | ||
| 26 | |||
| 27 | die "..usage: $0 ID|ID/ID|URL\n" unless ($ARGV[0]); | ||
| 28 | |||
| 29 | my $song = new StreamFinder::SoundCloud($ARGV[0]); | ||
| 30 | |||
| 31 | die "Invalid URL or no streams found!\n" unless ($song); | ||
| 32 | |||
| 33 | my @streams = $song->get(); | ||
| 34 | |||
| 35 | my $url = $song->getURL(); | ||
| 36 | |||
| 37 | print "Stream URL=$url\n"; | ||
| 38 | |||
| 39 | my $songTitle = $song->getTitle(); | ||
| 40 | |||
| 41 | print "Title=$songTitle\n"; | ||
| 42 | |||
| 43 | my $songDescription = $song->getTitle('desc'); | ||
| 44 | |||
| 45 | print "Description=$songDescription\n"; | ||
| 46 | |||
| 47 | my $songID = $song->getID(); | ||
| 48 | |||
| 49 | print "Song-ID ID=$songID\n"; | ||
| 50 | |||
| 51 | my $genre = $song->{'genre'}; | ||
| 52 | |||
| 53 | print "Genre=$genre\n" if ($genre); | ||
| 54 | |||
| 55 | my $artist = $song->{'artist'}; | ||
| 56 | |||
| 57 | print "Artist=$artist\n" if ($artist); | ||
| 58 | |||
| 59 | my $album = $song->{'album'}; | ||
| 60 | |||
| 61 | print "Album (podcast)=$album\n" if ($album); | ||
| 62 | |||
| 63 | my $albumartist = $song->{'albumartist'}; | ||
| 64 | |||
| 65 | print "Album Artist=$albumartist\n" if ($albumartist); | ||
| 66 | |||
| 67 | my $icon_url = $song->getIconURL(); | ||
| 68 | |||
| 69 | if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE: | ||
| 70 | |||
| 71 | my ($image_ext, $icon_image) = $song->getIconData(); | ||
| 72 | |||
| 73 | if ($icon_image && open IMGOUT, ">/tmp/${SongID}.$image_ext") { | ||
| 74 | |||
| 75 | binmode IMGOUT; | ||
| 76 | |||
| 77 | print IMGOUT $icon_image; | ||
| 78 | |||
| 79 | close IMGOUT; | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | } | ||
| 84 | |||
| 85 | my $stream_count = $song->count(); | ||
| 86 | |||
| 87 | print "--Stream count=$stream_count=\n"; | ||
| 88 | |||
| 89 | my @streams = $song->get(); | ||
| 90 | |||
| 91 | foreach my $s (@streams) { | ||
| 92 | |||
| 93 | print "------ stream URL=$s=\n"; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | =head1 DESCRIPTION | ||
| 98 | |||
| 99 | NOTE: Users should also consider StreamFinder::SoundCloud to now be | ||
| 100 | depreciatedand and may be removed in a later StreamFinder release, as they've | ||
| 101 | added cookie and tracker requirements making it impossible to search for songs | ||
| 102 | on their site without enabling, but song URLs (when known) seem to still work | ||
| 103 | for now, but without channel/artist icons. (Privacy-minded individuals should | ||
| 104 | now be cautious while using this site). | ||
| 105 | |||
| 106 | StreamFinder::SoundCloud accepts a valid artist-ID/song-ID, artist-ID, or URL | ||
| 107 | on SoundCloud.com and returns the actual stream URL(s), title, and cover art | ||
| 108 | icon. The purpose is that one needs one of these URLs in order to have the | ||
| 109 | option to stream the song in one's own choice of media player software rather | ||
| 110 | than using their web browser and accepting any / all flash, ads, javascript, | ||
| 111 | cookies, trackers, web-bugs, and other crapware that can come with that method | ||
| 112 | of play. The author uses his own custom all-purpose media player called | ||
| 113 | "fauxdacious" (his custom hacked version of the open-source "audacious" | ||
| 114 | audio player). "fauxdacious" can incorporate this module to decode and play | ||
| 115 | songs from SoundCloud.com. One or more streams can be returned for each song. | ||
| 116 | |||
| 117 | WARNING: SoundCloud streams usually EXPIRE VERY QUICKLY, ie. A FEW MINUTES, | ||
| 118 | and become unplayable (403 ERRORS), requiring re-adding to your playlist! | ||
| 119 | |||
| 120 | NOTE: SoundCloud REQUIRES youtube-dl / yt-dlp (StreamFinder::Youtube) to | ||
| 121 | extract the actual stream and only returns a SINGLE valid stream URL for a | ||
| 122 | song based on a boilerplate URL based on the author's + song's ID. However, | ||
| 123 | youtube-dl does NOT return the metadata, which we're able to extract here. | ||
| 124 | This may or may NOT work for a given song (particularly non-free / | ||
| 125 | subscription-required songs, ymmv)! NOTE: If just an artist-ID is | ||
| 126 | specified, then the first (latest?) song for that artist will be returned. | ||
| 127 | |||
| 128 | StreamFinder::SoundCloud is not capable of returning multisong playlists. | ||
| 129 | You therefore need to specify both an artist-ID and preferrably a song-ID or | ||
| 130 | a SoundCloud URL containing these, NOT a "set" (playlist/group) URL (ie. | ||
| 131 | "https://soundcloud.com/.../set/...", which will not work)! | ||
| 132 | |||
| 133 | Depends: | ||
| 134 | |||
| 135 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>, L<Streamfinder::Youtube>, | ||
| 136 | and the separate application program: youtube-dl, or a compatable program | ||
| 137 | such as yt-dlp. | ||
| 138 | |||
| 139 | =head1 SUBROUTINES/METHODS | ||
| 140 | |||
| 141 | =over 4 | ||
| 142 | |||
| 143 | =item B<new>(I<ID>|I<ID/ID>|I<url> [, I<-debug> [ => 0|1|2 ]]) | ||
| 144 | |||
| 145 | Accepts a soundcloud.com valid artist-ID/song-ID, artist-ID, or URL and creates | ||
| 146 | and returns a new song object, or I<undef> if the URL is not a valid SoundCloud | ||
| 147 | song or artist, or no streams are found. The URL can be the full URL, | ||
| 148 | ie. https://soundcloud.com/B<artist-id>/B<song-id>, | ||
| 149 | https://soundcloud.com/B<artist-id>, | ||
| 150 | or just I<artist-id> or I<artist-id>/I<song-id>. | ||
| 151 | |||
| 152 | Additional options: | ||
| 153 | |||
| 154 | Certain youtube-dl (L<StreamFinder::Youtube>) configuration options, | ||
| 155 | namely I<-format>, I<-formatonly>, I<-youtube-dl-args>, | ||
| 156 | and I<-youtube-dl-add-args> can be overridden here by specifying | ||
| 157 | I<-youtube-format>, I<-youtube-formatonly>, I<-youtube-dl-args>, | ||
| 158 | and I<-youtube-dl-add-args> arguments respectively. It is however, | ||
| 159 | recommended to specify these in the SoundCloud-specific configuration file | ||
| 160 | (see B<CONFIGURATION FILES> below. | ||
| 161 | |||
| 162 | I<-log> => "I<logfile>" | ||
| 163 | |||
| 164 | Specify path to a log file. If a valid and writable file is specified, A line will be | ||
| 165 | appended to this file every time one or more streams is successfully fetched for a url. | ||
| 166 | |||
| 167 | DEFAULT I<-none-> (no logging). | ||
| 168 | |||
| 169 | I<-logfmt> specifies a format string for lines written to the log file. | ||
| 170 | |||
| 171 | DEFAULT "I<[time] [url] - [site]: [title] ([total])>". | ||
| 172 | |||
| 173 | The valid field I<[variables]> are: [stream]: The url of the first/best stream found. | ||
| 174 | [site]: The site name (SoundCloud). [url]: The url searched for streams. | ||
| 175 | [time]: Perl timestamp when the line was logged. [title], [artist], [album], | ||
| 176 | [description], [year], [genre], [total], [albumartist]: The corresponding field data | ||
| 177 | returned (or "I<-na->", if no value). | ||
| 178 | |||
| 179 | =item $song->B<get>() | ||
| 180 | |||
| 181 | Returns an array of strings representing all stream URLs found (usually a single one). | ||
| 182 | |||
| 183 | =item $song->B<getURL>([I<options>]) | ||
| 184 | |||
| 185 | Similar to B<get>() except it only returns a single stream representing | ||
| 186 | the first valid stream found. | ||
| 187 | |||
| 188 | =item $song->B<count>() | ||
| 189 | |||
| 190 | Returns the number of streams found for the song. | ||
| 191 | |||
| 192 | =item $song->B<getTitle>(['desc']) | ||
| 193 | |||
| 194 | Returns the song's title, or (long description). | ||
| 195 | |||
| 196 | =item $song->B<getIconURL>(['artist']) | ||
| 197 | |||
| 198 | Returns the URL for the song's (or artist's) "cover art" icon image, if any. | ||
| 199 | Most SoundCloud artists have their own art-icon. | ||
| 200 | |||
| 201 | =item $song->B<getIconData>(['artist']) | ||
| 202 | |||
| 203 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 204 | "gif", "jpeg", etc.) and the actual icon image (binary data), if any. | ||
| 205 | |||
| 206 | NOTE: At most recent testing, artist-icons appear to perhaps no longer be | ||
| 207 | downloadable (embedded SVG images) - ymmv. | ||
| 208 | |||
| 209 | =item $song->B<getImageURL>(['artist']) | ||
| 210 | |||
| 211 | Returns the URL for the song's (or artist's) "cover art" (usually larger) | ||
| 212 | banner image, if any. NOTE: SoundCloud songs usually do NOT have a larger | ||
| 213 | art image, but most artists DO. | ||
| 214 | |||
| 215 | =item $song->B<getImageData>(['artist']) | ||
| 216 | |||
| 217 | Returns a two-element array consisting of the extension (ie. "png", | ||
| 218 | "gif", "jpeg", etc.) and the actual artist's banner image (binary data). | ||
| 219 | |||
| 220 | =item $song->B<getType>() | ||
| 221 | |||
| 222 | Returns the station's type ("SoundCloud"). | ||
| 223 | |||
| 224 | =back | ||
| 225 | |||
| 226 | =head1 CONFIGURATION FILES | ||
| 227 | |||
| 228 | The default root location directory for StreamFinder configuration files | ||
| 229 | is "~/.config/StreamFinder". To use an alternate location directory, | ||
| 230 | specify it in the "I<STREAMFINDER>" environment variable, ie.: | ||
| 231 | B<$ENV{STREAMFINDER} = "/etc/StreamFinder">. | ||
| 232 | |||
| 233 | =over 4 | ||
| 234 | |||
| 235 | =item ~/.config/StreamFinder/SoundCloud/config | ||
| 236 | |||
| 237 | Optional text file for specifying various configuration options | ||
| 238 | for a specific site (submodule). Each option is specified on a | ||
| 239 | separate line in the format below: | ||
| 240 | NOTE: Do not follow the lines with a semicolon, comma, or any other | ||
| 241 | separator. Non-numeric I<values> should be surrounded with quotes, either | ||
| 242 | single or double. Blank lines and lines beginning with a "#" sign as | ||
| 243 | their first non-blank character are ignored as comments. | ||
| 244 | |||
| 245 | 'option' => 'value' [,] | ||
| 246 | |||
| 247 | and the options are loaded into a hash used only by the specific | ||
| 248 | (submodule) specified. Valid options include | ||
| 249 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 250 | |||
| 251 | Options specified here override any specified in I<~/.config/StreamFinder/config>. | ||
| 252 | |||
| 253 | Among options valid for SoundCloud streams are | ||
| 254 | I<-youtube> options described in the B<new()> function. Also, | ||
| 255 | various youtube-dl (L<StreamFinder::Youtube>) configuration options, | ||
| 256 | namely I<format>, I<formatonly>, I<youtube-dl-args>, and I<youtube-dl-add-args> | ||
| 257 | can be overridden here by specifying I<youtube-format>, I<youtube-formatonly>, | ||
| 258 | I<youtube-dl-args>, and I<youtube-dl-add-args> arguments respectively. | ||
| 259 | NOTE: SoundCloud requires youtube-dl or yt-dlp to retrieve any streams! | ||
| 260 | |||
| 261 | =item ~/.config/StreamFinder/config | ||
| 262 | |||
| 263 | Optional text file for specifying various configuration options. | ||
| 264 | Each option is specified on a separate line in the format below: | ||
| 265 | |||
| 266 | 'option' => 'value' [,] | ||
| 267 | |||
| 268 | and the options are loaded into a hash used by all sites | ||
| 269 | (submodules) that support them. Valid options include | ||
| 270 | I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options. | ||
| 271 | |||
| 272 | =back | ||
| 273 | |||
| 274 | NOTE: Options specified in the options parameter list of the I<new()> | ||
| 275 | function will override those corresponding options specified in these files. | ||
| 276 | |||
| 277 | =head1 KEYWORDS | ||
| 278 | |||
| 279 | soundcloud | ||
| 280 | |||
| 281 | =head1 DEPENDENCIES | ||
| 282 | |||
| 283 | L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent> | ||
| 284 | |||
| 285 | youtube-dl (or yt-dlp, or other compatable program) | ||
| 286 | |||
| 287 | =head1 RECCOMENDS | ||
| 288 | |||
| 289 | wget | ||
| 290 | |||
| 291 | =head1 BUGS | ||
| 292 | |||
| 293 | Please report any bugs or feature requests to C<bug-streamFinder-soundcloud at rt.cpan.org>, or through | ||
| 294 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-SoundCloud>. I will be notified, and then you'll | ||
| 295 | automatically be notified of progress on your bug as I make changes. | ||
| 296 | |||
| 297 | =head1 SUPPORT | ||
| 298 | |||
| 299 | You can find documentation for this module with the perldoc command. | ||
| 300 | |||
| 301 | perldoc StreamFinder::SoundCloud | ||
| 302 | |||
| 303 | You can also look for information at: | ||
| 304 | |||
| 305 | =over 4 | ||
| 306 | |||
| 307 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 308 | |||
| 309 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-SoundCloud> | ||
| 310 | |||
| 311 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 312 | |||
| 313 | L<http://annocpan.org/dist/StreamFinder-SoundCloud> | ||
| 314 | |||
| 315 | =item * CPAN Ratings | ||
| 316 | |||
| 317 | L<http://cpanratings.perl.org/d/StreamFinder-SoundCloud> | ||
| 318 | |||
| 319 | =item * Search CPAN | ||
| 320 | |||
| 321 | L<http://search.cpan.org/dist/StreamFinder-SoundCloud/> | ||
| 322 | |||
| 323 | =back | ||
| 324 | |||
| 325 | =head1 LICENSE AND COPYRIGHT | ||
| 326 | |||
| 327 | Copyright 2023 Jim Turner. | ||
| 328 | |||
| 329 | This program is free software; you can redistribute it and/or modify it | ||
| 330 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 331 | copy of the full license at: | ||
| 332 | |||
| 333 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 334 | |||
| 335 | Any use, modification, and distribution of the Standard or Modified | ||
| 336 | Versions is governed by this Artistic License. By using, modifying or | ||
| 337 | distributing the Package, you accept this license. Do not use, modify, | ||
| 338 | or distribute the Package, if you do not accept this license. | ||
| 339 | |||
| 340 | If your Modified Version has been derived from a Modified Version made | ||
| 341 | by someone other than you, you are nevertheless required to ensure that | ||
| 342 | your Modified Version complies with the requirements of this license. | ||
| 343 | |||
| 344 | This license does not grant you the right to use any trademark, service | ||
| 345 | mark, tradename, or logo of the Copyright Holder. | ||
| 346 | |||
| 347 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 348 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 349 | otherwise transfer the Package with respect to any patent claims | ||
| 350 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 351 | Package. If you institute patent litigation (including a cross-claim or | ||
| 352 | counterclaim) against any party alleging that the Package constitutes | ||
| 353 | direct or contributory patent infringement, then this Artistic License | ||
| 354 | to you shall terminate on the date that such litigation is filed. | ||
| 355 | |||
| 356 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 357 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 358 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 359 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 360 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 361 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 362 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 363 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 364 | |||
| 365 | =cut | ||
| 366 | |||
| 367 | package StreamFinder::SoundCloud; | ||
| 368 | |||
| 369 | use strict; | ||
| 370 | use warnings; | ||
| 371 | use URI::Escape; | ||
| 372 | use HTML::Entities (); | ||
| 373 | use LWP::UserAgent (); | ||
| 374 | use parent 'StreamFinder::_Class'; | ||
| 375 | |||
| 376 | my $DEBUG = 0; | ||
| 377 | |||
| 378 | sub new | ||
| 379 | { | ||
| 380 | my $class = shift; | ||
| 381 | my $url = shift; | ||
| 382 | |||
| 383 | return undef unless ($url); | ||
| 384 | |||
| 385 | my $self = $class->SUPER::new('SoundCloud', @_); | ||
| 386 | $DEBUG = $self->{'debug'} if (defined $self->{'debug'}); | ||
| 387 | |||
| 388 | $self->{'notrim'} = 0; | ||
| 389 | while (@_) { | ||
| 390 | if ($_[0] =~ /^\-?notrim$/o) { | ||
| 391 | shift; | ||
| 392 | $self->{'notrim'} = (defined $_[0]) ? shift : 1; | ||
| 393 | } else { | ||
| 394 | shift; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | my $html = ''; | ||
| 399 | print STDERR "-0(SoundCloud): URL=$url=\n" if ($DEBUG); | ||
| 400 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 401 | $ua->timeout($self->{'timeout'}); | ||
| 402 | $ua->cookie_jar({}); | ||
| 403 | $ua->env_proxy; | ||
| 404 | |||
| 405 | (my $url2fetch = $url); | ||
| 406 | |||
| 407 | $self->{'_channel_url'} = ''; | ||
| 408 | my $tried = 0; | ||
| 409 | TRYIT: | ||
| 410 | if ($url2fetch =~ m#^https?\:#) { | ||
| 411 | $url =~ s#[\&\?].*$##; #REMOVE ANY TRAILING JUNK. | ||
| 412 | $url =~ s#\/$##; | ||
| 413 | ($self->{'id'} = $url) =~ s#^https?\:\/\/soundcloud\.\w+\/##; | ||
| 414 | } else { | ||
| 415 | $self->{'id'} = $url2fetch; | ||
| 416 | $url2fetch = 'https://soundcloud.com/' . $self->{'id'}; | ||
| 417 | } | ||
| 418 | print STDERR "-1 (try=$tried)FETCHING URL=$url2fetch= ID=".$self->{'id'}."=\n" if ($DEBUG); | ||
| 419 | my $response = $ua->get($url2fetch); | ||
| 420 | if ($response->is_success) { | ||
| 421 | $html = $response->decoded_content; | ||
| 422 | } else { | ||
| 423 | print STDERR $response->status_line if ($DEBUG); | ||
| 424 | my $no_wget = system('wget','-V'); | ||
| 425 | unless ($no_wget) { | ||
| 426 | print STDERR "\n..trying wget...\n" if ($DEBUG); | ||
| 427 | $html = `wget -t 2 -T 20 -O- -o /dev/null "$url2fetch" 2>/dev/null `; | ||
| 428 | } | ||
| 429 | } | ||
| 430 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 431 | $html =~ s/\\\"/\"\;/gs; | ||
| 432 | if ($html) { #EXTRACT METADATA, IF WE CAN: | ||
| 433 | print STDERR "-1: EXTRACTING METADATA...\n" if ($DEBUG); | ||
| 434 | if ($self->{'id'} !~ m#\/#) { #WE'RE AN ARTIST URL, FETCH 1ST (LATEST) HIT: | ||
| 435 | if ($tried) { | ||
| 436 | print STDERR "--WE'VE ALREADY TRIED, PUNT($tried)!\n" if ($DEBUG); | ||
| 437 | return undef; | ||
| 438 | } | ||
| 439 | ++$tried; | ||
| 440 | if ($html =~ m#\<h2\s+itemprop\=\"name\"\>\s*\<a\s+itemprop\=\"url\"\s+href\=\"\/?([^\"]+)#) { | ||
| 441 | $self->{'_channel_url'} = $url2fetch; | ||
| 442 | $url2fetch = $1; | ||
| 443 | $self->{'artimageurl'} = $1 if ($html =~ m#\"visual\_url\"\:\"(https?\:\/\/[^\"]+)\"#s); | ||
| 444 | $self->{'articonurl'} = $1 if ($html =~ m#\<meta\s+property\=\"twitter\:image\"\s+content\=\"(https?\:\/\/[^\"]+)\"#s); | ||
| 445 | print STDERR "---LATEST SONG ID=$url2fetch= IMGURL=".$self->{'artimageurl'}."=\n" if ($DEBUG); | ||
| 446 | goto TRYIT; | ||
| 447 | } | ||
| 448 | } else { #WE'RE A SONG URL: | ||
| 449 | my $haveYoutube = 0; | ||
| 450 | eval { require 'StreamFinder/Youtube.pm'; $haveYoutube = 1; }; | ||
| 451 | if ($haveYoutube) { | ||
| 452 | unless ($self->{'_channel_url'}) { | ||
| 453 | $self->{'_channel_url'} = $url2fetch; | ||
| 454 | $self->{'_channel_url'} =~ s#\/[^\/]+$##; | ||
| 455 | } | ||
| 456 | $self->{'title'} = $1 if ($html =~ m#\<meta\s+property\=\"twitter\:title\"\s+content\=\"(.+?)\"\>#s); | ||
| 457 | $self->{'description'} = $1 if ($html =~ m#\<meta\s+property\=\"twitter\:description\"\s+content\=\"(.+?)\"\>#s); | ||
| 458 | $self->{'artist'} = $1 if ($html =~ m#\<meta\s+itemprop\=\"name\"\s+content\=\"([^\"]+?)\"#s); | ||
| 459 | $self->{'genre'} = $1 if ($html =~ m#\<meta\s+itemprop\=\"genre\"\s+content\=\"([^\"]+?)\"#s); | ||
| 460 | $self->{'iconurl'} = ($html =~ m#\"twitter\:image\"\s+content\=\"([^\"]+)\"#) ? $1 : ''; | ||
| 461 | $self->{'imageurl'} = $self->{'iconurl'}; | ||
| 462 | $self->{'iconurl'} =~ s#\?.+$##; | ||
| 463 | if ($html =~ m#\"avatar\_url\"\:\"([^\"]+)\"#s) { | ||
| 464 | my $avitar_url = $1; | ||
| 465 | print STDERR "--AVITAR=".$avitar_url."= (WILL EXCLUDE *default_avitar*)!\n" if ($DEBUG); | ||
| 466 | $self->{'articonurl'} = $avitar_url | ||
| 467 | unless ($avitar_url =~ /default\_avatar/); | ||
| 468 | } | ||
| 469 | if ($html =~ m#\"last\_modified\"\:\"([^\"]+)\"#s) { | ||
| 470 | $self->{'created'} = $1; | ||
| 471 | $self->{'year'} = $1 if ($self->{'created'} =~ /(\d\d\d\d)/); | ||
| 472 | } | ||
| 473 | $self->{'albumartist'} = $1 if ($html =~ m#\"label\_name\"\:\"([^\"]+)\"#s); | ||
| 474 | |||
| 475 | my %globalArgs = ('-noiframes' => 1, '-fast' => 1, '-format' => 'any', | ||
| 476 | -debug => $DEBUG | ||
| 477 | ); | ||
| 478 | foreach my $arg (qw(secure log logfmt youtube-format youtube-format-fallback | ||
| 479 | youtube-formatonly youtube-dl-args youtube-dl-add-args)) { | ||
| 480 | (my $arg0 = $arg) =~ s/^youtube\-(?!dl)//o; | ||
| 481 | $globalArgs{$arg0} = $self->{$arg} if (defined $self->{$arg}); | ||
| 482 | } | ||
| 483 | my $yt = new StreamFinder::Youtube($url2fetch, %globalArgs); | ||
| 484 | if ($yt && $yt->count() > 0) { | ||
| 485 | my @ytStreams = $yt->get(); | ||
| 486 | foreach my $field (qw(title description)) { | ||
| 487 | $self->{$field} ||= $yt->{$field} if (defined($yt->{$field}) && $yt->{$field}); | ||
| 488 | } | ||
| 489 | print STDERR "i:Found stream(s) (".join('|',@ytStreams).") via youtube-dl.\n" if ($DEBUG); | ||
| 490 | @{$self->{'streams'}} = @ytStreams; | ||
| 491 | $self->{'cnt'} = scalar @ytStreams; | ||
| 492 | } | ||
| 493 | } else { | ||
| 494 | print STDERR "f:StreamFinder::Youtube REQUIRED to fetch SoundCloud streams, exiting!\n"; | ||
| 495 | return undef; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | } | ||
| 499 | return undef unless ($self->{'id'}); | ||
| 500 | |||
| 501 | foreach my $field (qw(description artist title genre)) { | ||
| 502 | $self->{$field} = HTML::Entities::decode_entities($self->{$field}); | ||
| 503 | $self->{$field} = uri_unescape($self->{$field}); | ||
| 504 | $self->{$field} =~ s/(?:\%|\\?u?00)([0-9A-Fa-f]{2})/chr(hex($1))/eg; | ||
| 505 | } | ||
| 506 | $self->{'total'} = $self->{'cnt'}; | ||
| 507 | $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : ''; | ||
| 508 | print STDERR "--SUCCESS: 1st stream=".$self->{'Url'}."= total=".$self->{'total'}."=\n" | ||
| 509 | if ($DEBUG && $self->{'cnt'} > 0); | ||
| 510 | print STDERR "\n--ID=".$self->{'id'}."=\n--TITLE=".$self->{'title'}."\n--ARTIST=".$self->{'artist'}."=\n--STREAMS=".join('|',@{$self->{'streams'}})."=\n" if ($DEBUG); | ||
| 511 | $self->_log($url); | ||
| 512 | |||
| 513 | bless $self, $class; #BLESS IT! | ||
| 514 | |||
| 515 | return $self; | ||
| 516 | } | ||
| 517 | |||
| 518 | sub fetchChannelPage { | ||
| 519 | my $self = shift; | ||
| 520 | my $whichImg = shift; | ||
| 521 | |||
| 522 | unless ($self->{'articonurl'}) { | ||
| 523 | my $html = ''; | ||
| 524 | return '' unless ($self->{'_channel_url'} && $self->{'_channel_url'} =~ m#^https?\:\/\/#); | ||
| 525 | |||
| 526 | my $url2fetch = $self->{'_channel_url'}; | ||
| 527 | print STDERR "-0(Fetch SoundCloud Channel for artist. icon from $url2fetch): \n" if ($DEBUG); | ||
| 528 | my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}}); | ||
| 529 | $ua->timeout($self->{'timeout'}); | ||
| 530 | $ua->cookie_jar({}); | ||
| 531 | $ua->env_proxy; | ||
| 532 | my $response = $ua->get($url2fetch); | ||
| 533 | if ($response->is_success) { | ||
| 534 | $html = $response->decoded_content; | ||
| 535 | } | ||
| 536 | |||
| 537 | print STDERR "-1: html=$html=\n" if ($DEBUG > 1); | ||
| 538 | return '' unless ($html); | ||
| 539 | |||
| 540 | $self->{'artimageurl'} = $1 if ($html =~ m#\"visual\_url\"\:\"(https?\:\/\/[^\"]+)\"#s); | ||
| 541 | $self->{'articonurl'} = $1 if ($html =~ m#\<meta\s+property\=\"twitter\:image\"\s+content\=\"(https?\:\/\/[^\"]+)\"#s); | ||
| 542 | print STDERR "--ART ($whichImg) URL=".$self->{$whichImg}."=\n" if ($DEBUG); | ||
| 543 | } | ||
| 544 | return $self->{$whichImg}; | ||
| 545 | } | ||
| 546 | |||
| 547 | |||
| 548 | sub getIconURL | ||
| 549 | { | ||
| 550 | my $self = shift; | ||
| 551 | return $self->{'iconurl'} unless (defined($_[0]) && $_[0] =~ /^\-?artist/i); | ||
| 552 | return $self->fetchChannelPage('articonurl'); | ||
| 553 | } | ||
| 554 | |||
| 555 | sub getImageURL | ||
| 556 | { | ||
| 557 | my $self = shift; | ||
| 558 | return $self->{'iconurl'} unless (defined($_[0]) && $_[0] =~ /^\-?artist/i); | ||
| 559 | return $self->fetchChannelPage('artimageurl'); | ||
| 560 | } | ||
| 561 | |||
| 562 | 1 |