-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathde
More file actions
executable file
·57 lines (47 loc) · 1.72 KB
/
de
File metadata and controls
executable file
·57 lines (47 loc) · 1.72 KB
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
#!/usr/bin/python3
from lxml import html
import argparse, sys, requests
# colorization
if sys.stdout.isatty():
normal = '\033[00m'
green = '\033[92m'
blue = '\033[94m'
else:
normal = ''
green = ''
blue = ''
# arguments
subargs = {};
langs = {
'en':'englisch',
'fr':'franz%C3%B6sisch',
'es':'spanisch',
'it':'italienisch',
'ch':'chinesisch',
'ru':'russisch',
'pt':'portugiesisch',
'pl':'polnisch'
}
parser = argparse.ArgumentParser(description='Translate a word from or to german with dict.leo.org.')
parser.add_argument('lang', choices=langs.keys(), help='language to translate from or to german')
parser.add_argument('word', help='word(s) to translate')
args = parser.parse_args()
# call
url = 'https://dict.leo.org/%s-deutsch/%s' % (langs[args.lang], args.word);
r = requests.get(url)
if r.status_code == 200:
content = html.fromstring(r.content)
for section in content.xpath('//*[@data-dz-role="section"]/@data-dz-name'):
print(content.xpath('//*[@data-dz-name="%s"]//h2/text()' % section)[0])
for entry in content.xpath('//*[@data-dz-name="%s"]//*[@data-dz-ui="dictentry"]/@data-dz-rel-uid' % section):
ol = ' '.join(content.xpath('//*[@data-dz-name="%s"]//*[@data-dz-rel-uid="%s"]//*[@lang="%s"]//text()' % (section, entry, args.lang)))
de = ' '.join(content.xpath('//*[@data-dz-name="%s"]//*[@data-dz-rel-uid="%s"]//*[@lang="de"]//text()' % (section, entry)))
ol = ' '.join(ol.split())
de = ' '.join(de.split())
print(' %s%s%s%s -- %s%s%s' % (green, ol, normal, ' ' * (30 - len(ol)), blue, de, normal))
elif r.status_code == 404:
print('Error: %s not found' % args.word, file=sys.stderr)
sys.exit(1)
else:
print('Error: dict.leo.org returned status code %i' % r.status_code, file=sys.stderr)
sys.exit(1)