jellyfin-accounts/jellyfin_accounts/setup.py

68 lines
2.3 KiB
Python
Raw Normal View History

from flask import request, jsonify, render_template
from configparser import RawConfigParser
from jellyfin_accounts.jf_api import Jellyfin
from jellyfin_accounts import config, config_path, app, first_run
from jellyfin_accounts import web_log as log
2020-06-21 19:21:33 +00:00
from jellyfin_accounts.web_api import resp
import os
if first_run:
2020-06-21 19:29:53 +00:00
def tempJF(server):
2020-06-21 19:29:53 +00:00
return Jellyfin(
server,
config["jellyfin"]["client"],
config["jellyfin"]["version"],
config["jellyfin"]["device"] + "_temp",
config["jellyfin"]["device_id"] + "_temp",
)
@app.errorhandler(404)
def page_not_found(e):
2020-06-21 19:29:53 +00:00
return render_template("404.html"), 404
2020-06-21 19:29:53 +00:00
@app.route("/", methods=["GET", "POST"])
def setup():
2020-06-21 19:29:53 +00:00
return render_template("setup.html")
2020-06-21 19:29:53 +00:00
@app.route("/<path:path>")
def static_proxy(path):
2020-06-21 19:29:53 +00:00
if "html" not in path:
return app.send_static_file(path)
else:
2020-06-21 19:29:53 +00:00
return render_template("404.html"), 404
2020-06-21 19:29:53 +00:00
@app.route("/modifyConfig", methods=["POST"])
def modifyConfig():
2020-06-21 19:29:53 +00:00
log.info("Config modification requested")
data = request.get_json()
2020-06-21 19:29:53 +00:00
temp_config = RawConfigParser(comment_prefixes="/", allow_no_value=True)
temp_config.read(config_path)
for section in data:
if section in temp_config:
for item in data[section]:
if item in temp_config[section]:
temp_config[section][item] = data[section][item]
data[section][item] = True
2020-06-21 19:29:53 +00:00
log.debug(f"{section}/{item} modified")
else:
data[section][item] = False
2020-06-21 19:29:53 +00:00
log.debug(f"{section}/{item} does not exist in config")
with open(config_path, "w") as config_file:
temp_config.write(config_file)
2020-06-21 19:29:53 +00:00
log.debug("Config written")
2020-06-21 19:21:33 +00:00
# ugly exit, sorry
os._exit(1)
return resp()
2020-06-21 19:29:53 +00:00
@app.route("/testJF", methods=["GET", "POST"])
def testJF():
data = request.get_json()
2020-06-21 19:29:53 +00:00
tempjf = tempJF(data["jfHost"])
try:
2020-06-21 19:29:53 +00:00
tempjf.authenticate(data["jfUser"], data["jfPassword"])
tempjf.getUsers(public=False)
return resp()
except:
return resp(False)