Google Tasks API case study

By William Entriken

2 minutes

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

Update 2011-05-12: The official API is now at https://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’m 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=<YOURUSERNAME@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/.*&nbsp;.*//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 +.

Comments

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.

William Entriken

Use these lines instead because they are cross platform (sed parses escape sequences differently 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

William Entriken

A quick python rewrite:

fab31

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

```python
#!/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('(.*)')
r3 = re.compile('(?ms) *(.*?) *$')

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()
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)
```

William Entriken

Please discuss this topic anywhere and let me know any great comments or media coverage I should link here.