Last active
April 10, 2021 01:49
-
-
Save mbierman/6473119 to your computer and use it in GitHub Desktop.
Fetch NPR shows
This file contains 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
#!/bin/bash | |
# Varibles | |
OS="$(uname -s | sed -e 's/Darwin/macos/g')" | |
MUSER=$(echo $USER | tr '[:upper:]' '[:lower:]' | awk '{for (i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2)} 1') | |
MACH=$(hostname -s) | |
PATH=$LOCATION:$PATH | |
UA="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36" | |
MKDIR="mkdir -p" | |
LOCATION=$(dirname $0) | |
ATTEMPTS=1 | |
COUNTER_S=1 | |
curFiles=0 | |
TODAY=$(date +"%d") | |
DAY=$(date +"%d") | |
MONTH=$(date +"%m") | |
YEAR=$(date +"%Y") | |
SHOWS="me (morning edition) | atc (All Things Considered) | wesat (Weekend Edition Saturday) | wesun (Weekend Edition Saturday) | fa (Fresh Air) | wwdtm (Wait, Wait, Don't Tell Me)" | |
VERBS="debug (d) | fetch (f) | clean (c) ] [ sleep ]" | |
START=$(date +"%s") | |
SECS=300 # used for the countDown timer. 300 = 5 min | |
NASPATH="/Volumes/michael/NPR" | |
HOMENET="woodstock" | |
REMOTE="true" # default for where to get the source for the show | |
SILENT="" | |
BASEDIR=$(dirname "$0") | |
APIKEY="$(cat $BASEDIR/getnprdata.txt | grep API | cut -f2 -d "=" )" | |
# set environmentalready downloaded | |
case $MACH in | |
"BigMac") | |
SHELL=/bin/bash | |
BASE="$HOME/Documents" | |
SYNC="" | |
;; | |
"SmallFry") # Second Home | |
SHELL=/bin/bash | |
BASE="$HOME/Documents" | |
SYNC="" | |
;; | |
"pigpen") # NAS | |
SHELL=/bin/bash | |
BASE="/volume1/michael" | |
SYNC="/usr/syno/bin/synoindex -A ${DEST}" | |
;; | |
*) | |
Machine="host unknown: Script not configured for this host." | |
SHELL=/bin/bash | |
BASE="$HOME/Documents" | |
SYNC="" | |
;; | |
esac | |
#Check for a couple of things we need. | |
perl --help >/dev/null 2>&1 || { echo -e >&2 "\n"$(basename $0)" requires 'perl' but it's not installed so you can't specify a date like, 'yesterday'. But this shouldn't be a problem so we continue.\n\n"; } | |
curl --help >/dev/null 2>&1 || { export ERROR="100"; echo -e >&2 "\n"$(basename $0)" requires 'curl' but it's not installed. Sorry, $MUSER. (Error: $ERROR)\n\n"; pushAlert $ERROR ; exit $ERROR; } | |
function cInputs () { | |
# Sort out what the user asked for | |
for i in "$@" ; do | |
# Normalize the input so we don't have to deal with mixed case. | |
li=$(echo "$i" | tr '[:upper:]' '[:lower:]') | |
case $li in | |
# Is it a show ? | |
"me" | "morning*") | |
SHOW="me" | |
LSHOW="morning-edition" | |
LSHOWs="Morning Edition" | |
SAVE=3000 | |
WORKFLOW="workflow://run-workflow?name=Download%20NPR&input=text&text=https%3A%2F%2Fwww.npr.org%2Fprograms%2Fmorning-edition" | |
icon="https://www.nationalpublicmedia.com/uploads/2019/11/Morning-Edition-From-NPR.jpg" | |
;; | |
"atc" | "all*") | |
SHOW="atc" | |
LSHOW="all-things-considered" | |
LSHOWs="All Things Considered" | |
SAVE=3000 | |
WORKFLOW="workflow://run-workflow?name=Download%20NPR&input=text&text=https%3A%2F%2Fwww.npr.org%2Fprograms%2Fall-things-considered" | |
icon="https://www.nationalpublicmedia.com/uploads/2019/10/npr_22267316_Small-e1573838055833.jpg" | |
;; | |
"wesat") | |
SHOW="wesat" | |
LSHOW="weekend-edition-saturday" | |
LSHOWs="Weekend Edition Saturday" | |
SAVE=10080 | |
WORKFLOW="workflow://run-workflow?name=Download%20NPR&input=text&text=https%3A%2F%2Fwww.npr.org%2Fprograms%2Fweekend-edition-saturday" | |
icon="https://www.nationalpublicmedia.com/uploads/2019/11/weekend-edition-tile-e1573248352835-640x641.jpg" | |
;; | |
"wesun") | |
SHOW="wesun" | |
LSHOW="weekend-edition-sunday" | |
LSHOWs="Weekend Edition Sunday" | |
SAVE=10080 | |
WORKFLOW="workflow://run-workflow?name=Download%20NPR&input=text&text=https%3A%2F%2Fwww.npr.org%2Fprograms%2Fweekend-edition-sunday" | |
icon="https://www.nationalpublicmedia.com/uploads/2019/11/weekend-edition-tile-e1573248352835-640x641.jpg" | |
;; | |
"fa" | "fresh") | |
SHOW="fresh" | |
LSHOW="fresh-air" | |
LSHOWs="Fresh Air" | |
SAVE=1000 | |
;; | |
"wwdtm" | "wait" | "w") | |
SHOW="wait" | |
LSHOW="wait-wait-dont-tell-me" | |
LSHOWs="Wait Wait Dont Tell Me" | |
SAVE=1000 | |
;; | |
# Where's the action? | |
"f" | "fetch" | "force" | "d" | "debug" | "c" | "clean" | "t" | "trim") | |
VERB=$li | |
;; | |
# And an optional date | |
[0-3][0-9]) | |
echo "DAY provided." | |
DAY="$(echo $li)" | |
;; | |
[0-3][0-9]-[0-1][0-9]) | |
echo "DAY-MONTH provided." | |
DAY="$(echo $li | cut -f1 -d'-')" | |
MONTH="$(echo $li | cut -f2 -d'-')" | |
;; | |
[0-1][0-9]-[0-3][0-9]-[0-9][0-9][0-9][0-9]) | |
echo "MONTH-DAY-YEAR provided." | |
DAY="$(echo $li | cut -f2 -d'-')" | |
MONTH="$(echo $li | cut -f1 -d'-')" | |
YEAR="$(echo $li | cut -f3 -d'-')" | |
;; | |
"y" | "yesterday") | |
echo "Yesterday...all my dreams were so far away..." | |
DAY=$(perl -e 'use POSIX qw(strftime);$now_string = strftime "%d", localtime(time-86400); print $now_string,"\n";') | |
MONTH=$(perl -e 'use POSIX qw(strftime);$now_string = strftime "%m", localtime(time-86400); print $now_string,"\n";') | |
echo "Day: "$DAY | |
echo "MONTH: "$MONTH | |
;; | |
mon* | tues* | wed* | thu* | fri* | sat* | sun*) | |
echo " ... Scanning ..." | |
if [ "$OS" = "macos" ] ; then | |
DAY=$(date -v -$li '+%d') | |
MONTH=$(date -v -$li '+%m') | |
else | |
DAY=$(date --date="last $li" '+%d') | |
MONTH=$(date --date="last $li" '+%m') | |
fi | |
;; | |
"t" | "today") | |
echo "Today is the first day of the rest of your life..." | |
;; | |
*[0-3][0-9]-[0-1][0-9]*.html) | |
# This loads the web page from a local location primarily for debugging purposes. | |
if [ -s "$i" -a -e "$i" ] ; then | |
# use $i not $li to preserve the case of the file passed. | |
LOCALSOURCE="$i" | |
REMOTE="false" | |
DAY=$(basename "$i" | cut -f1 -d'_' | cut -f1 -d-) | |
MONTH=$(basename "$i" | cut -f1 -d'_' | cut -f2 -d-) | |
echo $DAY $MONTH | |
echo "Source will be read from: $LOCALSOURCE" | |
else | |
export ERROR="101" | |
echo "The Source specified was not found. Sorry, $MUSER. (Error: $ERROR)" | |
pushAlert "$ERROR" | |
exit $ERROR | |
fi | |
;; | |
silent) | |
# No notifications | |
SILENT="silent" | |
;; | |
https://www.npr.org/programs/$LSHOW/$YEAR/*) | |
# This allows us to input any arbitrary show if the archive page is provided directly. | |
REMOTE="true" | |
SOURCEURL="$i" | |
YEAR=$(echo "$i" | cut -f2 -d'=' | cut -f1 -d"-") | |
MONTH=$(echo "$i" | cut -f2 -d'=' | cut -f2 -d"-") | |
DAY=$(echo "$i" | cut -f2 -d'=' | cut -f3 -d"-") | |
;; | |
sleep) | |
echo "Sleep on exit" | |
winks="100" | |
;; | |
*) | |
echo -e "\n\n ************************************************************\n \""$i"\" not a recognized input. This will be ignored.\n ************************************************************\n\n" | |
;; | |
esac | |
done | |
# Be sure we have everything we need to get going | |
if [[ -z "$LSHOWs" || -z "$VERB" ]] ; then | |
export ERROR="102" | |
echo -e "\n\n You must specify the show you want and an action.\n Options include a show name, an action and an optional date: \n\n $MACH% $(basename $0) [show] [verb] [date] [options]\n Shows: ${SHOWS[*])}\n Verbs: [ ${VERBS[*]}\n Date: MM-DD-YYYY | today | yesterday | mon-sun (of the last seven day period)]\n Options: You can also specify a link to a particular show e.g.\n\n \"https://www.npr.org/programs/weekend-edition-sunday/2016/09/11/493491872/weekend-edition-sunday-for-september-11-2016?showDate=$YEAR-MONTH-DAY\"\n\n or use a local file you downloaded for debugging. The name for a local file for debugging\n must be in the format, \"DD-MM*.html\". (Error: $ERROR)\n\n\n" | |
# pushAlert "$ERROR" | |
exit $ERROR | |
fi | |
# Check for what OS we're running on | |
if [ "$OS" = "macos" ] ; then | |
network_SSID="$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | sed -e "s/^ *SSID: //p" -e d)" | |
# Differences in the 'date' command between linux and macos. :( | |
FULLDATE="$(date -j -f '%m-%d-%Y' ${MONTH}-${DAY}-${YEAR} +'%B %e, %Y')" | |
FDATE="$(date -j -f '%d-%m' ${DAY}-${MONTH} +'%d-%B')" | |
else | |
FULLDATE="$(date -d $YEAR-$MONTH-$DAY +'%B %e, %Y')" | |
FDATE="$(date -d $MONTH/$DAY/$YEAR +'%d-%B')" | |
fi | |
CDATE=$(echo "$FULLDATE" | sed -e 's|^ *||g' -e 's/ \{2,\}/ /g') | |
ROOT="${BASE}/NPR/${LSHOWs}" | |
DEST="${ROOT}/${FDATE}" | |
} | |
# Function definitions | |
function wgets () { | |
# Centralize how we get things | |
curl -# -H "Accept-Language: en-us,en;q=0.5" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Connection: keep-alive" --user-agent "$UA" "$@"; | |
} | |
function getCoverart () { | |
if [[ ! -s ${ROOT}/Folder.jpg ]] ; then | |
echo "WARNING: No cover art found to copy. Show was fetched anyway. (Error 107)" | |
fi | |
# Copy cover images | |
if [ ! -s "${DEST}/Folder.jpg" ] ; then | |
echo "adding cover art..." | |
cp "${ROOT}"/Folder.* "${DEST}" | |
fi | |
} | |
function locateShow () { | |
# Get the show source URL. This just tells us the URL so we can fetch it in dnloadShow. | |
echo "Finding source..." | |
if [ "$REMOTE" == "true" ] ; then | |
if [ -z "$SOURCEURL" ] ; then | |
# this is when no local file is supplied, which is the default case | |
# Note this is going to the archive page and finding the particular show URL | |
# Locate the page for the edition of the show selected | |
# SOURCEURL=$(curl -q https://www.npr.org/programs/$LSHOW/archive?date=$MONTH-$DAY-$YEAR | | |
SOURCEURL=$(curl -q https://www.npr.org/programs/${LSHOW}/archive | | |
grep -A3 '<h2 class="program-show__title">' | | |
sed -e 's/^[[:space:]]*//g' | | |
grep -e "https://www.npr.org/programs/" | | |
sed -e 's/<a href="//g' -e 's/?.*//g' | | |
grep $YEAR/$MONTH/$DAY) | |
if [ -z "$SOURCEURL" ] ; then | |
SOURCEURL="https://www.npr.org/programs/${LSHOW}/" | |
fi | |
fi | |
# uregex='https?://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]' | |
# Try to make a more precise URL test. TODO get rid of this if the new one works. | |
uregex='https?://www.npr.org/programs/[-a-z0-9/?A-Z=]*' | |
if [[ $SOURCEURL =~ $uregex ]] ; then | |
# Appears we have a valid URL for the show! Ready to go! | |
echo -e "\n\n WWW src: $SOURCEURL" | |
else | |
# Something went wrong and we don't have a URL. | |
export ERROR="104" | |
echo -e "\n\n\nCan't find that show. Check: \n\n https://www.npr.org/programs/${LSHOW}/archive \n\n and try again. (Error: $ERROR)\n\n" ; | |
pushAlert $ERROR | |
if [ "$OS" = "macos" ] ; then | |
read -p "Would you like to launch that page now? " -n 1 -r | |
echo "" | |
if [[ $REPLY =~ ^[Yy]$ ]] ; then | |
open https://www.npr.org/programs/${LSHOW}/archive | |
fi | |
fi #macos | |
exit $ERROR | |
fi | |
fi | |
} | |
function dnloadShow () { | |
# Download the the show HTML | |
if [ ! -d "${ROOT}/src" ] ; then | |
echo "Creating directory (Vishnu):SRC" | |
$MKDIR "${ROOT}/src" | |
chmod a+wr "${ROOT}/src" | |
fi | |
if [ ! -d "${DEST}" ] ; then | |
echo "Creating directory (Vishnu):DEST" | |
$MKDIR "${DEST}" | |
chmod a+wr "${DEST}" | |
fi | |
cd "${DEST}" | |
if [ "$REMOTE" == "true" ] ; then | |
LOCALSOURCE=${ROOT}/src/${DAY}-${MONTH}_${LSHOW}_source.html | |
wgets -s "$SOURCEURL" -o "$LOCALSOURCE" | |
#EVALUATE IF WE STILL NEED THIS | |
if [ "$ATTEMPTS" -eq "1" ] ; then | |
echo -e " Saved to: $LOCALSOURCE \n" | |
else | |
echo -e " - Source refreshed. - " | |
fi | |
else | |
LOCALSOURCE="$LOCALSOURCE" | |
fi | |
} | |
function playList () { | |
# Make sure we have someting to work with (Story Titles and Download URLs) | |
if [ "$1" = "d" ]; then | |
STORYTITLES=$(grep 'class="rundown-segment__title"' "$LOCALSOURCE" | | |
sed -e 's/^[[:space:]]*/ /g' \ | |
-e 's/.* >/ /g' \ | |
-e 's/<\/a><\/h[1-4]>//g' ) | |
fi | |
STORYTITLECOUNT=$(grep 'class="rundown-segment__title"' "$LOCALSOURCE" | | |
sed -e 's/^[[:space:]]*//g' \ | |
-e 's/.* >//g' \ | |
-e 's/<\/a><\/h[1-4]>//g' | | |
wc -l | | |
sed -e 's/^[[:space:]]*//g' ) | |
DURLCOUNT=$(grep '<li class="audio-tool.*href="' "$LOCALSOURCE" | | |
grep -v 'play\.podtrac\.com' | | |
sed -e 's/^[[:space:]]*//g' \ | |
-e 's/.* >//g' \ | |
-e 's/<\/a><\/h[1-4]>//g' | | |
wc -l | | |
sed -e 's/^[[:space:]]*//g' ) | |
curFiles=$(find . -name '*.mp3' | wc -l | sed -e 's|^ *||g' ) | |
if [ $DURLCOUNT -lt 1 ] || [ $STORYTITLECOUNT -lt 1 ] ; then | |
export ERROR="107" | |
echo -e "No story titles or Download URLs were found. Most likely, NPR changed the format of story URLs. | |
\n\n\nPlease fix that and try again. Good Bye. (Error: $ERROR) \n" | |
echo -e " WWW source: "$SOURCEURL "\n\n" | |
echo -e " Local source:\n" $LOCALSOURCE | |
pushAlert "$ERROR" | |
exit $ERROR | |
fi | |
echo -e "\n" "URL COUNT: "$DURLCOUNT "\n" "STORY TITLE COUNT: "$STORYTITLECOUNT "\n" "Existing Local Files: $curFiles\n\n" | |
if [ "$DURLCOUNT" -gt "0" ] ; then | |
if [ "$DURLCOUNT" -eq "$STORYTITLECOUNT" ] ; then | |
READY="true" | |
# Full show | |
elif [ "$DURLCOUNT" -lt "$STORYTITLECOUNT" ]; then | |
READY="false" | |
echo -e "\n >>>>> Some stories not posted, fetching what I can... <<<<<\n\n" | |
elif [ "$DURLCOUNT" -gt "$STORYTITLECOUNT" ]; then | |
# This should never happen! Probably a change in the NPR HTML. | |
export ERROR="108" | |
echo -e "Somehow there are more Download URLs than Story Titles. Probably NPR has changed their HTML. | |
\n\n\nPlease fix that and try again. Good Bye. (Error: $ERROR) \n" | |
echo -e " WWW source: "$SOURCEURL "\n\n" | |
echo -e " Local source:\n" $LOCALSOURCE | |
pushAlert "$ERROR" | |
exit $ERROR | |
fi | |
else | |
echo -e "No URLs found?" | |
export ERROR="110" | |
exit | |
fi | |
} | |
function countDown () { | |
# countDown when we have to retry because a show isn't ready | |
SECS_l=$SECS | |
echo $SECS | |
while [ $SECS_l -gt 0 ] ; do | |
echo -ne "... $SECS_l seconds ... \033[0K\r"; | |
sleep 1; | |
: $((SECS_l--)); | |
done | |
let ATTEMPTS=$ATTEMPTS+1 | |
echo This is the $ATTEMPTS try. | |
COUNTER_S=1 # reset counter for next attempt. | |
COUNTER_Sp=0 | |
# This automatically does a partial download when we can. | |
echo -e "\n\nExecuting a partial download..." | |
export PARTIAL="true" | |
} | |
function trimShows () { | |
# Clean up old shows | |
echo -e "\n\nCleaning old shows..." | |
find "$ROOT" -maxdepth 1 -type d -name '[0-3][0-9]-[A-Z]*' -mmin +$SAVE -exec rm -rf {} \; -exec echo " " {} \; | |
echo -e "\n\nCleaning old source files...\n\n" | |
find "$ROOT"/src/*_source.html* -mmin +1080 -exec rm -rf {} \; -exec echo " " {} \; | |
# Disabling l ocal syncing as it really isn't helpful. Keeping it around for now just in case. | |
# if [ "$network_SSID" == "$HOMENET" ] ; then | |
# # mount volume $NAS [No longer needed] | |
# if [ -d $NASPATH ] | |
# then | |
# echo foo > /dev/null | |
# elif [ "$PARTIAL" = "false" ] | |
# then | |
# echo foo > /dev/null | |
# fi | |
# else | |
# echo foo > /dev/null | |
# fi | |
# # echo "'$LSHOWs' cleaned up." | |
} | |
function pushAlert () { | |
# Send push notification | |
echo $SILENT | |
if [ "$SILENT" != "silent" ] | |
then | |
# Partial show | |
if [ "$READY" != "true" -o "$PARTIAL" = "true" ] | |
then | |
json='{"value1":"'$LSHOWs': Partial: '$DURLCOUNT'/'$STORYTITLECOUNT' stories '$FINI' '$ERROR' @'$MACH'","value2":"'$WORKFLOW'","value3":"'$icon'"}' | |
curl -X POST -H "Content-Type: application/json" -d "$json" \ | |
https://maker.ifttt.com/trigger/NPRready/with/key/$APIKEY | |
elif [ "$READY" == "true" ]; then | |
json='{"value1":"'$LSHOWs': '$STORYTITLECOUNT' stories ready '$ERROR' @'$MACH'","value2":"'$WORKFLOW'","value3":"'$icon'"}' | |
curl -X POST -H "Content-Type: application/json" -d "$json" \ | |
https://maker.ifttt.com/trigger/NPRready/with/key/$APIKEY | |
else | |
echo "Wha?" | |
json='{"value1":"'$LSHOWs': '$COUNTER_Sp' some stories not ready '$ERROR' @'$MACH'","value2":"'$WORKFLOW'","value3":"'$icon'"}' | |
curl -X POST -H "Content-Type: application/json" -d "$json" \ | |
https://maker.ifttt.com/trigger/NPRready/with/key/$APIKEY | |
fi | |
fi | |
# ALL: [empty device_iden] | |
# iPad: -d device_iden="ujzPqBxLkf6sjAgRWvTbAi" | |
# iPhone: -d device_iden="ujzPqBxLkf6sjAzH22y5AW" | |
# Chrome: -d device_iden="ujzPqBxLkf6sjzYOTRxv3c" | |
} | |
# End of functions | |
# MAIN Event | |
cInputs "$@" | |
case $VERB in | |
"force" ) | |
echo -e "\n\n 🗑 clearing \"${LSHOWs}\"...\n\n\n" | |
$0 $SHOW $DAY c silent | |
echo -e " go \n\n" | |
$0 $SHOW $DAY f silent | |
;; | |
"f" | "fetch" ) | |
echo "Fetch" | |
locateShow "$@" | |
dnloadShow | |
playList | |
echo -e "\n\n\nbegin fetch...\n" | |
until [ "$DURLCOUNT" -eq "$STORYTITLECOUNT" ] && [ "$DURLCOUNT" -eq "$curFiles" ]; do | |
awk ' | |
BEGIN { OFS="\t" } | |
/<article id="/ { f=1 } | |
/<\/article>/ { f=0 } | |
f { | |
if ( sub(/.*<h4 class="audio-module-title">[[:space:]]*/,"") ) { | |
sub(/[[:space:]]*<\/h4>.*/,"") | |
if ( title != "" ) { | |
printf "Skipping title\n", FILENAME, titleFnr, title | "cat>&2" | |
} | |
title = $0 | |
titleFnr = FNR | |
} | |
else if ( sub(/.*<li class="audio-tool audio-tool-download">[^"]+"/,"") ) { | |
sub(/".*/,"") | |
url = $0 | |
if ( url ~ "https://play.podtrac.com/" ) { | |
url = "" | |
} | |
if ( title != "" && url != "" ) { | |
gsub("/","_",title) # get rid of slashes in titles | |
print url, title | |
} | |
else if ( title = "" && url != "" ) { | |
printf "Error@ %s[%d]: Got URL %s but no title.\n", FILENAME, FNR, url | "cat>&2" | |
} | |
else if ( title != "" && url = "" ) { | |
printf "Error@ %s[%d]: Got TITLE %s but no url.\n", FILENAME, FNR, url | "cat>&2" | |
} | |
title = url = "" | |
} | |
} | |
END { | |
if ( title != "" ) { | |
printf "Error@ %s[%d]: Got title, %s but no URL.\n\n", FILENAME, titleFnr, title | "cat>&2" | |
} | |
} | |
' "$LOCALSOURCE" | | |
while IFS=$'\t' read -r url title; do | |
# echo -e "what we got" $title $url | |
# format the counter | |
if [ "$COUNTER_S" -lt "10" ] ; then | |
export COUNTER_Sp=0$((COUNTER_S++)) | |
else | |
export COUNTER_Sp=$((COUNTER_S++)) | |
fi | |
echo -e "checking: ${COUNTER_Sp} $title ..." | |
# check to see if the file exists (and is > 0bytes) | |
if [ -s "${DEST}/${COUNTER_Sp} $title.mp3" ] ; then | |
echo -e " 😎 "$COUNTER_Sp $title "... already downloaded\n\n" | |
else # Here's the main action (believe it or not) | |
echo -e "+++ getting: $COUNTER_Sp $title ..." | |
wgets "$url" -o "${DEST}/${COUNTER_Sp} ${title}.mp3" | |
# Check to see if it completed | |
if [ -s "${DEST}/${COUNTER_Sp} ${title}.mp3" ] ; then | |
echo -e " ✅ "$COUNTER_Sp ${title}.mp3 " is ready!\n\n" | |
else | |
rm "${COUNTER_Sp} ${title}.mp3" | |
echo -e " ❌ "$COUNTER_Sp ${title}.mp3 " FAIL\n\n" | |
rm "$COUNTER_Sp ${title}.mp3" > /dev/null | |
fi | |
fi | |
# Get rid of any files that had been downloaded before the show was complete as that may have | |
# changed the segment prefix and left behind a duplicate story. | |
DEL=$(find . -name "*${title}.mp3" \! -name "${COUNTER_Sp} *" -exec echo {} \;) | |
if [ -n "$DEL" ]; then | |
echo -e ' 🗑 Removing stale story (Warning 108):' $DEL'\n' | |
rm "$DEL" | |
fi | |
# need to figure out if we can still determine when partial shows are there and change this. | |
done | |
if [ "$READY" == "false" ] ; then | |
countDown | |
dnloadShow | |
playList | |
getCoverart | |
fi | |
curFiles=$(find . -name '*.mp3' | wc -l | sed -e 's|^ *||g' ) | |
echo Existing Local Files: $curFiles | |
done | |
FINI=$(date) | |
END=$(date "+%s") | |
ELAPSED=$(( $END - $START )) | |
getCoverart | |
echo -e "Total Time: $ELAPSED seconds\n Completed: @ $FINI...\n ===============\n\n" | |
pushAlert "$FINI" | |
if [ "$OS" = "macos" ] ; then | |
echo -e " ...opening $DEST \n" | |
open "$DEST" | |
# If Mac AND option to sleep, then sleep when done. | |
for s in "$@"; do | |
if [ "$s" == "sleep" ] ; then | |
SECS_l=$winks | |
echo -e "\n\nGood Night, $MUSER." | |
while [ $SECS_l -gt 0 ] ; do | |
echo -ne " $MACH will sleep in ... $SECS_l seconds ... \033[0K\r"; | |
sleep 1; | |
: $((SECS_l--)); | |
done | |
/usr/bin/pmset sleepnow & | |
exit | |
fi | |
done | |
fi | |
trimShows | |
exit # fetch complete | |
;; | |
"d" | "debug") | |
echo -e "==============\ndebug mode" | |
locateShow "$@" | |
dnloadShow | |
playList "d" | |
# Varibles | |
echo " Machine: "$MACH | |
echo " USER: "$MUSER | |
echo " UA: "$UA | |
echo " OS: "$OS | |
echo " current is: "$LOCATION | |
echo " DAY: "$DAY | |
echo " MONTH: "$MONTH | |
echo " CDATE: "$CDATE | |
echo " YEAR: "$YEAR | |
echo " FDATE: "$FDATE | |
echo " FULLDATE: "$FULLDATE | |
echo " SHOW: "$SHOW | |
echo " LSHOW: "$LSHOW | |
echo " LSHOWs: "$LSHOWs | |
echo " NASPATH: "$NASPATH | |
echo " VERB: " $VERB | |
echo " SAVE: "$SAVE | |
echo " DEST: "$DEST | |
echo " SOURCEURL: "$SOURCEURL | |
echo " LOCALSOURCE: "$LOCALSOURCE | |
echo " ROOT: "$ROOT | |
echo " READY: "$READY | |
echo " COUNTER_S: "$COUNTER_S | |
echo " URLCOUNT: "$DURLCOUNT | |
echo " STORYTITLECOUNT: "$STORYTITLECOUNT | |
echo -e " List of stories: \n$STORYTITLES \n\n" | |
exit # debug complete | |
;; | |
"t" | "trim") | |
trimShows | |
exit # trim complete | |
;; | |
"c" | "clean") | |
echo "DEST is: ${DEST}" | |
rm -rf "${DEST}" | |
echo "$DEST Trimmed. All stories removed. (Shiva)" | |
exit # clean complete | |
;; | |
esac | |
# Shortcut for deploying to Synology | |
# cp /Users/bierman/Documents/Applications/bin/getnpr.sh /Volumes/michael/bin; cp /Users/bierman/Documents/Applications/bin/getnprdata.txt /Volumes/michael/bin | |
# empty directory, complete show. | |
# partial directory, complete show. | |
# empty directory, incomplete show. | |
# partial directory, incomplete show. | |
# Check which are running | |
# sudo launchctl list | |
# launchctl unload $HOME/Library/LaunchAgents/com.getnpr.atc.plist | |
# sleep 3 | |
# launchctl unload $HOME/Library/LaunchAgents/com.getnpr.atc.plist | |
# launchctl load -w $HOME/Library/LaunchAgents/com.getnpr.atc.plist | |
# launchctl load -w $HOME/Library/LaunchAgents/com.getnpr.atc.plist | |
# com.getnpr.atc.plist | |
# com.getnpr.morningedition.plist | |
# com.getnpr.wesat.plist | |
# com.getnpr.wesun.plist |
This allows you to download NPR shows like Morning Edition, All Things Considered, Weekend Edition Saturday and Weekend Edition Sunday. These shows don't have podcasts, so I wanted a way to fetch them for offline listening.
If you want the notifications to work you will need a IFTTT and an API key. Simply tweak the script to look that up (currently I have it look up that information from a standalone file so that it isn't in my gist).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/mbierman/2ed795d011e87e8e8fcd