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:
marco
2026-09-03 15:05:45 -03:00
parent ee398ff8a0
commit 5bc68e3627
4 changed files with 70 additions and 0 deletions
+14
View File
@@ -11,6 +11,7 @@ Sistema automatizado de envio de insights via Telegram, utilizando inteligência
- Intervalo entre envios configurável.
- Seleção aleatória de tópicos cadastrados.
- Suporte a múltiplos provedores de IA: OpenRouter, LlamaCPP local ou OpenAI.
- Todos os insights são salvos automaticamente em um banco de dados SQLite local (`insights.db`).
## Pré-requisitos
@@ -80,3 +81,16 @@ python3 main.py
| `TOPICS` | Lista de tópicos separados por vírgula | ver `.env` |
| `LANGUAGE` | Idioma dos insights | pt-BR |
| `MIN_LINES` | Mínimo de linhas por insight | 20 |
### Banco de dados SQLite
Todos os insights enviados são registrados automaticamente no arquivo `insights.db`, na mesma pasta do projeto. A tabela `insights` armazena:
| Campo | Descrição |
|---|---|
| `id` | Identificador único |
| `topic` | Tópico do insight |
| `content` | Conteúdo completo do insight |
| `provider` | Provedor de IA utilizado |
| `model` | Modelo utilizado |
| `sent_at` | Data e hora do envio |
+46
View File
@@ -0,0 +1,46 @@
import sqlite3
import os
from datetime import datetime
DB_PATH = os.path.join(os.path.dirname(__file__), "insights.db")
def init_db() -> None:
conn = sqlite3.connect(DB_PATH)
conn.execute("""
CREATE TABLE IF NOT EXISTS insights (
id INTEGER PRIMARY KEY AUTOINCREMENT,
topic TEXT NOT NULL,
content TEXT NOT NULL,
provider TEXT NOT NULL,
model TEXT NOT NULL,
sent_at TEXT NOT NULL
)
""")
conn.commit()
conn.close()
def save_insight(topic: str, content: str, provider: str, model: str) -> int:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
sent_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute(
"INSERT INTO insights (topic, content, provider, model, sent_at) VALUES (?, ?, ?, ?, ?)",
(topic, content, provider, model, sent_at),
)
insight_id = cursor.lastrowid
conn.commit()
conn.close()
return insight_id
def get_insights(limit: int = 50):
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT * FROM insights ORDER BY id DESC LIMIT ?", (limit,)
).fetchall()
conn.close()
return [dict(row) for row in rows]
BIN
View File
Binary file not shown.
+10
View File
@@ -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)