Skip to content

Instantly share code, notes, and snippets.

@Necroforger
Last active February 25, 2019 23:46
Show Gist options
  • Save Necroforger/9b9f78b21eb55e7b78d282978ed384ce to your computer and use it in GitHub Desktop.
Save Necroforger/9b9f78b21eb55e7b78d282978ed384ce to your computer and use it in GitHub Desktop.
script to manage downloading youtube playlists. should have python and youtube-dl installed
# Download.sh helps keep track of youtube playlists by downloading them in groups
# Directory for downloaded playlist metadata. includes the video IDs of the videos in each playlist
playlists_folder="playlists_json"
# Directory to place downloaded videos, thumbnails, and video metadata JSON files
videos_folder="videos"
# URL prefix for playlists
prefix=""
# try to find any installed version of python
python=`command -v py python3 python2 python | head -n 1`
if [ "$python" = "" ]; then
echo "Could not find an installed version of python"
echo "If you are on windows you should get version 3 from https://www.python.org/downloads/"
exit 1
fi
echo "found python executeable: $python"
# array of playlists to download when running 'download json' or 'download playlists'
# it should be formatted like
# playlists=(
# "PLAYLIST_ID PLAYLIST_NAME"
# "PLAYLIST_ID PLAYLIST_NAME"
# "..."
# )
# The name will be used for the JSON file produced for the playlist
playlists=(
)
# recursively read all lines of input from stdin
function read_all() {
read -t 0.1 line
if [ $? -ne 0 ] || [ "$line" = "" ]; then
printf ""
else
printf "\"$line\" $(read_all)"
fi
}
# Allow setting playlists by piping to STDIN
inp=$(read_all)
if [ -n "$inp" ]; then
echo "read playlists from stdin..."
playlists=("$inp")
for i in ${!playlists[@]}; do
echo "$i ${playlists[$i]}"
done
fi
# get_col <idx> <col_num>
# retrieve an index from the playlists array using python
function get_col() {
t=$(echo "${playlists[$1]}" | tr -d '"')
echo "
text = '$t'
parts = text.split(' ', 1)
parts = list(map(lambda x: x.strip(), parts))
print(parts[$2])" | python
}
# Download the videos at the givin URL using the archive file supplied
# usage:
# dl <archive_path> <playlist_url>
function dl() {
youtube-dl \
--download-archive "$1" \
--write-thumbnail \
--write-info-json \
-i \
"$2"
}
# saves a playlist json
# <NAME> <URL>
function save_playlist_json() {
youtube-dl \
--flat-playlist \
--skip-download \
--print-json \
"$2" > "$1.json"
}
# Download all the playlists in the playlists array
function download_playlists() {
if ! [ -d "$videos_folder" ]; then
echo "$videos_folder does not exist, creating it"
mkdir "$videos_folder"
fi
for i in ${!playlists[@]}
do
x="`get_col $i 0`"
n="`get_col $i 1`"
cd "$videos_folder"
echo "downloading playlist $prefix$x"
dl "../archive.txt" "$prefix$x"
cd ..
done
download_playlist_json
}
# Download json information for all playlists
# download_playlist_json <download_more>
function download_playlist_json() {
if ! [ -d "$playlists_folder" ]; then
echo "$playlists_folder does not exist, creating it"
mkdir "$playlists_folder"
fi
for i in ${!playlists[@]}
do
x="`get_col $i 0`"
n="`get_col $i 1`"
echo "Getting metadata for playlist $n ID: [$x]"
save_playlist_json "$playlists_folder/$n" "$prefix$x"
done
# compress results into a tar archive
cd "$playlists_folder"
tar -czvf "playlists`date +-%Y-%m-%d_%H-%M`.tar.gz" *.json
rm *.json
}
function printUsage() {
cat << EOF
usage:
If you want to change the downloaded playlists you can pipe a file or text to standard input
examples
echo "ID NAME" | ./download.sh playlists
cat playlists | ./download.sh
download playlists download your playlists in the playlists array
archive file is ./archive.txt
download json [extra] downloads json information for all playlists
download manual <playlist_url> <playlist_name> download a video manually and use an archive file
EOF
}
case "$1" in
"playlists") download_playlists;;
"json") download_playlist_json;;
"manual") playlists=("$2 $3"); download_playlists;;
*) printUsage;;
esac
# .....................................▓▓░░░░░ ▒▓░░░░░░░. .░▒▓░░░░░░░░. .........░. ....░▒▒▓▓▓▒▒▒▒▒░░░░░▒▒▓▓███████▓▓▒▒░.........░░░▒▒▓▓▒▒░...........
# ▒.....................................▒░░...░▓▒░░░░░. .░▒███▒░░░░░░░░. ... ....░░▒█▓▓▓▒▒▒▒▒▒░░░░░▒▓██▓▓▓███▓▓▓█▓▓▒▓▓██▓▒░...........░▒▓▓▓▓▒... .░..
# ..............▓▒..░░░░░░░░...........░░░░░▓▓▒▒▓▒░░. .▒███▓▓▒░░...... ...░░▒▒▓▓▓▓▒▒▒▒▒▒░░▒▒░.░▒▓███▒▒░░░▒▒▒░▒▒▒░▒▒▒▒▓████▒░..░....... .░▒▓▓▓▒. ....
# ..........................░░░░░░.....░░▒▓▓▒░░▒▒░░░░▓▓▓▒▒▒░░............░▒▓▓▓█▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒░░░▒████▓▓▒▒▒▒▒▒▒▒▒▒▓▒▓██▓▒▓██████▓░............░▓▒▓▒░. ..
# ....................▒░....░░░▒▒▒▒░..▒░░░░░░░░░░░░░▒▒░░░░░░░.░░░..░░▓▓▒▓██▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░.░▓██▓▒▒▒░░░▒▒▒▒▒▒▒▒▒▓███▓░▒██▓█████▒░...........▒▒▓▒.. .
# ....................░......░▒░░░░▒░░▒░░░░░░░░░░░░░░░░░░░░. ..░▒▒▓███▓▓▒▒▒▒▒▒▒▒▒░..........▒▓▓▒▓▒░░░░░░▒▒▓▓█▓▒▒▒▒▒▓▓▒▒▓▓████████▒░░.........░▓▒. ....
# ..........................░░░░▒░▒░░▒▒░░░░░░░░░░░░░░░░░....░▒▓▓████▓▓▒▒▒▒▒▒▒▒▒▒░...░░░░░...░░██▓▒▓▓▒▒░░▒░▒▒▓██▓▒▓███▓▓███▒░▓██▓▓███▓░░.░......▒▒.......
# ..........................░░░░░░░░░░░..░░░░.░░░░░░░░░░.░▒▓▓▓▓▒▓▒▒▒░░░░▒▒▒▒▒░░░▒▒▓▓███▓▓▓▓▓▒▓█▓▒░░░░▒░▒▒▒▒▒▓█▓▒░▓█████████▓██▓▒▒▓███▒░.......▒▒.......░
# ..................................░░.....░..░░░░░░░░░░░▒▒▒▒▒▒▒▒▒░░░▒▒▒▒░░░░▒▒▓█████████▓▓▓▓███▓▒░░░░▒▒▒▒▓▓███▓██▓▓████▓█████▓▒▓████▓░......░░.........
# ......................░░░░......░▓▓▓▓▓▒░░░...░░░░░░▒▒▒▒▒░▒▒▒▒▓▓▒▒▒▒▒░░. .......░▒▒▒▓▒▒░░░░▓██▓▒▒░░░▒▒░░▒███▓████▓██▓░░▒▓███▓▒░▒▓███▓░░░...░░ ..... .░
# ....................░░░░░░░...░▒▓█▓▓▓▓▓▓▓▒▒▒▒▒▒▒░▒▒▒▒▒▒▒▓▓▓▓▒▒░░▒▒░.. . ........░░▓██▓▒░░▒░░░▒▒▓███▓█▓▓█████▓▓▓█████▓▒▓█████░░..░░......░ . .
# ..................░░░░░░░░▒▒░░▒▒▒▒▒▓▓▓▒▒▒▒▓▓▓▓▓▒░▒▒▒▒▒▒▒▒░░░░░░.░.....░.░░░▒▒▒▒▓▒░░......░▓████▓▓▓▒░░▒▓███▓▒▒▒▓████████████████████▓░░░▒░....... ... .
# .................░░░░░░░▒░░▒▓▓▒▒▒░░░▒▒▒▒▒░▒▒▒▒▒▒▒▒▒░░░░░░..░...░.... ...░▒▓████████▓▒░░..░▒█████▓▓▓▓▓▓▓███▓░..░▒▓██████████████████▒░░▒░.....░░.... ░░
# ...........░░░.░░░▒▒░░▒▒░▒░░░▒▒▓▒▒▒░▒▓▓▒▒▒▒▒░░..░......... ....░..... .....░▒▓▓▓▓▓▓▓▒▒░.░░▓███▓▓▒▓████████▓▒▒░.▒████▓▓▓▒░▓███████▓▒░............. ..
# ........░░░░░..░░░▒▒░░▒▒▒▒▓▒▓▓▒▒▒▒▒▒░░▓▓▒▒▓▒▒▒▒▒░.......... ..░... .... ... .............░░▓███▓▒▒▓██████████▓░▓████▓▒▒▒▓▓██▓████▒░░░.............. .
# ......░░░░░░░░░░▒▒▓▒░▒▓▓▓▒▓▓██▒. ▒▒▒▒░░▒▓▒▒▓▒░............. ...... .░▒▒▒▒▒▒░..........░░░░▓████▒▓█▓▒▓███████▓▓██████▓████████▓▒░.░▒▓........ .. .
# .........░░░░░░▒▓█▒░▒▒▓█▓▓██▒. ▒▓▒▒▒░▒▓▓░.............. ..░.... .. ░▓█████▓▒░..........░░▓██████▓▒████████████████████████▓▒░░..░░........░░. ...
# ........░▒▒▒░░▒▒██▒░▓▓▒▓▓█▒. ....▒▓▒▒▒▒▒▓▓░.... ... .░....░░.......... .▒▓█████▓▒░..........░▒▓█████████▓▓██████████████████▒░▒░.............█▓. ...
# ........░▒▒▒▒░░▒▓▓▒▓▓██▓▓█▓██▒▒▒░▒▓▒▒▒▒▓▓░.....▒▓█▓▓█▓░........░░▒▓▓▓▓▓▒░..░░..░.░░.........░░.░░░▓███████▓▓█████▒▒████████▓▓░░░░..▒░........ ........
# ........░▒▒▒▒▒▒▒▒░▒░░░▓████████▓███▒▒▓▓▓▓░░▒▓▓▓▓▓▒▓▓▒▒▒░░░░.... ..░▓█████▓░..░.. ...........░░.....░▒▒▓██████████████████▒▒░░...░▒░..░▓...............
# ........░▒▒▒▒▒▒▒░.░░...▓██████████▓▒▓▓▓▒░▒▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░.....░▒▒▒▓▓▓▒░.░░. .................░...░░░▒▒▓▓▓▓▓▓▒▒▒▒░.░▒░.░░...░░....... ...... ..▒▒
# ........▒▓░▒▒▒▓▒▒▒▒▓▓▓▒░██████▓▓█▓░▓▓▒▒░▒▒▒▒▒▒▒▒▒▒▒▒▒░░░....░.░░▒▒▒░............░░.. ..............░.........░░.░.░░....░░........░...................
# .░░.....▒▓..▒▒█▒░░░░▓▓▓▓█████▓▓▓▓▓▒▓▒▒░▒▒▒▒▒▒▒▒▒▓▒░░░.........░.▒▓▓▓▒░......... .░░.. ...........░░............░░▒░...░.................... .. ..
# ......░.▒▒...▒▒▓░▒▒▒▒▒░▒▒▒▒▒▒▒▒▓█▓▓▒▒░▒▒▒▒▒░░░░▒▒░............░░▒▒▒▒▓▓▒.. ... .░░░ ....................▒▓░░░░...░. .......░..... ...... ......
# ......░.░▒░...░▒▓░▒▒▒▒░░░▒▒░░░▓▒▒▒▒▓▓▒░▒░▒░░░░░░..............▒▒▒▒▒▒▒▒▓▓▓▒▒░░. .. .░░░. ............░. .░▒▒▒░░.....░▒░.. ....░.......... .....
# ░░▒▒▒░░..▒░.....░░░▒▒▓▒▓▒░▒▒▒▒▓▒▒▒▒▒▓█▒░░░░░░░░...............░▒▒▒▒▒▒▒▒▒▒▓▓███▓▒░.. ░..░░.. .......... .░▒▒░░........░▓▒.░..... ................. ..
# ███▓░....░▒░.... .░▒▓▒▒▓▒░▓▒░▒▒▒▒▒▒▒▒█▓░░░░░░░░.............░▓░...░░▒▒▒▒▒▒▒▓▓▓████▓▓▒░. .░░. ........ .▒▒░░░░░......░ .░..........................
# █▓▒..░░▒▓▓░....░░.▓▓▓▓▓▓▓▓▓▒. .▒▒▒▒▒▒▓▓░░░░░░░░░░░░. ... ..░▒▒░.. ...░░░▒▒▒▓▓▓▓▓█████▓▒░.░▒░..░ ..░▒▒░░..▒▒...░░.░.... .▒░..░░░...................
# ░░.▒▓███▓░...░▒▒░..▒▒▒▒▒▒▒▒▒░..▒░░░░░▒░░░░░░░░░░░░░... ..░░▒▒▒▒▒▒▒░░. .. .....░░░▒▒▓▓▓███▒..░░▒░░....░░░░░░...........░.. .▓▒.░..... .. . .......
# ..▒████▓░.░▒▓██░....▒░░▒▒▒░░░▒▒▒░░░░░░░░░░░░░░░░░░░░..░░..▒▒▒▒▒▒▒▒▒▒▒░........ ...░░▒░....░▒▒▒▓▒▒▒░░..░░............░░..░..░.... .. ...............
# .░▓▓▒▒░..░▓███▓...░▓▓▒▒▒▒░░▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░..░▒▒▒▒▒▒▒▒▒░▒▒░.............. .. .....░░▒▒▒▒▒▒▒▒░...............░░░..... .▒. .... ............
# .░.......░██▓▒░...▒▓▓▓▒...░▒░░░░░░░░░░░░░░░░░...........▒▒░░░░░░░░░░░░░░.........................░░░░░░.░▓▒......................... ................
# .........▓▓▒░.....▒▓▓▒...... ........░░░............ ........░░░░░░░▒░░░░........................... .▓▓..........░............ ..................
# .........░........░▒░.... . .░..................░░░░░░..........░░░░░░░░░░░░░░.......................... ░.................... ..............░.......
# ............................ .░..............░░░░░░▒▒▒▒▒▒▒░......░░░░░░░░░░░░░░░.......................░▒░ .................. .......................
# ........................... .░..............░░░░░░░░░░...░░░░░...░.................░.................. .░. .......................... ........░...
# .........................░░...░░...............░░░░▒▒░░..........................░░....................░░░░░. .░░░........... .................░...
# .........................░░░..░░....................░..░░░.....░░░░░░░░░....░░░░░...................░░▒▒░░░░░░. ..░░........░.................. .░
# .......................░▒░.....░......░.............░░░......░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒░.....................░░░░░...░░. ..░........░░................. .
# ......................▒▒▒░....░░.................░░░░.....░░░░░░░▒▒▒▒▒▒▒▒▒▒▒░.... . .░......░...░.....░. .........................
# ....................░▒▒░▒░..░░..................░░░....░░░░░░░░░░░░▒▒▒▒▒░░░░. ... ░░.. .░..........░. .............. ..
# ...................░░.░▒░...░....................░......░░░░░░░░░░░░░░▒░.... ... .░................... ............
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment