<p>Hello all. This is gemlog from Terrace, bc, canada just up near the
alaska panhandle.</p>
<p>Some of you may know me from in COM chat on sdf dot org or as a
fedizen on the tilde dot zone instance of mastodon.</p>
<p>Now, the other day I finally got around to checking out HPR properly,
even though my masto-pal claw-dio-m turned me on to it a couple of years
ago.</p>
<p>Recently, on a friday night in irc on tilde radio, I noticed there
were whole series on hpr and not only single shows and that got me kind
of excited.<br />
I guess I'm easily excitable.</p>
<p>Anyhow, something I could listen to at work or while driving. Still,
I managed to forget about it until /just/ before I was leaving the house
for work on Monday morning. I rushed to copy over a few shows - nearly
at random onto my phone and headed out to work.</p>
<p>After I got my morning sorted at work, I told VLC to play-all and
enjoyed a couple of shows. I noticed that each show I had chosen had a
beg post at the beginning. I figured I could make one on at least
something from my messy gemlog/bin dir.</p>
<p>However, after a break, I came back and couldn't remember which 4
digit numbered dot mp3 I had finished up on, which mildly irked me.
Well, as we all know, irk becomes itch and I put my sad regex skills to
the test scraping the hpr website with a custom bash script later when I
got home.</p>
<p>A very custom bash script. Like all scrapers, if any of the guys at
hpr even breathe the wrong way, it will probably break horribly. On the
other hand, I've had scrapers that looked just as sad running for many
years against a canadian government site. So. Who knows?</p>
<p>All the script uses are some built-ins from bash along with sed and
wget for the actual getting. My local instance of searX N G was left
smoking as scrambled for sed incantations to string together. I'm not a
sed guy.</p>
<p>Usage is simple, as the script only accepts one argument: ... the
four digit series number of the show you want to download. It will
create a dir with the series name and download every mp3 it finds,
renaming each show to the show title.</p>
<p>I was tempted to doll it up with some niceties like options for
download dir, a selector for a series with a dialog of some kind... yada
yada yada.</p>
<p>But... we all know what happens when you stretch a quick hack with a
bash script too far for the scripting language: hours of misery wishing
you'd started with some other language.</p>
<p>So far, I've used the script to download 8 series. DU dash S H tells
me they add up to 2 dot 2 gig, so it seems to work well enough.</p>
<p>It comes with the same iron clad warranty as everything I write:</p>
<p>If it breaks, you get to keep all the pieces. Thanks for
listening.</p>
<pre><code>#!/bin/bash
# gemlog@gemlog.ca 2023-08-26
# License: CC BY-SA 4.0.
# not proud of my continuing lack of regex foo frankly...

if [ $# -lt 1 ]; then
  echo 1&gt;&amp;2 &quot;$0: You need to enter the HPR Series Number to download as 4 digits&quot;
  echo &quot;The full list of HPR Series is at https://hackerpublicradio.org/series/index.html&quot;
  exit 2
fi

snumber=$1
re=&#39;^[[:digit:]]{4}$&#39;
if [[ $snumber =~ $re ]]; then
    wget https://hackerpublicradio.org/series/$snumber.html -q -O /tmp/$snumber.html
    content=$(&lt;/tmp/$snumber.html)
    declare -a shows
    shows=$(grep -P &#39;^(?=.*h3)(?=.*title)&#39; /tmp/$snumber.html)
else
    echo &quot;&#39;$snumber&#39; is not exactly 4 digits like an HPR series number&quot;
    exit 2
fi

series=$(echo $content | sed -e :a -e &#39;s/&lt;[^&gt;]*&gt;//g;/&lt;/N;//ba&#39; | grep -o -P -m1 &#39;(?&lt;=In-Depth Series:).*(?=Number)&#39; | sed &#39;s/[ t]*$//&#39; )
series=$(echo ${series// /_} | cut -b 2-50 | sed &#39;s/_*$//&#39; | sed &#39;s/^_*//&#39; | sed s/[^A-Za-z0-9_.]/_/g)

#outdir=&quot;/home/gemlog/Music/Audio/HPR/$series-Se$snumber/&quot;
outdir=~/&quot;Downloads/HPR/$series-Se$snumber/&quot;
mkdir -p &quot;$outdir&quot;
echo &quot;Files for the series &quot;$series&quot; will be saved in $outdir&quot;

declare -a shows
declare -a url_array
shows=$(grep -P &#39;^(?=.*h3)(?=.*title)&#39; /tmp/$snumber.html)
IFS=$&#39;n&#39;

for line in $shows
  do
    f=$((f+1))
done
echo
echo
echo &quot;Downloading $f mp3 files&quot;


for line in $shows
  do
    i=$((i+1))
    title=$(echo $line | sed -e :a -e &#39;s/&lt;[^&gt;]*&gt;//g;/&lt;/N;//ba&#39; | grep -o -P &#39;(?&lt;=::).*(&#39;host&#39;)&#39;  | sed &#39;s/host//&#39; | sed &#39;s/[ t]*$//&#39; | sed s/[^A-Za-z0-9_.]/_/g | sed &#39;s/ /_/g&#39; | sed &#39;s/^_*//&#39; )
    enumber=$(echo $line | sed -e :a -e &#39;s/&lt;[^&gt;]*&gt;//g;/&lt;/N;//ba&#39; | grep -o -P &#39;(?&lt;=hpr).*(&#39;::&#39;)&#39; | sed &#39;s/:://&#39;)
    enumber=$(printf &quot;%04d&quot; $((enumber)) )
    outfile=$outdir$title-Ep$enumber.mp3
    url=&quot;https://www.hackerpublicradio.org/eps/hpr$enumber.mp3&quot;
    echo &quot;Downloading file $i: $title&quot;
    wget --verbose --max-redirect 2 $url -O $outfile
    sleep 2
  done


echo
ttlfiles=$(ls -1 $outdir | wc -l)
echo &quot;$ttlfiles files for the series &quot;$series&quot; were saved in $outdir&quot;

exit 0</code></pre>
