掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 4728|回复: 2

[问题反馈] GoldenDict 无法显示在线的 Urban Dictionary

[复制链接]

该用户从未签到

发表于 2016-7-26 22:16:25 | 显示全部楼层 |阅读模式
本帖最后由 PattiRoberts 于 2016-7-26 22:19 编辑 2 b0 Q, u0 {8 k/ ~, k

- ?3 N  O2 C( f% o. Z( M5 T% {似乎 GoldenDict 的网站词典查询速度要比浏览器要慢很多,关键是 Urban Dictionary 完全不能查询(即使翻了墙),而用浏览器完全没有问题
% s: m5 H0 c+ N% r+ R* D9 c& [0 m% Y( p. ]: q# l7 I/ }& j
词典是默认的 https://www.urbandictionary.com/define.php?term=%GDWORD% 词典: |9 {7 C8 ^, n1 b4 t2 S- m0 @+ w9 h
GoldenDict 版本 1.5.0-RC2-21-gdb6f369,浏览器是火狐,系统是 win8.1_x64
# e% m7 y' T$ _* n; c- @# g1 K6 S, p; e
除了使用离线版词典,请问各位有什么办法吗?谢谢
  • 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
    1. #!/usr/bin/env python& \, o( H( R  Q6 K2 b' Q" x) i/ J
    2. / k& f5 [- o7 _7 O
    3. # -*- coding: utf-8 -*-$ ?- i# T% y# g0 e( s: b) s+ ?1 K

    4. ; N* v4 A) N3 p- A8 q# I7 `
    5. import sys% |$ g' c9 V8 y3 J! D% h
    6. reload(sys)4 Z5 Q. P  e; N( H( b- b+ M6 q+ D
    7. 5 N7 l$ ~  D4 A  F7 Q
    8. sys.setdefaultencoding('utf-8')
      ; Q+ M2 l( P; P7 o& `9 B% }! ~8 q0 k' _

    9. " Q' S0 x" Z% z9 B
    10. # https://github.com/narfman0/helga-lingo2 L% {" B" d, a- T$ c* L
    11. # H- W3 d0 f$ E8 E
    12. import json, re, urllib, urllib2  _/ S/ w, E/ k4 q' J* c& `
    13. def ud(*args):
      2 v5 L) F; Q, r* T6 ^
    14.   """ Define from urban dictionary """
      ) w( [9 l4 f  @. p7 j- H% e* ^
    15.   if len(args) == 0:
      $ p* D/ T6 C2 O7 h- g- {* B
    16.   return u'You need to give me a term to look up.'
      6 n6 q/ F; a5 e) q$ m: H
    17.   term, index = parse_args(' '.join(args))
      - g% s* Q# [4 N6 S/ n; ?0 u
    18.   try:
      ; G8 h1 o& R+ J  G0 [
    19.   data = execute_request(term)' a4 _/ f! N  ]+ {
    20.   total = len(data['list'])0 W; \8 B" _5 [9 P( a7 G4 j

    21. ( v& {" m! K: l' d! t
    22.   # hack to ignore parameter 'index', print out all definitions+ \: z1 n7 p6 |
    23.   # defn = define(term, data, index)  x( F/ F1 o/ N" G+ u/ Y& `# d1 k  q
    24.   # example = define(term, data, index, 'example')
      3 u+ f, i$ A7 g" G1 Y0 P! F
    25.   # return '{0} e.g.: {1} [{2}/{3}]'.format(defn, example, index, total)
      * v) k( o8 n1 [: X  k2 h5 Y( J
    26.   if total == 0: sys.exit(0); e* I3 L$ z0 I. y) p
    27.   # %20 rendered by urllib.quote: \) ^* f) O! i2 t) M
    28.   result = urllib.unquote(term) + ' (total definitions: ' + str(total) + ')' + '<br><hr>'4 S! m) d( ]( _5 ?: d/ \. c
    29.   # for index in range(1, min(5, total)+1):! a) D& w  @: n8 E
    30.   for index in range(1, total+1):% C' @- l( w9 o. g$ c
    31.   defn = define(term, data, index).replace("\n","<br>")
      0 x" g' W3 m. J
    32.   example = define(term, data, index, 'example').replace("\n","<br>")
      * F6 Q, d% V1 V- w: J) K2 k% ^
    33.   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
    34.   return result
      + v4 C+ U8 L1 F- H' V' a
    35.   # hack end0 S* [( s' x3 V
    36.   except Exception as e:
        ?3 N- O8 Y" F$ ]) B2 y$ k
    37.   return unicode('Urban Dictionary returned exception for ' + term + ":" + str(e))
      % f) r$ `! E5 u* @6 U5 ^( B

    38. $ N6 C3 W9 N- _9 q8 J
    39. def execute_request(term):
      8 u( T3 g, O- D( s. {2 i* |
    40.   """ Invoke API to retrieve json hopefully representing term """
      0 B3 c- t! }; h6 a+ T+ l
    41.   api_url = 'http://api.urbandictionary.com/v0/define?term='" x9 r& D6 `  |8 ^3 ~7 U# j
    42.   # use requests
      ' s6 i0 m& ~4 ^: j1 J/ C2 ~
    43.   # import requests
      , h& q8 v6 u6 u: V# r
    44.   # response = requests.get(api_url + term)
      1 b  I+ y$ k3 ~' L
    45.   # if response.status_code != 200:( j& \3 D# T/ y" ~: q
    46.   # raise Exception('Error status code returned: ' + str(response.status_code))
      ) v5 z5 L* Z; W" p) r9 [5 n7 @3 t
    47.   # response_json = json.loads(response.content)
      8 Z3 N: h" e+ N; K5 h7 l
    48.   # if not response_json:& V/ J8 r8 E0 G$ t
    49.   # raise Exception('Response falsy for given term: ' + term)* g6 k9 u! I! y/ n' o
    50.   # return response_json
      + G1 h5 n6 u* x2 u5 s* U
    51. 0 F+ `# @+ p& c; M' y$ D" ?
    52.   # or use urllib2" l+ s: o2 `0 m
    53.   req = urllib2.Request(api_url + term)
      $ \) T$ ]* A" o
    54.   handler = urllib2.urlopen(req)
      1 N, @5 k- [8 n& j/ k7 p" Z# _' C
    55.   status_code = handler.getcode()) j  q6 j! a$ |8 U1 i( o# a1 @! e. k
    56.   # handler.headers.getheader('content-type')/ @: o) U  ^1 k! x- t- ]
    57.   content = handler.read(). q4 Z- P( Q) d6 K  j! j/ ?
    58.   if status_code != 200:
      / ]: r' i# j6 O, S
    59.   raise Exception('Error status code returned: ' + str(status_code)), P) ]& \7 c- D+ a) a
    60.   response_json = json.loads(content)0 O4 I3 o- k: }( [6 _$ t
    61.   if not response_json:" t) N+ P$ p. Z* H$ l$ O
    62.   raise Exception('Response falsy for given term: ' + term)- V  S6 O, D# r- M) n/ Z2 I  a* T
    63.   return response_json
      9 ?' \; d  w$ |/ `! L5 j0 u! y1 ^

    64. + S/ \0 D5 v2 R* w
    65. def define(term, data, index=1, action='definition'):
      ! L8 P. _" M) h& V8 C" Y% I
    66.   """ Retrieve the definition for the term """8 d9 e9 a9 O, b  @
    67.   return data['list'][index-1][action]. n7 A$ t7 v) ?0 x0 ?. U/ g& J

    68. # a* ~) x7 X+ y
    69. def parse_args(args):
      8 T) ?0 k! o  w) N8 i4 a9 O& V
    70.   """ Parse arguments to extract desired search term and index in def list """: D( ]. c' }  @- ~+ w, w
    71.   index = 1
      9 K1 M" L7 Z8 Q" g: g) r
    72.   match = re.search(r'\d+, args): m  |6 Z3 s0 q( @) }7 R
    73.   if match is not None:( ^* m) q% W, ^  z
    74.   index = int(match.group())
      " C1 ?  r  R* I
    75.   args=args[:-len(str(index))-1]
      8 H+ {# _& I, A. E9 e
    76.   term = urllib.quote(args)4 @' j$ V% B% z
    77.   return (term, index)
        E8 A. \& Z/ G8 `$ k1 A, `

    78. 1 P5 J/ G. w5 u: Y4 I  B6 Q" |
    79. if __name__ == "__main__":/ r' M- C2 h' Q7 y0 E. G; N
    80.   result = ud(sys.argv[1])( q6 D* _2 v" J; L& m+ }
    81.   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

    该用户从未签到

     楼主| 发表于 2016-8-8 00:35:15 | 显示全部楼层
    Launcher1 发表于 2016-8-7 16:580 X) \8 W. U; o& d2 {! @) F
    遇到同样问题……后来在 GitHub 找到了解决办法: B+ J$ u+ A( C; v. T
    - X( d: a: P- ]0 O
    原帖:https://github.com/goldendict/goldendict/issue ...
    : U8 n; g  h0 C& |. q1 v, \$ b
    感谢,有效" n/ J/ J0 e5 T3 ?, H! l( Y/ S

    2 |8 i6 c, S; p8 G建议到原贴复制,这里直接复制是不可以的,会有大量的乱码混入(Ctrl-A 可见)
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

    小黑屋|手机版|Archiver|PDAWIKI |网站地图

    GMT+8, 2024-5-11 18:43 , Processed in 0.036999 second(s), 12 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2023, Tencent Cloud.

    快速回复 返回顶部 返回列表