Browse Source

Add api endpoint for favourite track

master
parent
commit
b771275d5c
Signed by: Christian van Onzenoodt <christian.van-onzenoodt@uni-ulm.de> GPG Key ID: 7B2E632FDC852341
2 changed files with 22 additions and 3 deletions
  1. 3
    1
      trackify/server/server.py
  2. 19
    2
      trackify/server/views.py

+ 3
- 1
trackify/server/server.py View File

@@ -4,7 +4,7 @@ from flask_apscheduler import APScheduler
4 4
 from flask_socketio import SocketIO, emit
5 5
 from pony.flask import Pony
6 6
 from trackify.server.logger import Logger
7
-from trackify.server.views import HistoryView
7
+from trackify.server.views import HistoryView, FavouriteTrackView
8 8
 from trackify.tracker.tracker import scrape_current_track, \
9 9
     NotAuthorizedException
10 10
 
@@ -69,6 +69,8 @@ def test_handler(data):
69 69
 def register_routes(flask_app):
70 70
     flask_app.add_url_rule('/api/history',
71 71
                            view_func=HistoryView.as_view('history'))
72
+    flask_app.add_url_rule('/api/favourite/track',
73
+                           view_func=FavouriteTrackView.as_view('favouriteTrack'))
72 74
 
73 75
 
74 76
 def register_event_handlers(flask_socket):

+ 19
- 2
trackify/server/views.py View File

@@ -1,6 +1,7 @@
1
+import datetime
1 2
 from flask.views import MethodView
2
-from flask import jsonify
3
-from pony.orm import desc
3
+from flask import jsonify, request
4
+from pony.orm import desc, select, count
4 5
 from trackify.shared.models import Request
5 6
 
6 7
 
@@ -9,3 +10,19 @@ class HistoryView(MethodView):
9 10
         requests = list(Request.select()
10 11
                         .order_by(desc(Request.spotify_timestamp)))
11 12
         return jsonify(list(map(lambda x: x.to_dict(), requests)))
13
+
14
+
15
+class FavouriteTrackView(MethodView):
16
+    def get(self):
17
+        start = request.args.get('start')
18
+        end = request.args.get('end')
19
+        start = datetime.datetime.fromisoformat(start)
20
+        end = datetime.datetime.fromisoformat(end)
21
+
22
+        requests = select(r for r in Request
23
+                          if r.spotify_timestamp > end and
24
+                          r.spotify_timestamp < start)
25
+
26
+        top_tracks = select((r.item, count(r)) for r in requests).order_by(desc(2))
27
+
28
+        return jsonify(list(map(lambda i: {'item': i[0].to_dict(), 'plays': i[1]}, top_tracks)))

Loading…
Cancel
Save