update uploading functions

This commit is contained in:
SumGuyV5 committed 2026-09-19 00:05:54 +00:00
1 parent 61fe9eca25
commit d4db0b04a4
9 files changed
+85 -55

No files matched your search

+3 -2
View File
@@ -1,11 +1,12 @@
# package/database/__init__.py
# Expose components from individual modules using relative imports
from .database import TwitchVideo, Data, Database
from .database import TwitchVideo, Data, YoutubeInfo, Database
# Explicitly define what is exposed when importing the subpackage
__all__ = [
"TwitchVideo",
"Data",
"YoutubeInfo",
"Database",
]
]
+28 -8
View File
@@ -1,5 +1,5 @@
from sqlalchemy import create_engine, Column, String, Integer, Boolean, select
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy import create_engine, Column, String, Integer, Boolean, ForeignKey, select
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
from typing import Any
# 1. Define the base class for declarative models
@@ -26,6 +26,8 @@ class TwitchVideo(Base):
uploaded_yt_chats = Column(Boolean, nullable=False, default=False)
uploaded_yt_shorts = Column(Boolean, nullable=False, default=False)
# Relationship back to YoutubeInfo (uselist=False enforces a 1-to-1 structure)
youtube_info = relationship("YoutubeInfo", back_populates="twitch_video", uselist=False)
# 3. Define the data table
class Data(Base):
@@ -38,6 +40,23 @@ class Data(Base):
date_videos = Column(String, nullable=False)
date_videos_chat = Column(String, nullable=False)
class YoutubeInfo(Base):
__tablename__ = "youtube_info"
id = Column(String, ForeignKey("twitch_videos.id"), primary_key=True)
yt_id = Column(String, nullable=True)
yt_date = Column(String, nullable=True)
yt_short_id = Column(String, nullable=True)
yt_short_date = Column(String, nullable=True)
yt_chat_SS_id = Column(String, nullable=True)
yt_chat_SS_date = Column(String, nullable=True)
yt_chat_over_id = Column(String, nullable=True)
yt_chat_over_date = Column(String, nullable=True)
# Relationship linking back to the twitch video record
twitch_video = relationship("TwitchVideo", back_populates="youtube_info")
class Database:
def __init__(self, db_path: str = "database.db"):
# 4. Connect to the SQLite file and generate the tables
@@ -152,7 +171,10 @@ class Database:
stmt = select(Data)
return self.session.scalars(stmt).all()
def insert(self, data: TwitchVideo | Data):
def get_youtube_info(self, record_id) -> YoutubeInfo | None:
return self.session.get(YoutubeInfo, record_id)
def insert(self, data: TwitchVideo | Data | YoutubeInfo):
# SQLAlchemy 2.0 context manager handles commit/rollback automatically
try:
if isinstance(data, TwitchVideo):
@@ -170,11 +192,9 @@ class Database:
# 3. Add new instance if it doesn't exist
self.session.add(data)
print("➕ New TwitchVideo record inserted successfully!")
else:
# Handle standard insertion for the Data model
self.session.add(data)
print("➕ Data record inserted successfully!")
else:
self.session.merge(data)
print(f"➕ {type(data).__name__} record inserted successfully!")
# 4. Flush changes and commit the transaction
self.session.commit()