1. Dusty Phillips
  2. python-astrid


python-astrid / astrid.py

import requests
import time
import hashlib

from urllib import urlencode

class AstridAPI(object):
    '''Connect to the Astrid Tasks API and issue requests to a specific
    user's account.

    def __init__(self, conf):
        :param conf: a dictionary  of configuration values

        The conf dictionary must contain::

                "apikey": "<astrid api key>",
                "apisecret": "<astrid api secret>",
                "email": "you@example.com", # your astrid e-mail
                "password": "<astrid password>"

        The apikey and secret can come from http://astrid.com/api_keys

        The user associated with the email and
        password provided in the configuration dictionary
        will be automatically signed in.
        The token received from signin
        is stored on the class and automatically sent
        with future requests.
        self.server = 'https://astrid.com'
        self.astrid_api_version = 7
        self.apikey = conf['apikey']
        self.apisecret = conf['apisecret']
            self.token = self.send_request("user_signin",
        except KeyError:
            raise AttributeError("Unable to sign into astrid. Ensure your "
                "api key, api secret, email, and password are correct.")

    def send_request(self, method, **params):
        '''Send a request to the astrid API.
        The available methods and their parameters are described in

        This method does the necessary work
        to create an astrid signature line as required by the API.
        It then issues the request in the format Astrid expects.

        :param method: the method name to call in the Astrid API
        :param params: keyword arguments mapping parameters to values as
            required by the *method*
        :return: json decoded response of your Astrid API request
        :rtype: dict
        signature = []
        params['app_id'] = self.apikey
        params['time'] = time.time()
        if hasattr(self, 'token'):
            params['token'] = self.token
        for param in sorted(params):
            value = params[param]
            if isinstance(value, list):
                signature.extend([("%s[]%s" % (param, v)) for v in value])
                signature.append("%s%s" % (param, value))
        sig = "%s%s%s" % (method, "".join(signature), self.apisecret)
        sig = hashlib.md5(sig).hexdigest()
        params['sig'] = sig

        url = "%s/api/%s/%s?%s" % (

        return requests.post(url, data=params).json