-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdictcc.py
executable file
·109 lines (86 loc) · 3.48 KB
/
dictcc.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python3
import sys
import requests
import argparse
import os
import textwrap
from tabulate import tabulate
from bs4 import BeautifulSoup
_, columns = os.popen('stty size', 'r').read().split()
requests_session = requests.Session()
def request(word, f, t):
header = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0'}
payload = {'s': word}
try:
r = requests_session.get(
"https://{}{}.dict.cc/".format(f, t),
headers=header,
params=payload)
except Exception as exeption:
print(e)
exit(1)
return r.content
def parse_single_tag(tag):
str_tag = " ".join([a_tag.text for a_tag in tag.find_all('a')])
if (tag.dfn):
all_dfn = ", ".join([dfn_tag.text for dfn_tag in tag.find_all('dfn')])
str_tag = ' '.join([str_tag, '(' + all_dfn + ')'])
return '\n '.join(textwrap.wrap(str_tag, (int(columns) - 8) / 2))
def parse_response(html):
soup = BeautifulSoup(html, 'html.parser')
data = [tag for tag in soup.find_all('td', 'td7nl')]
raw_from_to = zip(data[::2], data[1::2])
res_from_to = list()
for f, t in raw_from_to:
res_from_to.append([parse_single_tag(f), parse_single_tag(t)])
return res_from_to
def parse_suggestions(html):
soup = BeautifulSoup(html, 'html.parser')
data = [tag.a.text for tag in soup.find_all('td', 'td3nl') if tag.a]
return data
def handle_translation(word, primary_lang, secondary_lang):
c = request(word, primary_lang, secondary_lang)
data = parse_response(c)
if data:
print(tabulate(data, [primary_lang, secondary_lang], tablefmt='orgtbl'))
else:
print(' '.join(["No translation found for:", word]))
suggestions = parse_suggestions(c)
print('\nHere are suggestions given by dict.cc:')
for s in suggestions:
print(" - {}".format(s))
def main(args):
if not args.console and args.word:
handle_translation(args.word[0], args.prim, args.sec)
elif args.console:
print('Starting console')
print('Enter your words for translation')
print('Enter q for exit')
user_input = input('>> ')
while user_input != 'q':
handle_translation(user_input, args.prim, args.sec)
user_input = input('>> ')
if __name__ == '__main__':
prim = ['de', 'en']
sec = ['bg', 'bs', 'cs', 'da', 'el', 'eo', 'es', 'fi', 'fr', 'hr',
'hu', 'is', 'it', 'la', 'nl', 'no', 'pl', 'pt', 'ro', 'ru', 'sk',
'sq', 'sr', 'sv', 'tr']
all_dict = prim + sec
parser = argparse.ArgumentParser(
description='Query dict.cc for a translation.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-p', '--prim', type=str, default='en', help='Primary language')
parser.add_argument('-s', '--sec', type=str, default='de', help='Secondary language')
parser.add_argument('-c', '--console', action='store_true')
parser.add_argument('word', nargs=argparse.REMAINDER, help='word to translate')
args = parser.parse_args()
if not args.prim in prim:
print("Primary lang must be in : [" + ", ".join(prim) + "]")
exit(1)
if not args.sec in all_dict:
print("Secondary lang must be in : [" + ", ".join(all_dict) + "]")
exit(1)
if args.prim == args.sec:
print("Given languages must be different. Given : \"{}\" and \"{}\"".format(args.prim, args.sec))
exit(1)
main(args)