Cataloguer/scripts/re-add_dates.py

46 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-02-17 12:54:37 +00:00
import json
with open("./data/books/log.json", "r", encoding="utf-8") as log_file:
log_items = json.load(log_file)
with open("./data/books/log-preprocess.json", "r", encoding="utf-8") as log_file:
old_log_items = json.load(log_file)[2:]
for i, item in enumerate(log_items[6:]):
if "title" in item:
title = item["title"]
else:
title = item["Title"]
#print(f"Processing '{title}'...")
for key in ["started", "finished", "added", "read"]:
keyCap = key.capitalize()
if (f"date_{key}" not in item and f"Date {keyCap}" not in item) or ( (f"date_{key}" in item and "" == item[f"date_{key}"]) or (f"Date {keyCap}" in item and "" == item[f"Date {keyCap}"])):
if f"Date {keyCap}" in old_log_items[i]:
if "" == old_log_items[i][f"Date {keyCap}"]:
print(
f"No date_{key} for {title}, and date in old log is empty"
)
item[f"date_{key}"] = None
else:
print(
f"No date_{key} for {title}, taking date {old_log_items[i][f'Date {keyCap}']} from old log"
)
item[f"date_{key}"] = old_log_items[i][f"Date {keyCap}"]
else:
print(
f"No date_{key} for {title}, no value in old log either."
)
item[f"date_{key}"] = None
log_items[i] = item
#print(f"Finished processing {title}.")
with open(f"./data/books/log.json", "w", encoding="utf-8") as log_file:
json.dump(log_items, log_file, indent=4)
print(f"Finished processing log.")