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
No comments:
Post a Comment