From 04446011f364909096cb49f2b66e4df96d713209 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Tue, 13 Apr 2021 18:17:14 +0200 Subject: Initial commit --- download_yt_playlist | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 download_yt_playlist (limited to 'download_yt_playlist') 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 \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`; -- cgit v1.2.3