Skip to content

Instantly share code, notes, and snippets.

@noureddin
Created July 29, 2017 18:29
Show Gist options
  • Save noureddin/08f622d31673d19b912198af963aef5d to your computer and use it in GitHub Desktop.
Save noureddin/08f622d31673d19b912198af963aef5d to your computer and use it in GitHub Desktop.
Download a shelf of playlists from Youtube
#!/usr/bin/env perl
# Download a shelf of playlists from Youtube
# using wget, perl and youtube-dl.
# After using it, you'll find folders with the playlists'
# names, and each has a file named '.get'. Issue `. .get`
# in your terminal to start downloading the playlist.
# You can run `for i in *; do cd "$i"; . .get; cd ..; done`
# to get them all after this script.
# License: CC0
use strict; use warnings;
my @ids; my @titles;
my $F = `wget -qO- "$ARGV[0]"`;
while ($F =~ m|href="/playlist\?list=(PL[^"]+)">([^<]+)|g) {
push(@ids, $1); push(@titles, $2);
}
undef $F;
my $zeros = length($#titles); # for zeropadding
for my $i (0..@ids-1) {
# The playlists are ordered. This line adds their
# number to be in the same order your file manager.
# You can comment it out if you don't want them.
$titles[$i] = sprintf("%0${zeros}d %s",
$i+1, $titles[$i]);
# TODO: We need to sanitize names.
# On Unixes, only a forward slash is problematic.
# On Windows, there are more.
mkdir $titles[$i];
chdir $titles[$i];
# You can modify this also to suit you.
# It numbers the videos in the playlist, too.
my $fname = ".get";
open(my $fh, ">", $fname)
or die "Can't open > $titles[$i]/$fname: $!";
print $fh "youtube-dl $ids[$i] "
."--write-description "
."--download-archive .archive "
."-o '%(playlist_index)s. %(title)s-%(id)s.%(ext)s' "
."\$@\n";
close($fh)
or warn("Can't close $titles[$i]/$fname: $!");
chdir '..';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment