2020-07-12 18:53:04 +00:00
|
|
|
# Views and endpoints for the initial setup
|
2020-05-02 17:32:58 +00:00
|
|
|
from flask import request, jsonify, render_template
|
|
|
|
from configparser import RawConfigParser
|
|
|
|
from jellyfin_accounts.jf_api import Jellyfin
|
2020-06-29 21:05:40 +00:00
|
|
|
from jellyfin_accounts import config, config_path, app, first_run, resp
|
2020-06-16 19:07:47 +00:00
|
|
|
from jellyfin_accounts import web_log as log
|
2020-05-02 17:32:58 +00:00
|
|
|
import os
|
2020-07-20 14:37:19 +00:00
|
|
|
import psutil
|
|
|
|
import sys
|
2020-05-02 17:32:58 +00:00
|
|
|
|
|
|
|
if first_run:
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-05-02 17:32:58 +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",
|
|
|
|
)
|
2020-05-02 17:32:58 +00:00
|
|
|
|
|
|
|
@app.errorhandler(404)
|
|
|
|
def page_not_found(e):
|
2020-06-21 19:29:53 +00:00
|
|
|
return render_template("404.html"), 404
|
2020-05-02 17:32:58 +00:00
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
@app.route("/", methods=["GET", "POST"])
|
2020-05-02 17:32:58 +00:00
|
|
|
def setup():
|
2020-06-21 19:29:53 +00:00
|
|
|
return render_template("setup.html")
|
2020-05-02 17:32:58 +00:00
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
@app.route("/<path:path>")
|
2020-05-02 17:32:58 +00:00
|
|
|
def static_proxy(path):
|
2020-06-21 19:29:53 +00:00
|
|
|
if "html" not in path:
|
2020-05-02 17:32:58 +00:00
|
|
|
return app.send_static_file(path)
|
|
|
|
else:
|
2020-06-21 19:29:53 +00:00
|
|
|
return render_template("404.html"), 404
|
2020-05-02 17:32:58 +00:00
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
@app.route("/modifyConfig", methods=["POST"])
|
2020-05-02 17:32:58 +00:00
|
|
|
def modifyConfig():
|
2020-06-21 19:29:53 +00:00
|
|
|
log.info("Config modification requested")
|
2020-05-02 17:32:58 +00:00
|
|
|
data = request.get_json()
|
2020-06-21 19:29:53 +00:00
|
|
|
temp_config = RawConfigParser(comment_prefixes="/", allow_no_value=True)
|
2020-05-02 17:32:58 +00:00
|
|
|
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")
|
2020-05-02 17:32:58 +00:00
|
|
|
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:
|
2020-05-02 17:32:58 +00:00
|
|
|
temp_config.write(config_file)
|
2020-06-21 19:29:53 +00:00
|
|
|
log.debug("Config written")
|
2020-07-20 14:37:19 +00:00
|
|
|
log.info('Restarting...')
|
|
|
|
try:
|
|
|
|
p = psutil.Process(os.getpid())
|
|
|
|
for handler in p.open_files() + p.connections():
|
|
|
|
os.close(handler.fd)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
python = sys.executable
|
|
|
|
os.execl(python, python, *sys.argv)
|
2020-05-02 17:32:58 +00:00
|
|
|
return resp()
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
@app.route("/testJF", methods=["GET", "POST"])
|
2020-05-02 17:32:58 +00:00
|
|
|
def testJF():
|
|
|
|
data = request.get_json()
|
2020-06-21 19:29:53 +00:00
|
|
|
tempjf = tempJF(data["jfHost"])
|
2020-05-02 17:32:58 +00:00
|
|
|
try:
|
2020-06-21 19:29:53 +00:00
|
|
|
tempjf.authenticate(data["jfUser"], data["jfPassword"])
|
2020-05-02 17:32:58 +00:00
|
|
|
tempjf.getUsers(public=False)
|
|
|
|
return resp()
|
|
|
|
except:
|
|
|
|
return resp(False)
|