mirror of
https://github.com/pacnpal/Pac-cogs.git
synced 2025-12-20 02:41:06 -05:00
Core Systems:
Component-based architecture with lifecycle management Enhanced error handling and recovery mechanisms Comprehensive state management and tracking Event-driven architecture with monitoring Queue Management: Multiple processing strategies for different scenarios Advanced state management with recovery Comprehensive metrics and health monitoring Sophisticated cleanup system with multiple strategies Processing Pipeline: Enhanced message handling with validation Improved URL extraction and processing Better queue management and monitoring Advanced cleanup mechanisms Overall Benefits: Better code organization and maintainability Improved error handling and recovery Enhanced monitoring and reporting More robust and reliable system
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
"""Database management for archived videos"""
|
||||
import sqlite3
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Optional, Tuple
|
||||
from typing import Optional, Dict, Any, List
|
||||
|
||||
from .schema_manager import SchemaManager
|
||||
from .query_manager import QueryManager
|
||||
from .connection_manager import ConnectionManager
|
||||
|
||||
logger = logging.getLogger("VideoArchiverDB")
|
||||
|
||||
@@ -10,70 +14,84 @@ class VideoArchiveDB:
|
||||
"""Manages the SQLite database for archived videos"""
|
||||
|
||||
def __init__(self, data_path: Path):
|
||||
"""Initialize the database connection"""
|
||||
"""Initialize the database and its components
|
||||
|
||||
Args:
|
||||
data_path: Path to the data directory
|
||||
"""
|
||||
# Set up database path
|
||||
self.db_path = data_path / "archived_videos.db"
|
||||
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self._init_db()
|
||||
|
||||
# Initialize managers
|
||||
self.connection_manager = ConnectionManager(self.db_path)
|
||||
self.schema_manager = SchemaManager(self.db_path)
|
||||
self.query_manager = QueryManager(self.connection_manager)
|
||||
|
||||
# Initialize database schema
|
||||
self.schema_manager.initialize_schema()
|
||||
logger.info("Video archive database initialized successfully")
|
||||
|
||||
def _init_db(self):
|
||||
"""Initialize the database schema"""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS archived_videos (
|
||||
original_url TEXT PRIMARY KEY,
|
||||
discord_url TEXT NOT NULL,
|
||||
message_id INTEGER NOT NULL,
|
||||
channel_id INTEGER NOT NULL,
|
||||
guild_id INTEGER NOT NULL,
|
||||
archived_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
except sqlite3.Error as e:
|
||||
logger.error(f"Database initialization error: {e}")
|
||||
raise
|
||||
|
||||
def add_archived_video(self, original_url: str, discord_url: str, message_id: int, channel_id: int, guild_id: int) -> bool:
|
||||
async def add_archived_video(
|
||||
self,
|
||||
original_url: str,
|
||||
discord_url: str,
|
||||
message_id: int,
|
||||
channel_id: int,
|
||||
guild_id: int,
|
||||
metadata: Optional[Dict[str, Any]] = None
|
||||
) -> bool:
|
||||
"""Add a newly archived video to the database"""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
INSERT OR REPLACE INTO archived_videos
|
||||
(original_url, discord_url, message_id, channel_id, guild_id)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""", (original_url, discord_url, message_id, channel_id, guild_id))
|
||||
conn.commit()
|
||||
return True
|
||||
except sqlite3.Error as e:
|
||||
logger.error(f"Error adding archived video: {e}")
|
||||
return False
|
||||
return await self.query_manager.add_archived_video(
|
||||
original_url,
|
||||
discord_url,
|
||||
message_id,
|
||||
channel_id,
|
||||
guild_id,
|
||||
metadata
|
||||
)
|
||||
|
||||
def get_archived_video(self, url: str) -> Optional[Tuple[str, int, int, int]]:
|
||||
async def get_archived_video(self, url: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get archived video information by original URL"""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
SELECT discord_url, message_id, channel_id, guild_id
|
||||
FROM archived_videos
|
||||
WHERE original_url = ?
|
||||
""", (url,))
|
||||
result = cursor.fetchone()
|
||||
return result if result else None
|
||||
except sqlite3.Error as e:
|
||||
logger.error(f"Error retrieving archived video: {e}")
|
||||
return None
|
||||
return await self.query_manager.get_archived_video(url)
|
||||
|
||||
def is_url_archived(self, url: str) -> bool:
|
||||
async def is_url_archived(self, url: str) -> bool:
|
||||
"""Check if a URL has already been archived"""
|
||||
return await self.query_manager.is_url_archived(url)
|
||||
|
||||
async def get_guild_stats(self, guild_id: int) -> Dict[str, Any]:
|
||||
"""Get archiving statistics for a guild"""
|
||||
return await self.query_manager.get_guild_stats(guild_id)
|
||||
|
||||
async def get_channel_videos(
|
||||
self,
|
||||
channel_id: int,
|
||||
limit: int = 100,
|
||||
offset: int = 0
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Get archived videos for a channel"""
|
||||
return await self.query_manager.get_channel_videos(
|
||||
channel_id,
|
||||
limit,
|
||||
offset
|
||||
)
|
||||
|
||||
async def cleanup_old_records(self, days: int) -> int:
|
||||
"""Clean up records older than specified days"""
|
||||
return await self.query_manager.cleanup_old_records(days)
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close all database connections"""
|
||||
try:
|
||||
with sqlite3.connect(self.db_path) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT 1 FROM archived_videos WHERE original_url = ?", (url,))
|
||||
return cursor.fetchone() is not None
|
||||
except sqlite3.Error as e:
|
||||
logger.error(f"Error checking archived status: {e}")
|
||||
return False
|
||||
self.connection_manager.close_all()
|
||||
logger.info("Database connections closed")
|
||||
except Exception as e:
|
||||
logger.error(f"Error closing database connections: {e}")
|
||||
|
||||
async def __aenter__(self):
|
||||
"""Async context manager entry"""
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
"""Async context manager exit"""
|
||||
self.close()
|
||||
|
||||
Reference in New Issue
Block a user