Update uWSGI to the latest version on Ubuntu 12.04 LTS Precise Pangolin

You sure know this problem: You run a stable Ubuntu version and the package you want is slightly older than the one advertised on the project’s site. So what, you install it via apt-get and that’s it. Well sometimes, that might not be the best idea.

The Ubuntu uWSGI package is on version 1.0.3. So when I had some tough problems last week I thought it was best to update from the Ubuntu version to the latest version 1.9.14. Here’s how:

Thankfully, uWSGI can be installed via PIP:

sudo pip install uwsgi

It installs in /usr/local/bin/uwsgi, but the version you probably have uses /usr/bin/uwsgi. No problem, just redo the symlink to the newer uwsig:

sudo ln -fs /usr/local/bin/uwsgi /usr/bin/uwsgi

One more thing: Because the PIP version of uWSGI has python already included, remove any plugins=python lines from your uWSGI config files.

That should be it. Stop all uwsgi processes (check ps aux to see they are really gone) and start again with service uwsgi start. Your system now uses the new uWSGI version while having the uWSGI setup that came with the Ubuntu package.

Query Google Analytics API with a service account in Python

I recently needed to display a visitor counter on a website by some client’s weird demand. Maybe the 90s are coming back. Anyway, I thought it makes sense to query Google Analytics directly, but Google’s API example were not really helping. After some digging around, I finally found a solution. You’ll need oauth2client and google-api-python-client for it to work. Then follow these instructions on Google Developers to create a service account that only has access to the API without the need for a human to request some oauth token. It will give you a private key to work with.

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
import httplib2

def get_analytics_visitors():
    f = file('privatekey.p12', 'rb')
    key = f.read()
    f.close()
    credentials = SignedJwtAssertionCredentials('[email protected]',
                                                key,
                                                scope='https://www.googleapis.com/auth/analytics.readonly')
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build('analytics', 'v3', http=http)
    data_query = service.data().ga().get(**{
        'ids': 'ga:YOUR_PROFILE_ID_NOT_UA',
        'metrics': 'ga:visitors',
        'start_date': '2013-01-01',
        'end_date': '2015-01-01'
        })
    feed = data_query.execute()
    return feed['rows'][0][0]

Here’s a Quick Way to make code look nicer in Firefox

I use Droid Sans Mono as my font for coding and in Putty and nearly everywhere. You can read and download it over at damieng.com.

If you want code and fixed-width text to appear nice in Firefox, what’s a better idea than using Droid Sans Mono there, too?

Go to Options > Content. Under Fonts & Colors click Advanced and choose Droid Sans Mono as monospaced font. It’s as easy as that. It will now affect every fixed-width text that is not formatted by CSS or something else. It will even change fonts in Firebug. I like it, it looks beautiful!