├── example1.png ├── example2.png ├── example3.png ├── README.md ├── setup.py └── bin └── dict /example1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/np-csu/pydict/HEAD/example1.png -------------------------------------------------------------------------------- /example2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/np-csu/pydict/HEAD/example2.png -------------------------------------------------------------------------------- /example3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/np-csu/pydict/HEAD/example3.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pydict 2 | 3 | 在命令行下使用的英/汉、汉/英字典 4 | 5 | ## 安装 6 | 7 | ```bash 8 | $ git clone https://github.com/np-csu/pydict.git 9 | $ cd pydict 10 | $ python setup.py install 11 | $ cd bin 12 | $ ln dict ~/bin/ 13 | ``` 14 | 15 | ## 如何使用 16 | 17 | ```bash 18 | $ dict [要查询的单词或句子] 19 | ``` 20 | 21 | ## 运行效果 22 | ![example1](example1.png) 23 | ![example2](example2.png) 24 | ![example3](example3.png) 25 | 26 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | try: 2 | from setuptools import setup 3 | except ImportError: 4 | from distutils.core import setup 5 | 6 | 7 | version = '0.1' 8 | 9 | setup( name='pydict', 10 | version=version, 11 | description='An En-Cn Dictionary for Terminal', 12 | author='nipan', 13 | author_email='nipan1988@gmail.com', 14 | url='https://github.com/np-csu/pydict.git', 15 | install_requires=['termcolor'] 16 | ) 17 | 18 | -------------------------------------------------------------------------------- /bin/dict: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #encoding=utf-8 3 | 4 | import urllib2 5 | import sys, getopt 6 | import json 7 | 8 | from termcolor import colored 9 | 10 | URL = "http://fanyi.youdao.com/openapi.do?keyfrom=sasfasdfasf&key=1177596287&type=data&doctype=json&version=1.1&q=" 11 | 12 | def showResult(result): 13 | if "basic" not in result.keys(): 14 | print colored("未找到您查询的单词/句子:" , 'yellow'), 15 | print colored(result["query"], 'red') 16 | print 17 | return 18 | else: 19 | print 20 | print colored('\t\t\t' + result["query"], 'green') 21 | print 22 | if "us-phonetic" in result["basic"] and "uk-phonetic" in result["basic"]: 23 | print '\t', "美音音标:", "[" + result["basic"]["us-phonetic"] + "]", "\t英音音标:", "[" + result["basic"]["uk-phonetic"] + "]" 24 | print 25 | print '\t', "基本释义:" 26 | for explain in result["basic"]["explains"]: 27 | print '\t\t' + explain 28 | print 29 | print '\t', "网络释义:" 30 | for webExp in result["web"]: 31 | print '\t\t%s' % alignString(webExp["key"], 20), 32 | for value in webExp["value"]: 33 | print value + ';', 34 | print 35 | print 36 | 37 | def getResult(word): 38 | try: 39 | word = urllib2.quote(word) 40 | return urllib2.urlopen(URL + word) 41 | except urllib2.URLError as e: 42 | print "错误: 向服务器发送查询请求未成功,请检查网络" 43 | print 44 | sys.exit() 45 | 46 | def parseJson(resp): 47 | return json.loads(resp.read()) 48 | 49 | def alignString(string, length=0): 50 | if length == 0: 51 | return string 52 | 53 | strLen = len(string) 54 | length -= strLen 55 | result = string 56 | placeholder = ' ' 57 | if string[0] >= u'\u4e00' and string[0] <= u'\u9fa5': 58 | #placeholder = u' ' 59 | length -= strLen 60 | else: 61 | pass 62 | #placeholder = ' ' 63 | while length >= 0: 64 | result += placeholder 65 | length -= 1 66 | return result 67 | 68 | def usage(): 69 | print ''' 70 | 使用说明: 71 | dict [需要查询的单词] 72 | 使用dict -h获得帮助 73 | ''' 74 | 75 | def main(): 76 | try: 77 | opts, args = getopt.getopt(sys.argv[1:], "h") 78 | for op, value in opts: 79 | if op == "-h": 80 | usage() 81 | sys.exit() 82 | if len(args) == 0: 83 | usage() 84 | sys.exit() 85 | except getopt.GetoptError as e: 86 | print "错误: 使用了无效的参数-%s" % e.opt 87 | usage() 88 | sys.exit() 89 | 90 | resp = getResult(" ".join(args).lower()) 91 | result = parseJson(resp) 92 | showResult(result) 93 | 94 | if __name__ == '__main__': 95 | main() 96 | --------------------------------------------------------------------------------