TA的每日心情 | 怒 2021-3-10 11:26 |
---|
签到天数: 1 天 [LV.1]初来乍到
|
发表于 2016-8-7 16:58:16
|
显示全部楼层
本帖最后由 Launcher1 于 2016-8-7 17:21 编辑
0 q6 r4 r5 F2 f$ s/ p) s' x! F
( o8 E2 \' h& S: c$ F遇到同样问题……后来在 GitHub 找到了解决办法
" T( H Q) x( x/ S9 b. R# x; n D( H/ h. o. ?/ ^
原帖:https://github.com/goldendict/goldendict/issues/430
2 }4 M7 N, L5 r l. b4 @+ d
, N5 q. X& T9 v0 j3 v) I- I, a' o" ?. z7 S, ~; [7 `$ i' d
1. 下载安装 Python 2。不要下 Python 3,语法会出问题
* m _+ v- x: \# _; b4 B 2. 把以下代码保存为 .py 脚本,复制到 GoldenDict 的安装目录
" @2 `0 v4 X' c- #!/usr/bin/env python
0 a$ J- i6 D) I! S( F5 m! B! F
' k: H4 A, N/ a5 T r/ ^( p- # -*- coding: utf-8 -*-5 u" s/ {# h6 Y* j6 T! d
- . P ]: l8 r5 I
- import sys
% t- N* Z8 U: E, m" _$ I - reload(sys)9 j2 T3 e4 b: x9 R& |
4 r0 n0 w3 e' y6 Q" J7 [7 Y& O1 ^- sys.setdefaultencoding('utf-8')
( x* b+ r9 d9 V: l/ x6 C - ) V( T3 y! a5 [+ F
- # https://github.com/narfman0/helga-lingo
8 q7 G9 ^5 x3 M' K/ I: N - ( ^: ]* B9 @( @) M) x
- import json, re, urllib, urllib2$ X. e2 l! @0 F
- def ud(*args):, z1 C5 x2 S0 A1 ~/ I }0 n
- """ Define from urban dictionary """: R+ w+ I2 i& P: _' X. X
- if len(args) == 0:3 b5 S* @) f" Y/ l# x2 F/ ?
- return u'You need to give me a term to look up.'
2 H6 e9 ~2 L& i+ r0 C - term, index = parse_args(' '.join(args))
) L2 X! R$ Y* g- H - try:
3 \: l5 t. I( E7 J! {' P1 {+ H - data = execute_request(term), j! c/ H3 u6 z, K' J& f' |$ ]
- total = len(data['list'])2 |% n# t1 M" o) u4 M1 X
- ) ]2 H6 B* ]' h# G/ i
- # hack to ignore parameter 'index', print out all definitions* y- I6 q! Z' l$ D. q) a, B) e# n
- # defn = define(term, data, index)# e% q& {; v2 `! |( \ p. N
- # example = define(term, data, index, 'example')
; Z% f/ F! Z) e1 e% \ - # return '{0} e.g.: {1} [{2}/{3}]'.format(defn, example, index, total)
# _7 l& l$ Z. n: D/ [ - if total == 0: sys.exit(0)* p$ o/ _4 P f* T3 O
- # %20 rendered by urllib.quote
- r: c. Q% Y& u7 i8 ~/ y/ K - result = urllib.unquote(term) + ' (total definitions: ' + str(total) + ')' + '<br><hr>': ^& ?; v+ X+ ^2 {' V. O
- # for index in range(1, min(5, total)+1):
* l6 ]7 m2 w# M; R3 ]; ] - for index in range(1, total+1):! ^; V# t8 \ T- W0 I$ I& w
- defn = define(term, data, index).replace("\n","<br>")) S. ]$ F7 g( F& V. O% D) Y
- example = define(term, data, index, 'example').replace("\n","<br>")9 ^, `, e" l) w; e7 b5 R
- result += '<b>{2}. </b>{0}<br><font color=grey>{1}</font><br><br>'.format(defn, example, index)
3 \. ~* Z' a# a& Z2 q8 W - return result
g+ g" j; r; Y+ M0 K - # hack end
1 \% b9 m# t+ p9 I F5 e - except Exception as e:
2 D/ v4 l" n6 x& A5 X - return unicode('Urban Dictionary returned exception for ' + term + ":" + str(e))- c% Q1 q3 x: R0 D) ?- e
- 4 f" P- |1 Y! D3 c: z- w( J1 r$ z
- def execute_request(term):! Z" {9 r9 U5 n" o9 t; i
- """ Invoke API to retrieve json hopefully representing term """
+ H* X/ W. g9 M* E# |$ r/ O8 O5 Y - api_url = 'http://api.urbandictionary.com/v0/define?term='8 w5 t4 [/ n0 c
- # use requests
4 o* ^* _* E% Q# ? - # import requests
# k" ~, e& a" S9 c2 L g - # response = requests.get(api_url + term)
) o4 ]6 K% e0 A+ W, }9 V - # if response.status_code != 200:. M) I" n- s: y$ ~! L+ i0 h
- # raise Exception('Error status code returned: ' + str(response.status_code))0 g4 L$ N2 R D( Z
- # response_json = json.loads(response.content)
& a& O% S8 P1 S+ ]8 J' s6 |; y - # if not response_json:
. G; Z' u6 v! y. h2 M- o4 |2 ~ - # raise Exception('Response falsy for given term: ' + term): f. o* r0 J, A) \" e9 G
- # return response_json9 \, c# F7 X# f( z
T7 Z; m3 a# w& Q; _3 K! W( ?. v6 P- # or use urllib2
, r# B/ _$ W" z4 L K4 | - req = urllib2.Request(api_url + term)7 n- C! [# \ S& p+ K
- handler = urllib2.urlopen(req)6 F2 ]/ h$ {5 ]7 }0 ^
- status_code = handler.getcode()
% X* p4 I8 B4 ]; D6 K5 S - # handler.headers.getheader('content-type')
/ F7 D- @4 A5 K9 Y8 E - content = handler.read()
. N" H' A/ k% g - if status_code != 200:
1 J3 O& I+ F3 K) p: m% J - raise Exception('Error status code returned: ' + str(status_code))
" H0 E" x. t4 c# o. x4 E) W - response_json = json.loads(content). n# V' _# c. w0 O* M
- if not response_json:
& h4 a/ U7 X+ F* L1 C! T. V - raise Exception('Response falsy for given term: ' + term)" U8 }: B( s% |& O1 U
- return response_json5 u$ W q4 a% F# d+ C
- M9 T* |: n4 E) g- def define(term, data, index=1, action='definition'):
$ B, p7 Y: O7 C# J7 \! U* P3 [9 h - """ Retrieve the definition for the term """; t- v$ ~) t7 C) G4 l. w% ^7 U
- return data['list'][index-1][action]
/ U F5 c( L# P) O! |2 X8 B - , I" D- L* N5 f
- def parse_args(args):+ j1 K& m0 f' \, ?, r& P7 d
- """ Parse arguments to extract desired search term and index in def list """
- W1 n3 u1 a* _7 N; ? - index = 1# c, z5 h, i; q6 ^
- match = re.search(r'\d+, args)
8 r; o: D% s4 M - if match is not None:+ Q0 p( ]$ T x- g' B6 e7 _
- index = int(match.group())* v& R* M4 m" d# i9 W
- args=args[:-len(str(index))-1]
; b/ j* X+ B; \$ B3 {. o" Z - term = urllib.quote(args)
3 I2 K: Y5 K/ B" p - return (term, index)" j ?( m& t. V& g9 E& a- {
- # L; v$ b" |/ k5 _# i8 \- p6 ?, ~
- if __name__ == "__main__":! O2 V! V) U- D
- result = ud(sys.argv[1]). A1 _) X8 i! ~8 I# m3 f, |
- print result
! {6 W R3 Z* [4 Y( z: S) K
复制代码 . X. r& w6 A( {' O6 E7 `; e
4 `7 z* b) L; c! }! g7 O, k
3. 编辑(E)→词典→词典来源→程序→如图设置(udgd.py 是刚刚保存的脚本的文件名)! U6 Y2 y6 S/ z/ y) W& C' P; Y: X
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
|