import os import sys from dotenv import load_dotenv import mysql.connector # Add the project root to the Python path project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(project_root) from ktvmanager.lib.encryption import decrypt_password from ktvmanager.lib.checker import single_account_check from ktvmanager.lib.get_urls import get_latest_urls_from_dns from typing import List, Dict, Any from mysql.connector.connection import MySQLConnection def get_all_accounts(db_connection: MySQLConnection) -> List[Dict[str, Any]]: """Retrieves all user accounts from the database. Args: db_connection: An active MySQL database connection. Returns: A list of dictionaries, where each dictionary represents a user account. """ cursor = db_connection.cursor(dictionary=True) query = "SELECT * FROM userAccounts where userId = 1" cursor.execute(query) accounts = cursor.fetchall() cursor.close() return accounts def main() -> None: """ Checks the validity of all accounts in the database against available stream URLs. """ load_dotenv() db_connection = mysql.connector.connect( host=os.getenv("DBHOST"), user=os.getenv("DBUSER"), password=os.getenv("DBPASS"), database=os.getenv("DATABASE"), port=os.getenv("DBPORT"), ) accounts = get_all_accounts(db_connection) stream_urls = get_latest_urls_from_dns() for account in accounts: try: decrypted_password = decrypt_password(account["password"]) except Exception as e: print(f"Could not decrypt password for {account['username']}: {e}") continue account_data = { "username": account["username"], "password": decrypted_password, } result = single_account_check(account_data, stream_urls) if result: print( f"Account {account['username']} on stream {account['stream']} is VALID." ) else: print( f"Account {account['username']} on stream {account['stream']} is INVALID." ) db_connection.close() if __name__ == "__main__": main()