diff --git a/backend/app.py b/backend/app.py index d10585c..195ba9f 100644 --- a/backend/app.py +++ b/backend/app.py @@ -7,6 +7,7 @@ import os import json from flask_basicauth import BasicAuth from helpers import nocache +from werkzeug.utils import secure_filename app = Flask(__name__, static_url_path='/static') basic_auth = BasicAuth(app) @@ -186,6 +187,54 @@ def query_songs_with_details_suggest(input_string=""): return jsonify(result) +@app.route("/api/songs/stats") +@nocache +# Return the data from long_term_stats as json +def get_stats(): + db_result = database.get_long_term_stats() + data = [] + for row in db_result: + data.append(dict(zip(['id', 'count'], row))) + return jsonify(data) + + +@app.route("/api/songs/stats.csv") +@nocache +# Return data from long_term_stats as csv +def get_stats_csv(): + db_result = database.get_long_term_stats() + print(db_result) + csv = "Id,Playbacks\n" + for row in db_result: + csv += str(row[0]) + "," + str(row[1]) + "\n" + return Response(csv, mimetype='text/csv') + + +@app.route("/api/songs/stats.csv", methods=['POST']) +@nocache +@basic_auth.required +# Update long_term_stats from csv +def update_stats_csv(): + if not request.files: + abort(400) + file = request.files['file'] + if file.filename is None: + abort(400) + else: + filename = secure_filename(file.filename) + if filename == '': + abort(400) + if not filename.endswith('.csv'): + abort(400) + if file: + if database.import_stats(file): + return Response('{"status": "OK"}', mimetype='text/json') + else: + return Response('{"status": "FAIL"}', mimetype='text/json', status=400) + else: + abort(400) + + @app.route("/api/songs/details/") def get_song_details(song_id): result = database.get_song_details(song_id) diff --git a/backend/database.py b/backend/database.py index 25cd81a..6d18d84 100644 --- a/backend/database.py +++ b/backend/database.py @@ -40,6 +40,20 @@ def import_songs(song_csv): return ("Imported songs ({} in Database)".format(num_songs)) +def import_stats(stats_csv): + print("Start importing Stats...") + df = pandas.read_csv(stats_csv, sep=',') + if (df.columns[0] != "Id" or df.columns[1] != "Playbacks"): + return False + with get_db_engine().connect() as conn: + for index, row in df.iterrows(): + stmt = text( + "INSERT INTO long_term_stats (Id,Playbacks) VALUES (:par_id,:par_playbacks) ON DUPLICATE KEY UPDATE Playbacks=:par_playbacks") + conn.execute(stmt, {"par_id": row["Id"], "par_playbacks": row["Playbacks"]}) + conn.commit() + return True + + def create_schema(): create_song_table() create_entry_table() @@ -161,6 +175,16 @@ def get_song_suggestions(count: int): return cur.fetchall() +def get_long_term_stats(): + with get_db_engine().connect() as conn: + stmt = text(""" + SELECT lts.Id, lts.Playbacks + FROM long_term_stats lts + """) + cur = conn.execute(stmt) + return cur.fetchall() + + def get_song_list(): with get_db_engine().connect() as conn: stmt = text("SELECT Artist || \" - \" || Title AS Song, Id FROM songs;") diff --git a/backend/templates/settings.html b/backend/templates/settings.html index 7722183..e368706 100644 --- a/backend/templates/settings.html +++ b/backend/templates/settings.html @@ -4,7 +4,8 @@

- +

@@ -14,7 +15,7 @@

@@ -24,7 +25,8 @@

- +

@@ -32,10 +34,69 @@

+
+

+ +

+
+ +
+
+ + +
+ +
+
+ +

+

+
Current config:
{% for key, val in config.items() %}{{key}}: {{val}}
{% endfor %}
{% endblock %} {% block extrajs %} + {% endblock %} \ No newline at end of file