Skip to content

Instantly share code, notes, and snippets.

@132ikl
Last active December 14, 2019 00:31
Show Gist options
  • Save 132ikl/a645b4bb7ccf4bf089be5f25a9947311 to your computer and use it in GitHub Desktop.
Save 132ikl/a645b4bb7ccf4bf089be5f25a9947311 to your computer and use it in GitHub Desktop.
YouTube subscription downloader
#!/usr/bin/env bash
set -e
IFS=$'\n'
for i in $(ls -1); do
echo "Playing $i"
sleep 0.5
mpv "$i"
read -p "Delete video? [Y/n] " yn
case $yn in
[Nn]* ) :;;
* ) rm "$i";;
esac
done
echo Finished playing videos.
read
#!/usr/bin/env bash
set -e
vids=()
pubs=()
vidCount=0
pubCount=0
echo Gathering videos...
for i in $(cat ".subscription_manager"); do
url="$(echo -n $i | grep xmlUrl | cut -d'"' -f2)"
if [[ "$url" == "" ]]; then
continue
fi
data="$(curl -s $url| grep -e 'link.*rel.*alternate.*watch.*' -A5)"
for x in $(echo -n $data); do
vid="$(echo $x | grep href | cut -d '"' -f2)"
pub="$(echo $x | grep published | cut -d ">" -f2 | cut -d "<" -f1)"
if [[ "$(echo $vid | wc -c)" -gt "2" ]]; then
vids[$vidCount]="$vid"
vidCount=$(($vidCount+1))
fi
if [[ "$pub" != "" ]]; then
pubs[$pubCount]="$pub"
pubCount=$(($pubCount+1))
fi
done
done
echo Done gathering. Processing...
if [[ -e ".YTDOWNLOAD_LAST" ]]; then
DATE=$(cat .YTDOWNLOAD_LAST)
else
DATE=$(date --date="yesterday" +%s)
fi
for (( i=0; i<${#vids[@]}; i++ )); do
vid=${vids[$i]}
pub=$(date --date "${pubs[$i]}" +%s)
if [[ "$pub" -gt "$DATE" ]]; then
youtube-dl -o "%(uploader)s: %(title)s" $vid
fi
done
date +%s > .YTDOWNLOAD_LAST
echo Done!

Setup

  1. Download this file from YouTube and place it in the folder with the script with the name .subscription_manager.
  2. Run ./.yt-subscriptions.sh. It will use youtube-dl to automatically download all videos since the last time it was run (or 24h if it has not been run before).
  3. Run ./.start.sh to automatically play each video, and prompt for deletion after playing.

I recommend setting a keybind to run .start.sh in a terminal. You can also use crontab to have .yt-subscriptions.sh automatically download videos in the background.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment