feat(db): add SQLite database persistence for insights
Implement local database storage to automatically save all sent insights with metadata including topic, content, AI provider, model, and timestamp. Add database module with initialization and query functions, integrate with main job flow, and update documentation with database schema details.
This commit is contained in:
@@ -6,6 +6,7 @@ import openai
|
||||
import schedule
|
||||
import time
|
||||
from datetime import datetime
|
||||
from database import init_db, save_insight
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -45,6 +46,10 @@ elif OPENAI_API_KEY:
|
||||
else:
|
||||
raise ValueError("Nenhuma chave de IA configurada no .env")
|
||||
|
||||
init_db()
|
||||
|
||||
CURRENT_PROVIDER = DEFAULT_AI_PROVIDER
|
||||
|
||||
TOPICS = [t.strip() for t in TOPICS_RAW.split(",") if t.strip()]
|
||||
if not TOPICS:
|
||||
raise ValueError("Nenhum tópico informado no .env (variável TOPICS)")
|
||||
@@ -148,6 +153,11 @@ def job():
|
||||
print(f"[{now.strftime('%Y-%m-%d %H:%M:%S')}] Tópico escolhido: {topic}")
|
||||
print(f"[{now.strftime('%Y-%m-%d %H:%M:%S')}] Gerando insight...")
|
||||
insight = generate_insight(topic)
|
||||
try:
|
||||
save_insight(topic=topic, content=insight, provider=CURRENT_PROVIDER, model=DEFAULT_AI_MODEL)
|
||||
print(f"[{now.strftime('%Y-%m-%d %H:%M:%S')}] Insight salvo no banco de dados.")
|
||||
except Exception as e:
|
||||
print(f"[{now.strftime('%Y-%m-%d %H:%M:%S')}] Erro ao salvar no banco: {e}")
|
||||
send_telegram_message(insight, topic)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user