diff options
author | Guillermo Ramos | 2021-04-13 18:17:14 +0200 |
---|---|---|
committer | Guillermo Ramos | 2021-04-13 18:17:14 +0200 |
commit | 04446011f364909096cb49f2b66e4df96d713209 (patch) | |
tree | 6c2bbb722d63487a766d91e5ecc9560f01442f44 /download_yt_playlist | |
download | cli-04446011f364909096cb49f2b66e4df96d713209.tar.gz |
Initial commit
Diffstat (limited to 'download_yt_playlist')
-rwxr-xr-x | download_yt_playlist | 28 |
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`; |