-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Videos.pm
405 lines (289 loc) · 10.7 KB
/
Videos.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package WWW::YoutubeViewer::Videos;
use utf8;
use 5.014;
use warnings;
=head1 NAME
WWW::YoutubeViewer::Videos - videos handler.
=head1 SYNOPSIS
use WWW::YoutubeViewer;
my $obj = WWW::YoutubeViewer->new(%opts);
my $info = $obj->video_details($videoID);
=head1 SUBROUTINES/METHODS
=cut
sub _make_videos_url {
my ($self, %opts) = @_;
return $self->_make_feed_url('videos', %opts);
}
{
no strict 'refs';
foreach my $part (
qw(
id
snippet
contentDetails
fileDetails
player
liveStreamingDetails
processingDetails
recordingDetails
statistics
status
suggestions
topicDetails
)
) {
*{__PACKAGE__ . '::' . 'video_' . $part} = sub {
my ($self, $id) = @_;
return $self->_get_results($self->_make_videos_url(id => $id, part => $part));
};
}
}
=head2 videos_from_category($category_id)
Get videos from a category ID.
=cut
sub videos_from_category {
my ($self, $cat_id) = @_;
my $videos = $self->_get_results(
$self->_make_videos_url(
chart => 'mostPopular',
videoCategoryId => $cat_id,
)
);
state $yv_utils = WWW::YoutubeViewer::Utils->new;
if (not $yv_utils->has_entries($videos)) {
$videos = $self->trending_videos_from_category($cat_id);
}
return $videos;
}
=head2 trending_videos_from_category($category_id)
Get popular videos from a category ID.
=cut
sub trending_videos_from_category {
my ($self, $cat_id) = @_;
my $results = do {
local $self->{publishedAfter} = do {
state $yv_utils = WWW::YoutubeViewer::Utils->new;
$yv_utils->period_to_date(1, 'w');
} if !defined($self->get_publishedAfter);
local $self->{videoCategoryId} = $cat_id;
local $self->{regionCode} = "US" if !defined($self->get_regionCode);
$self->search_videos(undef);
};
return $results;
}
=head2 popular_videos($channel_id)
Get the most popular videos for a given channel ID.
=cut
sub popular_videos {
my ($self, $id) = @_;
my $results = do {
local $self->{channelId} = $id;
local $self->{order} = 'viewCount';
$self->search_videos("");
};
return $results;
}
=head2 my_likes()
Get the videos liked by the authenticated user.
=cut
sub my_likes {
my ($self) = @_;
$self->get_access_token() // return;
$self->_get_results($self->_make_videos_url(myRating => 'like', pageToken => $self->page_token));
}
=head2 my_dislikes()
Get the videos disliked by the authenticated user.
=cut
sub my_dislikes {
my ($self) = @_;
$self->get_access_token() // return;
$self->_get_results($self->_make_videos_url(myRating => 'dislike', pageToken => $self->page_token));
}
=head2 send_rating_to_video($videoID, $rating)
Send rating to a video. $rating can be either 'like' or 'dislike'.
=cut
sub send_rating_to_video {
my ($self, $video_id, $rating) = @_;
if ($rating eq 'none' or $rating eq 'like' or $rating eq 'dislike') {
my $url = $self->_simple_feeds_url('videos/rate', id => $video_id, rating => $rating);
return defined($self->lwp_post($url, $self->_auth_lwp_header()));
}
return;
}
=head2 like_video($videoID)
Like a video. Returns true on success.
=cut
sub like_video {
my ($self, $video_id) = @_;
$self->send_rating_to_video($video_id, 'like');
}
=head2 dislike_video($videoID)
Dislike a video. Returns true on success.
=cut
sub dislike_video {
my ($self, $video_id) = @_;
$self->send_rating_to_video($video_id, 'dislike');
}
=head2 videos_details($id, $part)
Get info about a videoID, such as: channelId, title, description,
tags, and categoryId.
Available values for I<part> are: I<id>, I<snippet>, I<contentDetails>
I<player>, I<statistics>, I<status> and I<topicDetails>.
C<$part> string can contain more values, comma-separated.
Example:
part => 'snippet,contentDetails,statistics'
When C<$part> is C<undef>, it defaults to I<snippet>.
=cut
sub video_details {
my ($self, $ids, $part) = @_;
my $info = $self->_get_results($self->_make_videos_url(id => $ids, part => $part // 'snippet'));
state $yv_utils = WWW::YoutubeViewer::Utils->new;
if ($yv_utils->has_entries($info)) {
return $info;
}
if ($self->get_debug) {
say STDERR ":: Extracting video info using the fallback method...";
}
my @items;
foreach my $id (split(/,/, $ids)) {
# Fallback using the `get_video_info` URL
my %video_info = $self->_get_video_info($id);
my $video = $self->parse_json_string($video_info{player_response} // next);
if (exists $video->{videoDetails}) {
$video = $video->{videoDetails};
}
else {
next;
}
my $length = $video->{lengthSeconds};
my $duration = sprintf("PT%dH%dM%dS", int($length / 3600), int($length / 60) % 60, $length % 60);
my %details = (
contentDetails => {
definition => "hd",
dimension => "2d",
duration => $duration,
projection => "rectangular",
},
id => $id,
kind => "youtube#video",
snippet => {
channelId => $video->{channelId},
channelTitle => $video->{author},
description => $video->{shortDescription},
title => $video->{title},
tags => $video->{keywords},
liveBroadcastContent => ($video->{isLiveContent} ? 'live' : 'no'),
thumbnails => [default => $video->{thumbnail}{thumbnails}[0],
medium => $video->{thumbnail}{thumbnails}[1],
standard => $video->{thumbnail}{thumbnails}[2],
high => $video->{thumbnail}{thumbnails}[3],
maxres => $video->{thumbnail}{thumbnails}[4],
],
},
statistics => {
viewCount => $video->{viewCount},
},
);
push @items, \%details;
}
my %results = (
items => \@items,
kind => "youtube#videoListResponse",
pageInfo => {resultsPerPage => scalar(@items), totalResults => scalar(@items)},
);
return scalar {results => \%results};
}
=head2 Return details
Each function returns a HASH ref, with a key called 'results', and another key, called 'url'.
The 'url' key contains a string, which is the URL for the retrieved content.
The 'results' key contains another HASH ref with the keys 'etag', 'items' and 'kind'.
From the 'results' key, only the 'items' are relevant to us. This key contains an ARRAY ref,
with a HASH ref for each result. An example of the item array's content are shown below.
=over 4
=item video_contentDetails($videoID)
items => [
{
contentDetails => {
caption => "false",
definition => "sd",
dimension => "2d",
duration => "PT1H20M10S",
licensedContent => bless(do{\(my $o = 0)}, "JSON::XS::Boolean"),
},
etag => "\"5cYuq_ImPkYn_h2RKDdX8DHvM2g/KU_bqVk91zBQGXrMtEDZgkQMkhU\"",
id => "f6df3s3x3zo",
kind => "youtube#video",
},
]
=item video_id($videoID)
items => [
{
etag => "\"5cYuq_ImPkYn_h2RKDdX8DHvM2g/bvAWXfDY4QPsx_UgtmMPFcxPLQc\"",
id => "f6df3s3x3zo",
kind => "youtube#video",
},
],
=item video_player($videoID)
items => [
{
etag => "\"5cYuq_ImPkYn_h2RKDdX8DHvM2g/nr03GopgH8bb755ppx5BA_1VsF8\"",
id => "f6df3s3x3zo",
kind => "youtube#video",
player => {
embedHtml => "<iframe type='text/html' src='http://www.youtube.com/embed/f6df3s3x3zo' width='640' height='360' frameborder='0' allowfullscreen='true'/>",
},
},
],
=item video_statistics($videoID)
items => [
{
etag => "\"5cYuq_ImPkYn_h2RKDdX8DHvM2g/j01_qxKqxc3BMrFBbX2eiPWkAmo\"",
id => "f6df3s3x3zo",
kind => "youtube#video",
statistics => {
commentCount => 2,
dislikeCount => 1,
favoriteCount => 0,
likeCount => 5,
viewCount => 174,
},
},
],
=item video_status($videoID)
items => [
{
etag => "\"5cYuq_ImPkYn_h2RKDdX8DHvM2g/jaa690eVtSvHTYRSSPD3mc1mlIY\"",
id => "f6df3s3x3zo",
kind => "youtube#video",
status => {
embeddable => bless(do{\(my $o = 1)}, "JSON::XS::Boolean"),
license => "youtube",
privacyStatus => "public",
uploadStatus => "processed",
},
},
],
=item video_topicDetails($videoID)
items => [
{
etag => "\"5cYuq_ImPkYn_h2RKDdX8DHvM2g/XnxCuOGwiR8MNhH-iHNxHB-ROWM\"",
id => "f6df3s3x3zo",
kind => "youtube#video",
topicDetails => { topicIds => ["/m/0126n", "/m/0jpv", "/m/07h44"] },
},
],
=back
=cut
=head1 AUTHOR
Trizen, C<< <echo dHJpemVuQHByb3Rvbm1haWwuY29tCg== | base64 -d> >>
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc WWW::YoutubeViewer::Videos
=head1 LICENSE AND COPYRIGHT
Copyright 2013-2015 Trizen.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See L<http://dev.perl.org/licenses/> for more information.
=cut
1; # End of WWW::YoutubeViewer::Videos