diff --git a/func/main.py b/func/main.py index 2aa52e2..003ebec 100644 --- a/func/main.py +++ b/func/main.py @@ -249,4 +249,13 @@ def dumpToJson(objToDump, filePath): F.write(json.dumps(objToDump, ensure_ascii=True, indent=4).encode('utf8').decode()) return True except: - return False \ No newline at end of file + return False + +def createJsonFile(filepath, jsonObject, logSrc = "main"): + logString("Writing to file " + os.path.basename(filepath), logSrc) + try: + with open(filepath, "w+", encoding='utf-8') as F: + F.write(json.dumps(jsonObject, ensure_ascii=False, indent=4).encode('utf8').decode()) + logString("File generated: " + filepath, logSrc) + except: + logString(f"Cannot write json file: {filepath}", logSrc) \ No newline at end of file diff --git a/func/trim_list.py b/func/trim_list.py index 09564d2..5f01ebf 100644 --- a/func/trim_list.py +++ b/func/trim_list.py @@ -114,10 +114,7 @@ def trim_results(filepath, inputAnime, inputManga): # Write 'outputAnime' if jsonOutputAnime: - fMain.logString("Writing to file " + os.path.basename(outputAnime), logSrc) - with open(outputAnime, "w+", encoding='utf-8') as F: - F.write(json.dumps(jsonOutputAnime, ensure_ascii=False, indent=4).encode('utf8').decode()) - fMain.logString("File generated: " + outputAnime, logSrc) + fMain.createJsonFile(outputAnime, jsonOutputAnime, logSrc) # Write stats for Anime cTotal = cComplete + cCurrent + cHold + cPlan + cDrop @@ -196,10 +193,7 @@ def trim_results(filepath, inputAnime, inputManga): # Write 'outputManga' if jsonOutputManga: - fMain.logString("Writing to file " + os.path.basename(outputManga), logSrc) - with open(outputManga, "w+", encoding='utf-8') as F: - F.write(json.dumps(jsonOutputManga, ensure_ascii=False, indent=4).encode('utf8').decode()) - fMain.logString("File generated: " + outputManga, logSrc) + fMain.createJsonFile(outputManga, jsonOutputManga, logSrc) # Write stats for Manga cTotal = cComplete + cCurrent + cHold + cPlan + cDrop