~jaapz-b/t00mblr/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright 2008 Ryan Cox ( ryan.a.cox@gmail.com ) All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

'''A wrapper library for Tumblr's public web API: http://www.tumblr.com/api'''

__author__ = 'ryan.a.cox@gmail.com'
__version__ = '0.2'


from urllib2 import Request, urlopen, URLError, HTTPError
from urllib import urlencode, quote
import base64
import types
import re

try:
    import simplejson
except ImportError:
    from django.utils import simplejson

GENERATOR = 't00mblr'
PAGESIZE = 50  


class TumblrError(Exception):
        ''' General Tumblr error ''' 
        def __init__(self, msg):
                self.msg = msg 

        def __str__(self):
                return self.msg 

class TumblrAuthError(TumblrError):
        ''' Wraps a 403 result '''
        pass

class TumblrRequestError(TumblrError):
        ''' Wraps a 400 result '''
        pass

class TumblrIterator:
        def __init__(self, name, start, max, type):
                self.name = name
                self.start = start
                self.max = max 
                self.type = type
                self.results = None
                self.index = 0
                
        def __iter__(self):
                return self
        
        def next(self):
                if not self.results or (self.index == len(self.results['posts'])): 
                        self.start += self.index
                        self.index = 0
                        url = "http://%s.tumblr.com/api/read/json?start=%s&num=%s" % (self.name,self.start, PAGESIZE)
                        if self.type:
                                url += "&type=" + self.type
                        response = urlopen(url)
                        page = response.read()
                        m = re.match("^.*?({.*}).*$", page,re.DOTALL | re.MULTILINE | re.UNICODE)
                        self.results = simplejson.loads(m.group(1))

                if (self.index >= self.max) or len(self.results['posts']) == 0:
                        raise StopIteration

                self.index += 1
                return self.results['posts'][self.index-1]  


class Api:
        def __init__(self, name, email=None, password=None ):
                self.name = name
                self.is_authenticated = False
                self.email = email 
                self.password = password


        def auth_check(self):
                if self.is_authenticated:
                        return
                url = 'http://www.tumblr.com/api/authenticate'
                values = {      
                                'generator' : GENERATOR, 
                                'email': self.email, 
                                'password' : self.password }

                data = urlencode(values)
                req = Request(url, data)
                try: 
                        response = urlopen(req)
                        page = response.read()
                        self.url = page 
                        self.is_authenticated = True
                        return
                except HTTPError, e:
                        if 403 == e.code:
                                raise TumblrAuthError(str(e))
                        if 400 == e.code:
                                raise TumblrRequestError(str(e))
                except Exception, e:
                        raise TumblrError(str(e))


        def write_regular(self, title=None, body=None, **args): 
                import pprint
                pp = pprint.PrettyPrinter(indent=4)

                if title:       
                        args['title'] = title
                if body: 
                        args['body'] = body 
                args = self._fixnames(args)
                if not 'title' in args and not 'body' in args:
                        raise TumblrError("Must supply either body or title argument")

                self.auth_check()
                args['type'] = 'regular'
                return self._write(args)

        def write_photo(self, source=None, **args): 
                if source:
                        args['source'] = source 

                args = self._fixnames(args)
                if 'source' in args and 'data' in args:
                        raise TumblrError("Must  NOT supply both source and data arguments")

                if not 'source' in args and not 'data' in args:
                        raise TumblrError("Must supply source or data argument")
                
                self.auth_check()
                args['type'] = 'photo'
                return self._write(args)

        def write_quote(self, quote=None, **args): 
                if quote:
                        args['quote'] = quote
                args = self._fixnames(args)
                if not 'quote' in args: 
                        raise TumblrError("Must supply quote arguments")
                
                self.auth_check()
                args['type'] = 'quote'
                return self._write(args)

        def write_link(self, url=None, **args): 
                if url:
                        args['url'] = url
                args = self._fixnames(args)
                if not 'url' in args:
                        raise TumblrError("Must supply url argument")

                self.auth_check()
                args['type'] = 'link'
                return self._write(args)

        def write_conversation(self, conversation=None, **args): 
                if conversation:
                        args['conversation'] = conversation
                args = self._fixnames(args)
                if not 'conversation' in args:
                        raise TumblrError("Must supply conversation argument")

                self.auth_check()
                args['type'] = 'conversation'
                return self._write(args)

        def write_video(self, embed=None, **args): 
                if embed:
                        args['embed'] = embed
                args = self._fixnames(args)
                if 'embed' in args and 'data' in args:
                        raise TumblrError("Must  NOT supply both embed and data arguments")

                if not 'embed' in args and not 'data' in args:
                        raise TumblrError("Must supply embed or data argument")
                
                self.auth_check()
                args['type'] = 'video'
                return self._write(args)

        def _fixnames(self, args):
                for key in args: 
                        if '_' in key:
                                value = args[key]
                                del args[key]
                                args[key.replace('_', '-')] = value
                return args 

        def _write(self, params, headers=None): 
                self.auth_check()
                url = 'http://www.tumblr.com/api/write'
                params['email'] = self.email
                params['password'] = self.password
                params['generator'] = GENERATOR

                data = urlencode(params)
                if headers:
                        req = Request(url, data, headers)
                else:
                        req = Request(url, data)
                try: 
                        ret = urlopen(req)
                        #return self.read(id=ret.read())
                except HTTPError, e:
                        raise TumblrError(e.read()) 

        def read(self, id=None, start=0,max=2**31-1,type=None): 
                if id:
                        url = "http://%s.tumblr.com/api/read/json?id=%s" % (self.name,id)
                        response = urlopen(url)
                        page = response.read()
                        m = re.match("^.*?({.*}).*$", page,re.DOTALL | re.MULTILINE )
                        results = simplejson.loads(m.group(1))
                        if len(results['posts']) == 0:
                                return None 
                                
                        return results['posts'][0]  
                else:   
                        return TumblrIterator(self.name,start,max,type)

        def delete(self, id):
                self.auth_check()
                url = 'http://www.tumblr.com/api/delete'
                params = {}
                params['email'] = self.email
                params['password'] = self.password
                params['generator'] = GENERATOR
                params['post-id'] = id 
                data = urlencode(params)
                req = Request(url, data)
                try: 
                        urlopen(req)
                except HTTPError, e:
                        raise TumblrError(e.read()) 
                

if __name__ == "__main__":
        pass