Skip to content

Commit 1782a29

Browse files
authored
Merge pull request #7 from GeorgeK1ng/chronicles
Chronicles translation split
2 parents 2fe8df9 + 3495e5c commit 1782a29

File tree

1 file changed

+79
-16
lines changed

1 file changed

+79
-16
lines changed

.github/update.py

+79-16
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,69 @@ def get_translation_mods():
8989
def get_translation_mods_translation():
9090
translation_mods = get_translation_mods()
9191
data = {}
92+
9293
for key, value in translation_mods.items():
94+
print(f"\n--- Processing language: {key} ---")
9395
tmp = {}
96+
chronicles_found = False
97+
9498
for item in value[1]["translations"]:
9599
base_url = value[0].rsplit('/', 1)[0] + "/content/"
100+
print(f"Checking base translation file: {base_url + item}")
101+
96102
try:
97103
tmp_str = urllib.request.urlopen(base_url + item).read()
98-
except:
99-
tmp_str = urllib.request.urlopen((base_url + item).replace("content", "Content").replace("config", "Config")).read()
100-
tmp |= load_vcmi_json(tmp_str)
104+
except Exception as e:
105+
print(f"Error reading {base_url + item}: {e}")
106+
continue
107+
108+
if "chronicles.json" in item:
109+
print(f"Found chronicles.json in: {base_url + item}")
110+
chronicles_data = load_vcmi_json(tmp_str)
111+
prefixed_chronicles = {f"chronicles.{k}": v for k, v in chronicles_data.items()}
112+
tmp |= prefixed_chronicles
113+
chronicles_found = True
114+
else:
115+
tmp |= load_vcmi_json(tmp_str)
116+
117+
if not chronicles_found:
118+
try:
119+
repo_url_parts = value[0].split("/")
120+
repo_owner = repo_url_parts[3]
121+
repo_name = repo_url_parts[4]
122+
branch_name = repo_url_parts[5]
123+
api_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/git/trees/{branch_name}?recursive=1"
124+
125+
print(f"Fetching repo structure from: {api_url}")
126+
response = urllib.request.urlopen(api_url).read()
127+
repo_files = json5.loads(response)["tree"]
128+
129+
chronicles_json_files = [
130+
f["path"] for f in repo_files
131+
if "chronicles" in f["path"]
132+
and f["path"].endswith(".json")
133+
and "video" not in f["path"].lower()
134+
and not f["path"].endswith("mod.json")
135+
]
136+
137+
print(f"Found chronicles JSON files: {chronicles_json_files}")
138+
139+
for json_file in chronicles_json_files:
140+
json_file_url = f"https://raw.githubusercontent.com/{repo_owner}/{repo_name}/{branch_name}/{json_file}"
141+
print(f"Fetching JSON file: {json_file_url}")
142+
143+
try:
144+
tmp_str = urllib.request.urlopen(json_file_url).read()
145+
chronicles_data = load_vcmi_json(tmp_str)
146+
prefixed_chronicles = {f"chronicles.{k}": v for k, v in chronicles_data.items()}
147+
tmp |= prefixed_chronicles
148+
except Exception as e:
149+
print(f"Error reading JSON file {json_file_url}: {e}")
150+
except Exception as e:
151+
print(f"Error processing chronicles JSON files for {key}: {e}")
152+
101153
data[key] = tmp
154+
102155
return data
103156

104157
def get_translation_mods_translation_assets():
@@ -132,7 +185,7 @@ def translation_mod_ratio(translation_mods_translation):
132185

133186
for language in [key for key, value in translation_mods_translation.items() if key != "english"]:
134187
data_ns = {}
135-
namespaces = [None, "map", "campaign"]
188+
namespaces = [None, "map", "campaign", "chronicles"]
136189
for namespace in namespaces:
137190
translation = translation_mods_translation[language]
138191
count_equal = 0
@@ -186,12 +239,18 @@ def get_mod_translations(languages):
186239
for key, value in vcmi_mods.items():
187240
url = value["mod"].replace(" ", "%20")
188241
mod = load_vcmi_json(urllib.request.urlopen(url).read())
189-
if "language" not in mod:
190-
found_languages = []
191-
for language in languages:
192-
if language in mod:
193-
found_languages.append(language)
194-
data[key] = found_languages
242+
mod_name = mod.get("name", key)
243+
mod_type = mod.get("modType", "unknown").lower()
244+
245+
if mod_type == "translation":
246+
continue
247+
248+
found_languages = []
249+
for language in languages:
250+
if language in mod:
251+
found_languages.append(language)
252+
253+
data[key] = {"name": mod_name, "modType": mod_type, "languages": found_languages}
195254
return data
196255

197256
def create_md():
@@ -232,21 +291,25 @@ def format_value(percent):
232291
md.new_table(columns=df.shape[1], rows=df.shape[0], text=df.to_numpy().flatten(), text_align='center')
233292

234293
tmp = get_mod_translations(languages_translate)
235-
mod_counts = {language: sum([1 for mods in tmp.values() if language in mods]) for language in languages_translate}
294+
mod_counts = {language: sum(1 for mods in tmp.values() if language in mods["languages"]) for language in languages_translate}
236295
total_mods = len(tmp)
237296
percentages = [mod_counts[lang] / total_mods if total_mods > 0 else 0 for lang in languages_translate]
238-
297+
239298
md.new_header(level=2, title="Mods translation status")
240299
header = ["Language"] + languages_translate
241300
values = ["Translated mods"] + [format_value(percent) for percent in percentages]
242-
301+
243302
md.new_table(columns=len(header), rows=2, text=header + values, text_align='center')
244303

245304
md.new_header(level=2, title="Mods translation details")
246305
tmp = get_mod_translations(languages_translate)
247-
df = pd.DataFrame(columns=["Mod"] + languages_translate)
248-
for mod in tmp:
249-
df = pd.concat([df, pd.DataFrame({"Mod": "[" + mod + "](https://github.com/vcmi-mods/" + mod.replace(" ", "-") + ")"} | {x:["x" if x in tmp[mod] else ""] for x in languages_translate})], ignore_index=True)
306+
df = pd.DataFrame(columns=["Mod", "Type"] + languages_translate)
307+
308+
for mod, mod_data in tmp.items():
309+
df = pd.concat([df, pd.DataFrame({"Mod": "[" + mod_data["name"] + "](https://github.com/vcmi-mods/" + mod.replace(" ", "-") + ")", "Type": mod_data["modType"], **{x: ["x" if x in mod_data["languages"] else ""] for x in languages_translate}})], ignore_index=True)
310+
311+
df = df.sort_values(by=["Type", "Mod"])
312+
250313
df = df.T.reset_index().T
251314
md.new_table(columns=df.shape[1], rows=df.shape[0], text=df.to_numpy().flatten(), text_align='center')
252315

0 commit comments

Comments
 (0)