Get under surveillance script

From das_wiki
Jump to: navigation, search
#!/bin/ksh

# Quick-and-dirty script to fetch the KXCI radio show Under Surveillance from last Friday.
# Works with Ksh, Bash (and probably others).
# 
# Requires mid3v2, or id3v2 to set the metadata; curl to get the files.
# 
# by david (david@sidiprojects.us)
# Licensed under WTFPL

function write_line {
   TERMINAL_WIDTH=$(stty size | cut -d' ' -f2)
   for ((j=0; j<$TERMINAL_WIDTH; j++)); do
      echo -n "-"
   done
   echo
}

function display_banner {
   write_line; write_line
   echo Getting
   figlet -t "UNDER SURVEILLANCE"
   echo for $(date -d "${DATE_TO_GET}" +%F)
   write_line; write_line
}


# MAIN FUNCTION
if [[ $(date +%A) = "Saturday" ]]; then
   DATE_TO_GET="now" 
else
   DATE_TO_GET="last saturday"
fi
YEAR=$(date -d "${DATE_TO_GET}" +%Y)      # full year (e.g., 2015)
MONTHNAME=$(date -d "${DATE_TO_GET}" +%b) # month abbrev. name (e.g., Oct)
MONTH=$(date -d "${DATE_TO_GET}" +%m)     # month number (e.g., 10)
DAY=$(date -d "${DATE_TO_GET}" +%d)       # day, with 2 digits (e.g., 31)
SAVEDIR="${HOME}/Music/under_surveillance/${MONTHNAME}_${DAY}_${YEAR}"
SUCCESSES=0

if mkdir ${SAVEDIR} 2> /dev/null; then
   cd $SAVEDIR
   display_banner
else
   echo "Directory $SAVEDIR already exists. Exiting..."
   exit 1 
fi

for ((HR_NUM=5; HR_NUM <= 6; HR_NUM += 1)); do
   if [[ HR_NUM -eq 6 ]]; then
      MIN_END=55  # XXX: adjust when the show time changes...
   else
      MIN_END=55
   fi
   for MIN_NUM in $(seq -w 0 5 $MIN_END); do
      FILENAME="KXCI-${YEAR}${MONTH}${DAY}T0${HR_NUM}${MIN_NUM}00Z.mp3"

      if curl \
         -e "https://spinitron.com/KXCI/pl/12118663/Under-Surveillance" \
         -A "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0" \
         --output $FILENAME \
         https://ark-kxci.s3.us-east-1.amazonaws.com/${FILENAME}; then
            ((SUCCESSES += 1))
            mid3v2 \
               --artist="Dave Wright, Falcotronik" \
               --album="Under Surveillance" \
               --genre="Amish Grime" \
               --date=${YEAR}-${MONTH}-${DAY} \
               --comment="Shout out as always to Two toes McPhee and Pickle-faced Pete" \
               ${FILENAME}

      fi
   done
done

echo "Downloaded $SUCCESSES files successfully."