Blog moved to webfaction

I just moved this blog to webfaction.

A Python Twitter bot

This is a Python script to check a mailbox for Twitter emails. New followers are followed. Direct messages from administrators (defined in a list) are twittered by the bot.

import twitter

import logging

from imaplib import *
from email.Parser import Parser
import datetime, time, email, email.Utils
import re

import settings

# logging
logging.basicConfig(filename=settings.LOG_FILENAME,level=logging.DEBUG,)
logger = logging.getLogger('artandcode_twitter')

logger.debug('starting')
try:
    # Connect to email server
    server = IMAP4(settings.MAIL_SERVER)
    server.login(settings.EMAIL_USERNAME, settings.EMAIL_PASSWORD)
    r = server.select("INBOX")

    # Find all mail
    r, data = server.search(None, "(ALL)")

    # If there are messages in inbox
    if len(data[0]) > 0:

        api = twitter.Api(username=settings.TWITTER_USERNAME, password=settings.TWITTER_PASSWORD)

        p = Parser()

        # Loop through emails in inbox
        for num in data[0].split():

            r, fetch_data = server.fetch(num,
            '(BODY[HEADER.FIELDS (DATE SUBJECT FROM X-TwitterEmailType X-TwitterSenderScreenName X-TwitterCreatedAt X-TwitterRecipientScreenName)])')
            msg = p.parsestr(fetch_data[0][1])
            who = msg.__getitem__('From')
            matchemail = re.compile(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}')
            email_addy = matchemail.findall(who)[0]

            twitter_sender_screen_name = msg.__getitem__('X-TwitterSenderScreenName')

            if msg.__getitem__('X-TwitterRecipientScreenName') == settings.TWITTER_USERNAME:
                # if the message is a following message
                if msg.__getitem__('X-TwitterEmailType') == 'is_following':

                    # follow the user
                    api.CreateFriendship(twitter_sender_screen_name)

                # If the email is a direct message sent from Twitter
                if msg.__getitem__('X-TwitterEmailType') == 'direct_message':
                    # if the sender is an administrator
                    if [twitter_sender_screen_name in settings.ADMINISTRATORS]:
        	               # When direct message sent, convert to epoch seconds
                        twitter_time = msg.__getitem__('X-TwitterCreatedAt').strip()
                        time_tuple = email.Utils.parsedate(twitter_time)
                        epoch_seconds = time.mktime(time_tuple)

                        # Get body of email sent by Twitter
                        r, body_data = server.fetch(num, '(RFC822.TEXT)')
                        body = body_data[0][1]
                        twitter_direct_message = body.split("\r\n\r\n")[0].strip()

                        api.PostUpdate(twitter_direct_message[:140-1])

        # copy the messages out of the INBOX
        server.copy(','.join(data[0].split(' ')), 'PROCESSED')

        # mark the messages as deleted
        # Loop through emails in inbox
        for num in data[0].split():
            server.store(num, '+FLAGS', '\\Deleted')

        # expunge the messages. Not sure if this is needed
        server.expunge()

    # delete the messages on close
    server.close()

    # Logout of email server
    server.logout()
except Exception, error:
    logger.exception(str(error))

logger.debug('finished')

Installing PIL on Mac OS X 10.5

These instructions will install PIL properly on Mac OS X 10.5. The instructions are adapted from here.

Go to here and download PIL.

Now we set up the environment so that you can install it.

In Terminal, create a symbolic link for the system’s Python.framework
in the location expected by the pythonmac.org tools, like so:

cd /Library/Frameworks
sudo ln -s /System/Library/Frameworks/Python.framework/ Python.framework

Now run the installer. It should not complain and it should install successfully.

The final step installs PIL in your Python site packages.

cd /Library/Python/2.5/site-packages/
echo "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages
" > PIL.pth

Test your new PIL install with:

python -c "from PIL import Image"

if you do not get an error, then you have a correct PIL install!

A little update on Picasa Tab for Facebook

Looking back at my infrequent micro-blogging on this here site, I see a message about Picasa Tab for Facebook having 500 users. Well, now it has 23,000. w00t!

And one of these weekends, the new state of the art Google App Engine / pyFacebook / Django version will be switched on (replacing the crufty but reliable PHP version).

Mod_python is dead, long live mod_wsgi

I recently deployed a Django 1.0.2 site using mod_python. Turns out this was a mistake. I should have used mod_wsgi. Mod_python is no longer under development. See Django ticket about recommending mod_wsgi.

[Corrected: wrong link]

Mod Python Mailing List Archives

The link on the website is broken, the correct link is: mod python mailing list archives

Unique molecules




Unique molecules

Originally uploaded by chazmatazz

A teaser for my ‘corporate rebranding’

Picasa Tab for Facebook reaches 500 users

Just wanted to say that I passed the 500 user mark this morning. Not bad for two weeks. You too can install Picasa Tab for Facebook here.

Pollution Sensing Tank Top

I’m getting started on the pollution sensing tank top project. I’ve ordered the parts, picked out a tank top, and am ready to get electronic-ing.

Picasa Tab for Facebook launched!

I needed a way to display my Picasa photos on the new Facebook, so I created a Facebook app called Picasa Tab. To try it, go to http://apps.facebook.com/fotofan and install! Please provide feedback on its Facebook About page.