Python Google AdSense for Mobile code

I was amazed to discover that Google don’t currently support Python as a language for their mobile ads. AdMob doesn’t either (thinking it’s more likely that people might use perl or VBScript for a contemporary web site).

But it’s a particularly strange omission for Google, since it’s one of their three ‘official’ internal languages (together with Java and C++), and because their Google App Engine platform mandates it. Do they assume that no-one would create a mobile application in their cloud?

I don’t know – it’s probably just left-hand and right-hand out of sync.

Anyway, The Hollywood Walk of Fame mobile site has been a recent hobby for me, and I chose Django on Python for a bit of educational fun.

It was going well until I decided to throw some ads onto it.

So of course I needed to port the ad code. It’s very simple, although cuts a few corners (this does not support AdSense’s custom colour feature for example):

def google_ad(request, publisher_id, format='mobile_single'):

scheme = 'https://' if request.is_secure() else 'http://'

params = {

'ad_type':'text_image',

'channel':'',

'client':'ca-mb-' + publisher_id,

'dt':repr(floor(1000*time())),

'format':format,

'https':'on' if request.is_secure() else '',

'host':scheme + request.META.get('HTTP_HOST', ''),

'ip':request.META.get('REMOTE_ADDR', ''),

'markup':'xhtml',

'oe':'utf8',

'output':'xhtml',

'ref':request.META.get('HTTP_REFERER', ''),

'url':scheme + request.META.get('HTTP_HOST', '') + \

request.META.get('PATH_INFO', ''),

'useragent':request.META.get('HTTP_USER_AGENT', '')

}

screen_res = request.META.get('HTTP_UA_PIXELS', '')

delimiter = 'x'

if screen_res == '':

screen_res = request.META.get('HTTP_X_UP_DEVCAP_SCREENPIXELS', '')

delimiter = ','

res_array = screen_res.split(delimiter)

if len(res_array) == 2:

params['u_w'] = res_array[0]

params['u_h'] = res_array[1]

dcmguid = request.META.get('HTTP_X_DCMGUID', '')

if dcmguid != '':

params['dcmguid'] = dcmguid

url = 'http://pagead2.googlesyndication.com/pagead/ads?' + urlencode(params)

return urlopen(url).read()

 

I presume I’m allowed to do this, I know it’s only 15 minutes saved – but if it helps someone, great. No warranties – but it works for me!

Incidentally, in Django, I am placing this in a context_processor so that it gets put into a template variable that I know will turn up in every view when the site is in mobile ‘mode’. I use a switcher like this to toggle between http://hwof.com and http://hwof.mobi.

Hm. This post looks terrible in this theme. I think I need to change WordPress template soon.

3 comments on this post.
  1. sagey:

    Hello James,

    I’d appreciate your help implementing this script on my mobile website. its running on appengine using the webapp framework.

    i’m a complete python beginner so i’m unsure how to implement your code on a non django site.

    i’d also be interested to know if its possible to sort the custom colour functionality?
    i’d really like my ads to display inline with the design of my site.

    Any help would be appreciated.

    thanks in advance

  2. James:

    Hi,

    This is a straightforward function that returns HTML for direct insertion into your code. Just made sure it’s somewhere in you AppEngine app so that your page logic can see it.

    In its crudest form, you could just do

    self.response.out.write(google_ad(…))

    from within your RequestHandler. But if you’re using templates or something, I suppose there are far more elegant ways.

    The colours are handled slightly strangely in the original ad sense code, but I think if you just add the following parameters into the list at the top of the function (with #RRGGBB values), it should work:

    ‘color_border’
    ‘color_bg’
    ‘color_link’
    ‘color_text’
    ‘color_url’

    (Haven’t tried it though)

  3. John Boxall:

    Hey James,

    I hacked out an updated AdSense snippet from the latest code available from Google:
    http://github.com/johnboxall/django-mobileadsense

    urlopen can be made a little safer by wrapping it in a timeout!
    http://github.com/johnboxall/django-mobileadsense/blob/master/adsense.py#L71

    Cheers,

    John

Leave a comment