-
-
Save hafidbuilds/aaaa3da7583de2a7643df8091c58feed to your computer and use it in GitHub Desktop.
Related article: https://coderwall.com/p/n7kpqa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# If DEBUG environment variable is not defined set as false | |
if [[ "x${DEBUG}" == "x" ]] | |
then | |
DEBUG=false | |
fi | |
# Logger function. Display a message if DEBUG is true | |
logMessage() { | |
if [[ $DEBUG == "true" ]] | |
then | |
echo $1 | |
fi | |
} | |
# Configuration | |
FEED_URL="http://teamtreehouse.com/library/the-treehouse-show.rss?feed_token=********-****-****-****-***********" | |
DRIVE_MOUNT_POINT="/Volumes/Samsung" | |
TARGET_DIRECTORY="${DRIVE_MOUNT_POINT}/Plex/Series/Treehouse" | |
# Applications | |
CURL=`which curl` | |
# Check required applications | |
if [[ -z $CURL ]] | |
then | |
logMessage "cURL command line tool is not installed." | |
exit -1 | |
fi | |
# Check external drive | |
# Remove this section if you do not need to check the mount point | |
if [[ -n `mount | grep "${DRIVE_MOUNT_POINT}"` ]] | |
then | |
logMessage "External drive is mounted." | |
else | |
logMessage "External drive is not mounted." | |
exit -1 | |
fi | |
# If the target directory does not exist | |
if [[ ! -d $TARGET_DIRECTORY ]] | |
then | |
logMessage "Target directory does not exist. (${TARGET_DIRECTORY})" | |
logMessage "Create ${TARGET_DIRECTORY}" | |
# Create | |
mkdir -p $TARGET_DIRECTORY | |
fi | |
# Download the feed | |
logMessage "Download Feed..." | |
target_name=`basename $0` | |
curl -o /tmp/${target_name}.feed -s $FEED_URL | |
logMessage "Feed downloaded." | |
# Build an array of urls | |
logMessage "Parse video URIs." | |
videos=`grep -e "enclosure" /tmp/${target_name}.feed | grep -o 'http://[^"]\+' | sed -e 's/\.mp4.*/?hd=yes/g'` | |
for uri in $videos | |
do | |
# Fetch the direct link | |
target=`$CURL -s "${uri}" | grep -o 'http://[^"]\+'` | |
# Get the filename of the video | |
filename=`basename "$target" | sed -e "s/\?.*//"` | |
# If current video file exists then I got this episode | |
# and possible all others after that (feed order: publishedAt desc) | |
if [[ -f $TARGET_DIRECTORY"/"$filename ]] | |
then | |
logMessage "Done." | |
exit -1 | |
fi | |
# Download the video to the target directory | |
logMessage "Download ${filename}..." | |
$CURL -s -o $TARGET_DIRECTORY"/"$filename "${target}" | |
logMessage "Downloaded." | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment