import json
import pymysql

# MySQL Database Configurations
db_config = {
    "host": "127.0.0.1",
    "user": "uploadprocessor",
    "password": "UploadMyPh0to",
    "database": "timelapse"
}

# Output file for accounts
output_file = "accounts.json"

def fetch_b2_accounts():
    """Fetch B2 accounts from the MySQL database."""
    connection = pymysql.connect(**db_config)
    try:
        with connection.cursor() as cursor:
            cursor.execute("SELECT appKeyId as account_id, appKey as application_key FROM backBlazeKeys WHERE keyName='PHP-Scripting-RO'")
            accounts = cursor.fetchall()
            return [{"account_id": row[0], "application_key": row[1]} for row in accounts]
    finally:
        connection.close()

def save_accounts_to_file(accounts):
    """Save accounts to a JSON file."""
    with open(output_file, "w") as f:
        json.dump(accounts, f, indent=4)
    print(f"Account information saved to {output_file}")

def main():
    accounts = fetch_b2_accounts()
    if not accounts:
        print("No accounts found.")
        return
    save_accounts_to_file(accounts)

if __name__ == "__main__":
    main()
