StreamFinder/Vimeo.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 332 CC 48 SCAR 76.1
CC
48
Cov
0%
new
2352.0
getImageData
2.0
BEGIN
2.0
CRAP 2016.3
1 =head1 NAME
2  
3 StreamFinder::Vimeo - Fetch actual raw streamable URLs from Vimeo.com.
4  
5 =head1 AUTHOR
6  
7 This module is Copyright (C) 2017-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::Vimeo;
26  
27 die "..usage: $0 ID|URL\n" unless ($ARGV[0]);
28  
29 my $video = new StreamFinder::Vimeo($ARGV[0]);
30  
31 die "Invalid URL or no streams found!\n" unless ($video);
32  
33 my $firstStream = $video->get();
34  
35 print "First Stream URL=$firstStream\n";
36  
37 my $url = $video->getURL();
38  
39 print "Stream URL=$url\n";
40  
41 my $videoTitle = $video->getTitle();
42
43 print "Title=$videoTitle\n";
44
45 my $videoDescription = $video->getTitle('desc');
46
47 print "Description=$videoDescription\n";
48
49 my $videoID = $video->getID();
50  
51 print "Video ID=$videoID\n";
52
53 my $artist = $video->{'artist'};
54  
55 print "Artist=$artist\n" if ($artist);
56
57 my $albumartist = $video->{'albumartist'};
58  
59 print "Album Artist=$albumartist\n" if ($albumartist);
60
61 my $icon_url = $video->getIconURL();
62  
63 if ($icon_url) { #SAVE THE ICON TO A TEMP. FILE:
64  
65 my ($image_ext, $icon_image) = $video->getIconData();
66  
67 if ($icon_image && open IMGOUT, ">/tmp/${videoID}.$image_ext") {
68  
69 binmode IMGOUT;
70  
71 print IMGOUT $icon_image;
72  
73 close IMGOUT;
74  
75 }
76  
77 }
78  
79 my $stream_count = $video->count();
80  
81 print "--Stream count=$stream_count=\n";
82  
83 my @streams = $video->get();
84  
85 foreach my $s (@streams) {
86  
87 print "------ stream URL=$s=\n";
88  
89 }
90  
91 =head1 DESCRIPTION
92  
93 StreamFinder::Vimeo accepts a valid full Vimeo video ID or URL on
94 vimeo.com and returns the actual stream URL, title, and cover art icon
95 for that video. The purpose is that one needs this URL in order to have
96 he option to stream the video in one's own choice of media player
97 software rather than using their web browser and accepting any / all
98 flash, ads, javascript, cookies, trackers, web-bugs, and other crapware
99 that can come with that method of play. The author uses his own
100 custom all-purpose media player called "fauxdacious" (his custom hacked
101 version of the open-source "audacious" audio player). "fauxdacious"
102 incorporates this module to decode and play vimeo.com videos. This is a
103 submodule of the general StreamFinder module.
104  
105 Depends:
106  
107 L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>
108  
109 =head1 SUBROUTINES/METHODS
110  
111 =over 4
112  
113 =item B<new>(I<ID>|I<url> [, I<-debug> [ => 0|1|2 ]]
114 [, "-quality" => I<quality>] [, I<-secure> [ => 0|1 ]])
115  
116 Accepts a vimeo.com ID or URL and creates and returns a new video object,
117 or I<undef> if the URL is not a valid Vimeo video or no streams are
118 found. The URL can be the full URL,
119 ie. https://player.vimeo.com/video/B<video-id>, or just I<video-id>.
120  
121 The optional I<-quality> argument, which can be set to a "p number" optionally
122 preceeded by a relational operator ("<", ">", "=") - default: "<".
123 This limits the video quality.. For example: "720" would mean select
124 a stream "<= 720p", ">720" would mean ">= 720p", and "=1080" would
125 mean "only "1080p". See also the "vimeo_quality" config. file option
126 that does the same thing. Default is just use the best quality found.
127  
128 The optional I<-secure> argument can be either 0 or 1 (I<false> or I<true>). If 1
129 then only secure ("https://") streams will be returned.
130  
131 DEFAULT I<-secure> is 0 (false) - return all streams (http and https).
132  
133 Additional options:
134  
135 Certain youtube-dl (L<StreamFinder::Youtube>) configuration options,
136 namely I<format>, I<formatonly>, I<youtube-dl-args>, and I<youtube-dl-add-args>
137 can be overridden here by specifying I<youtube-format>, I<youtube-formatonly>,
138 I<youtube-dl-args>, and I<youtube-dl-add-args> arguments respectively. It is
139 however, recommended to specify these in the Vimeo-specific
140 configuration file (see B<CONFIGURATION FILES> below. NOTE: These are only
141 applicable when no streams are found the normal way and the user has youtube-dl
142 or a compatable program, such as yt-dlp installed.
143  
144 I<-log> => "I<logfile>"
145  
146 Specify path to a log file. If a valid and writable file is specified, A line will be
147 appended to this file every time one or more streams is successfully fetched for a url.
148  
149 DEFAULT I<-none-> (no logging).
150  
151 I<-logfmt> specifies a format string for lines written to the log file.
152  
153 DEFAULT "I<[time] [url] - [site]: [title] ([total])>".
154  
155 The valid field I<[variables]> are: [stream]: The url of the first/best stream found.
156 [site]: The site name (Vimeo). [url]: The url searched for streams.
157 [time]: Perl timestamp when the line was logged. [title], [artist], [album],
158 [description], [year], [genre], [total], [albumartist]: The corresponding field data
159 returned (or "I<-na->", if no value).
160  
161 =item $video->B<get>()
162  
163 Returns an array of strings representing all stream URLs found.
164  
165 =item $video->B<getURL>([I<options>])
166  
167 Similar to B<get>() except it only returns a single stream representing
168 the first valid stream found.
169  
170 Current options are: I<"random">, I<"nopls">, and I<"noplaylists">.
171 By default, the first ("best"?) stream is returned. If I<"random"> is
172 specified, then a random one is selected from the list of streams found.
173 If I<"nopls"> is specified, and the stream to be returned is a ".pls" playlist,
174 it is first fetched and the first entry (or a random entry if I<"random"> is
175 specified) is returned. This is needed by Fauxdacious Mediaplayer.
176 If I<"noplaylists"> is specified, and the stream to be returned is a
177 "playlist" (either .pls or .m3u? extension), it is first fetched and the first
178 entry (or a random entry if I<"random"> is specified) in the playlist
179 is returned.
180  
181 =item $video->B<count>()
182  
183 Returns the number of streams found for the video.
184  
185 =item $video->B<getID>()
186  
187 Returns the video's Vimeo ID (numeric).
188  
189 =item $video->B<getTitle>(['desc'])
190  
191 Returns the video's title, or (long description).
192  
193 =item $video->B<getIconURL>(['artist'])
194  
195 Returns the URL for the video's "cover art" icon image, if any.
196 If B<'artist'> is specified, the channel artist's icon url is returned,
197 if any.
198  
199 =item $video->B<getIconData>(['artist'])
200  
201 Returns a two-element array consisting of the extension (ie. "png",
202 "gif", "jpeg", etc.) and the actual icon image (binary data), if any.
203 If B<'artist'> is specified, the channel artist's icon data is returned,
204 if any.
205  
206 =item $video->B<getImageURL>()
207  
208 Returns the URL for the video's "cover art" (usually larger)
209 banner image.
210  
211 =item $video->B<getImageData>()
212  
213 Returns a two-element array consisting of the extension (ie. "png",
214 "gif", "jpeg", etc.) and the actual video's banner image (binary data).
215  
216 =item $video->B<getType>()
217  
218 Returns the video's type ("Vimeo").
219  
220 =back
221  
222 =head1 CONFIGURATION FILES
223  
224 The default root location directory for StreamFinder configuration files
225 is "~/.config/StreamFinder". To use an alternate location directory,
226 specify it in the "I<STREAMFINDER>" environment variable, ie.:
227 B<$ENV{STREAMFINDER} = "/etc/StreamFinder">.
228  
229 =over 4
230  
231 =item ~/.config/StreamFinder/Vimeo/config
232  
233 Optional text file for specifying various configuration options
234 for a specific site (submodule). Each option is specified on a
235 separate line in the format below:
236 NOTE: Do not follow the lines with a semicolon, comma, or any other
237 separator. Non-numeric I<values> should be surrounded with quotes, either
238 single or double. Blank lines and lines beginning with a "#" sign as
239 their first non-blank character are ignored as comments.
240  
241 'option' => 'value' [,]
242  
243 and the options are loaded into a hash used only by the specific
244 (submodule) specified. Valid options include
245 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
246  
247 Options specified here override any specified in I<~/.config/StreamFinder/config>.
248  
249 Among options valid for Vimeo videos are the I<-vimeo_quality>
250 option, which can be set to a "p" number optionally preceeded by
251 a relational operator ("<", ">", "=") - default: "<". This limits
252 the video quality. For example: "720" would mean select a stream
253 "<= 720p", ">720" would mean ">= 720p", and "=1080" would mean "only
254 "1080p". NOTE: The "<" and ">" are "inclusive", actually meaning "<=" and
255 ">=" respectively! This can be overridden with the I<-quality> argument to
256 the new() function. Also, various youtube-dl (L<StreamFinder::Youtube>)
257 configuration options, namely I<format>, I<formatonly>, I<youtube-dl-args>,
258 and I<youtube-dl-add-args> can be overridden here by specifying
259 I<youtube-format>, I<youtube-formatonly>, I<youtube-dl-args>, and
260 I<youtube-dl-add-args> arguments respectively.
261 NOTE: These are only applicable when no streams are found the normal way and
262 the user has youtube-dl or a compatable program, such as yt-dlp installed.
263  
264 =item ~/.config/StreamFinder/config
265  
266 Optional text file for specifying various configuration options.
267 Each option is specified on a separate line in the format below:
268  
269 'option' => 'value' [,]
270  
271 and the options are loaded into a hash used by all sites
272 (submodules) that support them. Valid options include
273 I<-debug> => [0|1|2] and most of the L<LWP::UserAgent> options.
274  
275 =back
276  
277 NOTE: Options specified in the options parameter list of the I<new()>
278 function will override those corresponding options specified in these files.
279  
280 =head1 KEYWORDS
281  
282 vimeo
283  
284 =head1 DEPENDENCIES
285  
286 L<URI::Escape>, L<HTML::Entities>, L<LWP::UserAgent>
287  
288 =head1 RECCOMENDS
289  
290 youtube-dl (or yt-dlp, or other compatable program)
291  
292 wget
293  
294 =head1 BUGS
295  
296 Please report any bugs or feature requests to C<bug-streamFinder-vimeo at rt.cpan.org>, or through
297 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=StreamFinder-Vimeo>. I will be notified, and then you'll
298 automatically be notified of progress on your bug as I make changes.
299  
300 =head1 SUPPORT
301  
302 You can find documentation for this module with the perldoc command.
303  
304 perldoc StreamFinder::Vimeo
305  
306 You can also look for information at:
307  
308 =over 4
309  
310 =item * RT: CPAN's request tracker (report bugs here)
311  
312 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=StreamFinder-Vimeo>
313  
314 =item * AnnoCPAN: Annotated CPAN documentation
315  
316 L<http://annocpan.org/dist/StreamFinder-Vimeo>
317  
318 =item * Search CPAN
319  
320 L<http://search.cpan.org/dist/StreamFinder-Vimeo/>
321  
322 =back
323  
324 =head1 LICENSE AND COPYRIGHT
325  
326 Copyright 2017-2023 Jim Turner.
327  
328 This program is free software; you can redistribute it and/or modify it
329 under the terms of the the Artistic License (2.0). You may obtain a
330 copy of the full license at:
331  
332 L<http://www.perlfoundation.org/artistic_license_2_0>
333  
334 Any use, modification, and distribution of the Standard or Modified
335 Versions is governed by this Artistic License. By using, modifying or
336 distributing the Package, you accept this license. Do not use, modify,
337 or distribute the Package, if you do not accept this license.
338  
339 If your Modified Version has been derived from a Modified Version made
340 by someone other than you, you are nevertheless required to ensure that
341 your Modified Version complies with the requirements of this license.
342  
343 This license does not grant you the right to use any trademark, service
344 mark, tradename, or logo of the Copyright Holder.
345  
346 This license includes the non-exclusive, worldwide, free-of-charge
347 patent license to make, have made, use, offer to sell, sell, import and
348 otherwise transfer the Package with respect to any patent claims
349 licensable by the Copyright Holder that are necessarily infringed by the
350 Package. If you institute patent litigation (including a cross-claim or
351 counterclaim) against any party alleging that the Package constitutes
352 direct or contributory patent infringement, then this Artistic License
353 to you shall terminate on the date that such litigation is filed.
354  
355 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
356 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
357 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
358 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
359 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
360 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
361 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
362 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363  
364 =cut
365  
366 package StreamFinder::Vimeo;
367  
368 use strict;
369 use warnings;
370 use URI::Escape;
371 use HTML::Entities ();
372 use LWP::UserAgent ();
373 use parent 'StreamFinder::_Class';
374  
375 my $DEBUG = 0;
376  
377 sub new
378 {
379 my $class = shift;
380 my $url = shift;
381  
382 return undef unless ($url);
383 # return undef if ($url =~ /\bplayer\.vimeo\b/);
384  
385 my $self = $class->SUPER::new('Vimeo', @_);
386 $DEBUG = $self->{'debug'} if (defined $self->{'debug'});
387  
388 while (@_) {
389 if ($_[0] =~ /^\-?quality$/o) {
390 shift;
391 $self->{'vimeo_quality'} = (defined $_[0]) ? shift : 0;
392 } else {
393 shift;
394 }
395 }
396  
397 print STDERR "-0(Vimeo): URL=$url= quality=".$self->{'vimeo_quality'}."=\n" if ($DEBUG);
398 if ($url =~ m#^https?\:#) {
399 $self->{'id'} = $1 if ($url =~ m#\/([^\/]+)\/?$#);
400 $self->{'id'} =~ s/[\?\&].*$//;
401 } else {
402 $self->{'id'} = $url;
403 }
404 my $player_url = 'https://player.vimeo.com/video/'. $self->{'id'};
405 print STDERR "-1 FETCHING URL=$player_url= ID=".$self->{'id'}."=\n" if ($DEBUG);
406 $self->{'genre'} = 'Video';
407 $self->{'albumartist'} = $player_url;
408  
409 #VIMEO VIDEOS BEST SCANNED MANUALLY!:
410  
411 my $html = '';
412 my $ua = LWP::UserAgent->new(@{$self->{'_userAgentOps'}});
413 $ua->timeout($self->{'timeout'});
414 $ua->cookie_jar({});
415 $ua->env_proxy;
416 my $response = $ua->get($player_url);
417 if ($response->is_success) {
418 $html = $response->decoded_content;
419 } else {
420 print STDERR $response->status_line if ($DEBUG);
421 my $no_wget = system('wget','-V');
422 unless ($no_wget) {
423 print STDERR "\n..trying wget...\n" if ($DEBUG);
424 $html = `wget -t 2 -T 20 -O- -o /dev/null "$player_url" 2>/dev/null `;
425 }
426 }
427 $html =~ s/\\\"/\&quot\;/gs;
428 $self->{'title'} = $1 if ($html =~ m#\"video\"\:\{\"id\"\:\"?$$self{'id'}\"?\,\"title\"\:\"([^\"]+)#s);
429 $self->{'title'} ||= ($html =~ m#\<title\>([^\<]+)#) ? $1 : '';
430 $self->{'imageurl'} = $1 if ($html =~ m#background\:\s+url\s*\(([^\)]+)#s) ? $1 : '';
431 if ($html =~ m#\"thumbs\"\:\{([^\}]+)#s) {
432 my $thumbnails = $1;
433 my %thumbhash = ();
434 while ($thumbnails =~ s#\"(\d+)\"\:\"(https?\:[^\"]+)\"##s) {
435 $thumbhash{$1} = $2;
436 }
437 #USUALLY MULTIPLE URLS OF VARYING SIZES FOR SAME IMAGE, SO WE USE THE
438 #SMALLEST ONE FOR iconurl AND LARGEST FOR imageurl:
439 foreach my $res (sort { $a <=> $b } keys %thumbhash) {
440 $self->{'iconurl'} ||= $thumbhash{$res};
441 $self->{'imageurl'} = $thumbhash{$res};
442 }
443 }
444 $self->{'description'} = $1 if ($html =~ m#\"description\"\:\"([^\"]+)#s);
445 if ($html =~ m#\"progressive\"\:(\[\{[^\]]+\])#s) {
446 (my $s = $1) =~ s/\"\:/\" \=\> /gs;
447 my $v;
448 eval "\$v = $s";
449 my %streams;
450 my $cnt = 0;
451 my $quality = defined ($self->{'vimeo_quality'}) ? $self->{'vimeo_quality'} : 0;
452 my $direction = ($quality =~ s/^([\<\=\>])//) ? $1 : '<';
453 print STDERR "--USER-REQ. QUALITY=$quality= DIR=$direction=\n" if ($DEBUG);
454 foreach my $i (@{$v}) {
455 $i->{'quality'} =~ s/\D+//o;
456 $streams{$i->{'quality'}} = $i->{'url'} if (!$quality || ($i->{'quality'} == $quality)
457 || ($direction eq '<' && $i->{'quality'} <= $quality)
458 || ($direction eq '>' && $i->{'quality'} >= $quality));
459 ++$cnt;
460 }
461 if ($cnt) {
462 foreach my $i (sort { $a <=> $b } keys %streams) {
463 unless ($self->{'secure'} && $streams{$i} !~ /^https/o) {
464 print STDERR "**** VIMEO STREAM FOUND: QUALITY($i)=$streams{$i}=\n" if ($DEBUG);
465 unshift @{$self->{'streams'}}, $streams{$i};
466 $self->{'cnt'}++;
467 }
468 }
469 if ($cnt) {
470 $self->{'artist'} = $1 if ($html =~ s#\"owner\"\:\{.*?\"name\"\:\"([^\"]+)\"#STREAMFINDERMARK#s);
471 $self->{'articonurl'} = $1 if ($html =~ m#STREAMFINDERMARK.*?\"img\"\:\"([^\"]+)\"#s);
472 print "----ALT ART (CHANNEL) ICON=".$self->{'articonurl'}."=\n" if ($DEBUG);
473 $self->{'albumartist'} = $1 if ($html =~ s#STREAMFINDERMARK.*?\"url\"\:\"([^\"]+)\"##s);
474 }
475 }
476 }
477  
478 #IF WE DIDN'T FIND ANY STREAMS IN THE PAGE, TRY youtube-dl:
479 unless ($self->{'cnt'} > 0) {
480 $url =~ s/\?autoplay\=true$//; #STRIP THIS OFF SO WE DON'T HAVE TO.
481 my $url2fetch = $url;
482 #DEPRECIATED (VIDEO-IDS NOW INCLUDE STUFF BEFORE THE DASH: ($self->{'id'} = $url) =~ s#^.*\-([a-z]\d+)\/?$#$1#;
483 $url2fetch = 'https://www.vimeo.com/' . $url unless ($url2fetch =~ m#^https?\:#);
484 my $haveYoutube = 0;
485 eval { require 'StreamFinder/Youtube.pm'; $haveYoutube = 1; };
486 print STDERR "\n-2 NO STREAMS FOUND IN PAGE (haveYoutube=$haveYoutube)\n" if ($DEBUG && $self->{'cnt'} <= 0);
487 if ($haveYoutube) {
488 print STDERR "\n-2 TRYING youtube-dl...\n" if ($DEBUG);
489 my %globalArgs = (
490 '-noiframes' => 1, '-fast' => 1, '-debug' => $DEBUG
491 );
492 foreach my $arg (qw(secure log logfmt youtube-format youtube-format-fallback
493 youtube-formatonly youtube-dl-args youtube-dl-add-args)) {
494 (my $arg0 = $arg) =~ s/^youtube\-(?!dl)//o;
495 $globalArgs{$arg0} = $self->{$arg} if (defined $self->{$arg});
496 }
497 my $yt = new StreamFinder::Youtube($url2fetch, %globalArgs);
498 if ($yt && $yt->count() > 0) {
499 my @ytStreams = $yt->get();
500 push @{$self->{'streams'}}, @ytStreams;
501 foreach my $field (qw(title description iconurl)) {
502 $self->{$field} ||= $yt->{$field} if (defined($yt->{$field}) && $yt->{$field});
503 }
504 $self->{'cnt'} = scalar @{$self->{'streams'}};
505 print STDERR "i:Found stream(s) (".join('|',@ytStreams).") via youtube-dl.\n" if ($DEBUG);
506 }
507 }
508 $self->{'imageurl'} ||= $self->{'iconurl'};
509 print STDERR "-count=".$self->{'cnt'}."= iconurl=".$self->{'iconurl'}."=\n" if ($DEBUG);
510 if ($self->{'description'} =~ /\w/) {
511 $self->{'description'} =~ s/\s+$//;
512 $self->{'description'} = HTML::Entities::decode_entities($self->{'description'});
513 $self->{'description'} = uri_unescape($self->{'description'});
514 }
515 }
516 $self->{'title'} = HTML::Entities::decode_entities($self->{'title'});
517 $self->{'title'} = uri_unescape($self->{'title'});
518 $self->{'description'} ||= $self->{'title'};
519 $self->{'iconurl'} ||= $self->{'imageurl'} if ($self->{'imageurl'});
520 $self->{'iconurl'} ||= $self->{'articonurl'} if ($self->{'articonurl'});
521 $self->{'total'} = $self->{'cnt'};
522 $self->{'Url'} = ($self->{'total'} > 0) ? $self->{'streams'}->[0] : '';
523 print STDERR "--SUCCESS: 1st stream=".$self->{'Url'}."= total=".$self->{'total'}."=\n"
524 if ($DEBUG && $self->{'cnt'} > 0);
525 print STDERR "\n--ID=".$self->{'id'}."=\n--TITLE=".$self->{'title'}."=\n--CNT=".$self->{'cnt'}."=\n--ICON=".$self->{'iconurl'}."=\n--1ST=".$self->{'Url'}."=\n--streams=".join('|',@{$self->{'streams'}})."=\n" if ($DEBUG);
526 $self->_log($url);
527  
528 bless $self, $class; #BLESS IT!
529  
530 return $self;
531 }
532  
533 sub getImageData
534 {
535 my $self = shift;
536 return $self->getIconData();
537 }
538  
539 1