TA的每日心情 | 怒 2021-3-10 11:26 |
---|
签到天数: 1 天 [LV.1]初来乍到
|
发表于 2016-8-7 16:58:16
|
显示全部楼层
本帖最后由 Launcher1 于 2016-8-7 17:21 编辑
2 \) C9 A1 c: Z) p" A# O: e5 S+ O) d/ g, {& E
遇到同样问题……后来在 GitHub 找到了解决办法7 p; w% z9 C8 k+ U
/ ]1 B( v/ Z0 a8 G: m0 |4 [
原帖:https://github.com/goldendict/goldendict/issues/430: r7 y) d/ S3 H; K' V
8 r6 e5 B y* C& A' `, i; I s/ |, \( S
1. 下载安装 Python 2。不要下 Python 3,语法会出问题4 w/ |# K: V' h# b
2. 把以下代码保存为 .py 脚本,复制到 GoldenDict 的安装目录/ a9 x$ R4 m' W, m8 D
- #!/usr/bin/env python& \, o( H( R Q6 K2 b' Q" x) i/ J
- / k& f5 [- o7 _7 O
- # -*- coding: utf-8 -*-$ ?- i# T% y# g0 e( s: b) s+ ?1 K
; N* v4 A) N3 p- A8 q# I7 `- import sys% |$ g' c9 V8 y3 J! D% h
- reload(sys)4 Z5 Q. P e; N( H( b- b+ M6 q+ D
- 5 N7 l$ ~ D4 A F7 Q
- sys.setdefaultencoding('utf-8')
; Q+ M2 l( P; P7 o& `9 B% }! ~8 q0 k' _
" Q' S0 x" Z% z9 B- # https://github.com/narfman0/helga-lingo2 L% {" B" d, a- T$ c* L
- # H- W3 d0 f$ E8 E
- import json, re, urllib, urllib2 _/ S/ w, E/ k4 q' J* c& `
- def ud(*args):
2 v5 L) F; Q, r* T6 ^ - """ Define from urban dictionary """
) w( [9 l4 f @. p7 j- H% e* ^ - if len(args) == 0:
$ p* D/ T6 C2 O7 h- g- {* B - return u'You need to give me a term to look up.'
6 n6 q/ F; a5 e) q$ m: H - term, index = parse_args(' '.join(args))
- g% s* Q# [4 N6 S/ n; ?0 u - try:
; G8 h1 o& R+ J G0 [ - data = execute_request(term)' a4 _/ f! N ]+ {
- total = len(data['list'])0 W; \8 B" _5 [9 P( a7 G4 j
( v& {" m! K: l' d! t- # hack to ignore parameter 'index', print out all definitions+ \: z1 n7 p6 |
- # defn = define(term, data, index) x( F/ F1 o/ N" G+ u/ Y& `# d1 k q
- # example = define(term, data, index, 'example')
3 u+ f, i$ A7 g" G1 Y0 P! F - # return '{0} e.g.: {1} [{2}/{3}]'.format(defn, example, index, total)
* v) k( o8 n1 [: X k2 h5 Y( J - if total == 0: sys.exit(0); e* I3 L$ z0 I. y) p
- # %20 rendered by urllib.quote: \) ^* f) O! i2 t) M
- result = urllib.unquote(term) + ' (total definitions: ' + str(total) + ')' + '<br><hr>'4 S! m) d( ]( _5 ?: d/ \. c
- # for index in range(1, min(5, total)+1):! a) D& w @: n8 E
- for index in range(1, total+1):% C' @- l( w9 o. g$ c
- defn = define(term, data, index).replace("\n","<br>")
0 x" g' W3 m. J - example = define(term, data, index, 'example').replace("\n","<br>")
* F6 Q, d% V1 V- w: J) K2 k% ^ - result += '<b>{2}. </b>{0}<br><font color=grey>{1}</font><br><br>'.format(defn, example, index)+ D+ k4 F. C8 K' V6 Q) T2 q
- return result
+ v4 C+ U8 L1 F- H' V' a - # hack end0 S* [( s' x3 V
- except Exception as e:
?3 N- O8 Y" F$ ]) B2 y$ k - return unicode('Urban Dictionary returned exception for ' + term + ":" + str(e))
% f) r$ `! E5 u* @6 U5 ^( B
$ N6 C3 W9 N- _9 q8 J- def execute_request(term):
8 u( T3 g, O- D( s. {2 i* | - """ Invoke API to retrieve json hopefully representing term """
0 B3 c- t! }; h6 a+ T+ l - api_url = 'http://api.urbandictionary.com/v0/define?term='" x9 r& D6 ` |8 ^3 ~7 U# j
- # use requests
' s6 i0 m& ~4 ^: j1 J/ C2 ~ - # import requests
, h& q8 v6 u6 u: V# r - # response = requests.get(api_url + term)
1 b I+ y$ k3 ~' L - # if response.status_code != 200:( j& \3 D# T/ y" ~: q
- # raise Exception('Error status code returned: ' + str(response.status_code))
) v5 z5 L* Z; W" p) r9 [5 n7 @3 t - # response_json = json.loads(response.content)
8 Z3 N: h" e+ N; K5 h7 l - # if not response_json:& V/ J8 r8 E0 G$ t
- # raise Exception('Response falsy for given term: ' + term)* g6 k9 u! I! y/ n' o
- # return response_json
+ G1 h5 n6 u* x2 u5 s* U - 0 F+ `# @+ p& c; M' y$ D" ?
- # or use urllib2" l+ s: o2 `0 m
- req = urllib2.Request(api_url + term)
$ \) T$ ]* A" o - handler = urllib2.urlopen(req)
1 N, @5 k- [8 n& j/ k7 p" Z# _' C - status_code = handler.getcode()) j q6 j! a$ |8 U1 i( o# a1 @! e. k
- # handler.headers.getheader('content-type')/ @: o) U ^1 k! x- t- ]
- content = handler.read(). q4 Z- P( Q) d6 K j! j/ ?
- if status_code != 200:
/ ]: r' i# j6 O, S - raise Exception('Error status code returned: ' + str(status_code)), P) ]& \7 c- D+ a) a
- response_json = json.loads(content)0 O4 I3 o- k: }( [6 _$ t
- if not response_json:" t) N+ P$ p. Z* H$ l$ O
- raise Exception('Response falsy for given term: ' + term)- V S6 O, D# r- M) n/ Z2 I a* T
- return response_json
9 ?' \; d w$ |/ `! L5 j0 u! y1 ^
+ S/ \0 D5 v2 R* w- def define(term, data, index=1, action='definition'):
! L8 P. _" M) h& V8 C" Y% I - """ Retrieve the definition for the term """8 d9 e9 a9 O, b @
- return data['list'][index-1][action]. n7 A$ t7 v) ?0 x0 ?. U/ g& J
# a* ~) x7 X+ y- def parse_args(args):
8 T) ?0 k! o w) N8 i4 a9 O& V - """ Parse arguments to extract desired search term and index in def list """: D( ]. c' } @- ~+ w, w
- index = 1
9 K1 M" L7 Z8 Q" g: g) r - match = re.search(r'\d+, args): m |6 Z3 s0 q( @) }7 R
- if match is not None:( ^* m) q% W, ^ z
- index = int(match.group())
" C1 ? r R* I - args=args[:-len(str(index))-1]
8 H+ {# _& I, A. E9 e - term = urllib.quote(args)4 @' j$ V% B% z
- return (term, index)
E8 A. \& Z/ G8 `$ k1 A, `
1 P5 J/ G. w5 u: Y4 I B6 Q" |- if __name__ == "__main__":/ r' M- C2 h' Q7 y0 E. G; N
- result = ud(sys.argv[1])( q6 D* _2 v" J; L& m+ }
- print result
% R8 [. C; }, l0 q
复制代码
* z- |/ | c1 U* ]# K) ^3 w e/ t: Z* ?* f! E
3. 编辑(E)→词典→词典来源→程序→如图设置(udgd.py 是刚刚保存的脚本的文件名); L, S& T0 w% h
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
|