forked from mcdona1d/RaspberryWechatPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweixin.py
353 lines (296 loc) · 10.7 KB
/
weixin.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import json
import urllib
import urllib2
__version__ = '0.1.0'
__author__ = 'Liang Cha ([email protected])'
'''
Python client SDK for Micro Message Public Platform API.
'''
try:
import memcache
except Exception, e:
print '\033[95mWrining: %s. use local filecache.\033[0m' %e
class APIError(StandardError):
'''
raise APIError if reciving json message indicating failure.
'''
def __init__(self, error_code, error_msg):
self.error_code = error_code
self.error_msg = error_msg
StandardError.__init__(self, error_msg)
def __str__(self):
return '%s:%s' %(self.error_code, self.error_msg)
class AccessTokenError(APIError):
'''
raise AccessTokenError if reciving json message indicating failure.
'''
def __init__(self, error_code, error_msg):
APIError.__init__(self, error_code, error_msg)
def __str__(self):
return APIError.__str__(self)
class JsonDict(dict):
' general json object that allows attributes to bound to and also behaves like a dict '
def __getattr__(self, attr):
try:
return self[attr]
except KeyError:
raise AttributeError(r"'JsonDict' object has no attribute '%s'" %(attr))
def __setattr__(self, attr, value):
self[attr] = value
def _parse_json(s):
' parse str into JsonDict '
def _obj_hook(pairs):
o = JsonDict()
for k, v in pairs.iteritems():
o[str(k)] = v
return o
return json.loads(s, object_hook = _obj_hook)
(_HTTP_GET, _HTTP_POST, _HTTP_FILE) = range(3)
def _encode_params(**kw):
'''
do url-encode parmeters
>>> _encode_params(a=1, b='R&D')
'a=1&b=R%26D'
'''
args = []
body = None
path = None
for k, v in kw.iteritems():
if k == 'body':
body = v
continue
if k in ['pic']:
continue
if k == 'path':
path = v
continue
if isinstance(v, basestring):
qv = v.encode('utf-8') if isinstance(v, unicode) else v
args.append('%s=%s' %(k, urllib.quote(qv)))
else:
if v == None:
args.append('%s=' %(k))
else:
qv = str(v)
args.append('%s=%s' %(k, urllib.quote(qv)))
return ('&'.join(args), body, path)
def _encode_multipart(**kw):
' build a multipart/form-data body with randomly generated boundary '
boundary = '----------%s' % hex(int(time.time()) * 1000)
data = []
for k, v in kw.iteritems():
if hasattr(v, 'read'):
data.append('--%s' % boundary)
filename = getattr(v, 'name', '')
if filename == None or len(filename) == 0:
filename = '/tmp/test.jpg'
content = v.read()
data.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (k, filename))
data.append('Content-Length: %d' % len(content))
#data.append('Content-Type: application/octet-stream')
data.append('Content-Type: image/jpeg')
data.append('Content-Transfer-Encoding: binary\r\n')
data.append(content)
break
data.append('--%s--\r\n' % boundary)
return '\r\n'.join(data), boundary
def _http_call(the_url, method, token, **kw):
'''
send an http request and return a json object if no error occurred.
'''
params = None
boundary = None
body = None
path = None
(params, body, path) = _encode_params(**kw)
if method == _HTTP_FILE:
the_url = the_url.replace('https://api.', 'http://file.api.')
body, boundary = _encode_multipart(**kw)
if token == None:
http_url = '%s?%s' %(the_url, params)
else:
#the_url = the_url + '?access_token=' + token
the_url = '%s?access_token=%s' %(the_url, token)
http_url = '%s&%s' %(the_url, params) if (method == _HTTP_GET or method == _HTTP_FILE) else the_url
http_body = str(body) if (method == _HTTP_POST) else body
req = urllib2.Request(http_url, data = http_body)
if boundary != None:
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
try:
resp = urllib2.urlopen(req, timeout = 5)
body = resp.read()
resp.close()
try:
rjson = _parse_json(body)
except Exception, e:
if resp.getcode() != 200:
raise e
if resp.headers['Content-Type'] == 'image/jpeg':
if path == None:
path = './WX_%d.jpg' %(int(time.time()))
else:
raise e
try:
fd = open(path, 'wb')
fd.write(body)
except Exception, e:
raise e
fd.close()
return _parse_json('{"path":"%s"}' %(path))
if hasattr(rjson, 'errcode') and rjson['errcode'] != 0:
if str(rjson['errcode']) in ('40001', '40014', '41001', '42001'):
raise AccessTokenError(str(rjson['errcode']), rjson['errmsg'])
raise APIError(str(rjson['errcode']), rjson['errmsg'])
return rjson
except urllib2.HTTPError, e:
try:
rjson = _parse_json(e.read())
except:
rjson = None
if hasattr(rjson, 'errcode'):
if str(rjson['errcode']) in ('40001', '40014', '41001', '42001'):
raise AccessTokenError(str(rjson['errcode']), rjson['errmsg'])
raise APIError(rjson['errcode'], rjson['errmsg'])
raise e
class filecache:
'''
the information is temporarily saved to the file.
'''
def __init__(self, path, create = False):
self.path = path
self.dict_data = None
fd = None
try:
fd = open(self.path, 'rb')
except Exception, e:
#print 'filecache open error:', e
if not create:
return None
else:
fd = open(self.path, 'wb')
fd.close()
fd = open(self.path, 'rb')
data = fd.read()
if len(data) == 0:
data = '{}'
self.dict_data = eval(data)
fd.close()
def get(self, key):
if self.dict_data.has_key(key):
return self.dict_data[key]
return None
def set(self, key, value, time = 0):
if self.dict_data.has_key(key):
self.dict_data[key] = value
else:
self.dict_data.update({key:value})
def delete(self, key, time = 0):
if self.dict_data.has_key(key):
del self.dict_data[key]
def save(self):
fd = open(self.path, 'wb')
fd.write(repr(self.dict_data))
fd.close()
def __str__(self):
data = []
for key in self.dict_data.keys():
data += ['"%s":"%s"' %(str(key), str(self.dict_data[key]))]
return '{%s}' %(', '.join(data))
class WeiXinClient(object):
'''
API clinet using synchronized invocation.
>>> fc = False
'use memcache save access_token, otherwise use filecache, path=[file_path | ip_addr]'
'''
def __init__(self, appID, appsecret, fc = False, path = '127.0.0.1:11211'):
self.api_url = 'https://api.weixin.qq.com/cgi-bin/'
self.app_id = appID
self.app_secret = appsecret
self.access_token = None
self.expires = 0
self.fc = fc
self.file_cache = None
if not self.fc:
self.mc = memcache.Client([path], debug = 0)
else:
self.file_cache = '%s/access_token' %(path)
self.mc = filecache(self.file_cache, True)
def request_access_token(self):
token_key = 'access_token_%s' %(self.app_id)
expires_key = 'expires_%s' %(self.app_id)
access_token = self.mc.get(token_key)
expires = self.mc.get(expires_key)
if access_token == None or expires == None or \
int(expires) < int(time.time()):
rjson =_http_call(self.api_url + 'token', _HTTP_GET, \
None, grant_type = 'client_credential', \
appid = self.app_id, secret = self.app_secret)
self.access_token = str(rjson['access_token'])
self.expires = int(time.time()) + int(rjson['expires_in'])
self.mc.set(token_key, self.access_token, \
time = self.expires - int(time.time()))
self.mc.set(expires_key, str(self.expires), \
time = self.expires - int(time.time()))
if self.fc:
self.mc.save()
else:
self.access_token = str(access_token)
self.expires = int(expires)
def del_access_token(self):
import os
token_key = 'access_token_%s' %(self.app_id)
expires_key = 'expires_%s' %(self.app_id)
self.access_token = None
self.expires = 0
if self.fc:
os.remove(self.file_cache)
else:
self.mc.delete(token_key)
self.mc.delete(expires_key)
def refurbish_access_token(self):
self.del_access_token()
self.request_access_token()
def set_access_token(self, token, expires):
self.access_token = token
self.expires = expires
def is_expires(self):
return not self.access_token or int(time.time()) >= (self.expires - 10)
def __getattr__(self, attr):
return _Callable(self, attr)
def __str__(self):
return 'url=%s\napp_id=%s\napp_secret=%s\naccess_token=%s\nexpires=%d' \
%(self.api_url, self.app_id, self.app_secret, self.access_token, self.expires)
class _Executable(object):
def __init__(self, client, method, path):
self._client = client
self._method = method
self._path = path
def __call__(self, **kw):
return _http_call('%s%s' %(self._client.api_url, self._path), \
self._method, self._client.access_token, **kw)
def __str__(self):
return '_Executable (%s)' %(self._path)
__repr__ = __str__
class _Callable(object):
def __init__(self, client, name):
self._client = client
self._name = name
def __getattr__(self, attr):
if attr == '_get':
return _Executable(self._client, _HTTP_GET, self._name)
if attr == 'post':
return _Executable(self._client, _HTTP_POST, self._name)
if attr == 'file':
return _Executable(self._client, _HTTP_FILE, self._name)
name = '%s/%s' %(self._name, attr)
return _Callable(self._client, name)
def __str__(self):
return '_Callable (%s)' %(self._name)
def test():
' test the API '
pass
if __name__ == '__main__':
test()