<p>In this episode I explain how I use python to track the number of shows in the HPR queue and then turn on a blinkstick to indicate the size of the queue.</p>
<ul>
<li><p>Stack exchange<br />
<a href="https://stackoverflow.com/" class="uri">https://stackoverflow.com/</a></p></li>
<li><p>urllib (Python 3), urllib2 (Python 2)<br />
<a href="https://docs.python.org/2/library/urllib.html" class="uri">https://docs.python.org/2/library/urllib.html</a></p></li>
<li><p>re - regular expressions in python<br />
<a href="https://docs.python.org/2/library/re.html" class="uri">https://docs.python.org/2/library/re.html</a></p></li>
<li><p>Blinkstick<br />
<a href="https://www.blinkstick.com/" class="uri">https://www.blinkstick.com/</a></p></li>
</ul>
<p>Python code included below</p>
<pre><code>#!/usr/bin/env python3

### This is a scratchpad file I&#39;ve created to try out snippets of code in python

# The script below is for use with Python 3
# This script should work out of the box on most systems running a version of Python 3 
# If you happen to have a blinkstick lying about then your can uncomment the blinkstick module
# and uncomment the references at the bottom of the program that call the blinkstick functions
# Regards, Mr X


# Imported modules
from time import sleep          # used to pause program
#from blinkstick import blinkstick  # used to control blinkstick nano attached to usb port of raspberry pi
import urllib.request           # used to capture hpr webpage content to get the number of HPR shows in the que
import re               # regular expressions, used to find sting in HPR webpage (get_hpr_que)


# These functions control a blink stick nano attached to my raspberry pi USB port #################
# They can be ignored or deleted if you don&#39;t have one


def bstick_off():
# Search for all attached blinksticks and turn them all off
    for bstick in blinkstick.find_all():
        bstick.turn_off()   # Turn front blinkstick LED off
        bstick.set_color(channel=0, index=1, name=&quot;black&quot;)  # Turn rear blinkstick led off
        print(&quot;Blinkstick: &quot; + bstick.get_serial() + &quot; turned off&quot;)


def bstick_on(colour):
# Turn blinkstick on and set led colour to string value stored in var colour
# valid colours are, black, silver, gray, white, maroon, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, aqua
    for bstick in blinkstick.find_all():
        bstick.set_max_rgb_value(30)        # Sets max blinkstick RGB value to 15, makes LED dimm
        bstick.set_color(name=colour)       # Turn blinkstick on, var colour determines colour
        print (&quot;Blinkstick: &quot; + bstick.get_serial() + &quot; | Colour: &quot; + bstick.get_color(color_format=&quot;hex&quot;) + &quot; [&quot; + colour + &quot;]&quot;)
#hex

def bstick_on_random():
# Turn blinkstick on colour random
    for bstick in blinkstick.find_all():
        bstick.set_random_color()
        print (&quot;Blinkstick: &quot; + bstick.get_serial() + &quot; | Colour: &quot; + bstick.get_color(color_format=&quot;hex&quot;))


def bstick_blink_red():
# Flash blinkstick colour red
    for bstick in blinkstick.find_all():
        bstick.blink(name=&quot;red&quot;)
        print (&quot;Blinkstick: &quot; + bstick.get_serial() + &quot; | Colour: &quot; + bstick.get_color(color_format=&quot;hex&quot;))

################################################################################


def get_hpr_que():
# Goto hacker public radio calendar page and extract the number of shows in the queue
# then return the number of shows as an integer
# also turns on blinkstick LED and sends number of HPR shows in the que to the display

    url = &#39;https://hackerpublicradio.org/calendar.php&#39;   # HPR url for calendar page
    try:
        html_content = urllib.request.urlopen(url).read()   # Try to read hpr calendar page
    except:
        print(&quot;ERROR: Problem acessing url &quot; + url)     # if error accessing url then return -1
        hpr_shows = -1
        return hpr_shows
    html_page = str(html_content)   # convert to string
    line_begin = html_page.find(&#39;There are only &lt;strong&gt;&#39;) # find position of string in html page
    line_end = line_begin + 70 # Store line end position (start position + 70)
    line = html_page[line_begin:line_end]  # Capture string line
    #print(line) # DEBUG Print line string
    digit = re.findall(r&#39;\d+&#39;,line)         # Find digits in line
    #print(digit[0])    # print the 1st digit
    try:
        hpr_shows = int(digit[0])   # convert digit list to integer days
    except:                         # If show numbers not found then return -1
        print(&quot;ERROR: Problem getting number of HPR shows in que.&quot;)
        hpr_shows = -1
        return hpr_shows
    #print(hpr_shows) # DEBUG
    #return hpr_shows
    if hpr_shows &gt; 9:       # If hpr show que &gt; 9 turn on green LED
        print(&quot;Turn on green blinkstick LED&quot;)
        #bstick_on(&quot;green&quot;)
    elif hpr_shows &gt; 5:     # Else if hpr show que &gt; 5 turn on blue LED
        print(&quot;Turn on blue blinkstick LED&quot;)
        #bstick_on(&quot;blue&quot;)
    elif hpr_shows &gt; -1:    # Else if hpr show que &gt; -1 turn on ref LED
        print(&quot;Turn on red blinkstick LED&quot;)     
        #bstick_on(&quot;red&quot;)
    else:
        print(&quot;Flash red blinkstick LED&quot;)
        #bstick_blink_red() # Else blink LED to show error
    print(&quot;The are &quot; + str(hpr_shows) + &quot; shows in the HPR que...&quot;)
    sleep(4)
    print(&quot;Turn off all blinkstick LED&#39;s&quot;)
    #bstick_off()           # Turn blinkstick off


# Main program
get_hpr_que()</code></pre>
