mirror of
https://github.com/Ladebeze66/mini_tools.git
synced 2025-12-13 09:06:51 +01:00
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
import os
|
|
import json
|
|
from datetime import datetime
|
|
|
|
# === Config ===
|
|
CURSOR_CHAT_DIR = os.path.expanduser("~/.cursor/chat/")
|
|
OUTPUT_FILE = "cursor_history.md"
|
|
|
|
# === Initialisation du contenu ===
|
|
md_output = ""
|
|
|
|
# === Chargement des discussions Cursor ===
|
|
for filename in sorted(os.listdir(CURSOR_CHAT_DIR)):
|
|
if not filename.endswith(".json"):
|
|
continue
|
|
|
|
filepath = os.path.join(CURSOR_CHAT_DIR, filename)
|
|
|
|
with open(filepath, "r", encoding="utf-8") as f:
|
|
try:
|
|
chat_data = json.load(f)
|
|
except json.JSONDecodeError:
|
|
continue # Fichier corrompu ou non lisible
|
|
|
|
created_at_raw = chat_data.get("createdAt", "")
|
|
try:
|
|
created_at = datetime.fromisoformat(created_at_raw.replace("Z", ""))
|
|
except ValueError:
|
|
created_at = datetime.now()
|
|
|
|
formatted_time = created_at.strftime("%Y-%m-%d %H:%M:%S")
|
|
md_output += f"\n---\n\n## Session du {formatted_time}\n\n"
|
|
|
|
for msg in chat_data.get("messages", []):
|
|
role = msg.get("role", "")
|
|
content = msg.get("content", "").strip()
|
|
if not content:
|
|
continue
|
|
|
|
if role == "user":
|
|
md_output += f"** Utilisateur :**\n{content}\n\n"
|
|
elif role == "assistant":
|
|
md_output += f"** Assistant :**\n{content}\n\n"
|
|
|
|
# === Écriture / ajout dans le fichier final ===
|
|
with open(OUTPUT_FILE, "a", encoding="utf-8") as output_file:
|
|
output_file.write(md_output)
|
|
|
|
print(f" Export terminé ! Discussions ajoutées à : {OUTPUT_FILE}")
|