├── .gitignore ├── README.md ├── example.png ├── setup.py └── termdic ├── __init__.py └── termdic.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | # https://github.com/github/gitignore/blob/master/Python.gitignore 4 | *.py[cod] 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Packages 10 | *.egg 11 | *.egg-info 12 | dist 13 | build 14 | eggs 15 | parts 16 | bin 17 | var 18 | sdist 19 | develop-eggs 20 | .installed.cfg 21 | lib 22 | lib64 23 | __pycache__ 24 | 25 | # Installer logs 26 | pip-log.txt 27 | 28 | # Unit test / coverage reports 29 | .coverage 30 | .tox 31 | nosetests.xml 32 | 33 | # Translations 34 | *.mo 35 | 36 | # Mr Developer 37 | .mr.developer.cfg 38 | .project 39 | .pydevproject 40 | 41 | mcms/db.sqlite3 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # termdic 2 | 3 | 4 | 5 | 基于python的命令行查词工具, 使用有道api。 6 | 7 | ## Installation 安装 8 | 9 | >sudo pip install termdic 10 | 11 | ## Usage 使用 12 | 13 | >tdic word 14 | 15 | >[wɜːd] 16 | 17 | >n. [语] 单词;话语;消息;诺言;命令 18 | 19 | >vt. 用言辞表达 20 | 21 | >n. (Word)人名;(英)沃德 22 | 23 | 查询单词并发音 24 | 25 | >tdic word -p 26 | 27 | 查询版本号 28 | 29 | >tdic -v 30 | 31 | 查看帮助 32 | >tdic -h 33 | 34 | -------------------------------------------------------------------------------- /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hzwer/termdic/1ddbec8235afd0afa6c2ecba3b15968373911cea/example.png -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from setuptools import setup, find_packages 4 | 5 | version = '0.1.3' 6 | 7 | setup( 8 | name = 'termdic', 9 | version = version, 10 | author = 'hzwer', 11 | author_mail = '598460606@163.com', 12 | url = 'https://github.com/hzwer/termdic', 13 | description = 'Dictionary in your terminal', 14 | keywords = 'dictionary, terminal', 15 | packages = find_packages(), 16 | include_package_data = True, 17 | zip_safe = False, 18 | install_requires = [ 19 | 'requests', 20 | 'termcolor', 21 | ], 22 | entry_points = { 23 | 'console_scripts': [ 24 | 'tdic = termdic.termdic:main' 25 | ] 26 | }, 27 | ) 28 | -------------------------------------------------------------------------------- /termdic/__init__.py: -------------------------------------------------------------------------------- 1 | import termdic 2 | __version__ = '0.1.3' 3 | -------------------------------------------------------------------------------- /termdic/termdic.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import os 4 | import re 5 | import sys 6 | import argparse 7 | import requests 8 | from termcolor import colored as cl 9 | 10 | # from termdic import __version__ 11 | __version__ = '0.1.3' 12 | 13 | def look_up(word): 14 | url = 'http://dict.youdao.com/search?q=' 15 | r = requests.get(url + word) 16 | content = r.text 17 | 18 | try: 19 | ps = re.findall(re.compile('"phonetic">(.*?)'), content) 20 | pattern = re.compile('"trans-container">(.*?)', re.S) 21 | tmp = re.findall(pattern, content) 22 | mean = re.findall(re.compile('
  • (.*?)
  • '), tmp[0]) 23 | except: 24 | print('Can\'t find it') 25 | return 26 | 27 | if len(ps) == 2: 28 | print(cl(u'英{0} 美{1}'.format(ps[0], ps[1]), 'cyan')) 29 | else: 30 | try: 31 | print(cl(ps[0], 'cyan')) 32 | except: 33 | pass 34 | for line in mean: 35 | words = line.split('.', 1) 36 | if len(words) is 2: 37 | words[0] += '.' 38 | print(u'{0}{1}'.format(cl(words[0], 'green'), cl(words[1], 'blue'))) 39 | # word 40 | else: 41 | print(u'{0}'.format(cl(words[0], 'blue'))) 42 | # phrase 43 | 44 | 45 | def main(): 46 | parser = argparse.ArgumentParser() 47 | parser.add_argument('-p', '--pronounce', 48 | action='store_true', 49 | help='pronounce the word') 50 | parser.add_argument('-v', '--version', 51 | action='version', 52 | version='termdic v{}'.format(__version__)) 53 | parser.add_argument('word', type=str, nargs='+') 54 | args = parser.parse_args() 55 | word = " ".join(args.word) 56 | 57 | if word: 58 | look_up(word) 59 | if args.pronounce: 60 | try: 61 | if sys.platform == 'darwin': 62 | os.system('say ' + str(word)) 63 | else: 64 | os.system('espeak ' + str(word)) 65 | except: 66 | pass 67 | 68 | if __name__ == '__main__': 69 | main() 70 | --------------------------------------------------------------------------------