mpv_commander/mpv_dbbuilder.py
2025-04-24 15:28:53 -04:00

72 lines
2.2 KiB
Python

import sqlite3
import json
import sys
destroy_table = r'''
DROP TABLE IF EXISTS buttons
'''
make_table = r'''
CREATE TABLE IF NOT EXISTS buttons
(button_name TEXT PRIMARY KEY,
video_url TEXT NOT NULL,
is_default BOOLEAN NOT NULL CHECK (is_default IN (0, 1)),
is_active BOOLEAN NOT NULL CHECK (is_active IN (0, 1)))
'''
insert_row = r'''
INSERT INTO buttons VALUES
(?, ?, ?, ?)
'''
with open("config.json", "r") as config_file:
config_json = json.loads(config_file.read())
config_issues = ""
if not "instance_name" in config_json.keys():
config_issues = config_issues + "The config item instance_name is required\n"
for i in range(len(config_json["buttons"])):
if not "name" in config_json["buttons"][i].keys():
config_issues = config_issues + "Config index {index} is missing the required name parameter\n".format(index = (i + 1))
if not "video_url" in config_json["buttons"][i].keys():
config_issues = config_issues + "Config index {index} is missing the required video_url parameter\n".format(index = (i + 1))
if not "default" in config_json["buttons"][i].keys():
config_issues = config_issues + "Config index {index} is missing the required default parameter\n".format(index = (i + 1))
all_defaults = [element for element in config_json["buttons"] if element["default"]]
if len(all_defaults) > 1:
config_issues = config_issues + "More than one button config is set as default, only one can be the default, remove one of these: {button_list}\n".format(
button_list = ", ".join([element["name"] if "name" in element else "NAME MISSING" for element in all_defaults])
)
elif len(all_defaults) == 0:
config_issues = config_issues = "At least one button config must be set as the default\n"
if len(config_issues) != 0:
print("Config issues detected! Please correct these issues before trying again")
print(config_issues)
sys.exit(1)
db = sqlite3.connect("mpvcommander.db")
db.execute(destroy_table)
db.execute(make_table)
for button in config_json["buttons"]:
db.execute(insert_row, [
button["name"],
button["video_url"],
1 if button["default"] else 0,
1 if button["default"] else 0
])
db.commit()
db.close()