33
33
from collections import defaultdict
34
34
from functools import wraps
35
35
from hashlib import md5
36
+ from hashlib import sha1
36
37
37
38
import cookielib
38
39
from aqt import mw
50
51
51
52
52
53
__all__ = [
53
- 'register' , 'export' , 'copy_static_file' , 'with_styles' , 'parse_html' , 'service_wrap' ,
54
+ 'register' , 'export' , 'copy_static_file' , 'with_styles' , 'parse_html' , 'service_wrap' , 'get_hex_name' ,
54
55
'Service' , 'WebService' , 'LocalService' , 'MdxService' , 'StardictService' , 'QueryResult'
55
56
]
56
57
57
58
59
+ def get_hex_name (prefix , val , suffix ):
60
+ ''' get sha1 hax name '''
61
+ hex_digest = sha1 (
62
+ val .encode ('utf-8' ) if isinstance (val , unicode )
63
+ else val
64
+ ).hexdigest ().lower ()
65
+ name = '.' .join ([
66
+ '-' .join ([
67
+ prefix , hex_digest [:8 ], hex_digest [8 :16 ],
68
+ hex_digest [16 :24 ], hex_digest [24 :32 ], hex_digest [32 :],
69
+ ]),
70
+ suffix ,
71
+ ])
72
+ return name
73
+
74
+ def _is_method_or_func (object ):
75
+ return inspect .isfunction (object ) or inspect .ismethod (object )
76
+
58
77
def register (labels ):
59
78
"""
60
79
register the dict service with a labels, which will be shown in the dicts list.
61
80
"""
62
81
def _deco (cls ):
63
82
cls .__register_label__ = _cl (labels )
64
83
65
- methods = inspect .getmembers (cls , predicate = inspect . ismethod )
84
+ methods = inspect .getmembers (cls , predicate = _is_method_or_func )
66
85
exports = []
67
86
for method in methods :
68
87
attrs = getattr (method [1 ], '__export_attrs__' , None )
@@ -198,10 +217,16 @@ def cache_this(self, result):
198
217
return result
199
218
200
219
def cached (self , key ):
201
- return (self .word in self .cache ) and self .cache [self .word ]. has_key ( key )
220
+ return (self .word in self .cache ) and ( key in self .cache [self .word ])
202
221
203
222
def cache_result (self , key ):
204
223
return self .cache [self .word ].get (key , u'' )
224
+
225
+ def _get_from_api (self ):
226
+ return {}
227
+
228
+ def _get_field (self , key , default = u'' ):
229
+ return self .cache_result (key ) if self .cached (key ) else self ._get_from_api ().get (key , default )
205
230
206
231
@property
207
232
def unique (self ):
@@ -210,6 +235,13 @@ def unique(self):
210
235
@unique .setter
211
236
def unique (self , value ):
212
237
self ._unique = value
238
+
239
+ @property
240
+ def quote_word (self ):
241
+ return urllib2 .quote (
242
+ self .word .encode ('utf-8' ) if isinstance (self .word , unicode )
243
+ else self .word
244
+ )
213
245
214
246
@property
215
247
def support (self ):
@@ -269,8 +301,10 @@ def title(self):
269
301
return getattr (self , '__register_label__' , self .unique )
270
302
271
303
def get_response (self , url , data = None , headers = None , timeout = 10 ):
272
- default_headers = {'User-Agent' : 'Anki WordQuery' ,
273
- 'Accept-Encoding' : 'gzip' }
304
+ default_headers = {
305
+ 'User-Agent' : 'Mozilla/5.0' ,
306
+ 'Accept-Encoding' : 'gzip'
307
+ }
274
308
if headers :
275
309
default_headers .update (headers )
276
310
@@ -282,7 +316,7 @@ def get_response(self, url, data=None, headers=None, timeout=10):
282
316
data = zlib .decompress (data , 16 + zlib .MAX_WBITS )
283
317
return data
284
318
except :
285
- return ''
319
+ return u ''
286
320
287
321
@classmethod
288
322
def download (cls , url , filename , timeout = 15 ):
0 commit comments