79 lines
1.9 KiB
Python
Executable File
79 lines
1.9 KiB
Python
Executable File
#! /usr/bin/env python3
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
import requests
|
|
from bs4 import BeautifulSoup, Tag
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
print("leo.py must be run with at least 1 parameter (word to translate)")
|
|
sys.exit(1)
|
|
|
|
url = 'https://dict.leo.org/englisch-deutsch/' + " ".join(sys.argv[1:])
|
|
r = requests.get(url)
|
|
|
|
soup = BeautifulSoup(r.text, "html.parser")
|
|
|
|
def format_dict_line(s):
|
|
s = re.sub(r"\.([\w\.]+) ", r". (\1) ", s)
|
|
s = s.replace(" - ", ": ")
|
|
|
|
s = re.sub(r"[\s\xa0]+", " ", s)
|
|
|
|
s = re.sub("\u21d4 (\\w+)", r"\1", s)
|
|
|
|
s = s.replace("|", "[", 1)
|
|
s = s.replace("|", "]", 1)
|
|
s = s.replace("|", "[", 1)
|
|
s = s.replace("|", "]", 1)
|
|
s = re.sub(r"\[ ", "[", s)
|
|
s = re.sub(r" \]", "]", s)
|
|
s = s.replace(")[",") [")
|
|
|
|
s = s.strip()
|
|
return s
|
|
|
|
def align(table, delim="|"):
|
|
max_widths = [0] * len(table[0])
|
|
string_table = ""
|
|
for row in table:
|
|
for i, element in enumerate(row):
|
|
if (width := len(element)) > max_widths[i]:
|
|
max_widths[i] = width
|
|
for i, row in enumerate(table):
|
|
for j, element in enumerate(row):
|
|
table[i][j] = element.ljust(max_widths[j])
|
|
string_table += delim.join(table[i]) + "\n"
|
|
return string_table
|
|
|
|
os.system("")
|
|
for tbody in soup.select("table.tblf1.tblf-fullwidth.tblf-alternate"):
|
|
h2_tag = tbody.find("h2")
|
|
if not h2_tag:
|
|
continue
|
|
heading = h2_tag.text
|
|
table = tbody.find("tbody")
|
|
if not table:
|
|
continue
|
|
print("\x1b[33m"+"#"*10, heading, "#"*10+"\x1b[0m")
|
|
leo_entry = []
|
|
for line in table:
|
|
try:
|
|
en = line.select("td[lang=en]")[0].text if isinstance(line, Tag) else ''
|
|
de = line.select("td[lang=de]")[0].text if isinstance(line, Tag) else ''
|
|
if en and de:
|
|
leo_entry.append([format_dict_line(en), format_dict_line(de)])
|
|
except:
|
|
continue
|
|
try:
|
|
print(align(leo_entry, " | "))
|
|
except:
|
|
continue
|
|
|
|
|
|
# print("\x1b[3A")
|
|
sys.exit(0)
|