Skip to content

Commit

Permalink
limit maxium lines per api rquest
Browse files Browse the repository at this point in the history
  • Loading branch information
scrpr committed Mar 7, 2024
1 parent 1388086 commit db079cd
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import json
import os
import shutil
import requests
from requests.exceptions import RequestException

def cleanup_ass_file(input_file, output_file):
with open(input_file, encoding='utf-8-sig', mode='r') as f:
Expand All @@ -14,34 +16,42 @@ def cleanup_ass_file(input_file, output_file):
with open(output_file, encoding='utf-8-sig', mode='w+') as f_out:
subs.dump_file(f_out)

def traditionalize_text(input_text, user_pre_replace=""):
def traditionalize_text(input_text, user_pre_replace="", timeout=5, max_tries=3):
url = "https://api.zhconvert.org/convert"
data = {
"text": input_text,
"converter": "Taiwan",
"userPreReplace": user_pre_replace
}
response = requests.post(url, data=data)

try:
response = requests.post(url, data=data)
response.raise_for_status()
result = response.json()
if "data" in result and "text" in result["data"]:
return result["data"]["text"]
else:
return "Error: Unexpected response format"
except requests.exceptions.RequestException as e:
return "Error: {}".format(e)
for _ in range(max_tries):
try:
response = requests.post(url, data=data, timeout=timeout)
response.raise_for_status()
result = response.json()
if "data" in result and "text" in result["data"]:
return result["data"]["text"]
else:
raise Exception("Error: Unexpected response format")
except RequestException as e:
print(f"Request failed: {e}")

raise Exception("Error: Maximum number of tries exceeded")

def traditionalize_ass(input_file, output_file, user_pre_replace=""):
with open(input_file, encoding='utf-8-sig', mode='r') as f:
doc = ass.parse(f)
new_texts = []
temp_texts = []
for i in range(len(doc.events)):
new_texts.append(doc.events[i].text)
temp_texts.append(doc.events[i].text)
print(f"Traditionalizing {input_file}")
new_texts = json.loads(traditionalize_text(json.dumps(new_texts, ensure_ascii=False), user_pre_replace))
for i in range(0, len(temp_texts), 50):
print(f"Traditionalizing line {i} to {i+50}...")
slice_texts = new_texts[i:i+50]
slice_texts = json.dumps(slice_texts, ensure_ascii=False)
traditionalized_slice = traditionalize_text(slice_texts, user_pre_replace)
new_texts += json.loads(traditionalized_slice)
for i in range(len(new_texts)):
doc.events[i].text = new_texts[i].replace("思源黑体", "Source Han Sans TC").replace("思源宋体", "Source Han Serif TC").replace("Source Han Sans SC", "Source Han Sans TC").replace("Source Han Sans TC", "Source Han Serif TC")
# replace import commands
Expand Down

0 comments on commit db079cd

Please sign in to comment.