-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmdfile-translator.py
61 lines (46 loc) · 2.03 KB
/
mdfile-translator.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
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
DIR_PATH = r'./output'
# if directory exists
if os.path.exists(DIR_PATH) and os.path.isdir(DIR_PATH):
print('[Info]: Directory mentioned in DIR_NAME exists. ')
# Iterating over all the files in directory
for filename in os.listdir(DIR_PATH):
browser = webdriver.Chrome()
browser.get(('https://translate.google.co.in/'))
print('[Info]: Working in file := ' + filename)
_, fileExt = os.path.splitext(filename)
# Only work in markdown files.
if fileExt == '.md':
translatedFile = []
with open(DIR_PATH+'/'+filename, 'r', encoding="utf-8") as fileRead:
lines = []
ct = 0
tobetranslatedbox = browser.find_element_by_id('source')
str = ""
for line in fileRead:
str = str + line
#print(str)
original_text = str.splitlines()
ctx = 0
bigy = original_text
# passing the original text.
tobetranslatedbox.send_keys(str)
time.sleep(5)
# This element holds the translated texts.
translated_text = browser.find_element_by_css_selector('span.tlid-translation.translation').text
translated_text = translated_text.splitlines()
bigx = translated_text
towrite = '|DE|EN|\n|---|---|\n'
for i in range(len(bigx)):
towrite = towrite + '|' + bigy[i] + '|' + bigx[i] + '|\n'
with open(DIR_PATH+'/'+filename, 'w', encoding="utf-8") as fw:
fw.writelines(towrite)
fw.close()
browser.quit()
else:
print('[Error]: Directory mentioned in DIR_NAME doesn\'t exist.')