掌上百科 - PDAWIKI

 找回密码
 免费注册

QQ登录

只需一步,快速开始

查看: 5163|回复: 2

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

[复制链接]

该用户从未签到

发表于 2016-7-26 22:16:25 | 显示全部楼层 |阅读模式
本帖最后由 PattiRoberts 于 2016-7-26 22:19 编辑
- v# A8 E; Z1 i/ a+ F8 \) M/ k# x' o# l, U  k
似乎 GoldenDict 的网站词典查询速度要比浏览器要慢很多,关键是 Urban Dictionary 完全不能查询(即使翻了墙),而用浏览器完全没有问题+ j- H" ]  {( z( b0 V' K
7 ?0 {2 `' p) e5 J
词典是默认的 https://www.urbandictionary.com/define.php?term=%GDWORD% 词典
. c( N  l  Y; p9 y  T+ t6 ZGoldenDict 版本 1.5.0-RC2-21-gdb6f369,浏览器是火狐,系统是 win8.1_x64
/ d# z0 D; O8 {6 [. x
' b! r: j6 c! z& c9 a, w' ?除了使用离线版词典,请问各位有什么办法吗?谢谢
  • 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
    1. #!/usr/bin/env python+ X$ U- z( n- G- P0 W% H/ l
    2. # J. ]: `, T8 L1 W2 n7 V
    3. # -*- coding: utf-8 -*-
      6 r. I  A: w5 a

    4. - A& w: g9 r+ d( S7 f
    5. import sys' I. n6 A2 H) l3 _* _/ }
    6. reload(sys)( V  ^1 C: c, p; p
    7. % ~/ J$ a3 d# B, }' k
    8. sys.setdefaultencoding('utf-8')6 x2 R1 F" |0 b3 x+ D
    9. * h6 p9 J8 h% E- W/ W& `4 E' m
    10. # https://github.com/narfman0/helga-lingo6 [; L' V; y0 b' L8 l3 c8 N  ^

    11. 7 z0 Z% f+ }: s* G3 _9 w( d5 r+ @2 U
    12. import json, re, urllib, urllib2
      / P$ g# w5 d; O: ?5 G1 z5 d0 |9 `
    13. def ud(*args):
      5 n/ _4 \* t. v
    14.   """ Define from urban dictionary """
      % R6 m/ O* s' k8 m* i
    15.   if len(args) == 0:/ h% E1 i; a/ {" z0 A, l8 _
    16.   return u'You need to give me a term to look up.'* Z* t/ x# Y7 S* ^$ ^! b
    17.   term, index = parse_args(' '.join(args))$ k& R5 ?# O& V& g9 o) l* D
    18.   try:
      : }+ P) e6 W+ d' N  ]( A( q
    19.   data = execute_request(term)$ ^7 A0 k( u/ N/ _! m
    20.   total = len(data['list'])
      5 Q9 @$ c# V+ k) A- j
    21. ; l. i3 Q9 M$ R' W. H
    22.   # hack to ignore parameter 'index', print out all definitions
      0 i) q6 u9 G8 e/ u. F7 V2 V, c! N
    23.   # defn = define(term, data, index)
      . N( R# T; A$ O" g! d- t- _, U3 ~0 j
    24.   # example = define(term, data, index, 'example')
      9 d& j/ ]2 P! ?
    25.   # return '{0} e.g.: {1} [{2}/{3}]'.format(defn, example, index, total)% `. h0 E$ I3 k! i. J
    26.   if total == 0: sys.exit(0)8 {  D# D9 R5 r1 X7 @
    27.   # %20 rendered by urllib.quote6 ?$ C$ R* ~: ~% c' ^5 Y3 o" g1 n
    28.   result = urllib.unquote(term) + ' (total definitions: ' + str(total) + ')' + '<br><hr>'
      5 u# L6 z$ Z  F- ]
    29.   # for index in range(1, min(5, total)+1):: B$ P6 a/ A9 i& f+ _
    30.   for index in range(1, total+1):9 y6 z3 n+ n9 ~- e$ C2 {
    31.   defn = define(term, data, index).replace("\n","<br>"); [+ u9 i8 |3 n3 P. z: v
    32.   example = define(term, data, index, 'example').replace("\n","<br>")
      9 o/ ]' ~" C0 F$ `: C$ `
    33.   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
    34.   return result
      ) i6 V4 O3 j' L5 N4 [. l- z0 T
    35.   # hack end
      1 J& @  b: v' k& e1 [
    36.   except Exception as e:! Y) r4 e- H* ]1 ]
    37.   return unicode('Urban Dictionary returned exception for ' + term + ":" + str(e))
      : P9 d# W( @% y# z3 ?& ~' G, m6 G

    38. 3 K7 J9 v2 U" V4 d, c+ B8 C/ o' [1 ?
    39. def execute_request(term):% g) m% Y$ V" e5 E: X
    40.   """ Invoke API to retrieve json hopefully representing term """
      " @" j: T, v4 B/ V- D. h
    41.   api_url = 'http://api.urbandictionary.com/v0/define?term='3 u0 B; p' K% U' E+ M7 [
    42.   # use requests4 V4 D: r' q3 F4 }2 N- I
    43.   # import requests% ?# C) M; [+ L  `7 n9 \6 I$ |
    44.   # response = requests.get(api_url + term)
      ! V6 S/ G+ F) R- c
    45.   # if response.status_code != 200:, N! I: W" N' }% \' A  i
    46.   # raise Exception('Error status code returned: ' + str(response.status_code))
      , j4 k2 v9 W+ g) N+ Y- u7 p
    47.   # response_json = json.loads(response.content)
      6 f: T9 i1 v) P( m3 _" A
    48.   # if not response_json:
      * ]  b; P/ ?9 a9 W) w8 Z
    49.   # raise Exception('Response falsy for given term: ' + term), Z, k, ^3 D, }: e/ c& [' w
    50.   # return response_json8 _' M  e* A  O! [+ [0 i0 ~
    51. : y# G& B1 @1 s+ J! ]+ I; W( x' i, z
    52.   # or use urllib2
      / c$ P* T9 M! J7 b1 M- v) N* c
    53.   req = urllib2.Request(api_url + term)- n) q6 x/ K: f' H/ g( A. I! m9 v
    54.   handler = urllib2.urlopen(req)
      ! ]' P- D. k  |& k
    55.   status_code = handler.getcode()
      + N+ H9 I3 l/ P" d  b
    56.   # handler.headers.getheader('content-type')- Z; N# d) E# p7 E
    57.   content = handler.read()  W$ j1 x0 ^- `1 p* r: w
    58.   if status_code != 200:
      ; F: A! |, h8 ?
    59.   raise Exception('Error status code returned: ' + str(status_code))
      0 ]. T# x& V; n! T3 }7 M
    60.   response_json = json.loads(content)+ b1 ]8 u9 n/ d* k7 z" J$ g' R$ V
    61.   if not response_json:: \  u1 s6 |4 U6 o" S# H; ^. q- N  w2 D
    62.   raise Exception('Response falsy for given term: ' + term)3 H3 ?. T; P" d; i. j
    63.   return response_json
      : ^' n+ |4 P  T! @' m9 g& i7 C

    64. 8 z1 h7 k4 T/ h  ~3 B6 d" m
    65. def define(term, data, index=1, action='definition'):9 R! P$ t7 T0 D9 t# H
    66.   """ Retrieve the definition for the term """
      $ @1 q9 o" U4 o3 ^: [# C
    67.   return data['list'][index-1][action]
      , @) H8 R, ~4 B5 x7 Z
    68. " M+ z* E6 h0 w( F- B
    69. def parse_args(args):* V' {9 f7 e9 @1 z
    70.   """ Parse arguments to extract desired search term and index in def list """" M0 q8 i$ n/ k1 Y
    71.   index = 1& B6 P6 O8 z$ b" T: [( _; L
    72.   match = re.search(r'\d+, args)& E$ w, m& V0 f" a
    73.   if match is not None:: x/ u! k, m8 B6 o9 _
    74.   index = int(match.group())4 f% t1 a  W  @& l# Q; P
    75.   args=args[:-len(str(index))-1]! M$ L' x+ T- @6 I% S
    76.   term = urllib.quote(args)5 N3 H1 e5 s% I/ u+ s; h$ \& [
    77.   return (term, index)
      4 }5 W/ ?9 n; U- U+ Y0 E5 _9 v

    78. - e$ p( E+ h3 t1 a( \: D+ T
    79. if __name__ == "__main__":, z9 o$ M) q5 }  {7 Q
    80.   result = ud(sys.argv[1])
      8 z; q3 N2 ^  h4 I8 }
    81.   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

    该用户从未签到

     楼主| 发表于 2016-8-8 00:35:15 | 显示全部楼层
    Launcher1 发表于 2016-8-7 16:58
      q; B  x% |; D+ Y遇到同样问题……后来在 GitHub 找到了解决办法
    , ~, H- T& m1 n* _" p/ T) }( E$ S# c5 R& ^+ s4 y! x$ n; N5 \3 l
    原帖:https://github.com/goldendict/goldendict/issue ...

    - @( u2 ?! A$ u: w, E$ r感谢,有效6 s4 T) C; \8 ~) }- n% d; O
    ' F. M- V2 i' ]0 Z' D( a
    建议到原贴复制,这里直接复制是不可以的,会有大量的乱码混入(Ctrl-A 可见)
    您需要登录后才可以回帖 登录 | 免费注册

    本版积分规则

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

    GMT+8, 2025-7-3 03:56 , Processed in 0.022361 second(s), 24 queries .

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

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