Episode: 2340
Title: HPR2340: Tracking the HPR queue in Python
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2340/hpr2340.mp3
Transcribed: 2025-10-19 01:29:22

---

This is HPR episode 2,340 entitled, Tracking the HP RQ in Python.
It is hosted by MrX and is about 22 minutes long and carrying a clean flag.
The summary is, I explain how I capture the number of HPR shown in the QU in Python.
This episode of HPR is brought to you by an honesthost.com.
Get 15% discount on all shared hosting with the offer code HPR15, that's HPR15.
Better web hosting that's honest and fair at An Honesthost.com.
Hello and welcome Hacker Public Radio audience.
My name is MrX and welcome to this podcast.
I hope you enjoy it.
I'll let you start by thanking the people at HPR for making this service available.
And HPR is a community led podcast created by the community for the community.
If you all created one show, just one show a year, we'd have more shows and we'd know what to do with.
I'm sure you must have something interesting we'd love to hear.
Anyway, on with the show.
Okay, so when I was doing the previous show was about the Bo thing UV5R handheld transceiver.
It took a bit of time to pull the series together.
And of course I was looking at the Q, the HPRQ and noticing that it was getting quite small.
And the back of my mind I thought, oh no, what if I put all this effort into producing these set of shows and then HPR folds.
And obviously all it's been a big waste of time.
So I thought, hmm, and to keep an eye on it on the Q and make sure it's not going to go too low.
And towards the end of the, as I got near the end of creating the set of the series on the Bo thing and depth series on the Bo thing.
I thought it would quite handy if I could keep track of the number of shows in the HPRQ without having to open a tablet or a PC or something.
So I thought, what if I could do something and Python?
So I've got a project which this, this conbubulation project of things, it's like a monster that's grown in all different directions.
It's not been properly planned.
I started that many years ago and I hope the time now to spend on it.
But I can attack it onto that.
And I do plan eventually some point in HPRQ last long enough to do a show on this project and I'll not elaborate any further than that.
On this, this thing that I've built, I have a Blink Stick attached.
A Blink Stick is a read device which you may well know about.
I think they come in various forum factors.
It's a USB thing, the one I've got plugs into the side of your Raspberry Pi, for example.
And you can control it with software.
It's with an RGB LED and you can make it any red, green, blue or mixing the colors you can make it any color, basically.
And it's very useful for signaling.
I use it for a few things on this mystical project which I just say I'm not going to go into.
Anyway, I managed to hack up this bit of Python code and I'm not a programmer.
I'll start by saying that.
And it's probably a very bad code and hacked together.
But it seems to work. It seems reliable enough.
And no other compilation of code that seems to work and seems to be reasonably reliable.
And that's really all I'm looking for.
So I thought I'd be going to go through the code without too much detail because I don't want to bore you all.
So I've got the, I've actually got a file here called Scratchpad.
And I find that I'm finding that quite useful.
So I can maybe put a few functions in it and play with it in Python.
And then without upsetting this monster of bit of code that I've got, it's not monster bit.
For me it is. It's a thousand lines of code, something like that.
But I can play with it in isolation sort of thing.
And I've now implemented this bit of code into the monster project and it works.
Okay, I'll start describing this messy bit of code that seems to work.
I called the function, I've got a, I'm sorry, I'll start by again.
I've got a file called Scratchpad and I just work with it as a code and see how it works.
And then transfer it into the main project once I'm happy with it.
So the function, I called it getHPRQ.
So DevSpace, get underscoreHPR underscoreQ.
Open brackets, close brackets and finish that with a, what did you call it?
Double vertical dots, what did you call it?
I can't remember, it's a colon, brain feed.
Right, okay.
So the first thing, the first problem I had was, how do I getHPRQ, HTTP content?
You know, I'll web page, how do I capture that and Python?
I literally be told using the excellent site, found it on, using a Google search,
I found it on a stack exchange, fantastic site for looking for some pieces like this.
I'm not, as I said, I'm not a programmer and definitely I'm not a professional in Python.
I've probably got more experience of bash scripting, though I don't proclaim to be a super guru like our Dave Morris,
but nonetheless, this seems to do the job.
So the command you need to capture a web page content is you are Lib, you are L-L-I-B.
And in order to use that, you need to load that module.
And of course, you load a module in Python by using the command import space, you are Lib.request.
So that's you've got the module loaded.
And then the command I used was urlib.request.url open, open brackets, then they'll actually address,
which is the calendar, the HPR calendar page, close brackets.read, open brackets, close brackets.
And then, so that gives you the HTML content from the page.
And I passed that to a variable using a pass that by to the variable HTML underscore content.
So HTML content equals in in that command.
And that's all good and well, but I think that's stored as a list.
I think you call it as opposed to a string.
So to make it easier to deal with, I had to convert it into a string.
So I used the STR function Python.
So I used the STR open bracket, HTML underscore content, close brackets.
And that converts the HTML content, the information in that HTML content variable to a string.
And I passed that to a variable HTML page.
HTML page variable contains the string, the whole page as a string.
So the next thing I'm going to do is find a bit in the page where the queue is stored.
So I used the find command for that.
And I don't think I need a module for that.
So it's a standard as part of Python.
So to find the string I was looking for is there are only angle bracket, strong angle bracket.
That's enough to find the chunk in the page where the queue is held.
So HTML underscore page, which remember contains the string of the web page dot find open brackets.
Quote, there are only angle bracket, strong angle bracket, quote, and enclosed with a, I'm, I'm bracket.
And that, that, that finds the location in the string of the page.
And it returns a number.
It could be hundreds of characters along from the start of the string.
It counts, you know, it counts from the first character on the first line.
And it just counts each, each character basically in the, it's one big, you know, within the whole big string.
So it could be hundreds of characters along before it finds that, that, that string.
And that, and I passed that to a variable called line begin.
That's it found the position where there are only bloody, bloody, bloody, okay.
And then a, a move an arbitrary number of characters from that beginning at that point.
And I thought 70 was long enough.
So I used line begin, line underscore begin plus 70.
So that takes the number that came back with add 70 to it.
And I passed that to a variable called line end.
So I've now got the line begin and the line end numbers character number.
So that's fine.
So then I need to, to grab that chunk of text.
I used html underscore page, which you remember contains a whole page.
Square bracket, line underscore begin, colon, line underscore end, square bracket.
Now that's the way that you, you slice things.
I believe that's the terminology used in Python.
And so that grabs the beginning point and the end point.
And I passed that to a variable called line.
So that's kind of contains the line that we're looking for.
Now from that line, I need to find the, the, the digit, you know, the actual number.
And it could be a one digit number, it could be a two digit number, you know,
you don't know how many keys, how many shows are going to be an accuse, you need to find that digit.
I'm going to use stack exchange to find out the command for that.
And, and that turned out to be re dot find all open brackets are,
um, quote, backslash d plus, um, quote, comma, line.
So that, I'm going to close up with a, a bracket, obviously.
And I pass that to the variable digit.
So that's using regular expressions.
Um, this re dot find all, uh, and assume the slash d plus is finding digits.
Uh, I don't fully, I'm not, as I say, I'm just, um, I'm not an expert in Python.
So this is a, I'll connect this bits a bit new to, I've never used regular expressions in Python before.
And of course, in order to use regular expressions in Python, you need to load the module for that for quite to mention that.
So import space re does that allows you to use that to, that to command.
So we now have the digit.
Now, the trouble is that, I think again, the digit is stored, um, the digit we've captured is stored as a list.
Um, so you need to turn that into an integer, uh, so that you can work with it.
So again, uh, use the command int int open bracket, digit, square bracket, zero, close, square bracket,
and then close the, the normal bracket.
And what that does is that, that catches, captures the first element of the list, digit.
And, uh, they're all only be, um, should only be one item in that list, because it should just find the one digit and that's it.
Um, so that's passed to the variable HPR shows.
So the HPR shows now contains, hopefully, the number of shows in the queue.
And, um, I then wanted to control my blank stick and I just use standard if statements and Python.
Uh, and I've got some, some made up modules for dealing with the blank stick simple ones.
I've got a, um, B stick underscore on and then you give it a, a color in it and it turns a stick the stick on.
And, um, whatever I've got, I've got to, uh, I've also got B stick underscore blink underscore red.
And that turns the blank stick on and blinks at red.
So these are the functions that I've used for that.
And I won't go into the detail.
You can look at how to use a, a blank stick if you like yourself.
Um, but, uh, so the, the, the if statements I've got is, if HPR shows is greater than nine,
then turn the blank stick on green.
So in other words, that means if it's 10 or higher than it, then it shows green to us.
Let me just choke you.
Find else if HPR shows is greater than five.
Blink stick on blue.
Um, so in other words, if it's, uh, six or, or, or between six and nine, then it turned on blue.
Now, I thought about using red green amber, but amber uses, uh, I think it's yellow and green mixes colors.
And if you've got the LEDs and a load intensity, don't, doesn't it look off a good, so I used blue instead.
And then finally else if, well, no, not, not finally.
There's another one else.
If HPR shows is, it's greater than minus one.
Just in other words, if it's zero, uh, and between zero and, um, five, then be stick.
Turn the blank stick on red.
Um, and if it's not that, if it's none of those, if it's not, not within any of these, these ranges, then else.
Blink stick, blank red.
So a blank thread.
So what happens here is if the shows are, it's greater than 10, then green.
Blink stick comes on green.
It was greater than, between five and, and, um, nine, then blue.
And if it's between, uh, it was between zero and, um, if it's, let me think if it's, if it's, uh, between zero and five, then red.
And if it's below, um, else, if it's something else and it's just flash red, I hope that makes sense.
Right.
And then, um, it displays that for four seconds where the sleep command, um, when, of course, to use sleep, you need to import, um, from time import sleep.
And so that you need that and, um, steep open bracket for plus bracket.
And so that, that pauses it for four seconds.
And finally, blink stick off.
And that turns the blink stick off.
So that's how I approached it.
But then, of course, I realized that, well, what happens if the URL isn't available, you know, if HPR is down or something like that?
Well, it would just crash out and now it's not any good.
So, um, I played around with, um, um, um, raising an exception.
So I haven't really, this is the first time I've really done this because I'm, I'm lazy at a Python.
It's a quick way of getting things done sort of thing out of the time these days to, to be particularly choosing about writing good code.
So it's all slapped actually.
So, um, I, again, I'm not sure how these things really work.
I believe you've got, you raise exceptions on your support.
There's, there's a whole host of exceptions that you can raise.
Um, but I just chose the top level one.
So it catches all sort of thing.
I don't think I should do, but, um, it worked anyway.
So that, that first line that I told you about that catches the HTML content.
What I did was that I, I used the command.
Try colon, then indent HTML content equals URL lib.request. URL open, um, URL.read.
Know that, that command I gave you before.
So you indent that with a, a, a, a try above it.
So it tries that.
And if it fails, then below that, you put the command except a colon.
And so what happens is if that fails and it tries to seize the commands that are undentied from within the accept command.
So in other words, if it fails, then print, I've got to say print error problem accessing URL.
And it's so prints out the URL that it fails.
Um, and the other problem you've got is, uh, okay, what are the web pages there?
But the, uh, you can't find the digit.
And then if it tries to pass, pass the, the, the digit on, uh, turn it into an integer and pass it to HPR shows.
Then of course, it would all bomb out because it wouldn't be there.
So again, you use a try command and then indent hpr shows equals int open bracket, digit square bracket, zero square bracket.
Close, uh, bracket.
And what, if that fails passing the value across to HPR shows, then you use the command except and colon.
And, um, I print error problem getting number of HPR shows in queue.
So that, that solves that.
And then I set the value HPR shows to the value of minus one.
And that would then just blink the red LED further down.
And that, that seems to work.
Um, and so, uh, when I turn on this, this project of mine, which I'll describe, another date.
Um, the light, the blinks that comes on and at a glance, I can tell that it is a show queue, OK, medium or, we're getting really low.
And as I noticed today, it's, um, it flashed, it went red, so it's below, uh, five.
So, um, we need to be, we only need to be putting out a few shows I reckon really.
Anyway, I hope you all find that interesting.
And, um, I'll speak to you later.
Uh, I think that's all I have to say for this show.
Thanks very much for listening.
OK, so if you want to contact me, I can be contacted at MrX at HPR at googlemail.com.
That's MRX, AT, HPR, the at symbol, googlemail.com.
So until next time, thank you, and goodbye.
You've been listening to Hecopublic Radio at HecopublicRadio.org.
We are a community podcast network that releases shows every weekday Monday through Friday.
Today's show, like all our shows, was contributed by an HPR listener like yourself.
If you ever thought of recording a podcast, then click on our contributing to find out
how easy it really is.
Hecopublic Radio was founded by the Digital Dove Pound and the Infonomicon Computer Club,
and is part of the binary revolution at binrev.com.
If you have comments on today's show, please email the host directly, leave a comment on
the website or record a follow-up episode yourself.
Unless otherwise status, today's show is released under Creative Commons, Attribution,
ShareLite, 3.0 license.
