Files
skill-template/content-manager/content_manager/db/videos_repository.py
2026-04-04 10:35:02 +08:00

101 lines
2.6 KiB
Python

"""videos 表:仅保存文件相对路径等元数据。"""
from __future__ import annotations
import sqlite3
from typing import Any, List, Optional, Tuple
def insert_row(
conn: sqlite3.Connection,
file_path: str,
title: Optional[str],
duration_ms: Optional[int],
status: str,
source: str,
account_id: Optional[str],
error_msg: Optional[str],
extra_json: Optional[str],
created_at: int,
updated_at: int,
) -> int:
cur = conn.cursor()
cur.execute(
"""
INSERT INTO videos (
file_path, title, duration_ms, status, source, account_id, error_msg, extra_json, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
file_path,
title,
duration_ms,
status,
source,
account_id,
error_msg,
extra_json,
created_at,
updated_at,
),
)
return int(cur.lastrowid)
def update_file_path(conn: sqlite3.Connection, video_id: int, file_path: str, updated_at: int) -> None:
cur = conn.cursor()
cur.execute(
"UPDATE videos SET file_path = ?, updated_at = ? WHERE id = ?",
(file_path, updated_at, video_id),
)
def fetch_by_id(conn: sqlite3.Connection, video_id: int) -> Optional[Tuple[Any, ...]]:
cur = conn.cursor()
cur.execute(
"""
SELECT id, file_path, title, duration_ms, status, source, account_id, error_msg, extra_json, created_at, updated_at
FROM videos WHERE id = ?
""",
(video_id,),
)
return cur.fetchone()
def list_recent(conn: sqlite3.Connection, limit: int) -> List[Tuple[Any, ...]]:
cur = conn.cursor()
cur.execute(
"""
SELECT id, file_path, title, duration_ms, status, source, account_id, error_msg, extra_json, created_at, updated_at
FROM videos ORDER BY updated_at DESC, id DESC
LIMIT ?
""",
(int(limit),),
)
return list(cur.fetchall())
def delete_by_id(conn: sqlite3.Connection, video_id: int) -> int:
cur = conn.cursor()
cur.execute("DELETE FROM videos WHERE id = ?", (video_id,))
return int(cur.rowcount)
def update_feedback(
conn: sqlite3.Connection,
video_id: int,
status: str,
account_id: Optional[str],
error_msg: Optional[str],
updated_at: int,
) -> None:
cur = conn.cursor()
cur.execute(
"""
UPDATE videos
SET status = ?, account_id = ?, error_msg = ?, updated_at = ?
WHERE id = ?
""",
(status, account_id, error_msg, updated_at, video_id),
)