Add config

This commit is contained in:
Phillip Kühne 2019-05-26 02:55:11 +02:00
parent 2e65679c06
commit c62314d9eb
3 changed files with 31 additions and 5 deletions

4
config.json Normal file
View File

@ -0,0 +1,4 @@
{
"password": "w",
"username": "admin"
}

View File

@ -1,5 +1,9 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import json
import os
config_file = "config.json"
def get_catalog_url(): def get_catalog_url():
r = requests.get('https://www.karafun.de/karaoke-song-list.html') r = requests.get('https://www.karafun.de/karaoke-song-list.html')
@ -10,3 +14,20 @@ def get_catalog_url():
def get_songs(url): def get_songs(url):
r = requests.get(url) r = requests.get(url)
return r.text 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']

11
main.py
View File

@ -6,9 +6,6 @@ import json
from flask_basicauth import BasicAuth from flask_basicauth import BasicAuth
app = Flask(__name__, static_url_path='/static') app = Flask(__name__, static_url_path='/static')
app.config['BASIC_AUTH_USERNAME'] = 'admin'
app.config['BASIC_AUTH_PASSWORD'] = 'Karaoke2019blubb'
basic_auth = BasicAuth(app) basic_auth = BasicAuth(app)
@app.route("/") @app.route("/")
@ -38,10 +35,12 @@ def songs():
return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json') return Response(json.dumps(list, ensure_ascii=False).encode('utf-8'), mimetype='text/json')
@app.route("/api/songs/update") @app.route("/api/songs/update")
@basic_auth.required
def update_songs(): def update_songs():
database.delete_all_entries() database.delete_all_entries()
database.import_songs(helpers.get_songs(helpers.get_catalog_url())) status = database.import_songs(helpers.get_songs(helpers.get_catalog_url()))
return Response('{"status":"OK"}', mimetype='text/json') print(status)
return Response('{"status": "%s" }' % status, mimetype='text/json')
@app.route("/api/songs/compl") @app.route("/api/songs/compl")
@ -82,6 +81,8 @@ def activate_job():
database.create_entry_table() database.create_entry_table()
database.create_song_table() database.create_song_table()
database.create_list_view() database.create_list_view()
helpers.setup_config(app)
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0') app.run(debug=True, host='0.0.0.0')