掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 5097|回复: 2

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

[复制链接]

该用户从未签到

发表于 2016-7-26 22:16:25 | 显示全部楼层 |阅读模式
本帖最后由 PattiRoberts 于 2016-7-26 22:19 编辑
, r4 S: R) \3 f% l* D! X) g
) ]/ V1 w5 K# w3 U, Q* B/ e& z/ J似乎 GoldenDict 的网站词典查询速度要比浏览器要慢很多,关键是 Urban Dictionary 完全不能查询(即使翻了墙),而用浏览器完全没有问题+ Q4 C5 V: E  x/ u
5 M( M. j/ ^0 V2 [, e  x. @
词典是默认的 https://www.urbandictionary.com/define.php?term=%GDWORD% 词典
( Y1 `! j+ |, i2 x. T4 kGoldenDict 版本 1.5.0-RC2-21-gdb6f369,浏览器是火狐,系统是 win8.1_x64
& R; p$ V$ T4 A' K9 ]
% _5 a# A" X" k6 u; b& u# T除了使用离线版词典,请问各位有什么办法吗?谢谢
  • 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
    1. #!/usr/bin/env python
      0 a$ J- i6 D) I! S( F5 m! B! F

    2. ' k: H4 A, N/ a5 T  r/ ^( p
    3. # -*- coding: utf-8 -*-5 u" s/ {# h6 Y* j6 T! d
    4. . P  ]: l8 r5 I
    5. import sys
      % t- N* Z8 U: E, m" _$ I
    6. reload(sys)9 j2 T3 e4 b: x9 R& |

    7. 4 r0 n0 w3 e' y6 Q" J7 [7 Y& O1 ^
    8. sys.setdefaultencoding('utf-8')
      ( x* b+ r9 d9 V: l/ x6 C
    9. ) V( T3 y! a5 [+ F
    10. # https://github.com/narfman0/helga-lingo
      8 q7 G9 ^5 x3 M' K/ I: N
    11. ( ^: ]* B9 @( @) M) x
    12. import json, re, urllib, urllib2$ X. e2 l! @0 F
    13. def ud(*args):, z1 C5 x2 S0 A1 ~/ I  }0 n
    14.   """ Define from urban dictionary """: R+ w+ I2 i& P: _' X. X
    15.   if len(args) == 0:3 b5 S* @) f" Y/ l# x2 F/ ?
    16.   return u'You need to give me a term to look up.'
      2 H6 e9 ~2 L& i+ r0 C
    17.   term, index = parse_args(' '.join(args))
      ) L2 X! R$ Y* g- H
    18.   try:
      3 \: l5 t. I( E7 J! {' P1 {+ H
    19.   data = execute_request(term), j! c/ H3 u6 z, K' J& f' |$ ]
    20.   total = len(data['list'])2 |% n# t1 M" o) u4 M1 X
    21. ) ]2 H6 B* ]' h# G/ i
    22.   # hack to ignore parameter 'index', print out all definitions* y- I6 q! Z' l$ D. q) a, B) e# n
    23.   # defn = define(term, data, index)# e% q& {; v2 `! |( \  p. N
    24.   # example = define(term, data, index, 'example')
      ; Z% f/ F! Z) e1 e% \
    25.   # return '{0} e.g.: {1} [{2}/{3}]'.format(defn, example, index, total)
      # _7 l& l$ Z. n: D/ [
    26.   if total == 0: sys.exit(0)* p$ o/ _4 P  f* T3 O
    27.   # %20 rendered by urllib.quote
      - r: c. Q% Y& u7 i8 ~/ y/ K
    28.   result = urllib.unquote(term) + ' (total definitions: ' + str(total) + ')' + '<br><hr>': ^& ?; v+ X+ ^2 {' V. O
    29.   # for index in range(1, min(5, total)+1):
      * l6 ]7 m2 w# M; R3 ]; ]
    30.   for index in range(1, total+1):! ^; V# t8 \  T- W0 I$ I& w
    31.   defn = define(term, data, index).replace("\n","<br>")) S. ]$ F7 g( F& V. O% D) Y
    32.   example = define(term, data, index, 'example').replace("\n","<br>")9 ^, `, e" l) w; e7 b5 R
    33.   result += '<b>{2}. </b>{0}<br><font color=grey>{1}</font><br><br>'.format(defn, example, index)
      3 \. ~* Z' a# a& Z2 q8 W
    34.   return result
        g+ g" j; r; Y+ M0 K
    35.   # hack end
      1 \% b9 m# t+ p9 I  F5 e
    36.   except Exception as e:
      2 D/ v4 l" n6 x& A5 X
    37.   return unicode('Urban Dictionary returned exception for ' + term + ":" + str(e))- c% Q1 q3 x: R0 D) ?- e
    38. 4 f" P- |1 Y! D3 c: z- w( J1 r$ z
    39. def execute_request(term):! Z" {9 r9 U5 n" o9 t; i
    40.   """ Invoke API to retrieve json hopefully representing term """
      + H* X/ W. g9 M* E# |$ r/ O8 O5 Y
    41.   api_url = 'http://api.urbandictionary.com/v0/define?term='8 w5 t4 [/ n0 c
    42.   # use requests
      4 o* ^* _* E% Q# ?
    43.   # import requests
      # k" ~, e& a" S9 c2 L  g
    44.   # response = requests.get(api_url + term)
      ) o4 ]6 K% e0 A+ W, }9 V
    45.   # if response.status_code != 200:. M) I" n- s: y$ ~! L+ i0 h
    46.   # raise Exception('Error status code returned: ' + str(response.status_code))0 g4 L$ N2 R  D( Z
    47.   # response_json = json.loads(response.content)
      & a& O% S8 P1 S+ ]8 J' s6 |; y
    48.   # if not response_json:
      . G; Z' u6 v! y. h2 M- o4 |2 ~
    49.   # raise Exception('Response falsy for given term: ' + term): f. o* r0 J, A) \" e9 G
    50.   # return response_json9 \, c# F7 X# f( z

    51.   T7 Z; m3 a# w& Q; _3 K! W( ?. v6 P
    52.   # or use urllib2
      , r# B/ _$ W" z4 L  K4 |
    53.   req = urllib2.Request(api_url + term)7 n- C! [# \  S& p+ K
    54.   handler = urllib2.urlopen(req)6 F2 ]/ h$ {5 ]7 }0 ^
    55.   status_code = handler.getcode()
      % X* p4 I8 B4 ]; D6 K5 S
    56.   # handler.headers.getheader('content-type')
      / F7 D- @4 A5 K9 Y8 E
    57.   content = handler.read()
      . N" H' A/ k% g
    58.   if status_code != 200:
      1 J3 O& I+ F3 K) p: m% J
    59.   raise Exception('Error status code returned: ' + str(status_code))
      " H0 E" x. t4 c# o. x4 E) W
    60.   response_json = json.loads(content). n# V' _# c. w0 O* M
    61.   if not response_json:
      & h4 a/ U7 X+ F* L1 C! T. V
    62.   raise Exception('Response falsy for given term: ' + term)" U8 }: B( s% |& O1 U
    63.   return response_json5 u$ W  q4 a% F# d+ C

    64. - M9 T* |: n4 E) g
    65. def define(term, data, index=1, action='definition'):
      $ B, p7 Y: O7 C# J7 \! U* P3 [9 h
    66.   """ Retrieve the definition for the term """; t- v$ ~) t7 C) G4 l. w% ^7 U
    67.   return data['list'][index-1][action]
      / U  F5 c( L# P) O! |2 X8 B
    68. , I" D- L* N5 f
    69. def parse_args(args):+ j1 K& m0 f' \, ?, r& P7 d
    70.   """ Parse arguments to extract desired search term and index in def list """
      - W1 n3 u1 a* _7 N; ?
    71.   index = 1# c, z5 h, i; q6 ^
    72.   match = re.search(r'\d+, args)
      8 r; o: D% s4 M
    73.   if match is not None:+ Q0 p( ]$ T  x- g' B6 e7 _
    74.   index = int(match.group())* v& R* M4 m" d# i9 W
    75.   args=args[:-len(str(index))-1]
      ; b/ j* X+ B; \$ B3 {. o" Z
    76.   term = urllib.quote(args)
      3 I2 K: Y5 K/ B" p
    77.   return (term, index)" j  ?( m& t. V& g9 E& a- {
    78. # L; v$ b" |/ k5 _# i8 \- p6 ?, ~
    79. if __name__ == "__main__":! O2 V! V) U- D
    80.   result = ud(sys.argv[1]). A1 _) X8 i! ~8 I# m3 f, |
    81.   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

    该用户从未签到

     楼主| 发表于 2016-8-8 00:35:15 | 显示全部楼层
    Launcher1 发表于 2016-8-7 16:58& l* Y; D! H5 V, }: h" Y
    遇到同样问题……后来在 GitHub 找到了解决办法
    ! m$ b4 ?/ A' h/ a: k' i$ z7 _: R
    1 L5 P" L; Z* g  d原帖:https://github.com/goldendict/goldendict/issue ...

    $ I5 P* Y; O& Y9 `感谢,有效
    , h8 E1 {$ m: E5 w- d  v" E* n- L2 z! w  k
    建议到原贴复制,这里直接复制是不可以的,会有大量的乱码混入(Ctrl-A 可见)
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

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

    GMT+8, 2025-5-6 04:09 , Processed in 0.023024 second(s), 24 queries .

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

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