1 # -*- coding: utf-8 -*-
2 # Canola2 Remember The Milk Plugin
3 # Authors: Andrey Popelo <andrey@popelo.com>
5 # Based on Python module for Remember The Milk API
6 # (http://intellectronica.net/python-rtm)
7 # originally created by Tom Berger <tom.berger@gmail.com>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # Additional permission under GNU GPL version 3 section 7
24 # If you modify this Program, or any covered work, by linking or combining it
25 # with Canola2 and its core components (or a modified version of any of those),
26 # containing parts covered by the terms of Instituto Nokia de Tecnologia End
27 # User Software Agreement, the licensors of this Program grant you additional
28 # permission to convey the resulting work.
30 from urllib import urlencode
31 from urllib2 import urlopen
36 """Remember The Milk API method.
38 This class represents an RTM API method. Together with Client class and
39 Category class it allows to access RTM API methods using dot-notation.
42 Client --^ ^ ^-- Method
46 def __init__(self, client, category_name, method_name):
48 self.category_name = category_name
49 self.method_name = method_name
51 def __call__(self, **kwargs):
52 return self.client.get(
53 method='rtm.%s.%s' % (self.category_name, self.method_name),
54 auth_token=self.client.token,
58 class Category(object):
59 """Remember The Milk API category.
61 This class represents an RTM API category. Together with Client class and
62 Method class it allows to access RTM API methods using dot-notation.
65 Client --^ ^ ^-- Method
69 def __init__(self, client, category_name):
71 self.category_name = category_name
73 def __getattr__(self, attr):
74 return Method(self.client, self.category_name, attr)
78 """Remember The Milk Backend.
80 This class provides an interface to manage data on Remember The Milk
82 You can access RTM API methods using dot-notation.
85 Client --^ ^ ^-- Method
89 URL_SERVICE_REST = "http://api.rememberthemilk.com/services/rest/"
90 URL_SERVICE_AUTH = "http://www.rememberthemilk.com/services/auth/"
92 def __init__(self, api_key, secret, token=None):
93 self.api_key = api_key
98 def __getattr__(self, attr):
99 return Category(self, attr)
101 def sign(self, params):
102 data = self.secret + ''.join(
103 k + params[k] for k in sorted(params.keys()))
106 print "\n Sign data: " + data + "\n"
108 return md5(data).hexdigest()
110 def fetch(self, url, **kwargs):
112 url = url + '?' + urlencode(kwargs)
115 print "\nRTM Request:\n " + url + "\n"
117 return urlopen(url).read()
119 def get(self, **params):
120 params['api_key'] = self.api_key
121 params['format'] = 'json'
122 params['api_sig'] = self.sign(params)
124 json = self.fetch(self.URL_SERVICE_REST, **params)
127 print "\nRTM Response:\n " + json + "\n"
129 # TODO parse json and return valid data
136 'api_key': self.api_key,
140 params['api_sig'] = self.sign(params)
141 return self.URL_SERVICE_AUTH + '?' + urlencode(params)
145 if self._token is None:
147 rsp = self.get(method='rtm.auth.getToken', frob=frob)
148 self._token = rsp.auth.token
153 if self._frob is None:
154 rsp = self.get(method='rtm.auth.getFrob')
155 self._frob = rsp.frob