mirror of
https://github.com/pacnpal/Pac-cogs.git
synced 2025-12-20 10:51:05 -05:00
fixed databasequerymanager
This commit is contained in:
@@ -7,7 +7,8 @@ from datetime import datetime
|
|||||||
|
|
||||||
logger = logging.getLogger("DBQueryManager")
|
logger = logging.getLogger("DBQueryManager")
|
||||||
|
|
||||||
class QueryManager:
|
|
||||||
|
class DatabaseQueryManager:
|
||||||
"""Manages database queries and operations"""
|
"""Manages database queries and operations"""
|
||||||
|
|
||||||
def __init__(self, connection_manager):
|
def __init__(self, connection_manager):
|
||||||
@@ -20,7 +21,7 @@ class QueryManager:
|
|||||||
message_id: int,
|
message_id: int,
|
||||||
channel_id: int,
|
channel_id: int,
|
||||||
guild_id: int,
|
guild_id: int,
|
||||||
metadata: Optional[Dict[str, Any]] = None
|
metadata: Optional[Dict[str, Any]] = None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Add a newly archived video to the database"""
|
"""Add a newly archived video to the database"""
|
||||||
try:
|
try:
|
||||||
@@ -43,11 +44,11 @@ class QueryManager:
|
|||||||
message_id,
|
message_id,
|
||||||
channel_id,
|
channel_id,
|
||||||
guild_id,
|
guild_id,
|
||||||
metadata.get('file_size'),
|
metadata.get("file_size"),
|
||||||
metadata.get('duration'),
|
metadata.get("duration"),
|
||||||
metadata.get('format'),
|
metadata.get("format"),
|
||||||
metadata.get('resolution'),
|
metadata.get("resolution"),
|
||||||
metadata.get('bitrate')
|
metadata.get("bitrate"),
|
||||||
)
|
)
|
||||||
|
|
||||||
cursor.execute(query, params)
|
cursor.execute(query, params)
|
||||||
@@ -58,37 +59,37 @@ class QueryManager:
|
|||||||
logger.error(f"Error adding archived video: {e}")
|
logger.error(f"Error adding archived video: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def get_archived_video(
|
async def get_archived_video(self, url: str) -> Optional[Dict[str, Any]]:
|
||||||
self,
|
|
||||||
url: str
|
|
||||||
) -> Optional[Dict[str, Any]]:
|
|
||||||
"""Get archived video information by original URL"""
|
"""Get archived video information by original URL"""
|
||||||
try:
|
try:
|
||||||
with self.connection_manager.get_connection() as conn:
|
with self.connection_manager.get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
SELECT discord_url, message_id, channel_id, guild_id,
|
SELECT discord_url, message_id, channel_id, guild_id,
|
||||||
file_size, duration, format, resolution, bitrate,
|
file_size, duration, format, resolution, bitrate,
|
||||||
archived_at
|
archived_at
|
||||||
FROM archived_videos
|
FROM archived_videos
|
||||||
WHERE original_url = ?
|
WHERE original_url = ?
|
||||||
""", (url,))
|
""",
|
||||||
|
(url,),
|
||||||
|
)
|
||||||
|
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
if not result:
|
if not result:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'discord_url': result[0],
|
"discord_url": result[0],
|
||||||
'message_id': result[1],
|
"message_id": result[1],
|
||||||
'channel_id': result[2],
|
"channel_id": result[2],
|
||||||
'guild_id': result[3],
|
"guild_id": result[3],
|
||||||
'file_size': result[4],
|
"file_size": result[4],
|
||||||
'duration': result[5],
|
"duration": result[5],
|
||||||
'format': result[6],
|
"format": result[6],
|
||||||
'resolution': result[7],
|
"resolution": result[7],
|
||||||
'bitrate': result[8],
|
"bitrate": result[8],
|
||||||
'archived_at': result[9]
|
"archived_at": result[9],
|
||||||
}
|
}
|
||||||
|
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
@@ -101,8 +102,7 @@ class QueryManager:
|
|||||||
with self.connection_manager.get_connection() as conn:
|
with self.connection_manager.get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"SELECT 1 FROM archived_videos WHERE original_url = ?",
|
"SELECT 1 FROM archived_videos WHERE original_url = ?", (url,)
|
||||||
(url,)
|
|
||||||
)
|
)
|
||||||
return cursor.fetchone() is not None
|
return cursor.fetchone() is not None
|
||||||
|
|
||||||
@@ -115,7 +115,8 @@ class QueryManager:
|
|||||||
try:
|
try:
|
||||||
with self.connection_manager.get_connection() as conn:
|
with self.connection_manager.get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(*) as total_videos,
|
COUNT(*) as total_videos,
|
||||||
SUM(file_size) as total_size,
|
SUM(file_size) as total_size,
|
||||||
@@ -123,36 +124,36 @@ class QueryManager:
|
|||||||
MAX(archived_at) as last_archived
|
MAX(archived_at) as last_archived
|
||||||
FROM archived_videos
|
FROM archived_videos
|
||||||
WHERE guild_id = ?
|
WHERE guild_id = ?
|
||||||
""", (guild_id,))
|
""",
|
||||||
|
(guild_id,),
|
||||||
|
)
|
||||||
|
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
return {
|
return {
|
||||||
'total_videos': result[0],
|
"total_videos": result[0],
|
||||||
'total_size': result[1] or 0,
|
"total_size": result[1] or 0,
|
||||||
'avg_duration': result[2] or 0,
|
"avg_duration": result[2] or 0,
|
||||||
'last_archived': result[3]
|
"last_archived": result[3],
|
||||||
}
|
}
|
||||||
|
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
logger.error(f"Error getting guild stats: {e}")
|
logger.error(f"Error getting guild stats: {e}")
|
||||||
return {
|
return {
|
||||||
'total_videos': 0,
|
"total_videos": 0,
|
||||||
'total_size': 0,
|
"total_size": 0,
|
||||||
'avg_duration': 0,
|
"avg_duration": 0,
|
||||||
'last_archived': None
|
"last_archived": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
async def get_channel_videos(
|
async def get_channel_videos(
|
||||||
self,
|
self, channel_id: int, limit: int = 100, offset: int = 0
|
||||||
channel_id: int,
|
|
||||||
limit: int = 100,
|
|
||||||
offset: int = 0
|
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Get archived videos for a channel"""
|
"""Get archived videos for a channel"""
|
||||||
try:
|
try:
|
||||||
with self.connection_manager.get_connection() as conn:
|
with self.connection_manager.get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
SELECT original_url, discord_url, message_id,
|
SELECT original_url, discord_url, message_id,
|
||||||
file_size, duration, format, resolution,
|
file_size, duration, format, resolution,
|
||||||
archived_at
|
archived_at
|
||||||
@@ -160,19 +161,24 @@ class QueryManager:
|
|||||||
WHERE channel_id = ?
|
WHERE channel_id = ?
|
||||||
ORDER BY archived_at DESC
|
ORDER BY archived_at DESC
|
||||||
LIMIT ? OFFSET ?
|
LIMIT ? OFFSET ?
|
||||||
""", (channel_id, limit, offset))
|
""",
|
||||||
|
(channel_id, limit, offset),
|
||||||
|
)
|
||||||
|
|
||||||
results = cursor.fetchall()
|
results = cursor.fetchall()
|
||||||
return [{
|
return [
|
||||||
'original_url': row[0],
|
{
|
||||||
'discord_url': row[1],
|
"original_url": row[0],
|
||||||
'message_id': row[2],
|
"discord_url": row[1],
|
||||||
'file_size': row[3],
|
"message_id": row[2],
|
||||||
'duration': row[4],
|
"file_size": row[3],
|
||||||
'format': row[5],
|
"duration": row[4],
|
||||||
'resolution': row[6],
|
"format": row[5],
|
||||||
'archived_at': row[7]
|
"resolution": row[6],
|
||||||
} for row in results]
|
"archived_at": row[7],
|
||||||
|
}
|
||||||
|
for row in results
|
||||||
|
]
|
||||||
|
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
logger.error(f"Error getting channel videos: {e}")
|
logger.error(f"Error getting channel videos: {e}")
|
||||||
@@ -183,10 +189,13 @@ class QueryManager:
|
|||||||
try:
|
try:
|
||||||
with self.connection_manager.get_connection() as conn:
|
with self.connection_manager.get_connection() as conn:
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute("""
|
cursor.execute(
|
||||||
|
"""
|
||||||
DELETE FROM archived_videos
|
DELETE FROM archived_videos
|
||||||
WHERE archived_at < datetime('now', ? || ' days')
|
WHERE archived_at < datetime('now', ? || ' days')
|
||||||
""", (-days,))
|
""",
|
||||||
|
(-days,),
|
||||||
|
)
|
||||||
|
|
||||||
deleted = cursor.rowcount
|
deleted = cursor.rowcount
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ from pathlib import Path
|
|||||||
from typing import Optional, Dict, Any, List
|
from typing import Optional, Dict, Any, List
|
||||||
|
|
||||||
from .schema_manager import SchemaManager
|
from .schema_manager import SchemaManager
|
||||||
from .query_manager import QueryManager
|
from .query_manager import DatabaseQueryManager
|
||||||
from .connection_manager import ConnectionManager
|
from .connection_manager import ConnectionManager
|
||||||
|
|
||||||
logger = logging.getLogger("VideoArchiverDB")
|
logger = logging.getLogger("VideoArchiverDB")
|
||||||
|
|
||||||
|
|
||||||
class VideoArchiveDB:
|
class VideoArchiveDB:
|
||||||
"""Manages the SQLite database for archived videos"""
|
"""Manages the SQLite database for archived videos"""
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ class VideoArchiveDB:
|
|||||||
# Initialize managers
|
# Initialize managers
|
||||||
self.connection_manager = ConnectionManager(self.db_path)
|
self.connection_manager = ConnectionManager(self.db_path)
|
||||||
self.schema_manager = SchemaManager(self.db_path)
|
self.schema_manager = SchemaManager(self.db_path)
|
||||||
self.query_manager = QueryManager(self.connection_manager)
|
self.query_manager = DatabaseQueryManager(self.connection_manager)
|
||||||
|
|
||||||
# Initialize database schema
|
# Initialize database schema
|
||||||
self.schema_manager.initialize_schema()
|
self.schema_manager.initialize_schema()
|
||||||
@@ -39,16 +40,11 @@ class VideoArchiveDB:
|
|||||||
message_id: int,
|
message_id: int,
|
||||||
channel_id: int,
|
channel_id: int,
|
||||||
guild_id: int,
|
guild_id: int,
|
||||||
metadata: Optional[Dict[str, Any]] = None
|
metadata: Optional[Dict[str, Any]] = None,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Add a newly archived video to the database"""
|
"""Add a newly archived video to the database"""
|
||||||
return await self.query_manager.add_archived_video(
|
return await self.query_manager.add_archived_video(
|
||||||
original_url,
|
original_url, discord_url, message_id, channel_id, guild_id, metadata
|
||||||
discord_url,
|
|
||||||
message_id,
|
|
||||||
channel_id,
|
|
||||||
guild_id,
|
|
||||||
metadata
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def get_archived_video(self, url: str) -> Optional[Dict[str, Any]]:
|
async def get_archived_video(self, url: str) -> Optional[Dict[str, Any]]:
|
||||||
@@ -64,17 +60,10 @@ class VideoArchiveDB:
|
|||||||
return await self.query_manager.get_guild_stats(guild_id)
|
return await self.query_manager.get_guild_stats(guild_id)
|
||||||
|
|
||||||
async def get_channel_videos(
|
async def get_channel_videos(
|
||||||
self,
|
self, channel_id: int, limit: int = 100, offset: int = 0
|
||||||
channel_id: int,
|
|
||||||
limit: int = 100,
|
|
||||||
offset: int = 0
|
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Get archived videos for a channel"""
|
"""Get archived videos for a channel"""
|
||||||
return await self.query_manager.get_channel_videos(
|
return await self.query_manager.get_channel_videos(channel_id, limit, offset)
|
||||||
channel_id,
|
|
||||||
limit,
|
|
||||||
offset
|
|
||||||
)
|
|
||||||
|
|
||||||
async def cleanup_old_records(self, days: int) -> int:
|
async def cleanup_old_records(self, days: int) -> int:
|
||||||
"""Clean up records older than specified days"""
|
"""Clean up records older than specified days"""
|
||||||
|
|||||||
Reference in New Issue
Block a user