Made some chanages and now Can't remmebr what I did
This commit is contained in:
+27
-23
@@ -1,7 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from datetime import date as datetime_date
|
||||
from pathlib import Path
|
||||
|
||||
class Database:
|
||||
@@ -15,7 +14,7 @@ class Database:
|
||||
self.CONN = sqlite3.connect("database.db")
|
||||
self.CURSOR = self.CONN.cursor()
|
||||
|
||||
if self.table == "vods":
|
||||
if self.table == "videos":
|
||||
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"
|
||||
@@ -24,10 +23,14 @@ class Database:
|
||||
self.create_database()
|
||||
|
||||
def __del__(self):
|
||||
self.close_database()
|
||||
# Destructors are unpredictable in Python; explicitly close when done instead
|
||||
try:
|
||||
self.close_database()
|
||||
except:
|
||||
pass
|
||||
|
||||
def create_database(self):
|
||||
"""Creates a table structured explicitly for Twitch clip properties."""
|
||||
"""Creates a table structured explicitly for Twitch clips properties."""
|
||||
self.CURSOR.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS clips (
|
||||
@@ -44,10 +47,10 @@ class Database:
|
||||
"""
|
||||
)
|
||||
|
||||
"""Creates a table structured explicitly for Twitch vods properties."""
|
||||
"""Creates a table structured explicitly for Twitch videos properties."""
|
||||
self.CURSOR.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS vods (
|
||||
CREATE TABLE IF NOT EXISTS videos (
|
||||
id INTEGER PRIMARY KEY,
|
||||
date TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
@@ -63,8 +66,9 @@ class Database:
|
||||
|
||||
def close_database(self):
|
||||
"""Commit before we close."""
|
||||
self.CONN.commit()
|
||||
self.CONN.close()
|
||||
if self.CONN:
|
||||
self.CONN.commit()
|
||||
self.CONN.close()
|
||||
|
||||
def __mark_as(self, record_id: int, set_sql: str):
|
||||
id = "id"
|
||||
@@ -117,32 +121,32 @@ class Database:
|
||||
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."""
|
||||
def insert_videos_record(self, record_id: int, record_date_str: str, title: str, gamename: str):
|
||||
"""Inserts a record with ID, full datetime string, gamename, and title."""
|
||||
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)
|
||||
# Parses into a Python datetime object, then drops timezone info to create a clean string
|
||||
dt_obj = datetime.strptime(record_date_str, "%Y-%m-%dT%H:%M:%SZ")
|
||||
clean_datetime = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
|
||||
except (ValueError, TypeError):
|
||||
clean_datetime = record_date_str
|
||||
|
||||
# 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),
|
||||
(record_id, str(clean_datetime), 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."""
|
||||
"""Cleans up ISO-8601 strings into full datetime 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
|
||||
# Parses into a Python datetime object, then drops timezone info to create a clean string
|
||||
dt_obj = datetime.strptime(record_date_str, "%Y-%m-%dT%H:%M:%SZ")
|
||||
clean_datetime = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
|
||||
except (ValueError, TypeError):
|
||||
clean_datetime = 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),
|
||||
(slug, str(clean_datetime), title, gamename, clip_by, views, False, False, False),
|
||||
)
|
||||
self.CONN.commit()
|
||||
Reference in New Issue
Block a user