blob: 32b50ebc6b2545e85647bebb9b540c268e2a02ea (
plain) (
blame)
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
|
#!/usr/bin/env perl
use strict;
use warnings;
use JSON;
my $ENDPOINT = "https://www.googleapis.com/youtube/v3";
my $YT_API_KEY = $ENV{'YOUTUBE_API_KEY'}
|| die "Error: You need to define the env variable 'YOUTUBE_API_KEY'\n";
sub decode_json_utf8 {
my $json = join "", @_;
utf8::encode($json);
return decode_json($json);
}
sub api_playlist_videos {
my $id = shift;
my $videos = (decode_json_utf8 `curl -s '$ENDPOINT/playlistItems?part=snippet&maxResults=1000&playlistId=$id&key=$YT_API_KEY'`)->{items};
return map { $_->{snippet}{resourceId}{videoId} } @$videos;
}
@ARGV == 1 or die "Usage: $0 <playlist_id>\n";
my $pl_id = $ARGV[0];
my @videos = map { "https://www.youtube.com/watch?v=" . $_ } api_playlist_videos($pl_id);
`mkdir $pl_id && cd $pl_id && youtube-dl @videos\n`;
|