Get under surveillance script: Difference between revisions

From Sidiprojects Wiki
Jump to navigation Jump to search
mNo edit summary
(updated get_under_surveillance.ksh script to work again)
Line 1: Line 1:
<pre>
<pre>
#!/bin/ksh
#!/bin/ksh
 
#
# Quick-and-dirty script to fetch the KXCI radio show Under Surveillance from last Friday.
# Quick-and-dirty Script to fetch the KXCI radio show Under Surveillance from  
# Works with Ksh, Bash (and probably others).
# last Friday (available that Saturday). Tell me if it stops working!
#
#
# Requires mid3v2, or id3v2 to set the metadata; curl to get the files.
# Works on ksh, bash, and probably others. Requires awk, ffmpeg, mid3v2.
#  
#  
# by david (david@sidiprojects.us)
# by david, david@sidiprojects.us (0x5AFF330ADEFD0AE2C59E9C3DD7362C75283B6B3D)
# 5 Jan 2022
#
# Licensed under WTFPL
# 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 {
function write_line {
   TERMINAL_WIDTH=$(stty size | cut -d' ' -f2)
   TERMINAL_WIDTH=$(stty size | cut -d' ' -f2)
   for ((j=0; j<$TERMINAL_WIDTH; j++)); do
   for ((j=0; j<$TERMINAL_WIDTH; j++)); do
       echo -n "-"
       echo -n "-" >&2
   done
   done
   echo
   echo >&2
}
}


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


# MAIN FUNCTION
# MAIN FUNCTION
if [[ $(date +%A) = "Saturday" ]]; then
[[ $(date +%A) = "Saturday" ]] && DATE_TO_GET="now" || DATE_TO_GET="last saturday"
  DATE_TO_GET="now"  
else
  DATE_TO_GET="last saturday"
fi
YEAR=$(date -d "${DATE_TO_GET}" +%Y)      # full year (e.g., 2015)
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)
MONTHNAME=$(date -d "${DATE_TO_GET}" +%b) # month abbrev. name (e.g., Oct)
Line 38: Line 52:
DAY=$(date -d "${DATE_TO_GET}" +%d)      # day, with 2 digits (e.g., 31)
DAY=$(date -d "${DATE_TO_GET}" +%d)      # day, with 2 digits (e.g., 31)
SAVEDIR="${HOME}/Music/under_surveillance/${MONTHNAME}_${DAY}_${YEAR}"
SAVEDIR="${HOME}/Music/under_surveillance/${MONTHNAME}_${DAY}_${YEAR}"
SUCCESSES=0
ORIGINAL_MANIFEST_NAME="index.m3u8"
NEW_MANIFEST_NAME="newindex.m3u8"


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


for ((HR_NUM=5; HR_NUM <= 6; HR_NUM += 1)); do
remote_dirname="KXCI-${YEAR}${MONTH}${DAY}T060000Z"
  if [[ HR_NUM -eq 6 ]]; then
local_filename="Under-Surveillance_${DAY}-${MONTHNAME}-${YEAR}.mp3"
      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 \
echo "Getting manifest, cutting it down to size..." >&2
        -e "https://spinitron.com/KXCI/pl/12118663/Under-Surveillance" \
wget "https://ark2.spinitron.com/ark2/${remote_dirname}/index.m3u8" -O ${ORIGINAL_MANIFEST_NAME}
        -A "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0" \
 
        --output $FILENAME \
awk '
        https://ark-kxci.s3.us-east-1.amazonaws.com/${FILENAME}; then
  /https:\/\/ark2\.spinitron\.com\/ark2\/KXCI-20220101T060000Z\/seg-368-a1\.ts/ {
            ((SUCCESSES += 1))
      print($0)
            mid3v2 \
      FLAG=1
              --artist="Dave Wright, Falcotronik" \
  }
              --album="Under Surveillance" \
 
              --genre="Amish Grime" \
  FLAG {
              --date=${YEAR}-${MONTH}-${DAY} \
      next
              --comment="Shout out as always to Two toes McPhee and Pickle-faced Pete" \
  }  
              ${FILENAME}
 
  # 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


      fi
  done
done


echo "Downloaded $SUCCESSES files successfully."
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


</pre>
</pre>

Revision as of 18:48, 5 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 '
   /https:\/\/ark2\.spinitron\.com\/ark2\/KXCI-20220101T060000Z\/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