From dba3388567818cb5c60be9d406d3e1cccc84b3ea Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 29 Apr 2026 14:22:03 +0200 Subject: Refactor database connections and add integration tests - Use context managers for sqlite3 connections in cron, vclogs, and database utils - Add URL helper utility for yt-dlp with clean_url_for_ytdlp function - Add slow integration test markers and new integration test files - Fix test imports to use conftest directly instead of tests.conftest - Improve downloader error handling and use COPYPARTY_URL_UPLOAD env var - Update pytest.ini with slow test markers and warning filters - Document slow test usage in AGENTS.md --- cogs/utils/database.py | 17 ++++++++++++++++- cogs/utils/url_helper.py | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 cogs/utils/url_helper.py (limited to 'cogs/utils') diff --git a/cogs/utils/database.py b/cogs/utils/database.py index 67cbc9f..ea7d356 100644 --- a/cogs/utils/database.py +++ b/cogs/utils/database.py @@ -1,6 +1,7 @@ import os import sqlite3 import logging +from contextlib import contextmanager logger = logging.getLogger(__name__) @@ -10,7 +11,8 @@ _initialized = False def get_connection(): - return sqlite3.connect(get_db_path()) + conn = sqlite3.connect(get_db_path()) + return _ConnectionContextManager(conn) def get_db_path(): @@ -22,6 +24,19 @@ def set_db_path(path): DB_PATH = path +class _ConnectionContextManager: + """Context manager for sqlite3 connections that auto-closes.""" + def __init__(self, conn): + self.conn = conn + + def __enter__(self): + return self.conn + + def __exit__(self, exc_type, exc_val, exc_tb): + self.conn.close() + return False + + def init_database(): global _initialized if _initialized: diff --git a/cogs/utils/url_helper.py b/cogs/utils/url_helper.py new file mode 100644 index 0000000..eac06f1 --- /dev/null +++ b/cogs/utils/url_helper.py @@ -0,0 +1,21 @@ +# This file is part of Senator Sperma +# Licensed under the GPL-3.0 License +# Copyright (C) 2026 Salmonik @ salmonik@tuta.io +# https://codeberg.org/salmonik/senator-sperma + + +def clean_url_for_ytdlp(url: str) -> str: + """Clean URL by removing everything after the first '&'. + + This prevents yt-dlp from interpreting additional query parameters + (like &list=...) as playlist URLs. + + Example: + Input: https://www.youtube.com/watch?v=lPGipwoJiOM&list=RDlPGipwoJiOM& + Output: https://www.youtube.com/watch?v=lPGipwoJiOM + """ + # Find the first '&' and truncate + first_ampersand = url.find('&') + if first_ampersand != -1: + return url[:first_ampersand] + return url -- cgit v1.2.3