import json import os from datetime import datetime from utils.config import OUTPUT_DIR HISTORY_FILE = os.path.join(OUTPUT_DIR, "history.json") def load_history(): """Load podcast generation history from JSON file""" if not os.path.exists(HISTORY_FILE): return [] try: with open(HISTORY_FILE, 'r') as f: return json.load(f) except Exception as e: print(f"Error loading history: {e}") return [] def save_to_history(url, audio_path, script_length): """Save a podcast generation to history""" history = load_history() entry = { "url": url, "audio_path": audio_path, "script_length": script_length, "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "audio_filename": os.path.basename(audio_path) } history.append(entry) try: with open(HISTORY_FILE, 'w') as f: json.dump(history, f, indent=2) print(f"✓ Saved to history: {url}") except Exception as e: print(f"Error saving to history: {e}") def get_history_items(): """Get history items formatted for Gradio display""" history = load_history() if not history: return [] # Return in reverse order (newest first) items = [] for entry in reversed(history): items.append({ "timestamp": entry["timestamp"], "url": entry["url"], "audio_path": entry["audio_path"], "script_length": entry.get("script_length", "N/A") }) return items