2025-05-10 13:58:58 +01:00
|
|
|
import os
|
|
|
|
import mysql.connector
|
2025-07-13 19:40:04 +01:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
from flask import jsonify, request
|
|
|
|
from ktvmanager.lib.checker import single_account_check
|
|
|
|
from ktvmanager.lib.encryption import decrypt_password
|
2025-05-10 13:58:58 +01:00
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def _create_connection():
|
|
|
|
return mysql.connector.connect(
|
|
|
|
host=os.getenv("DBHOST"),
|
|
|
|
user=os.getenv("DBUSER"),
|
|
|
|
password=os.getenv("DBPASS"),
|
|
|
|
database=os.getenv("DATABASE"),
|
|
|
|
port=os.getenv("DBPORT")
|
2025-05-10 13:58:58 +01:00
|
|
|
)
|
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def _execute_query(query, params=None):
|
|
|
|
conn = _create_connection()
|
|
|
|
cursor = conn.cursor(dictionary=True)
|
|
|
|
try:
|
|
|
|
cursor.execute(query, params)
|
|
|
|
if query.strip().upper().startswith("SELECT"):
|
|
|
|
result = cursor.fetchall()
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
result = {"affected_rows": cursor.rowcount}
|
|
|
|
return result
|
|
|
|
finally:
|
|
|
|
cursor.close()
|
|
|
|
conn.close()
|
2025-05-10 13:58:58 +01:00
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def get_user_id_from_username(username):
|
|
|
|
query = "SELECT id FROM users WHERE username = %s"
|
|
|
|
result = _execute_query(query, (username,))
|
|
|
|
if result:
|
|
|
|
return result[0]['id']
|
|
|
|
return None
|
2025-05-10 13:58:58 +01:00
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def get_user_accounts(user_id):
|
|
|
|
query = "SELECT * FROM userAccounts WHERE userID = %s"
|
|
|
|
accounts = _execute_query(query, (user_id,))
|
|
|
|
for account in accounts:
|
|
|
|
account['password'] = decrypt_password(account['password'])
|
|
|
|
return jsonify(accounts)
|
2025-05-10 13:58:58 +01:00
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def get_stream_names():
|
|
|
|
query = "SELECT streamName FROM streams"
|
|
|
|
results = _execute_query(query)
|
|
|
|
stream_names = [row['streamName'] for row in results]
|
|
|
|
return jsonify(stream_names)
|
2025-05-10 13:58:58 +01:00
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def single_check():
|
|
|
|
data = request.get_json()
|
|
|
|
# This is a placeholder for getting stream URLs. In a real application,
|
|
|
|
# this would likely come from a database query or a configuration file.
|
|
|
|
stream_urls = ["http://example.com", "http://example.org"]
|
|
|
|
result = single_account_check(data, stream_urls)
|
|
|
|
if result:
|
|
|
|
# Here you would typically update the database with the new information
|
|
|
|
return jsonify(result)
|
|
|
|
return jsonify({"message": "All checks failed"}), 400
|
2025-05-10 13:58:58 +01:00
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def add_account():
|
|
|
|
data = request.get_json()
|
|
|
|
query = "INSERT INTO userAccounts (username, stream, streamURL, expiaryDate, password, userID) VALUES (%s, %s, %s, %s, %s, %s)"
|
|
|
|
params = (data['username'], data['stream'], data['streamURL'], data['expiaryDate'], data['password'], data['userID'])
|
|
|
|
result = _execute_query(query, params)
|
|
|
|
return jsonify(result)
|
2025-05-10 13:58:58 +01:00
|
|
|
|
2025-07-13 19:40:04 +01:00
|
|
|
def delete_account():
|
|
|
|
data = request.get_json()
|
|
|
|
query = "DELETE FROM userAccounts WHERE id = %s"
|
|
|
|
params = (data['id'],)
|
|
|
|
result = _execute_query(query, params)
|
|
|
|
return jsonify(result)
|