Skip to content

Instantly share code, notes, and snippets.

@monokrome
Last active December 2, 2020 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monokrome/21266b50385d88b908c650334429ea61 to your computer and use it in GitHub Desktop.
Save monokrome/21266b50385d88b908c650334429ea61 to your computer and use it in GitHub Desktop.
Use youtube-dl to automatically archive entire playlists, channels, or users.
#!/usr/bin/env zsh
# Downloads entire channels, users, or playlists from YouTube based on a given
# name. Set up cronjobs to keep archives of them. For instance, you may want to
# download three archives like so:
# 0 22 * * * user-archive my-music
# 5 22 * * * user-archive my-channel
# 10 22 * * * user-archive my-user
set -euo pipefail
# Replace this with your destination path
archive_path="${HOME}/Video/archive"
ytdlopts=(
'-ci'
'--user-agent' 'Mozilla/5.0 (X11; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0'
'-f' 'best'
)
ensure_path() {
[[ ! -e "${1}" ]] && mkdir -p "${1}"
cd "${1}"
}
ensure_channel() {
channel_name="${1}"
channel_uri="https://www.youtube.com/channel/${channel_name}"
ensure_path "${archive_path}/channel/${1}"
youtube-dl ${ytdlopts} -v "${channel_uri}" || true
youtube-dl ${ytdlopts} \
-o '%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' \
-v "${channel_uri}/playlists" || true
}
ensure_user() {
user_name="${1}"
user_uri="https://www.youtube.com/user/${user_name}"
ensure_path "${archive_path}/user/${1}"
youtube-dl ${ytdlopts} "${user_uri}" || true
youtube-dl ${ytdlopts} \
-o '%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' \
"${user_uri}/playlists" || true
}
ensure_playlist() {
name="${1}"
pid="${2}"
ensure_path "${archive_path}/playlist/${name}"
youtube-dl ${ytdlopts} \
-o '%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' \
"${pid}" || true
}
ensure_path "${archive_path}"
command="${1}"
case "${command}" in
my-playlist)
ensure_playlist 'playlist' 'playlist-id'
;;
my-channel)
ensure_channel 'channel-id'
;;
my-fuser)
ensure_user 'user-id'
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment