transcriptor/chat_format.py
2025-03-21 12:53:32 +01:00

30 lines
1.1 KiB
Python

import json
from datetime import datetime
with open("chat/madmonq/chat_2397919008.json", "r", encoding="utf-8") as f:
formated_log = []
chat_log = json.load(f)
if isinstance(chat_log, dict) and "comments" in chat_log:
chat_log = chat_log["comments"]
for comment in chat_log:
if not isinstance(comment, dict):
continue
# Parse the timestamp; %f is used for the fractional seconds and 'Z' is matched literally
try:
# Try parsing with fractional seconds
dt = datetime.strptime(comment['created_at'], "%Y-%m-%dT%H:%M:%S.%fZ")
except ValueError:
# Fallback for timestamps without fractional seconds
dt = datetime.strptime(comment['created_at'], "%Y-%m-%dT%H:%M:%SZ")
formatted_time = dt.strftime("%H:%M:%S")
message_text = formatted_time + " --> " + f"{comment['commenter']['display_name']:30}" + ": " + comment['message']['body']
formated_log.append(message_text)
with open("chat_2397919008_formated.txt", "w") as file:
file.write("\n".join(formated_log) + "\n")