Skip to content

Commit

Permalink
CLI is improved to handle exceptions that occur during the translatio…
Browse files Browse the repository at this point in the history
…n phase
  • Loading branch information
mebaysan committed Sep 29, 2022
1 parent 75669b9 commit 81583df
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 40 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
author="@mebaysan",
description="https://tureng.com/en/turkish-english CLI for translation",
name="tureng-cli",
version="0.1.0",
version="0.2.0",
packages=find_packages(include=["tureng_cli", "tureng_cli.*"]),
install_requires=["beautifulsoup4", "click", "requests"],
python_requires=">=3.8",
Expand Down
59 changes: 20 additions & 39 deletions tureng_cli/commands/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
from bs4 import BeautifulSoup
from tureng_cli.utilities.constants import CRACKER_HEADER
from tureng_cli.utilities.helpers import get_turkish_translate_url
from tureng_cli.utilities.translate.helpers import (
extract_translate_header_text,
extract_translate_table_headers,
extract_translate_table_rows,
check_is_there_translation_result
)


@click.command()
@click.option(
Expand All @@ -19,42 +26,16 @@ def translate(word, n_result):
target_url = get_turkish_translate_url(word)
res = requests.get(target_url, headers=CRACKER_HEADER)
soup = BeautifulSoup(res.text, "html.parser")

# get header text
header = soup.find("h2")
header_text = "".join([i for i in header.strings])

# get table
table = soup.find_all("table")[0]
rows = table.find_all("tr")

# separate rows
table_header = rows[0]
table_rows = rows[1:]

# table header
table_headers = table_header.find_all("th")
table_id = "#"
table_category = table_headers[1].string
table_turkish = table_headers[2].string
table_english = table_headers[3].string
table_header_text = " \t".join(
[table_id, table_category, table_turkish, table_english]
)

# table rows
table_rows_texts = []
for table_row in table_rows:
row_columns = table_row.find_all("td")
if len(row_columns) >= 4:
row_id = row_columns[0].string
row_category = row_columns[1].string
row_turkish = row_columns[2].find_all("a")[0].string
row_english = row_columns[3].find_all("a")[0].string
row_text = "\t+ ".join([row_id, row_category, row_turkish, row_english])
table_rows_texts.append(row_text)

# echo outputs
click.echo(header_text)
click.echo(table_header_text)
click.echo("\n".join(table_rows_texts[:n_result]))


if check_is_there_translation_result(soup):
header_text = extract_translate_header_text(soup)
table_header_text = extract_translate_table_headers(soup)
table_rows_texts = extract_translate_table_rows(soup)

# echo outputs
click.echo(header_text)
click.echo(table_header_text)
click.echo("\n".join(table_rows_texts[:n_result]))
else:
click.echo(f'There is no translation for this word: {word}')
1 change: 1 addition & 0 deletions tureng_cli/utilities/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ def bind_commands_to_cli(cli, *args, **kwargs):

def get_turkish_translate_url(word):
return BASE_URL_TR + word

Empty file.
54 changes: 54 additions & 0 deletions tureng_cli/utilities/translate/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
def extract_translate_header_text(soup):
# get header text
header = soup.find("h2")
header_text = "".join([i for i in header.strings])
return header_text


def extract_translate_table_headers(soup):
# get table
table = soup.find_all("table")[0]
rows = table.find_all("tr")

# handle header row
table_header = rows[0]

# table header
table_headers = table_header.find_all("th")
table_id = "#"
table_category = table_headers[1].string
table_turkish = table_headers[2].string
table_english = table_headers[3].string
table_header_text = " \t".join(
[table_id, table_category, table_turkish, table_english]
)
return table_header_text


def extract_translate_table_rows(soup):
# get table
table = soup.find_all("table")[0]
rows = table.find_all("tr")

# handle result rows
table_rows = rows[1:]

# table rows
table_rows_texts = []
for table_row in table_rows:
row_columns = table_row.find_all("td")
if len(row_columns) >= 4:
row_id = row_columns[0].string
row_category = row_columns[1].string
row_turkish = row_columns[2].find_all("a")[0].string
row_english = row_columns[3].find_all("a")[0].string
row_text = "\t+ ".join([row_id, row_category, row_turkish, row_english])
table_rows_texts.append(row_text)

return table_rows_texts


def check_is_there_translation_result(soup):
# get header text
result = soup.find_all("table", class_="searchResultsTable")
return True if len(result) >= 1 else False

0 comments on commit 81583df

Please sign in to comment.