Here are two very obvious things: I have a blog (you’re reading this aren’t you?) and people view it (you again!).
I don’t think there are many blog owners out there who aren’t interested in how many people are viewing their content, after all, you went to all that trouble to write it.
I like to keep an eye on this blog out of pure interest, especially on those days where it might jump a few thousand views and you have to rummage around looking at what might have gone viral.
So, I decided I’d try and find a way to use my Raspberry Pi to display my daily page views, using something a little less barbaric than my previous screen-scraping techniques that you might have seen here before.
Let me show you how…
The WordPress API
I wanted to use a ‘proper’ API this time as hitting & scraping the same webpage over and over is a little bit ‘average‘ for this ‘average man‘. I’ve heard that you can get your IP blocked and all sorts of nasty business.
A quick Google for ‘WordPress API’ brought up this page which gave some examples. I still had no idea how to use an API but dived in head-first hoping for the best.
It turns out an API isn’t something you install, doesn’t come in a box and certainly isn’t available from Tesco. It just looks like any other URL link, but contains stuff that makes the magic happen. I’m not sure if all APIs are the same, but this will do for my initial learning.
As an example, this is the WordPress API from the site I found:
Breaking it down, it contains the following sections:
- https://stats.wordpress.com/csv.php? – the place you’re getting the data from (and format – CSV)
- api_key=abcdef123456 – the API key for your WordPress site
- &blog_uri=http://yourblog.com – your blog’s URL
- &table=views&days=1 – the data you want to fetch, in this case, page views over the last day
So I assumed all I needed to do was change the API key and my blog URL, but where can I get my WordPress API key?
Askimet API key
I remembered that I entered an API key into my Askimet plugin when I installed it on this blog. Askimet is a spam comment blocking plugin for WordPress, which I assume most bloggers on the platform will be using.
I looked in the plugin settings and there it was, an API key! Was it the right one? Was it just a separate thing for Askimet? Only one way to find out…
I chucked the API key into the URL and then added my blog address. My API URL was now looking like this: (I haven’t shared my real API key of course, too many nasty people on the internet)
I then clicked the URL and it gave me an empty web page with just the following lines of text:
Checking against my WordPress stats, it was correct! At that point in the day I had 316 page views. Smashing!
Reading the API with Python
My API was working, so the next task was to pull that data into a Python script so that I can use it in a project.
I’m using urllib2 again, which I used in my social network monitor and high tide tracker projects. This library can read the raw source of a web page and return it as lines, which is perfect for this API as it returns a super simple page with just 2 lines of data.
The script I use on the ZeroSeg has error handling and some other funky stuff, but I thought it would be good to first show you this script in its simplest form, just printing the view count in a terminal window:
import time from urllib2 import Request, urlopen, URLError while True: # Give the script the API URL to check out someurl= 'https://stats.wordpress.com/csv.php?api_key=MYAPIKEY&amp;amp;blog_uri=http://www.averagemanvsraspberrypi.com/&amp;amp;table=views&amp;amp;days=1' # Request the URL req = Request(someurl) # Open the URl response = urlopen(req) # read the lines from the URL apidata = response.readlines() for line in apidata: # If we find a '-' in a line, look at this line only if '-' in line: # Chop 13 from the front and 1 from the rear line = line[13:-1] print line # Wait 3 minutes time.sleep(180)
I wanted to display the view count on my ZeroSeg, and then have that on display in my office at all times. It’ll let me know if views are low (something might be broken) or high (maybe someone shared a post and it’s popular).
All I had to was add the import:
import ZeroSeg.led as led
Add a line to set up the display devices:
device = led.sevensegment(cascaded=2)
Then instead of printing the data, I tell the ZeroSeg to display it:
The final code for my project, including exception handling for the URL, display text and some terminal printing, looks like this:
import ZeroSeg.led as led import time from urllib2 import Request, urlopen, URLError device = led.sevensegment(cascaded=2) print "START" while True: print "-------- REQUESTING DATA FROM API --------" someurl= 'https://stats.wordpress.com/csv.php?api_key=MYAPIKEY&amp;blog_uri=http://www.averagemanvsraspberrypi.com/&amp;table=views&amp;days=1' req = Request(someurl) try: response = urlopen(req) print "Trying URL..." except URLError as e: if hasattr(e, 'reason'): print 'We failed to reach a server.' print 'Reason: ', e.reason device.write_text(1,"FAIL 1") time.sleep(60) device.clear() elif hasattr(e, 'code'): print 'The server couldn\'t fulfill the request.' print 'Error code: ', e.code device.write_text(1,"FAIL 2") time.sleep(60) device.clear() else: print "URL open success!" apidata = response.readlines() print apidata for line in apidata: if '-' in line: print line line = line[13:-1] print line pre = "DAY " myline = pre + line device.write_text(1,myline) time.sleep(180) device.clear()
There are a whole bunch of other WordPress API options you can use – it’s not just restricted to page views.
Checking the original source of my WordPress API info, it seems there are options to pull back things like the last 500 days of page views, group views by week and other useful stuff.
There are also more ways to display this data on the ZeroSeg. I’m going to tinker with the on-board buttons to allow me to select which view I want to display – by day, week, total, the time, date…anything I want!
If you’re reading this, then you’ve just made the number change on the display in my office. That’s pretty cool huh?!
I guess now’s as good a time as any to start exploring other APIs. I thought they’d be hard to use but it turns out it’s easier than a lot of the other things I’ve done with the Pi.
Have a great weekend everyone, until next time…