Skip to content

Instantly share code, notes, and snippets.

@dsprenkels
Last active April 27, 2019 07:32
Show Gist options
  • Save dsprenkels/6236548fe26c6f81e860cfe91dd9700f to your computer and use it in GitHub Desktop.
Save dsprenkels/6236548fe26c6f81e860cfe91dd9700f to your computer and use it in GitHub Desktop.
#!/bin/sh
readonly OUTPUT_FMT="%(uploader)s [%(upload_date)s] - %(title)s.%(ext)s"
readonly COOKIES_FILE="$(dirname "$0")/.config/cookies.txt"
readonly ONLY_DOWNLOAD_LAST_N="10"
SYNC="0"
if [ "$1" = "sync" ]; then
# Add a channel; i.e. prefill the cache
SYNC="1"
elif [ $# -eq 1 ]; then
# Download only a single video
set -v
youtube-dl --cookies "$COOKIES_FILE" \
--format "248+251/247+251/bestvideo+bestaudio" \
--output "$OUTPUT_FMT" \
"$1"
exit 0
elif [ $# -gt 1 ]; then
echo "Error: too many arguments" >&2
exit 1
fi
for channel_dir in "$(dirname "$0")"/.config/*/; do
channel_url="$(cat "${channel_dir}url.txt")"
channel_archive="${channel_dir}archive.txt"
# To fill the archive files without downloading the videos, use:
if [ "$SYNC" = "1" ]; then
youtube-dl --download-archive "$channel_archive" --get-id "$channel_url" \
| sed 's/^/youtube /g' \
| tail --lines="+$ONLY_DOWNLOAD_LAST_N" >"$channel_archive"'.$$'
mv "$channel_archive" "$channel_archive.old"
cat "$channel_archive"'.$$' "$channel_archive.old" | sort --unique >"$channel_archive"
rm "$channel_archive"'.$$' "$channel_archive.old"
fi
# Download the new videos
youtube-dl --cookies "$COOKIES_FILE" \
--format "248+251/247+251/bestvideo+bestaudio" \
--output "$OUTPUT_FMT" \
--download-archive "$channel_archive" \
"$channel_url"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment