python
book plug
Here’s a little plug for our book from our Colleague Mark Guzdial at Georgia Tech. This was written last September right after the book was published but I didn’t find out about it until this morning. Thanks Mark!
In addition, David and I have started a new blog to post corrections and updates to Python Programming in Context Over here If you are already using the book please check it out.
python + growl + remember the milk = launchbar task management
Intro
Once I could add tasks to my RTM account through LaunchBar I wanted a way to quickly pull up a view of what tasks were due today through LaunchBar. The Growl library provides a nice way of doing this.
The overview is as follows: Activate LaunchBar and type due. This due is installed as a search shortcut and you can search for today, tomorrow, or all (abbreviations are also easy). The search shortcut runs the python script that searches your tasks on Remember the Milk. For each task it finds it puts up a sticky Growl notification so you get a nice list of tasks on your screen. You could easily customize the script to put all the tasks in a single notification but I like them separate.
Since the Growl module registers this script as an application you can also use the Growl Preference Pane to customize the look and feel or even the location of your notifications. You can also customize whether you want the notifications to be sticky or not.
The Code
Here is the code for doing all of this. It makes use of the filter parameter on getList.
#!/usr/bin/env python
from rtm import *
import sys
import Growl
def sendNotify(ts):
if type(ts.task) == list:
for j in range(len(ts.task)):
notifier.notify("today","Task Due: "+ts.task[j].due[:10],ts.name,sticky=True)
else:
notifier.notify("today","Task Due: "+ts.task.due[:10],ts.name,sticky=True)
if len(sys.argv) == 2:
command = sys.argv[1]
else:
command = "today"
apiKey = "get your own"
secret = "this too"
token = "You will create this"
name = "RTMDue"
notifications = ["today","tomorrow"]
notifier = Growl.GrowlNotifier(name,notifications)
notifier.register()
if command[:3] == "tod" or command == '':
cutoff = 'today'
elif command[:3] == "tom":
cutoff = 'tomorrow'
else:
cutoff = None
rtm = createRTM(apiKey, secret, token)
if cutoff:
filterString = 'status:incomplete and (due:%s or dueBefore:%s)'%(cutoff,cutoff)
else:
filterString = 'status:incomplete'
theTasks = rtm.tasks.getList(filter=filterString)
if type(theTasks.tasks.list) == list:
for i in range(len(theTasks.tasks.list)):
if type(theTasks.tasks.list[i].taskseries) == list:
for j in range(len(theTasks.tasks.list[i].taskseries)):
ts = theTasks.tasks.list[i].taskseries[j]
sendNotify(ts)
else:
ts = theTasks.tasks.list[i].taskseries
sendNotify(ts)
else:
if type(theTasks.tasks.list.taskseries) == list:
for i in range(len(theTasks.tasks.list.taskseries)):
ts = theTasks.tasks.list.taskseries[i]
sendNotify(ts)
else:
ts = theTasks.tasks.list.taskseries
sendNotify(ts)
Next Steps
It would be great if I can figure out a way to have the Growl notification box call a script to mark the task as done. Feel free to leave comments or suggestions or improvements in the comments.
creating a group twitter repository
For the trip to Silicon Valley I wanted to have everyone be able to twitter about the experience using their own twitter account, but I also want to have a central place for everyone following the trip as a whole to see all of our tweets. How to do that? Python and the Twitter API to the rescue. You can see the results of this by checking out @lutherlive on twitter
import twitter
USER = “Your group account”
PASS = “your group password”
TAG = “tag contained in each message”
api = twitter.Api(username=USER,password=PASS)
# figure out when my last post was
statuses = api.GetUserTimeline(user=USER)
last_post = statuses[0].GetCreatedAt()
# Get the timelines for all friends since my last_post
tl = api.GetFriendsTimeline(user=USER,since=last_post)
for post in tl:
# since my posts may show up in friends timelines avoid reposting loop
if post.user.screen_name != USER and TAG in post.text:
api.PostUpdate(post.user.screen_name + “: " + post.text)
This little script logs in using the twitter account you create for the group. Importantly, this account must follow all of the group members that you want to be able to re-tweet. When one of the group members wants a tweet to show up on the group account they must use the TAG, in our case @lutherlive, somewhere in their post.
After logging in, the script grabs the timeline for all of the members of the group. This timeline is restricted to the posts since the last a post was made by the group user. This is important to do otherwise you would end up with duplicate re-tweets every time the script is run.
Next the script simply loops over all of the posts and checks for the tag. If the tag is in the post the post is updated along with the screen name of the original group member that made the post.
I put the script into a cron-job on the computer science server at Luther to run every 15 minutes, so while the @lutherlive user isn’t a real time reflection of its group members tweets, its pretty darn close.