-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·66 lines (59 loc) · 2.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# coding=utf-8
from __future__ import print_function
import os
import sys
import json
import requests
import argparse
from datetime import datetime
from collections import Counter
HISTORY_FILE = '/var/tmp/t/history.json'
file_dir = os.path.split(HISTORY_FILE)[0]
if not os.path.isdir(file_dir):
os.makedirs(file_dir)
if not os.path.isfile(HISTORY_FILE):
os.system('echo "{}" > %s' % HISTORY_FILE)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='一个命令行的翻译工具')
parser.add_argument('words', nargs='*', type=str, help='一个英文单词或语句')
parser.add_argument('-t', nargs='?', type=int, default=0, help='top, 按词频查看单词')
parser.add_argument('-l', nargs='?', type=int, default=0, help='list, 查看翻译过的单词')
args = parser.parse_args()
words = ' '.join(args.words)
history = json.load(open(HISTORY_FILE, 'r'))
if words:
dictionary = history.get('dictionary', {})
history_list = history.get('history', [])
json.dump(history, open(HISTORY_FILE, 'w'))
data = dictionary.get(words)
if not data:
response = requests.get('http://fanyi.youdao.com/openapi.do?keyfrom=HTransPlugin&key=260187501&type=data&doctype=json&version=1.1&q=%s' % words)
data = response.json()
dictionary[words] = data
history['dictionary'] = dictionary
history_list.append({
'words': words,
'datetime': str(datetime.now())
})
history['history'] = history_list
json.dump(history, open(HISTORY_FILE, 'w'))
if 'translation' in data:
print('翻译:')
print('\n'.join(map(lambda s: '\t' + s, data['translation'])))
if 'basic' in data:
print('\n说明:')
print('\n'.join(map(lambda s: '\t' + s, data['basic']['explains'])))
elif args.t != 0:
top_num = args.t or 20
history_list = history.get('history', [])
counter = Counter([i.get('words') for i in history_list])
for i, item in enumerate(counter.most_common(top_num)):
print('\t%s:%s\t%s次'%(str(i + 1), item[0], item[1]))
elif args.l != 0:
list_len = args.l or 20
history_list = history.get('history', [])[::-1][:list_len]
for item in history_list:
print('\t', item.get("words"))
else:
print('请输入一个单词,或者一个短语')