-
Notifications
You must be signed in to change notification settings - Fork 1
/
turenglate.py
executable file
·167 lines (140 loc) · 5 KB
/
turenglate.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
# Turenglate : A terminal program to get Turkish-English or
# English-Turkish translations from the online dictionary tureng.com
# Copyright (C) 2022-2023 Ulvican Kahya aka ulville
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from math import floor
import requests
import urllib.parse
from bs4 import BeautifulSoup
import argparse
import argcomplete
from tabulate import tabulate, multiline_formats
import shutil
from textwrap import wrap
def tureng_phrase_completer(prefix, parsed_args, **kwargs):
url = "https://ac.tureng.co/"
querystring = {"t": urllib.parse.quote(prefix), "l": "entr"}
return requests.get(url, params=querystring).json()
parser = argparse.ArgumentParser(
description="Get Turkish-English or English-Turkish translations from Tureng",
epilog=(
"Hitting <TAB> twice, after typing a couple of characters will get the "
"suggestions. If it only has one suggestion, a single <TAB> will complete "
"the phrase for you."
),
)
parser.add_argument(
"-r",
"--related",
help="show other phrases containing the query phrase",
action="store_true",
)
parser.add_argument(
"-e",
"--english",
help="use english header and category names",
action="store_true",
)
parser.add_argument(
"-s",
"--style",
help="set the style of the table. Hit <TAB> to see the options",
default="fancy_grid",
choices=multiline_formats,
metavar="STYLE",
)
parser.add_argument(
"phrase", nargs="*", help="specify the phrase to translate"
).completer = tureng_phrase_completer
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.english:
lang = "en/turkish-english/"
rel_filter = "other"
else:
lang = "tr/turkce-ingilizce/"
rel_filter = "terimlerle"
query_raw = ""
if args.phrase:
query_raw = " ".join(args.phrase)
query = urllib.parse.quote(query_raw)
else:
parser.print_help()
exit()
base_url = "https://tureng.com/"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0"
}
url = base_url + lang + str(query)
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "lxml")
tables = soup.find_all("table", class_="searchResultsTable")
if tables:
h2s = soup.find_all("h2")
table_format = args.style
terminal_width = shutil.get_terminal_size().columns
max_col_width = floor((terminal_width - 16) / 3)
for i, table in enumerate(tables):
if (not (rel_filter in h2s[i].text)) or args.related:
print()
print(h2s[i].text)
print()
rows = table.find_all("tr")
for ri, row in enumerate(rows):
if "class" in row.attrs.keys() and "termresults-ad-tr" in row["class"]:
rows.pop(ri)
_headers = list(
map(lambda cell: cell.text.strip("\n "), rows[0].find_all("th"))
)
_headers.pop(0)
_headers.pop()
for index, header in enumerate(_headers):
_headers[index] = "\n".join(
list(
map(
lambda header_line: f"\033[01m{header_line}\033[0m",
wrap(header, max_col_width),
)
)
)
tureng_table = []
for rindex, row in enumerate(rows):
cells = list(
map(lambda cell: cell.text.strip("\n "), row.find_all("td"))
)
if (len(cells)) >= 5:
cells.pop(0)
cells.pop()
for index, cell in enumerate(cells):
if rindex % 2 == 0:
cell = f"\033[2m{cell}\033[0m"
# '\n'.join(wrap(cell, max_col_width))
cells[index] = cell
tureng_table.append(cells)
print(
tabulate(
tureng_table,
headers=_headers,
tablefmt=table_format,
maxcolwidths=max_col_width,
)
)
else:
message = soup.find("h1")
print(message.text)
suggestion_ul = soup.find("ul", class_="suggestion-list")
if suggestion_ul:
suggestions = suggestion_ul.find_all("a")
for suggestion in suggestions:
print(suggestion.text)