5
5
from bs4 import BeautifulSoup
6
6
import frames
7
7
import json
8
-
9
-
8
+ import os
9
+ import logging
10
10
class Translater :
11
11
def __init__ (self ,):
12
12
self .main_url = "http://tureng.com/tr/turkce-ingilizce/{}"
13
13
self .subject_dict = {}
14
14
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 )
16
17
def word_crawler (self , word , type ):
17
18
webpage = requests .get (self .main_url .format (word [0 ]))
18
19
content = webpage .content
@@ -55,7 +56,7 @@ def subject_remaker(self, word_t):
55
56
self .subject_dict .update ({counter : i })
56
57
57
58
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' )]
59
60
for i in range (len (data )):
60
61
print (i + 1 , "-)" , data [i ]['query_word' ][0 ])
61
62
@@ -108,8 +109,9 @@ def writer(self, main_word, type, is_history=False):
108
109
else :
109
110
data ["outcome_word1" ] = outcome_list [2 ]
110
111
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 " )
113
115
json .dump (data , outfile , ensure_ascii = False )
114
116
print (frames .frame_last )
115
117
@@ -121,21 +123,22 @@ def writer(self, main_word, type, is_history=False):
121
123
elif sys .argv [1 ] == "en" :
122
124
main .writer (sys .argv [2 :], "tr ts" )
123
125
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' )]
131
135
try :
132
136
get_query = data [int (sys .argv [2 ])]
133
137
except IndexError as ixerr :
134
138
print (ixerr )
135
139
else :
140
+ logging .info (f"Printing Translated Sentence from history { get_query ['query_word' ]} " )
136
141
main .writer (get_query ['query_word' ],
137
142
get_query ['query_type' ],
138
143
is_history = True ,
139
144
)
140
- else :
141
- main .history ()
0 commit comments