Panic's new StatusBoard iPad app was all the rage last week. It's a fun application that I'm sure many people who actually have statuses they need to check will appreciate. I'm not one of those people, but it's Panic so I bought the app anyway.
The thing that has the nerds all excited is the fact that you can create your own custom widgety things. One of the more common uses people are coming up with is to track analytics for their website. There are already solutions for Google Analytics and Gauges, but I use Clicky and nobody has gotten around to doing anything with that. Also, nothing I saw was using Python to create the data and that needed to be rectified.
Clicky offers a fairly extensive api so I decided to fiddle around with something that would show, for a 7 day period, the number of visitors per day along with the number of actions they've taken. The script just gets some JSON1, parses it2, and spits out a csv file into my Dropbox folder suitable for StatusBoard viewing. Make it executable3, hook it up to launchd via Lingon or LaunchControl and you're golden.
#!/usr/local/bin/python3 import json import csv from urllib import request SITE_ID = "aSiteId" SITEKEY = "aSiteKey" clicky = "http://api.clicky.com/api/stats/4?site_id=%s&sitekey=%s&type=actions,visitors&date=last-7-days&daily=1&output=json" % (SITE_ID, SITEKEY) def getJSON(url): req = request.urlopen(url) encoding = req.headers.get_content_charset() obj = json.loads(req.read().decode(encoding)) j = getJSON(clicky) visitors = j['dates'] actions = j['dates'] header = ['|>', 'Visitors', 'Actions'] data = [[visitors[i]['date'], visitors[i]['items']['value'], actions[i]['items']['value'] for i in range(7)] data.reverse() with open('/path/to/Dropbox/stats.csv', 'w', newline='') as csvfile: my_writer = csv.writer(csvfile) my_writer.writerow(header) my_writer.writerows(data)
There are a bunch of other things you could do with this code. Off the top of my head, you could make a pretty table comparing the top five countries your visitors are coming from or what operating system they're using. And of course you're not limited to Clicky. You could plug in any JSON source and fiddle with the parsing to get what you wanted.