Skip to content
This repository was archived by the owner on Sep 12, 2022. It is now read-only.

Commit d8c8eb0

Browse files
author
realveli24@gmail.com
committed
+Add: Minor Logging
+Bug Fix: json data pathway is fixed +Bug Fix: Now Json Write module dont break the json structure
1 parent a12c987 commit d8c8eb0

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Pytureng/tureng.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
from bs4 import BeautifulSoup
66
import frames
77
import json
8-
9-
8+
import os
9+
import logging
1010
class Translater:
1111
def __init__(self,):
1212
self.main_url = "http://tureng.com/tr/turkce-ingilizce/{}"
1313
self.subject_dict = {}
1414
self.mytds = ""
15-
15+
self.json_file_name = "data.json"
16+
self.json_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.json_file_name)
1617
def word_crawler(self, word, type):
1718
webpage = requests.get(self.main_url.format(word[0]))
1819
content = webpage.content
@@ -55,7 +56,7 @@ def subject_remaker(self, word_t):
5556
self.subject_dict.update({counter: i})
5657

5758
def history(self):
58-
data = [json.loads(line) for line in open('data.json', 'r')]
59+
data = [json.loads(line) for line in open(self.json_file_path, 'r')]
5960
for i in range(len(data)):
6061
print(i+1, "-)", data[i]['query_word'][0])
6162

@@ -108,8 +109,9 @@ def writer(self, main_word, type, is_history=False):
108109
else:
109110
data["outcome_word1"] = outcome_list[2]
110111
data["outcome_subject1"] = outcome_list[3]
111-
with open('data.json', 'a', encoding="utf8") as outfile:
112-
outfile.write("\n")
112+
with open(self.json_file_path, 'a', encoding="utf8") as outfile:
113+
if os.stat(self.json_file_path).st_size != 0:
114+
outfile.write("\n")
113115
json.dump(data, outfile, ensure_ascii=False)
114116
print(frames.frame_last)
115117

@@ -121,21 +123,22 @@ def writer(self, main_word, type, is_history=False):
121123
elif sys.argv[1] == "en":
122124
main.writer(sys.argv[2:], "tr ts")
123125
elif sys.argv[1] == "history":
124-
if sys.argv[2]:
125-
try:
126-
int(sys.argv[2])
127-
except ValueError:
128-
print("Unvalid Paramater.")
129-
else:
130-
data = [json.loads(line) for line in open('data.json', 'r')]
126+
try:
127+
sys.argv[2]
128+
except IndexError:
129+
logging.info("History Log Printing.. ")
130+
main.history()
131+
else:
132+
if sys.argv[2]:
133+
print("sysargv2")
134+
data = [json.loads(line) for line in open(main.json_file_path, 'r')]
131135
try:
132136
get_query = data[int(sys.argv[2])]
133137
except IndexError as ixerr:
134138
print(ixerr)
135139
else:
140+
logging.info(f"Printing Translated Sentence from history {get_query['query_word']}")
136141
main.writer(get_query['query_word'],
137142
get_query['query_type'],
138143
is_history=True,
139144
)
140-
else:
141-
main.history()

0 commit comments

Comments
 (0)