aboutsummaryrefslogtreecommitdiff
path: root/download_yt_playlist
diff options
context:
space:
mode:
Diffstat (limited to 'download_yt_playlist')
-rwxr-xr-xdownload_yt_playlist28
1 files changed, 28 insertions, 0 deletions
diff --git a/download_yt_playlist b/download_yt_playlist
new file mode 100755
index 0000000..32b50eb
--- /dev/null
+++ b/download_yt_playlist
@@ -0,0 +1,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`;