import sqlite3 from datetime import datetime from datetime import date as datetime_date from pathlib import Path class Database: def __init__(self, table): 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, chat_upload_yt" elif self.table == "clips": self.columns = "slug, date, title, gamename, clip_by, view_count, downloaded, uploaded_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, chat_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_uploaded(self, record_id: int): """Flags a specific clip row record to uploaded (1).""" id = "id" if self.table == "clips": id = "slug" self.CURSOR.execute( f"UPDATE {self.table} SET uploaded_yt = 1 WHERE {id} = ?", (record_id,) ) self.CONN.commit() def mark_as_downloaded(self, record_id: int): """Updates the downloaded status to True (1) for a specific record ID.""" id = "id" if self.table == "clips": id = "slug" # Updates the row matching the specific ID # TODO clips uses slug self.CURSOR.execute( f"UPDATE {self.table} SET downloaded = 1 WHERE {id} = ?", (record_id,) ) self.CONN.commit() def get_unuploaded(self): """Retrieves all clip rows remaining to be chat 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), ) self.CONN.commit()