Pages

Subscribe:

Labels

2010-09-20

Google Tasks API case study

This post is an update of http://privacylog.blogspot.com/2010/07/api-for-google-tasks.html

Update 2011-05-12 the official API is now at http://code.google.com/apis/tasks/index.html thanks to Josh for the ping.

There is no official API for Google Tasks at this time. But, below is full read-only access to all your tasks across all your lists. I am including a simple app that sends this to you via email (This mail WILL go to your spam folder). I have this running at home to automatically print out a copy of all task lists periodically.
#!/bin/bash

cd /tmp
rm -f message; touch message

curl https://www.google.com/accounts/ClientLogin \
-d Email=YOURUSENAME@gmail.com \
-d Passwd=YOURPASSWORD \
-d source=privacylog \
-d service=goanna_mobile > token

AUTH=$(sed -n '/Au/s/A/a/p' token)
HEADER="Authorization: GoogleLogin $AUTH"
URL="https://mail.google.com/tasks/m"
curl --header "$HEADER" "$URL" > main

for list in $(grep -o '"[0-9:]\{20,25\}:0"' main | tr -d '"' | sort -u)
do 
curl --header "$HEADER" "$URL?listid=$list" > list
echo >> message
title=$(sed -n 's|.*selected="selected">\([^<]\+\).*|\1|gp' list | head -n1)
echo "### $title ###" >> message
sed -n 's|\([^<]\{1,\}\).*|[ ]  \1|p' list >> message
# This one below indents sub tasks but only works on Linux
# sed -n -e 's/.* .*//p' -e 's|\([^<]\+\).*|\1|p' list | sed -n -e '/./!{N;s/\n/  /}' -e '/./p' | sed 's/^/[ ]  /' >> message
done

mail -s "Weekly review" YOURUSERNAME@gmail.com < message

rm token main list message
Notes for Mac: use * instead of \+,

4 comments:

Full Decent said...

So now when you wake up, the coffee is already brewed and the task list is automatically printed. Put a wireless printer in the kitchen. No, I'm serious.

Full Decent said...

Use these lines instead because they are cross platform (sed parses escape sequences different on Mac and Linux)

title=$(perl -ne 'die "$1\n" for m/"selected">([^<]+).*/' list 2>&1)
perl -ne 'print "[ ] $1\n" for m/"text">([^<]+)/' list >> message

fab31 said...

A quick python rewrite:

http://pastebin.ca/1994952

Full Decent said...

Great job fab31! here is fulltext for posterity:
A quick python rewrite, can be a start to clean up & improve...


#!/usr/bin/env python
YOURUSENAME = raw_input('login:')
YOURPASSWORD = raw_input('password:')

import re
from urllib import urlencode
import urllib2 as urllib

login = urllib.urlopen('https://www.google.com/accounts/ClientLogin', data=urlencode({
'Email' : YOURUSENAME+'@gmail.com' ,
'Passwd' : YOURPASSWORD ,
'source' : 'privacylog' ,
'service': 'goanna_mobile' ,
}))

token = login.read()
AUTH = (line.split('=', 1)[1] for line in token.split('\n') if line.startswith('Auth=')).next()
HEADER = {'Authorization': 'GoogleLogin auth='+AUTH}
URL = "https://mail.google.com/tasks/m"
main = urllib.urlopen(urllib.Request(URL, headers=HEADER))

data = main.read()
file('/tmp/out.html', 'w').write(data)

r = re.compile('.*"([0-9:]{20,25}:0)"')
r2 = re.compile('selected="selected">([^<]+).*')
#r3 = re.compile('<td +class="text" *>(.*)')
r3 = re.compile('(?ms)<input +type="checkbox" +name="tc" +value="(\d+:\d+:\d+)".*?class="text"> *(.*?) *</td></tr>$')

for line in data.split('\n'):
m = r.match(line)
if m:
list_id = m.groups()[0]
site = urllib.urlopen(urllib.Request(URL+"?listid="+list_id, headers=HEADER))
content = site.read()
#r3 = re.compile('value="'+list_id+':\d+" *> [^\n]*('
#print "X"*80
#print list_id
title = r2.findall(content)[0]
tasks = r3.findall(content)
print title.center(80)
for t in tasks:
if t:
print t
open('/tmp/tid_'+list_id, 'w').write(content)

Post a Comment