Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr2340 :: Tracking the HPR queue in Python

I explain how I capture the number of HPR shows in the queue using python

<< First, < Previous, , Latest >>

Hosted by MrX on Friday, 2017-07-21 is flagged as Clean and is released under a CC-BY-SA license.
Python, Programming, Hardware, BlinkStick. 4.
The show is available on the Internet Archive at: https://archive.org/details/hpr2340

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:21:32

general.

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.

Python code included below

#!/usr/bin/env python3

### This is a scratchpad file I'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'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="black")  # Turn rear blinkstick led off
        print("Blinkstick: " + bstick.get_serial() + " turned off")


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 ("Blinkstick: " + bstick.get_serial() + " | Colour: " + bstick.get_color(color_format="hex") + " [" + colour + "]")
#hex

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


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

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


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 = 'https://hackerpublicradio.org/calendar.php'   # HPR url for calendar page
    try:
        html_content = urllib.request.urlopen(url).read()   # Try to read hpr calendar page
    except:
        print("ERROR: Problem acessing url " + 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('There are only <strong>') # 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'\d+',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("ERROR: Problem getting number of HPR shows in que.")
        hpr_shows = -1
        return hpr_shows
    #print(hpr_shows) # DEBUG
    #return hpr_shows
    if hpr_shows > 9:       # If hpr show que > 9 turn on green LED
        print("Turn on green blinkstick LED")
        #bstick_on("green")
    elif hpr_shows > 5:     # Else if hpr show que > 5 turn on blue LED
        print("Turn on blue blinkstick LED")
        #bstick_on("blue")
    elif hpr_shows > -1:    # Else if hpr show que > -1 turn on ref LED
        print("Turn on red blinkstick LED")     
        #bstick_on("red")
    else:
        print("Flash red blinkstick LED")
        #bstick_blink_red() # Else blink LED to show error
    print("The are " + str(hpr_shows) + " shows in the HPR que...")
    sleep(4)
    print("Turn off all blinkstick LED's")
    #bstick_off()           # Turn blinkstick off


# Main program
get_hpr_que()

Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2017-05-25 08:55:18 by Ken Fallon

You don't need to scrape

Hi MrX,

Haven't listened to the show yet but you don't need to scrape hpr. This is your network and if you want a statistics we can give it to you. There is this page https://hackerpublicradio.org/calendar.php but if there is an easier format to get the information, we can make it.

Ken,

Comment #2 posted on 2017-05-31 19:09:37 by MrX

Re you don't need to scrape

Hi Ken sorry for the delay in replying as I've been on holiday.

Thanks for the comment, very good to know, never thought about asking for a special page generally when you visit a site you get what you see and I would never normally think about asking for something tailored for my own very specific needs.

My script was hacked together and I just wanted the job done I'm sure there are better ways to do it, it was a good learning experience.

As it stands the script downloads the calender page and grabs the numeric value of the number of shows in the queue. It only gets run once a day and shouldn't put much of a strain on the HPR servers even in the unlikely event that many people find it useful.

Basically I need to capture the number of shows left in the HPR queue. I would imagine the simplest way would be to serve a page giving a numeric value of the number of shows in the HPR queue. If you can arrange for that or think of a better solution that would be great.

I'll then have a think about how to modify my script and perhaps if I get time will do a quick follow up show

Cheers MrX

Comment #3 posted on 2017-06-01 08:49:33 by Dave Morriss

See show 1986

Hi Mr X,

I haven't listened yet, but judging from the notes this looks like a great topic, and an interesting show.

You might find it useful to look at my show 1986, one of the sed series. In it, in example 2, I showed how to parse the current queue level out of the stats file you can look at on the HPR site. The link to the example is:

https://hackerpublicradio.org/eps/hpr1986/full_shownotes.html#example-2

The link to the stats you'd need is in the Links section of that show, and I also mention it in show 2255.

You might prefer the challenge of scraping HTML, but this is a pretty easy route to the information you want

Dave

Comment #4 posted on 2017-06-01 16:35:15 by MrX

re: See show 1986

Hi Dave thanks for getting back to me, yes this would be a more eloquent solution, I remember listening to the show and really enjoying it though I was unable to give it the full attention it deserved, these days free time is in short supply.

The stats page is exactly what I'm looking for and it should be very easy for me to grab the required info from it. I seem to remember you and Ken mentioning the stats page on more that one occasion, if only I'd taken the time to look at it, oh well it was a good learning experience.

At some point I'll redo my script and post an updated show time permitting

best regards

MrX

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?