From c62314d9eb75c4635be8620cef008e2e1f09b04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Sun, 26 May 2019 02:55:11 +0200 Subject: [PATCH] Add config --- config.json | 4 ++++ helpers.py | 21 +++++++++++++++++++++ main.py | 11 ++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..14219c9 --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "password": "w", + "username": "admin" +} \ No newline at end of file diff --git a/helpers.py b/helpers.py index 2fd1234..80af65f 100644 --- a/helpers.py +++ b/helpers.py @@ -1,5 +1,9 @@ import requests from bs4 import BeautifulSoup +import json +import os + +config_file = "config.json" def get_catalog_url(): r = requests.get('https://www.karafun.de/karaoke-song-list.html') @@ -10,3 +14,20 @@ def get_catalog_url(): def get_songs(url): r = requests.get(url) return r.text + +def check_config_exists(): + return os.path.isfile(config_file) + +def setup_config(app): + if check_config_exists(): + config = json.load(open(config_file)) + with open(config_file, 'r') as handle: + config = json.load(handle) + print("Loaded existing config") + else: + config = {'username': 'admin', 'password': 'Karaoke2019blubb'} + with open(config_file, 'w') as handle: + json.dump(config, handle, indent=4, sort_keys=True) + print("Wrote new config") + app.config['BASIC_AUTH_USERNAME'] = config['username'] + app.config['BASIC_AUTH_PASSWORD'] = config['password'] diff --git a/main.py b/main.py index fa092d6..98d81ac 100644 --- a/main.py +++ b/main.py @@ -6,9 +6,6 @@ import json from flask_basicauth import BasicAuth app = Flask(__name__, static_url_path='/static') -app.config['BASIC_AUTH_USERNAME'] = 'admin' -app.config['BASIC_AUTH_PASSWORD'] = 'Karaoke2019blubb' - basic_auth = BasicAuth(app) @app.route("/") @@ -38,10 +35,12 @@ def songs(): return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') @app.route("/api/songs/update") +@basic_auth.required def update_songs(): database.delete_all_entries() - database.import_songs(helpers.get_songs(helpers.get_catalog_url())) - return Response('{"status":"OK"}', mimetype='text/json') + status = database.import_songs(helpers.get_songs(helpers.get_catalog_url())) + print(status) + return Response('{"status": "%s" }' % status, mimetype='text/json') @app.route("/api/songs/compl") @@ -82,6 +81,8 @@ def activate_job(): database.create_entry_table() database.create_song_table() database.create_list_view() + helpers.setup_config(app) + if __name__ == "__main__": app.run(debug=True, host='0.0.0.0')