Skip to content
Snippets Groups Projects
Commit faba1014 authored by jamie's avatar jamie
Browse files

Start of work on creating generic feed

parent dfdf8e5b
No related branches found
No related tags found
1 merge request!23Merging current work on feedMicroservice
......@@ -9,7 +9,7 @@ CORS(app)
def hello_world():
return 'musicMicroservice is running on port 5051'
@app.route('/getFeed', methods=['POST', 'GET'])
@app.route('/getFeed', methods=['GET'])
def getFeed():
if request.method == 'GET':
userID = request.args.get('userID')
......@@ -45,6 +45,11 @@ def getFeed():
ratingsFeed.append(song)
sortedFeed = sorted(ratingsFeed, key=lambda x: x[2], reverse=True)
return sortedFeed
@app.route('/getFeedGeneric', methods=['GET'])
def getFeedGeneric():
return
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5051)
\ No newline at end of file
......@@ -2,6 +2,7 @@ from flask import Flask, request
from flask_cors import CORS
import sqlite3
from sqlite3 import Error
from flask import jsonify
app = Flask(__name__)
CORS(app)
......@@ -123,7 +124,25 @@ def all_ratings():
cursor.execute("SELECT track_id, rating, updated_at FROM ratings WHERE user_id=? ", (userID))
ratings = cursor.fetchall()
conn.close()
return ratings
return ratings
@app.route('/top_ratings/')
def top_ratings():
conn = sqlite3.connect('best_listens.db')
cursor = conn.cursor()
# Execute SQL query to group ratings by track_id and calculate average rating
cursor.execute("SELECT track_id, AVG(rating) as avg_rating FROM ratings GROUP BY track_id ORDER BY avg_rating DESC LIMIT 10")
results = cursor.fetchall()
# Create a list of dictionaries to store the results
top_tracks = []
for result in results:
track_dict = {'track_id': result[0], 'avg_rating': result[1]}
top_tracks.append(track_dict)
conn.close()
return jsonify({'top_tracks': top_tracks})
if __name__ == '__main__':
app.run(host='0.0.0.0',port=5050)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment