Get under surveillance script: Difference between revisions

From Sidiprojects Wiki
Jump to navigation Jump to search
(updated get_under_surveillance.ksh script to work again)
m (fixed bug in awk part of the script)
Line 70: Line 70:


awk '
awk '
   /https:\/\/ark2\.spinitron\.com\/ark2\/KXCI-20220101T060000Z\/seg-368-a1\.ts/ {
   /seg-368-a1\.ts/ {
       print($0)
       print($0)
       FLAG=1
       FLAG=1

Revision as of 19:12, 11 January 2022

#!/bin/ksh
#
# Quick-and-dirty Script to fetch the KXCI radio show Under Surveillance from 
# last Friday (available that Saturday). Tell me if it stops working!
#
# Works on ksh, bash, and probably others. Requires awk, ffmpeg, mid3v2.
# 
# by david, david@sidiprojects.us (0x5AFF330ADEFD0AE2C59E9C3DD7362C75283B6B3D)
# 5 Jan 2022
#
# Licensed under WTFPL


function do_exit_tasks {
   rm "${SAVEDIR}/${ORIGINAL_MANIFEST_NAME}" 2>/dev/null
   rm "${SAVEDIR}/${NEW_MANIFEST_NAME}" 2>/dev/null
}
trap do_exit_tasks EXIT


function do_interrupt_tasks {
   read answer?"Erase the directory for this week's Under Surveillance (${SAVEDIR}) [y/N]?"
   [[ "${answer}" = "y" ]] && rm -rf "${SAVEDIR}"
}
trap do_interrupt_tasks SIGINT


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


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


# MAIN FUNCTION
[[ $(date +%A) = "Saturday" ]] && DATE_TO_GET="now" || DATE_TO_GET="last saturday"
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}"
ORIGINAL_MANIFEST_NAME="index.m3u8"
NEW_MANIFEST_NAME="newindex.m3u8"

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

remote_dirname="KXCI-${YEAR}${MONTH}${DAY}T060000Z"
local_filename="Under-Surveillance_${DAY}-${MONTHNAME}-${YEAR}.mp3"

echo "Getting manifest, cutting it down to size..." >&2
wget "https://ark2.spinitron.com/ark2/${remote_dirname}/index.m3u8" -O ${ORIGINAL_MANIFEST_NAME}

awk '
   /seg-368-a1\.ts/ {
      print($0)
      FLAG=1
   } 
   
   FLAG {
      next
   } 
   
   # fall-through case
   {
      print($0)
   }
   
   END {
      print("#EXT-X-ENDLIST")
   }' \
   ./index.m3u8 > "${NEW_MANIFEST_NAME}"
echo "... done."

echo "Reading fragments and combining to mp3 (this may take a minute)..." >&2
sleep 2;
ffmpeg -protocol_whitelist "file,tcp,tls,https" -i newindex.m3u8 "${local_filename}" >/dev/null 2>&1
echo "... done." >&2


mid3v2 \
   --artist="Dave Wright, Falcotronik" \
   --album="Under Surveillance" \
   --genre="IDM Square-Dance and Atmospherics" \
   --date=${YEAR}-${MONTH}-${DAY} \
   --comment="Shout out as always to Two toes McPhee and Pickle-faced Pete" \
   ${local_filename}

echo "Done: Enjoy Under Surveillance." >&2