TA的每日心情 | 怒 2021-3-10 11:26 |
---|
签到天数: 1 天 [LV.1]初来乍到
|
发表于 2016-8-7 16:58:16
|
显示全部楼层
本帖最后由 Launcher1 于 2016-8-7 17:21 编辑 2 Y8 L6 U4 z" b) g2 c% R
) I- _. r' h o1 d0 P# a遇到同样问题……后来在 GitHub 找到了解决办法- h& n& S% p r9 [6 N& v( D
2 p5 x, E: i. K/ J* T: @+ n原帖:https://github.com/goldendict/goldendict/issues/430
/ L( k! M ?# U2 n" |
5 V2 p U! w" r* d2 P
i3 s1 d& S }( l$ z 1. 下载安装 Python 2。不要下 Python 3,语法会出问题
! ]: f: L# ~8 _" p+ H 2. 把以下代码保存为 .py 脚本,复制到 GoldenDict 的安装目录
) f @5 @ e& A- #!/usr/bin/env python+ X$ U- z( n- G- P0 W% H/ l
- # J. ]: `, T8 L1 W2 n7 V
- # -*- coding: utf-8 -*-
6 r. I A: w5 a
- A& w: g9 r+ d( S7 f- import sys' I. n6 A2 H) l3 _* _/ }
- reload(sys)( V ^1 C: c, p; p
- % ~/ J$ a3 d# B, }' k
- sys.setdefaultencoding('utf-8')6 x2 R1 F" |0 b3 x+ D
- * h6 p9 J8 h% E- W/ W& `4 E' m
- # https://github.com/narfman0/helga-lingo6 [; L' V; y0 b' L8 l3 c8 N ^
7 z0 Z% f+ }: s* G3 _9 w( d5 r+ @2 U- import json, re, urllib, urllib2
/ P$ g# w5 d; O: ?5 G1 z5 d0 |9 ` - def ud(*args):
5 n/ _4 \* t. v - """ Define from urban dictionary """
% R6 m/ O* s' k8 m* i - if len(args) == 0:/ h% E1 i; a/ {" z0 A, l8 _
- return u'You need to give me a term to look up.'* Z* t/ x# Y7 S* ^$ ^! b
- term, index = parse_args(' '.join(args))$ k& R5 ?# O& V& g9 o) l* D
- try:
: }+ P) e6 W+ d' N ]( A( q - data = execute_request(term)$ ^7 A0 k( u/ N/ _! m
- total = len(data['list'])
5 Q9 @$ c# V+ k) A- j - ; l. i3 Q9 M$ R' W. H
- # hack to ignore parameter 'index', print out all definitions
0 i) q6 u9 G8 e/ u. F7 V2 V, c! N - # defn = define(term, data, index)
. N( R# T; A$ O" g! d- t- _, U3 ~0 j - # example = define(term, data, index, 'example')
9 d& j/ ]2 P! ? - # return '{0} e.g.: {1} [{2}/{3}]'.format(defn, example, index, total)% `. h0 E$ I3 k! i. J
- if total == 0: sys.exit(0)8 { D# D9 R5 r1 X7 @
- # %20 rendered by urllib.quote6 ?$ C$ R* ~: ~% c' ^5 Y3 o" g1 n
- result = urllib.unquote(term) + ' (total definitions: ' + str(total) + ')' + '<br><hr>'
5 u# L6 z$ Z F- ] - # for index in range(1, min(5, total)+1):: B$ P6 a/ A9 i& f+ _
- for index in range(1, total+1):9 y6 z3 n+ n9 ~- e$ C2 {
- defn = define(term, data, index).replace("\n","<br>"); [+ u9 i8 |3 n3 P. z: v
- example = define(term, data, index, 'example').replace("\n","<br>")
9 o/ ]' ~" C0 F$ `: C$ ` - result += '<b>{2}. </b>{0}<br><font color=grey>{1}</font><br><br>'.format(defn, example, index)
; o, Q( B9 Z$ ]6 n: f( e6 b- G5 x - return result
) i6 V4 O3 j' L5 N4 [. l- z0 T - # hack end
1 J& @ b: v' k& e1 [ - except Exception as e:! Y) r4 e- H* ]1 ]
- return unicode('Urban Dictionary returned exception for ' + term + ":" + str(e))
: P9 d# W( @% y# z3 ?& ~' G, m6 G
3 K7 J9 v2 U" V4 d, c+ B8 C/ o' [1 ?- def execute_request(term):% g) m% Y$ V" e5 E: X
- """ Invoke API to retrieve json hopefully representing term """
" @" j: T, v4 B/ V- D. h - api_url = 'http://api.urbandictionary.com/v0/define?term='3 u0 B; p' K% U' E+ M7 [
- # use requests4 V4 D: r' q3 F4 }2 N- I
- # import requests% ?# C) M; [+ L `7 n9 \6 I$ |
- # response = requests.get(api_url + term)
! V6 S/ G+ F) R- c - # if response.status_code != 200:, N! I: W" N' }% \' A i
- # raise Exception('Error status code returned: ' + str(response.status_code))
, j4 k2 v9 W+ g) N+ Y- u7 p - # response_json = json.loads(response.content)
6 f: T9 i1 v) P( m3 _" A - # if not response_json:
* ] b; P/ ?9 a9 W) w8 Z - # raise Exception('Response falsy for given term: ' + term), Z, k, ^3 D, }: e/ c& [' w
- # return response_json8 _' M e* A O! [+ [0 i0 ~
- : y# G& B1 @1 s+ J! ]+ I; W( x' i, z
- # or use urllib2
/ c$ P* T9 M! J7 b1 M- v) N* c - req = urllib2.Request(api_url + term)- n) q6 x/ K: f' H/ g( A. I! m9 v
- handler = urllib2.urlopen(req)
! ]' P- D. k |& k - status_code = handler.getcode()
+ N+ H9 I3 l/ P" d b - # handler.headers.getheader('content-type')- Z; N# d) E# p7 E
- content = handler.read() W$ j1 x0 ^- `1 p* r: w
- if status_code != 200:
; F: A! |, h8 ? - raise Exception('Error status code returned: ' + str(status_code))
0 ]. T# x& V; n! T3 }7 M - response_json = json.loads(content)+ b1 ]8 u9 n/ d* k7 z" J$ g' R$ V
- if not response_json:: \ u1 s6 |4 U6 o" S# H; ^. q- N w2 D
- raise Exception('Response falsy for given term: ' + term)3 H3 ?. T; P" d; i. j
- return response_json
: ^' n+ |4 P T! @' m9 g& i7 C
8 z1 h7 k4 T/ h ~3 B6 d" m- def define(term, data, index=1, action='definition'):9 R! P$ t7 T0 D9 t# H
- """ Retrieve the definition for the term """
$ @1 q9 o" U4 o3 ^: [# C - return data['list'][index-1][action]
, @) H8 R, ~4 B5 x7 Z - " M+ z* E6 h0 w( F- B
- def parse_args(args):* V' {9 f7 e9 @1 z
- """ Parse arguments to extract desired search term and index in def list """" M0 q8 i$ n/ k1 Y
- index = 1& B6 P6 O8 z$ b" T: [( _; L
- match = re.search(r'\d+, args)& E$ w, m& V0 f" a
- if match is not None:: x/ u! k, m8 B6 o9 _
- index = int(match.group())4 f% t1 a W @& l# Q; P
- args=args[:-len(str(index))-1]! M$ L' x+ T- @6 I% S
- term = urllib.quote(args)5 N3 H1 e5 s% I/ u+ s; h$ \& [
- return (term, index)
4 }5 W/ ?9 n; U- U+ Y0 E5 _9 v
- e$ p( E+ h3 t1 a( \: D+ T- if __name__ == "__main__":, z9 o$ M) q5 } {7 Q
- result = ud(sys.argv[1])
8 z; q3 N2 ^ h4 I8 } - print result5 o7 x+ v# y+ b, S! |0 H
复制代码
) N. }( _7 x, a1 q5 @* n, v2 I. M+ g9 Y$ G8 {. E
3. 编辑(E)→词典→词典来源→程序→如图设置(udgd.py 是刚刚保存的脚本的文件名)
, U7 Z: j/ X, c' I& D; q( Y' d; \ |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
|