Display WordPress Page Views on a Raspberry Pi

Raspberry Pi WordPress page viewsEarly in the day, 316 page views. How informative!

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.

I found a WordPress API, worked out how to use it, and then pushed this data on to my very own ZeroSeg display board.

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.

phoxis.org WordPress API

A thanks to phoxis.org for the useful API information and examples

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:

https://stats.wordpress.com/csv.php?api_key=abcdef123456&blog_uri=http://yourblog.com&table=views&days=1

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…

Askimet API key

Handy – Askimet shows my WordPress API key

API Test

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)

https://stats.wordpress.com/csv.php?api_key=MYAPIKEY&blog_uri=http://www.averagemanvsraspberrypi.com&table=views&days=1

I then clicked the URL and it gave me an empty web page with just the following lines of text:

"date","views"
"2016-10-14",316

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&blog_uri=http://www.averagemanvsraspberrypi.com/&table=views&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)

ZeroSeg Display

I designed the ZeroSeg earlier this year, which I released in partnership with The Pi Hut, so I figured it was about time I made a useful project with it.

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).

ZeroSeg

The ZeroSeg is available at the Pi Hut for a cheeky tenner

Adding the ZeroSeg into the mix was very easy, thanks to the clever library originally made by Richard Hull. Some new functions he recently introduced make displaying static strings super easy.

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:

device.write_text(1,myline)

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=YOUR-API-KEY&blog_uri=YOUR-WEBSITE-URL&table=views&days=1'
    req = Request(someurl) # Request the URL

    try:
        response = urlopen(req)
        print "Trying URL..."
        
    except URLError as e: # This section is for error handling
    
        if hasattr(e, 'reason'):
            print 'We failed to reach a server.'
            print 'Reason: ', e.reason
            
            device.write_text(1,"FAIL 1")
            
            time.sleep(60) #Wait 1 minute before trying again
            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) #Wait 1 minute before trying again
            device.clear()
            
    else:
        print "URL open success!"
        
        apidata = response.readlines()
        
        print apidata
        
        for line in apidata: # For statement
        
            if '-' in line: # If the Pi finds this string in the line...
            
                print <span class="hiddenGrammarError" pre="">line
                
                line</span> = line[13:-1]

                print line
                
                pre = "DAY "
                
                newline = pre + line
                
                device.write_text(1,newline)
             
                time.sleep(180) #Wait 3 minutes
                device.clear()

Further Development

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!

Summary

If you’re reading this, then you’ve just made the number change on the display in my office. That’s pretty cool huh?!

Now, on days where I don’t access WordPress and I’m not writing anything, I can still get an idea of what’s going on via my little Pi Zero and ZeroSeg board.

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…

3 Comments on "Display WordPress Page Views on a Raspberry Pi"

  1. I got to have a go at this. Thanks for such a good article.

  2. Malcolm Otis | 31/10/2016 at 03:53 | Reply

    The ZeroSeg Display is a well documented board. Easy to install. I love it.

    QUESTION:
    Can multiple ZeroSeg Displays be addressed from one Raspberry Pi?
    E.g., “date(device, 0)” writes to the device so is it possible to write to a second device with “date(device, 1)” and how do I determine which device is “0” and which device is “1”
    Thanks!

  3. Hi there, i am trying to display data from MYSQL on to the Zeroseg LED, Can you please help me how to do this .

Leave a comment

Your email address will not be published.


*