You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.0 KiB
Python

2 months ago
import psycopg2.extras
from config import get_local_db_connection # central config
# ───────── DB HELPER ───────── #
2 months ago
def db_get_videos(username: str = None):
conn, cur = get_local_db_connection()
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
2 months ago
query = """
SELECT video_id, username, site AS platform,
filepath, size, duration, gender,
created_at, updated_at, thumbnail
FROM videos
WHERE status != 'missing'
"""
params = []
if username:
2 months ago
query += " AND username = %s"
params.append(username)
query += " ORDER BY created_at DESC"
2 months ago
cur.execute(query, params)
rows = cur.fetchall()
cur.close(); conn.close()
return [dict(r) for r in rows]
def db_get_video(video_id: str):
conn, cur = get_local_db_connection()
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("""
SELECT
video_id, username, site AS platform,
filepath, size, duration, gender,
created_at, updated_at, thumbnail
FROM videos
WHERE video_id = %s
""", (video_id,))
row = cur.fetchone()
cur.close(); conn.close()
2 months ago
return dict(row) if row else None
def db_get_recent(page: int, per_page: int):
offset = (page - 1) * per_page
conn, cur = get_local_db_connection()
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("""
SELECT
video_id, username, site AS platform,
filepath, size, duration, gender,
created_at, updated_at, thumbnail
FROM videos
WHERE status != 'missing'
ORDER BY created_at DESC NULLS LAST, updated_at DESC NULLS LAST
LIMIT %s OFFSET %s
""", (per_page, offset))
rows = [dict(r) for r in cur.fetchall()]
cur.execute("SELECT COUNT(*) FROM videos WHERE status != 'missing'")
total = cur.fetchone()[0]
cur.close(); conn.close()
return rows, total