line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::YTSearch; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Search YouTube |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.0304'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
1183
|
use strictures 2; |
|
1
|
|
|
|
|
1717
|
|
|
1
|
|
|
|
|
45
|
|
9
|
1
|
|
|
1
|
|
236
|
use Carp qw(croak); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
50
|
|
10
|
1
|
|
|
1
|
|
12
|
use Mojo::UserAgent (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
11
|
1
|
|
|
1
|
|
5
|
use Mojo::JSON qw(decode_json); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
12
|
1
|
|
|
1
|
|
7
|
use Mojo::URL (); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
14
|
|
13
|
1
|
|
|
1
|
|
596
|
use Moo; |
|
1
|
|
|
|
|
10683
|
|
|
1
|
|
|
|
|
4
|
|
14
|
1
|
|
|
1
|
|
2072
|
use Try::Tiny; |
|
1
|
|
|
|
|
1315
|
|
|
1
|
|
|
|
|
55
|
|
15
|
1
|
|
|
1
|
|
470
|
use namespace::clean; |
|
1
|
|
|
|
|
9555
|
|
|
1
|
|
|
|
|
6
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has key => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
required => 1, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has base => ( |
25
|
|
|
|
|
|
|
is => 'rw', |
26
|
|
|
|
|
|
|
default => sub { 'https://www.googleapis.com' }, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has ua => ( |
31
|
|
|
|
|
|
|
is => 'rw', |
32
|
|
|
|
|
|
|
default => sub { Mojo::UserAgent->new }, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub search { |
37
|
1
|
|
|
1
|
1
|
30427
|
my ( $self, %args ) = @_; |
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
|
|
16
|
my $url = Mojo::URL->new( $self->base ) |
40
|
|
|
|
|
|
|
->path('youtube/v3/search') |
41
|
|
|
|
|
|
|
->query( |
42
|
|
|
|
|
|
|
%args, |
43
|
|
|
|
|
|
|
part => 'snippet', |
44
|
|
|
|
|
|
|
key => $self->key, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
600
|
my $tx = $self->ua->get($url); |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
25375
|
my $data = _handle_response($tx); |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
8
|
return $data; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _handle_response { |
55
|
1
|
|
|
1
|
|
4
|
my ($tx) = @_; |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
28
|
my $data; |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
12
|
my $res = $tx->result; |
60
|
|
|
|
|
|
|
|
61
|
1
|
50
|
|
|
|
31
|
if ( $res->is_success ) { |
62
|
1
|
|
|
|
|
22
|
my $body = $res->body; |
63
|
|
|
|
|
|
|
try { |
64
|
1
|
|
|
1
|
|
129
|
$data = decode_json($body); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
catch { |
67
|
0
|
|
|
0
|
|
0
|
croak $body, "\n"; |
68
|
1
|
|
|
|
|
29
|
}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
0
|
|
|
|
|
0
|
croak "Connection error: ", $res->message, "\n"; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
148
|
return $data; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |