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.

57 lines
1.9 KiB
Python

# ───────── DB HELPER ───────── #
def db_get_videos(username = None):
conn, cur = get_local_db_connection()
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
query = "SELECT video_id, username, site AS platform, filepath, size, duration, gender, created_at, updated_at, thumbnail FROM videos WHERE status != 'missing'"
if username:
query += f" AND username = '{username}'"
if True:
query += " ORDER BY created_at DESC"
cur.execute(query)
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()
return dict(row)
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)
# Order by created_at desc (fallback to updated_at if you prefer)
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