Sunday, October 31, 2010

Sending Identi.ca Countdown Messages

If you following me on Identi.ca you'll occasionally see some of my daily count down messages. For example right now I'm running a daily count down message to the Charleston Bar Camp. Here is my message from Sunday October 31, 2010: "13 days until BarCamp Charleston Sat Nov 13 2010 #barcampchs". I am also running a reverse countdown since my Nexus One mobile phone was sent off to repair. Here is that message as of October 31, 2010: "5 days since I sent my !NexusOne to @htc for repair. What is taking so long? !Android http://is.gd/gj3Mn".

I send these automated microblog messages by adding cron job entries that call a python script. I pass the python script two parameters the reference date in yyyymmdd format and the message to be sent.

Below is the cron job entries in my system as of this writing:

cron

# m h  dom mon dow   command
1       0       *       *       *       python /home/htplainf/Ubuntu\ One/scripts/dentcountdown.py 20101113 "BarCamp Charleston Sat Nov 13 2010 #barcampchs"
1       0       *       *       *       python /home/htplainf/Ubuntu\ One/scripts/dentcountdown.py 20101026 'I sent my !NexusOne to @htc for repair. What is taking so long? !Android http://is.gd/gj3Mn'
Below is the python script I wrote to send these countdown messages.

Python script


#!/usr/bin/python
# Python script to send a countdown message to Identi.ca 
#
# Pass a date in yyyymmdd format along with some text for your message. 
# Example: python dentcountdown.py 20100611 "SouthEast LinuxFest"


import sys
import httplib, urllib
from datetime import datetime, date, time
import base64

username = "YourUserNameGoesHere"
password = "YourSecretPasswordGoesHere"
date_format = "%Y%m%d"
today = datetime.today()
targetDate = datetime.strptime(sys.argv[1], date_format)
delta = targetDate - today
days = delta.days + 1

if days == -1: 
 verb = " day since " 
elif days == 1: 
 verb = " day until " 
elif days < 0: 
 verb = " days since " 
else: verb = " days until "
if days == 0:
 message = sys.argv[2] + " is today"
else: message = str(abs(days)) + verb + sys.argv[2]
params = urllib.urlencode({'status': message})
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authorizationString = "Basic " + base64string
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain", "Authorization" : authorizationString}
connection = httplib.HTTPConnection("identi.ca")
connection.request("POST", "/api/statuses/update.xml", params, headers)
response = connection.getresponse()
print response.status, response.reason 
Post a Comment