#!/bin/bash
usage='usage:
./get_youtube_playlist
target_folder: (default: songs will be downloaded in current folder)
num_songs: number of songs to get (default: 50)
examples:
./get_youtube_playlist RD02HIkZaLeuF9k
./get_youtube_playlist RD02HIkZaLeuF9k "instrumental hip-hop beats" 10
'
playlist_id=$1
target_folder=$2
num_songs=$3
if [ -z "$playlist_id" ]; then
echo "$usage"
exit 1
fi
if ! [[ "$num_songs" =~ ^[0-9]+$ ]] ; then
num_songs=50
fi
if [ -z "$target_folder" ]; then
target_folder='./'
elif [ ! -d "$target_folder" ]; then
echo "Parameter target_folder is incorrect, $usage"
exit 1
fi
# используем Youtube API для получения списка песен
# https://developers.google.com/youtube/2.0/developers_guide_protocol_playlist_search
youtube_api="`wget -qO- https://gdata.youtube.com/feeds/api/playlists/$playlist_id\?max-results\=$num_songs`"
if [ -z "$youtube_api" ]; then
echo "Playlist ID is incorrect, $usage"
exit 1
fi
# cписок ID песен помещаем в массив songs
songs=(
$(echo $youtube_api | \
grep -P -o "