import sqlite3 import os import hashlib from typing import List, Dict, Any DB_PATH = "/app/data/stock.db" def _conn() -> sqlite3.Connection: os.makedirs(os.path.dirname(DB_PATH), exist_ok=True) conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row return conn def init_db(): with _conn() as conn: conn.execute(""" CREATE TABLE IF NOT EXISTS articles ( id INTEGER PRIMARY KEY AUTOINCREMENT, hash TEXT UNIQUE NOT NULL, category TEXT DEFAULT 'domestic', title TEXT NOT NULL, link TEXT, summary TEXT, press TEXT, pub_date TEXT, crawled_at TEXT ) """) conn.execute("CREATE INDEX IF NOT EXISTS idx_articles_crawled ON articles(crawled_at DESC)") # 컬럼 추가 (기존 테이블 마이그레이션) cols = {r["name"] for r in conn.execute("PRAGMA table_info(articles)").fetchall()} if "category" not in cols: conn.execute("ALTER TABLE articles ADD COLUMN category TEXT DEFAULT 'domestic'") conn.execute(""" CREATE TABLE IF NOT EXISTS portfolio ( id INTEGER PRIMARY KEY AUTOINCREMENT, broker TEXT NOT NULL, ticker TEXT NOT NULL, name TEXT NOT NULL, quantity INTEGER NOT NULL, avg_price INTEGER NOT NULL, created_at TEXT DEFAULT (datetime('now','localtime')), updated_at TEXT DEFAULT (datetime('now','localtime')) ) """) def save_articles(articles: List[Dict[str, str]]) -> int: count = 0 with _conn() as conn: for a in articles: # 중복 체크용 해시 (제목+링크) unique_str = f"{a['title']}|{a['link']}" h = hashlib.md5(unique_str.encode()).hexdigest() try: cat = a.get("category", "domestic") conn.execute(""" INSERT INTO articles (hash, category, title, link, summary, press, pub_date, crawled_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) """, (h, cat, a['title'], a['link'], a['summary'], a['press'], a['date'], a['crawled_at'])) count += 1 except sqlite3.IntegrityError: pass # 이미 존재함 return count def get_latest_articles(limit: int = 20, category: str = None) -> List[Dict[str, Any]]: with _conn() as conn: if category: rows = conn.execute( "SELECT * FROM articles WHERE category = ? ORDER BY crawled_at DESC, id DESC LIMIT ?", (category, limit) ).fetchall() else: rows = conn.execute( "SELECT * FROM articles ORDER BY crawled_at DESC, id DESC LIMIT ?", (limit,) ).fetchall() return [dict(r) for r in rows] # --- Portfolio CRUD --- def add_portfolio_item(broker: str, ticker: str, name: str, quantity: int, avg_price: int) -> int: with _conn() as conn: cur = conn.execute( "INSERT INTO portfolio (broker, ticker, name, quantity, avg_price) VALUES (?, ?, ?, ?, ?)", (broker, ticker, name, quantity, avg_price), ) return cur.lastrowid def get_all_portfolio() -> List[Dict[str, Any]]: with _conn() as conn: rows = conn.execute("SELECT * FROM portfolio ORDER BY id").fetchall() return [dict(r) for r in rows] def get_portfolio_item(item_id: int) -> Dict[str, Any] | None: with _conn() as conn: row = conn.execute("SELECT * FROM portfolio WHERE id = ?", (item_id,)).fetchone() return dict(row) if row else None def update_portfolio_item(item_id: int, **kwargs) -> bool: allowed = {"broker", "ticker", "name", "quantity", "avg_price"} fields = {k: v for k, v in kwargs.items() if k in allowed and v is not None} if not fields: return False fields["updated_at"] = __import__("datetime").datetime.now().strftime("%Y-%m-%d %H:%M:%S") set_clause = ", ".join(f"{k} = ?" for k in fields) values = list(fields.values()) + [item_id] with _conn() as conn: cur = conn.execute(f"UPDATE portfolio SET {set_clause} WHERE id = ?", values) return cur.rowcount > 0 def delete_portfolio_item(item_id: int) -> bool: with _conn() as conn: cur = conn.execute("DELETE FROM portfolio WHERE id = ?", (item_id,)) return cur.rowcount > 0