Last active
January 23, 2021 10:15
-
-
Save Atreyagaurav/9c82b0ce0c82176a8a33ae08ae0b9a8f to your computer and use it in GitHub Desktop.
Automated Translation ffrom jp to en
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"「":"\"", | |
"」":"\"", | |
"『":"\"", | |
"』":"\"", | |
"―":"-" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrgFile(): | |
def __init__(self,filename): | |
self.filename = filename | |
self.chapters = [] | |
def add_chapter(self,chapname=None,chapcontent=None): | |
with open(self.filename,'a') as writer: | |
writer.write(f'\n* {chapname}\n') | |
writer.write(chapcontent.replace('\n','\n\n')) | |
self.chapters.append(chapname) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import make_orgs | |
import re | |
from string import Template | |
# first_chap = 114,189 # v4 | |
chapters_rng = range(503,505) #arc 7 | |
# rezero = n2267be | |
chap_url = Template("http://ncode.syosetu.com/n2267be/${chapter}/") | |
headers = { | |
'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0" | |
} | |
def get_soup(url): | |
print(f'connecting...{url}') | |
r = requests.get(url, headers = headers) | |
print(':Connected:') | |
return BeautifulSoup(r.text,'html.parser') | |
def add_chapter(org_file,chap_no): | |
soup = get_soup(chap_url.substitute(chapter=chap_no)) | |
entry = soup.find('div',{'id':'novel_contents'}) | |
chapname = entry.find('p',{'class':'novel_subtitle'}) | |
content = entry.find('div',{'id':'novel_honbun'}) | |
chaptitle = chapname.text | |
contents = content.text | |
org_file.add_chapter(chaptitle,contents) | |
print(f'Completed::{chap_no}-{chaptitle}') | |
org = make_orgs.OrgFile('./rezero/arc7/arc-7-jp.org') | |
for chap in chapters_rng: | |
add_chapter(org,chap) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyautogui as auto | |
import pyperclip as pc | |
import time | |
import math | |
import json | |
import re | |
copy, paste = pc.determine_clipboard() | |
try: | |
delay_sec = int(input('Enter Delay:<10>')) | |
except ValueError: | |
delay_sec = 10 | |
wrd = "./rezero/arc7/" | |
input_file = f'{wrd}/arc-7-jp.org' | |
replaced_file = f'{wrd}/arc7-rep.org' | |
output_file = f'{wrd}/arc7-deepl-en.org' | |
replacements = './rezero/replacements.json' | |
input(f"Continue with input file: {input_file}") | |
def replace_words(): | |
with open(input_file,'r') as r: | |
l = r.read() | |
if replacements=='': | |
rep = dict() | |
else: | |
with open(replacements,'r') as r: | |
rep = json.load(r) | |
l = re.sub(r'\n\n+','\n\n',l) | |
for k,v in rep.items(): | |
l = l.replace(k,v) | |
with open(replaced_file,'w') as w: | |
w.write(l) | |
def automate(delay_sec = 10, start_line = 0, end_line=math.inf): | |
time.sleep(delay_sec) | |
r = open(replaced_file,'r') | |
lines = '' | |
for i in range(start_line): | |
next(r) | |
i = start_line | |
while i<end_line: | |
try: | |
new_line = f'{next(r)}\n' | |
except StopIteration: | |
end_line = i | |
new_line = '' | |
# python's character count and deepl's count seems to be different. | |
if (len(lines)+len(new_line))>4500 or i==end_line: | |
process_line(lines, comment=f'{i}-{len(lines)}-{time.time()}') | |
print(f'{i} lines processed') | |
lines = new_line | |
else: | |
lines += new_line | |
i+=1 | |
r.close() | |
def process_line(lines, comment=None): | |
copy(lines) | |
time.sleep(0.1) | |
try: | |
auto.click(300,350) | |
time.sleep(0.5) | |
auto.hotkey('ctrl','a') | |
time.sleep(0.5) | |
auto.hotkey('ctrl','v') | |
time.sleep(20) | |
auto.scroll(10) | |
time.sleep(10) | |
#didn't work | |
# while True: | |
# time.sleep(1) | |
# if not auto.locateOnScreen('./screenshot.png'): | |
# break | |
auto.click(1000,350) | |
time.sleep(0.5) | |
auto.hotkey('ctrl','a') | |
translated = paste(primary=True) | |
if comment: | |
save_translated(f'# c:{comment}\n{translated}') | |
else: | |
save_translated(translated) | |
except auto.FailSafeException: | |
raise | |
def save_translated(lines): | |
with open(output_file,'a') as w: | |
w.write(lines) | |
replace_words() | |
automate(delay_sec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment