Files
python_scripts/database.py
T
2026-07-21 16:50:05 -04:00

148 lines
5.3 KiB
Python

#!/usr/bin/env python3
import sqlite3
from datetime import datetime
from datetime import date as datetime_date
from pathlib import Path
class Database:
def __init__(self, table: str):
self.table = table
self.columns = ""
# IMPORTANT Must check if database.db exists before connecting to it.
file_exists = Path("database.db").is_file()
self.CONN = sqlite3.connect("database.db")
self.CURSOR = self.CONN.cursor()
if self.table == "vods":
self.columns = "id, date, title, gamename, downloaded, uploaded_yt, chats_upload_yt"
elif self.table == "clips":
self.columns = "slug, date, title, gamename, clip_by, view_count, downloaded, uploaded_yt, shorts_upload_yt"
if not file_exists:
self.create_database()
def __del__(self):
self.close_database()
def create_database(self):
"""Creates a table structured explicitly for Twitch clip properties."""
self.CURSOR.execute(
"""
CREATE TABLE IF NOT EXISTS clips (
slug TEXT PRIMARY KEY,
date TEXT NOT NULL,
title TEXT NOT NULL,
gamename TEXT NOT NULL,
clip_by TEXT NOT NULL,
view_count INTEGER NOT NULL,
downloaded INTEGER NOT NULL,
uploaded_yt INTEGER NOT NULL,
shorts_upload_yt INTEGER NOT NULL
)
"""
)
"""Creates a table structured explicitly for Twitch vods properties."""
self.CURSOR.execute(
"""
CREATE TABLE IF NOT EXISTS vods (
id INTEGER PRIMARY KEY,
date TEXT NOT NULL,
title TEXT NOT NULL,
gamename TEXT NOT NULL,
downloaded INTEGER NOT NULL,
uploaded_yt INTEGER NOT NULL,
chats_upload_yt INTEGER NOT NULL
)
"""
)
self.CONN.commit()
def close_database(self):
"""Commit before we close."""
self.CONN.commit()
self.CONN.close()
def __mark_as(self, record_id: int, set_sql: str):
id = "id"
if self.table == "clips":
id = "slug"
self.CURSOR.execute(
f"UPDATE {self.table} SET {set_sql} = 1 WHERE {id} = ?",
(record_id,)
)
self.CONN.commit()
def mark_as_uploaded_shorts_chats(self, record_id: int):
"""Flags a specific row record to uploaded (1)."""
set_sql = "chats_upload_yt"
if self.table == "clips":
set_sql = "shorts_uploaded_yt"
self.__mark_as(record_id, set_sql)
def mark_as_uploaded(self, record_id: int):
"""Flags a specific row record to uploaded (1)."""
self.__mark_as(record_id, "uploaded_yt")
def mark_as_downloaded(self, record_id: int):
"""Updates the downloaded status to True (1) for a specific record ID."""
self.__mark_as(record_id, "downloaded")
def get_unuploaded_shorts_chats(self):
"""Retrieves all clip rows remaining to be chat uploaded."""
where_sql = "chats_upload_yt"
if self.table == "clips":
where_sql = "shorts_uploaded_yt"
self.CURSOR.execute(f"SELECT {self.columns} FROM {self.table} WHERE downloaded = 1 AND uploaded_yt = 1 AND {where_sql} = 0")
return self.CURSOR.fetchall()
def get_unuploaded(self):
"""Retrieves all clip rows remaining to be uploaded."""
self.CURSOR.execute(f"SELECT {self.columns} FROM {self.table} WHERE downloaded = 1 AND uploaded_yt = 0")
return self.CURSOR.fetchall()
def get_undownloaded(self):
"""Retrieves all rows where downloaded status is False (0)."""
self.CURSOR.execute(f"SELECT {self.columns} FROM {self.table} WHERE downloaded = 0")
return self.CURSOR.fetchall()
def insert_vods_record(self, record_id: int, record_date_str: datetime_date, title: str, gamename: str):
"""Inserts a record with ID, date, gamename, and title into a SQLite database."""
try:
clean_date = datetime.strptime(record_date_str, "%Y-%m-%dT%H:%M:%SZ").date()
except ValueError:
clean_date = record_date_str
# Connects to database file (creates it if missing)
# Inserts data using parameterized queries to prevent SQL injection
self.CURSOR.execute(
f"INSERT OR IGNORE INTO {self.table} ({self.columns}) VALUES (?, ?, ?, ?, ?, ?, ?)",
(record_id, str(clean_date), title, gamename, False, False, False),
)
# Saves changes and closes the connection
self.CONN.commit()
def insert_clips_record(self, slug: str, record_date_str: str, title: str, gamename: str, clip_by: str, views: int):
"""Cleans up ISO-8601 strings into unified date structures for the database."""
try:
clean_date = datetime.strptime(record_date_str, "%Y-%m-%dT%H:%M:%SZ").date()
except ValueError:
clean_date = record_date_str
self.CURSOR.execute(
f"INSERT OR IGNORE INTO {self.table} ({self.columns}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
(slug, str(clean_date), title, gamename, clip_by, views, False, False, False),
)
self.CONN.commit()