-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffxiv_guide_xlsx_to_file.py
310 lines (279 loc) · 11.6 KB
/
ffxiv_guide_xlsx_to_file.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# -*- coding: utf-8 -*-
# coding: utf8
from logging import Logger
import os
import traceback
# traceback.print_exc()
import errno
from typing import Any
import yaml
from yaml.loader import SafeLoader
from ffxiv_aku import print_pretty_json, pretty_json, print_color_green, sys, readJsonFile, writeJsonFile, storeFilesInTmp, sortJsonData
import python_scripts.generatePatch as gp
from python_scripts.header import addHeader
from python_scripts.guide import addGuide
from python_scripts.fileimports import logdata, logdata_lower
from python_scripts.helper import uglyContentNameFix, getContentName, EntryType
# from python_scripts.constants import *
from python_scripts.custom_logger import getLogger
from python_scripts.xlsx_entry_helper import get_header_from_xlsx, getEntryData, getPrevAndNextContentOrder, read_xlsx_file, Worksheet, excel_to_json, getEntryDataOld
import python_scripts.convert_skills_to_guide_form as csgf
import python_scripts.generateLinks as gl
import python_scripts.generateHousingMissions as ghm
import python_scripts.get_achivments as ga
import python_scripts.getBlueSideQuestData as gbsq
import python_scripts.FFXIV_Charachter_Config as fcc
import python_scripts.get_item_sets as gis
import python_scripts.treasurespot as ts
import python_scripts.quests as quests
logger: Logger = getLogger(50)
disable_green_print: bool = True
disable_yellow_print: bool = True
disable_blue_print: bool = True
disable_red_print: bool = True
debug_row_number: int = 0
print_debug: bool = False
LANGUAGES: list[str] = ["de", "en", "fr", "ja", "cn", "ko"]
LANGUAGES_MAPPING: dict[str, str] = {
"de": "de-DE",
"en": "en-US",
"fr": "fr-FR",
"ja": "ja-JP",
"cn": "cn-CN",
"ko": "ko-KR"
}
translations:dict[str, dict[str, dict[str, str]]] = {
"de": {},
"en": {},
"fr": {},
"ja": {},
"cn": {},
"ko": {}
}
def translate_content_files(entry: EntryType) -> None:
global translations
if entry['categories'] == "":
return
_type: str = entry['instanceType']
for lang in LANGUAGES:
if not translations[lang].get(_type, None):
translations[lang][_type] = {}
name: str = entry['titles'][lang]
name = uglyContentNameFix(name, instanceType=entry['instanceType'], difficulty=entry['difficulty'])
name = name.replace(f' ({entry["difficulty"].lower()})', "")
translations[lang][_type][f"Summary_{_type}_{entry['titles']['en'].lower()}"] = name
def create_translation_files() -> None:
global translations
for lang, cat_data in translations.items():
for cat, data in cat_data.items():
tmp = None
if os.path.exists(f"../assets/translations/summary/{cat}/{LANGUAGES_MAPPING[lang]}.json"):
tmp = readJsonFile(f"../assets/translations/summary/{cat}/{LANGUAGES_MAPPING[lang]}.json")
if tmp is None or tmp != data:
writeJsonFile(f"../assets/translations/summary/{cat}/{LANGUAGES_MAPPING[lang]}.json", data)
def get_old_content_if_file_is_found(_existing_filename: str) -> dict[Any, Any]:
if os.path.exists(_existing_filename):
with open(_existing_filename, encoding="utf8") as f:
doc = list(yaml.load_all(f, Loader=SafeLoader))[0]
return doc
return {}
def try_to_create_file(filename: str) -> None:
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
def cleanup_logdata(logdata_instance_content: dict[Any, Any]) -> tuple[dict[Any, Any], list[str] | None, list[str] | None]:
try:
del logdata_instance_content["combatants"]
except Exception:
pass
try:
del logdata_instance_content["zone"]
except Exception:
pass
try:
del logdata_instance_content["contentzoneid"]
except Exception:
pass
fates: list[str]|None = None
try:
fates = logdata_instance_content["fates"]
del logdata_instance_content["fates"]
except Exception:
pass
music: list[str]|None = None
try:
music = logdata_instance_content["music"]
del logdata_instance_content["music"]
except Exception:
pass
for _, enemy in logdata_instance_content.items():
try:
del enemy["tether"]
except Exception:
pass
try:
del enemy["headmarker"]
except Exception:
pass
new_lic = {}
for k, v in logdata_instance_content.items():
new_lic[k] = v
return new_lic, music, fates
def getDataFromLogfile(entry: dict[str, Any]):
logdata_instance_content = None
music = None
fates = None
contentzoneid = ""
# get correct title capitalization to read data from logdata
title = uglyContentNameFix(entry["titles"]['de'].title(), entry["instanceType"], entry["difficulty"])
# get the latest data from logdata
if not entry["titles"]['de'] == "" and logdata_lower.get(entry["titles"]['de'].lower()):
try:
logdata_instance_content = dict(logdata[getContentName(title, lang="de")])
except Exception:
logdata_instance_content = dict(logdata[title])
if logdata_instance_content.get('contentzoneid', None):
contentzoneid = logdata_instance_content['contentzoneid']
logdata_instance_content, music, fates = cleanup_logdata(logdata_instance_content)
return logdata_instance_content, music, contentzoneid, fates
def writeFileIfNoDifferent(filename: str, filedata: str, aku_write: bool = False) -> None:
if aku_write:
try:
x_data = readJsonFile(filename)
except Exception:
x_data = {}
#res = all((x_data.get(k) == v for k, v in filedata.items()))
if not x_data == filedata: # type: ignore
writeJsonFile(filename, filedata)
print_color_green(f"[MAIN:writeFileIfNoDifferent] Wrote new data to file {filename}")
else:
try:
with open(filename, "r", encoding="utf8") as f:
x_data = f.read()
except Exception:
x_data = None
if not filedata == x_data:
with open(filename, "w", encoding="utf8") as fi:
fi.write(filedata)
print_color_green(f"[MAIN:writeFileIfNoDifferent] Wrote new data to file {filename}")
def write_content_to_file(entry: dict[str, Any], filename: str, old_data: dict[str, Any], content_translations: dict[str, Any] ) -> None:
logdata_instance_content, music, contentzoneid, fates = getDataFromLogfile(entry)
filedata = '---\n'
filedata += addHeader(entry, old_data, music, contentzoneid, content_translations)
filedata += addGuide(entry, old_data, logdata_instance_content, fates, content_translations)
filedata += '---'
filedata += '\n'
writeFileIfNoDifferent(filename, filedata)
expansion_list: dict[str, str] = {
"arr": "2_arr",
"hw": "3_hw",
"sb": "4_sb",
"shb": "5_shb",
"ew": "6_ew",
"dt": "7_dt"
}
def run(googledata: dict[str, EntryType], orderedContent: dict[str, str]) -> None:
global debug_row_number # used in debugger to verify entry
global print_debug
filename = ""
for key, value in googledata.items():
i = int(key)
try:
debug_row_number = i
if not True:
#if debug_row_number > 3 :
if debug_row_number not in [795]:
print_debug = True
continue
entry: EntryType = getEntryData(value, i, orderedContent)
if print_debug:
print(f"[MAIN:run] {entry['title']}")
logger.info(pretty_json(entry))
# if the done collumn is not prefilled
if entry["exclude"] == "end":
print("[MAIN:run] END FLAG WAS FOUND!")
break
translate_content_files(entry)
# continue
if not (entry["exclude"] or entry["done"]):
content_translations = {}
for lang in LANGUAGES:
content_translations[lang] = {}
# print(f"{entry['instanceType']}: {entry['title']}")
if entry['categories'] == "":
print("[MAIN:run] Skipped due to no expansion specified")
continue
expansion: str = expansion_list[entry['categories']]
filename: str = f"{expansion}_new/{entry['instanceType']}/{entry['date'].replace('.', '-')}--{entry['patchNumber']}--{entry['sortid'].zfill(5)}--{entry['slug'].replace(',', '')}.md"
existing_filename: str = f"{expansion}/{entry['instanceType']}/{entry['date'].replace('.', '-')}--{entry['patchNumber']}--{entry['sortid'].zfill(5)}--{entry['slug'].replace(',', '')}.md"
old_data = get_old_content_if_file_is_found(existing_filename)
#print_pretty_json(old_data)
if old_data != {}:
filename = existing_filename
try_to_create_file(filename)
write_content_to_file(entry, filename, old_data, content_translations)
filename_translation_location: str = f"../assets/translations/content/{entry['categories']}/{entry['instanceType']}/{entry['slug'].replace(',', '').replace('_', '-')}"
if not os.path.exists(filename_translation_location):
os.makedirs(filename_translation_location)
for lang in LANGUAGES:
writeFileIfNoDifferent(filename_translation_location + f"/{LANGUAGES_MAPPING[lang]}.json", content_translations[lang], True)
elif entry['title'] != "":
print_color_green(f"[MAIN:run] Skip {entry['title']} as its marked as {entry['exclude']}/{entry['done']}")
except Exception as e:
msg = f"Error when handeling '{filename}' with line id '{i}' ({e})"
logger.critical(msg)
traceback.print_exception(*sys.exc_info())
def main() -> None:
global translations
logger.critical('START')
tmp_path = storeFilesInTmp(True) # set tmp path
sheet: Worksheet
max_row: int
max_column: int
google_file_name = os.path.join(tmp_path, "google.json")
if not os.path.exists(google_file_name):
sheet, max_row, max_column = read_xlsx_file()
#print_pretty_json(sheet)
XLSXELEMENTS: list[str] = get_header_from_xlsx(sheet, max_column)
# first run to create all files
orderedContent: dict[str, str] = getPrevAndNextContentOrder(sheet, XLSXELEMENTS, max_row)
googledata: dict[str, EntryType] = excel_to_json(sheet)
googledata = sortJsonData(googledata, sort_sub_keys=False) # type: ignore
tmp = {
"google": googledata,
"ordered": orderedContent
}
writeJsonFile(google_file_name, tmp)
else:
print_color_green("[MAIN:main] SKIP DOWNLOADING CONTENT FILE!")
tmp = readJsonFile(google_file_name)
googledata = tmp["google"]
orderedContent = tmp["ordered"]
csgf.load_global_data()
# change into _posts dir
os.chdir("./_posts")
#logger.debug(orderedContent)
try:
run(googledata, orderedContent)
pass
except Exception:
traceback.print_exception(*sys.exc_info())
if not print_debug:
csgf.run()
gl.run()
gp.run()
ghm.run()
ga.run()
gbsq.run()
fcc.run(translations)
gis.run()
ts.run()
quests.run()
create_translation_files()
logger.critical('STOP')
if __name__ == "__main__":
print(f"[MAIN:if] {sys.version}")
main()