Skip to content

Commit

Permalink
v0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gökçe committed Apr 8, 2022
1 parent ea6b0fa commit 83f621d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
requires = [line.strip() for line in file.readlines()]

VERSION = m.__version__
DESCRIPTION = "Command-line tool for TDK Dictionary (sozluk.gov.tr) with rich output."
DESCRIPTION = "Command-line tool for TDK Dictionary, sozluk.gov.tr with rich output."

setup(
name="tdk-cli",
version=VERSION,
url="https://github.com/agmmnn/turengcli",
url="https://github.com/agmmnn/tdk-cli",
description=DESCRIPTION,
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
13 changes: 3 additions & 10 deletions tdk_cli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .cli import TDKDict
import argparse

__version__ = "0.0.1"
__version__ = "0.2.1"

# parse arguments
ap = argparse.ArgumentParser()
Expand All @@ -16,14 +16,7 @@
"--plain",
action="store_true",
default=False,
help="returns plain text output",
)
ap.add_argument(
"-f",
"--fuzzy",
action="store_true",
default=False,
help="returns fuzzy search results",
help="returns plain text output.",
)
ap.add_argument("-v", "--version", action="version", version="%(prog)s v" + __version__)
args = ap.parse_args()
Expand All @@ -32,7 +25,7 @@
def cli():
word = " ".join(args.word)
if word == "":
print("Please enter a word.")
print("Kelime giriniz...")
else:
if args.plain:
TDKDict(word).plain()
Expand Down
61 changes: 49 additions & 12 deletions tdk_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
from urllib.parse import quote
from urllib.request import urlopen
from urllib.request import Request, urlopen
from json import loads

from rich import box
from rich.table import Table
from rich.console import Console
from rich import print


Expand All @@ -14,25 +13,63 @@ def __init__(self, word):
self.word = word
# gts, bati, tarama, derleme, atasozu, kilavuz, lehceler
url = "https://sozluk.gov.tr/gts?ara=" + quote(word)
self.data = loads(urlopen(url).read())
req = Request(url, headers={"User-Agent": "Mozilla/5.0"})
self.data = loads(urlopen(req).read())
# print(self.data)
if "error" in self.data:
print("Error")
print(self.data["error"])
exit()

def rich(self):
data = self.data
word = self.word
table = Table(title=word + " - TDK", show_header=False, box=box.SQUARE)
for i in range(len(data)):
table = Table(
box=box.ROUNDED,
show_footer=(
True
if ("atasozu" in data[i]) or (data[i]["birlesikler"] != None)
else False
),
footer_style="grey62",
)
table.add_column(
"[cyan]❯ "
+ data[i]["madde"]
+ (
" (" + data[i]["anlam_gor"] + ")"
if (len(data) > 1 and data[i]["anlam_gor"] != "0")
else ""
)
+ "[/cyan]",
(
(
"Atasözleri, Deyimler veya Birleşik Fiiller:\n"
+ str([i["madde"] for i in data[i]["atasozu"]])[1:-1]
if "atasozu" in data[i]
else ""
)
+ (
"\n\n"
if ("atasozu" in data[i]) and (data[i]["birlesikler"] != None)
else ""
)
+ (
("Birleşik Kelimeler:\n") + data[i]["birlesikler"]
if data[i]["birlesikler"] != None
else ""
)
),
)
# lang
if data[i]["lisan"] != "":
table.add_row(data[i]["lisan"] + ":")
table.add_row(data[i]["lisan"])
else:
# suffix
if data[i]["taki"] != None:
table.add_row(word + ", " + data[i]["taki"] + ":")
else:
table.add_row(word + ":")
table.add_row(word + ", " + data[i]["taki"])
elif (data[i]["telaffuz"] != None) and (data[i]["ozel_mi"] == "1"):
table.add_row("özel, " + data[i]["telaffuz"])
for j in range(len(data[i]["anlamlarListe"])):
# meaning
table.add_row(
Expand All @@ -47,9 +84,9 @@ def rich(self):
+ "”"
)
# space after each item except last one
if len(data) > 1 and i != range(len(data))[1]:
table.add_row("")
print(table)
# if len(data) > 1 and i != range(len(data))[1]:
# table.add_row("")
print(table)

def plain(self):
data = self.data
Expand Down

0 comments on commit 83f621d

Please sign in to comment.