WatchLater/YouTube.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 68.7% 11 / 16 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 81.8% 81 / 99 CC 12 SCAR 10.8
CC
12
Cov
84%
new
4.7
request
4.4
find_video_id
4.0
CRAP 2.9
« WatchLater.pm ↑ Summary
1 package App::WatchLater::YouTube;
2  
3 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 App::WatchLater::YouTube - The YouTube Data API
10  
11 =head1 VERSION
12  
13 Version 0.03
14  
15 =cut
16  
17 3 our $VERSION = '0.03';
18  
19  
20 =head1 SYNOPSIS
21  
22 This is a simple module for making requests to the YouTube Data API.
23 Authorization is required, and can be obtained by registering for an API key
24 from the Google Developer L<API
25 Console|https://console.developers.google.com/apis/credentials>. Alternatively,
26 obtain user authorization through OAuth2 using the yt-oauth(1) script.
27  
28 my $api = App::WatchLater::YouTube->new(
29 access_token => ...,
30 api_key => ...,
31 );
32  
33 # returns the body of the HTTP response as a string
34 my $body = $api->request('GET', '/videos', {
35 id => 'Ks-_Mh1QhMc',
36 part => 'snippet'
37 });
38 ...
39  
40 =head1 EXPORT
41  
42 =over 4
43  
44 =item *
45  
46 C<find_video_id> - exported by default.
47  
48 =back
49  
50 =cut
51  
52 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 require Exporter;
54 3 our @ISA = qw(Exporter);
55 3 our @EXPORT = qw(find_video_id);
56 3 our @EXPORT_OK = qw(find_video_id);
57 }
58  
59 =head1 SUBROUTINES/METHODS
60  
61 =head2 find_video_id
62  
63 my $video_id = find_video_id($url);
64  
65 Find a YouTube video ID from a YouTube watch URL. Also accepts I<youtu.be>
66 shortened URLs and literal video IDs.
67  
68 =cut
69  
70 3 my $regex = qr/[a-z0-9_-]+/i;
71  
72 4 ▶sub find_video_id {
Subroutine find_video_id: called CC=4 SCAR=13.9
Pod documented
73 4 local $_ = shift;
74 4 ▶ return $1 if m{youtube\.com/watch.*\bv=($regex)};
Branch: 1 branch
BranchTrueFalseTotal
if m[youtube\.com/watch.*\bv=($regex)]u 1 3 4
75 3 ▶ return $1 if m{youtu.be/($regex)};
Branch: 1 branch
BranchTrueFalseTotal
if m[youtu.be/($regex)]u 1 2 3
76 2 ▶ return $1 if m{^($regex)$};
Branch: 1 branch
BranchTrueFalseTotal
if /^($regex)$/u 1 1 2
77 1 die "'$_' is not a valid video ID";
78 }
79  
80 =head2 new
81  
82 my $api = App::WatchLater::YouTube->new(%opts)
83  
84 This constructor returns a new API object. Attributes include:
85  
86 =over 4
87  
88 =item *
89  
90 C<http> - an instance of HTTP::Tiny. If none is provided, a default new instance
91 is used.
92  
93 =item *
94  
95 C<api_key> - an API key.
96  
97 =item *
98  
99 C<access_token> - an OAuth2 access token.
100  
101 =back
102  
103 At least one of C<api_key> and C<access_token> must be provided. If both are
104 provided, C<access_token> is used for authorization.
105  
106 =cut
107  
108 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
113 3 my ($ok, $why) = HTTP::Tiny->can_ssl;
114 3 ▶ croak $why unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 3 3
115 }
116  
117 1 ▶sub new {
Subroutine new: called CC=4 SCAR=15.5
Pod documented
118 1 my ($class, %opts) = @_;
119  
120 1 ▶ my $http = $opts{http} // HTTP::Tiny->new;
Condition33%1/3
$opts{"http"} // "HTTP::Tiny"->new
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{"http"} // "HTTP::Tiny"->new
$opts{"http"} "HTTP::Tiny"->new
Truth table33%1/3
A // B
A= $opts{"http"}B= "HTTP::Tiny"->new
ABresult
1X1
011
000
121 1 my $key = $opts{api_key};
122 1 my $token = $opts{access_token};
123  
124 1 ▶ defined $key || defined $token
Branch: 1 branch
BranchTrueFalseTotal
defined $key or defined $token or croak("no API key or access token, aborting") 1 0 1
Condition33%1/3
defined $key or defined $token
l!l&&r!l&&!r
100
MC/DC0%0/2
defined $key or defined $token
defined $key defined $token
Truth table33%1/3
A or B
A= defined $keyB= defined $token
ABresult
1X1
011
000
125 or croak "no API key or access token, aborting";
126  
127 1 bless {
128 http => $http,
129 key => $key,
130 token => $token,
131 } => $class;
132 }
133  
134 =head2 request
135  
136 my $body = $api->request($method, $endpoint, %params);
137  
138 Send a request to the specified API endpoint using the given HTTP method. Query
139 parameters may be specified in C<%params>. Croaks if the request fails.
140  
141 =cut
142  
143 1 ▶sub request {
Subroutine request: called CC=4 SCAR=14.8
Pod documented
144 1 my ($self, $method, $endpoint, %params) = @_;
145 1 my $url = 'https://www.googleapis.com/youtube/v3' . $endpoint;
146  
147 1 my %headers;
148  
149 1 ▶ if (defined $self->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'token'}) { } 0 1 1
150 0 $headers{Authorization} = 'Bearer ' . $self->{token};
151 } else {
152 1 ▶ $params{key} ||= $self->{key};
Condition33%1/3
$params{"key"} ||= $self->{"key"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$params{"key"} ||= $self->{"key"}
$params{"key"} $self->{"key"}
Truth table33%1/3
A ||= B
A= $params{"key"}B= $self->{"key"}
ABresult
1X1
011
000
153 }
154  
155 1 my $query = $self->{http}->www_form_urlencode(\%params);
156 1 my $response = $self->{http}->request($method, "$url?$query", {
157 headers => \%headers,
158 });
159 1 ▶ croak "$response->{status} $response->{reason}" unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 1 1
160 1 $response->{content};
161 }
162  
163 =head2 get_video
164  
165 my \%snippet = $api->get_video($video_id);
166  
167 Retrieves a YouTube video resource, including the snippet, for the video given
168 by C<$video_id>. Croaks if no such video is found.
169  
170 =cut
171  
172 1 ▶sub get_video {
Subroutine get_video: called CC=2 SCAR=7.0
Pod documented
173 1 my ($self, $video_id) = @_;
174 1 my $json = $self->request(
175 'GET', '/videos',
176 id => $video_id,
177 part => 'snippet',
178 );
179 1 my $obj = decode_json($json);
180 1 ▶ my $item = $obj->{items}[0] or croak "no video with id $video_id";
Branch: 1 branch
BranchTrueFalseTotal
my $item = $obj->{"items"}[0] or croak("no video with id $video_id") 1 0 1
181 1 $item->{snippet};
182 }
183  
184 =head1 AUTHOR
185  
186 Aaron L. Zeng, C<< <me at bcc32.com> >>
187  
188 =head1 BUGS
189  
190 Please report any bugs or feature requests to C<bug-app-watchlater at rt.cpan.org>, or through
191 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-WatchLater>. I will be notified, and then you'll
192 automatically be notified of progress on your bug as I make changes.
193  
194  
195  
196  
197 =head1 SUPPORT
198  
199 You can find documentation for this module with the perldoc command.
200  
201 perldoc App::WatchLater::YouTube
202  
203  
204 You can also look for information at:
205  
206 =over 4
207  
208 =item * RT: CPAN's request tracker (report bugs here)
209  
210 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-WatchLater>
211  
212 =item * AnnoCPAN: Annotated CPAN documentation
213  
214 L<http://annocpan.org/dist/App-WatchLater>
215  
216 =item * CPAN Ratings
217  
218 L<http://cpanratings.perl.org/d/App-WatchLater>
219  
220 =item * Search CPAN
221  
222 L<http://search.cpan.org/dist/App-WatchLater/>
223  
224 =back
225  
226  
227 =head1 ACKNOWLEDGEMENTS
228  
229  
230 =head1 LICENSE AND COPYRIGHT
231  
232 Copyright 2017 Aaron L. Zeng.
233  
234 This program is distributed under the MIT (X11) License:
235 L<http://www.opensource.org/licenses/mit-license.php>
236  
237 Permission is hereby granted, free of charge, to any person
238 obtaining a copy of this software and associated documentation
239 files (the "Software"), to deal in the Software without
240 restriction, including without limitation the rights to use,
241 copy, modify, merge, publish, distribute, sublicense, and/or sell
242 copies of the Software, and to permit persons to whom the
243 Software is furnished to do so, subject to the following
244 conditions:
245  
246 The above copyright notice and this permission notice shall be
247 included in all copies or substantial portions of the Software.
248  
249 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
250 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
251 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
252 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
253 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
254 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
255 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
256 OTHER DEALINGS IN THE SOFTWARE.
257  
258  
259 =cut
260  
261 3 1; # End of App::WatchLater::YouTube
« WatchLater.pm ↑ Summary