Python scripts

“Python ® is a dynamic object-oriented programming language that can be used for many kinds of software development.”

Python and Nike+

It’s pretty simple to make a script to read your stats from Nike+. Use a browser like script (there is one provided above), do some query to Nike+ and retrive some XML, that you can parse using BeautifuSoup. I did this to integrate my Nike+ stats with Django. I’m planning to post what and how I did it here. If you want to see the scripts earlier, don’t hesitate to contact me via email.

Python Simple Web Browser

Ever wanted to make a script that logins to a ticket system and retrieve certain informations from there? Or browse sites that require you to subscribe? You can make this using python 2.4 very easily. Just make sure your requests are using cookielib.

PS. It looks like Textile is messing up with my code sample and doesn’t ignore the textile tags between the PRE and CODE html tags, so in human readable language, there is one class, with two methods, the usual init (with underscores) and open. Be careful to indent them accordingly.

Update: I added ”==” in front of each paragraph and now the code looks much nicer.

Example class:



class SimpleBrowser(dict):
    """ Python 2.4 simple browser. Can browse sites that require cookies too. """
    urlopen = None
    Request = None
    cj = None
    #me user agent
    headers = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
    #no POST data
    postdata = None

    def __init__(self):
        """ Setup browser, init cookielib """
        import urllib2, cookielib
        self.cj = cookielib.LWPCookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(opener)
        self.urlopen = urllib2.urlopen
        self.Request = urllib2.Request

    def open(self,url):
        """ Open this url, return text of page. """
        req = self.Request(url, self.postdata, self.headers)
        handle = self.urlopen(req)
        return handle.read().strip()


Example code:


$python >>> sb = SimpleBrowser() >>> login_page = sb.open('http://www.jira.com/?os_username=juser&os_password=jpass&os_cookie=true') >>> next_page = sb.open('http://www.jira.com/browse/') >>> #or with POST data >>> sb = SimpleBrowser() >>> sb.postdata = 'os_username=juser&os_password=jpass&os_cookie=true' >>> login_page = sb.open('http://www.jira.com/') # and so on ...

dir2m4b

In case you didn’t know, Apple’s iPod can play audiobooks. If you convert your mp3’s in their audiobook ‘format’ you can benefit from some nice features like bookmark and audiobook group (all your audiobooks are available on the audiobook menu, not on the usual music menu).

Audiobook ‘format’ actualy is Apple Lossless encoded (aac,m4a) file with the extension changed to m4b. To convert a mp3 to m4b in linux you can use mpg123 to convert it from mp3 to wav and faac to convertit from wav to m4a (m4b).

I made this script to automate this conversion. Inspiration was given by Darren Kirby’s dir2ogg. Actually I removed the parts I thought I won’t use in my script and replaced the ogg converter with the a free implementation of Apple’s Lossless one (faac).

To use this script you need python (duh!), pyid3lib, faac and mpg123. The script tests if mpg123 exist, but unfortunately it cannot check if faac exists. I couldn’t found a combination of switches for which faac will return 0 :( and I didn’t find out how to check if a shell returned a zero status.

usage:

dir2m4b -dr mp3director

Watermark using Python Image Library (PIL)

First, create a transparent PNG (using Gimp) that will be applied to the image, then fire up python:



$python
>>> from PIL import Image
>>> fi = Image.open("filter.png") #open the filter
>>> fi.show() # have I opened the correct image? :D
>>> im = Image.open("image.jpg") #open the image
>>> im.show() # have I opened the correct image? :) 
>>> im.paste(fi,(im.size[0]-fi.size[0],im.size[1]-fi.size[1]),fi) 
>>> # paste the logo on the desired image. 
>>> # this logo will be placed on the right-down corner.
>>> im.show() # voila :)
>>> im.save("image_watermark.jpg", quality=95)