From 9bd1e249ed4e7dffea890a3c6fae8c6d0ee93f7f Mon Sep 17 00:00:00 2001 From: Oscar <122336651@umail.ucc.ie> Date: Thu, 13 Feb 2025 14:24:05 +0000 Subject: [PATCH] FEAT: Added offset to routes when searching for categories --- web_server/blueprints/streams.py | 10 ++++++---- web_server/utils/recommendation_utils.py | 14 +++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/web_server/blueprints/streams.py b/web_server/blueprints/streams.py index c252df2..999424a 100644 --- a/web_server/blueprints/streams.py +++ b/web_server/blueprints/streams.py @@ -36,14 +36,15 @@ def popular_streams(no_streams) -> list[dict]: return jsonify(streams) @stream_bp.route('/streams/popular/') -def popular_streams_by_category(category_name) -> list[dict]: +@stream_bp.route('/streams/popular///') +def popular_streams_by_category(category_name, no_streams=4, offset=0) -> list[dict]: """ Returns a list of streams live now with the highest viewers in a given category """ category_id = get_category_id(category_name) - streams = get_streams_based_on_category(category_id) + streams = get_streams_based_on_category(category_id, no_streams, offset) return jsonify(streams) @login_required @@ -71,7 +72,8 @@ def stream_data(streamer_id): ## Category Routes @stream_bp.route('/categories/popular/') -def popular_categories(no_categories) -> list[dict]: +@stream_bp.route('/categories/popular//') +def popular_categories(no_categories, offset=0) -> list[dict]: """ Returns a list of most popular categories """ @@ -81,7 +83,7 @@ def popular_categories(no_categories) -> list[dict]: elif no_categories > 100: no_categories = 100 - category_data = get_highest_view_categories(no_categories) + category_data = get_highest_view_categories(no_categories, offset) return jsonify(category_data) @login_required diff --git a/web_server/utils/recommendation_utils.py b/web_server/utils/recommendation_utils.py index ec921bd..347c6d6 100644 --- a/web_server/utils/recommendation_utils.py +++ b/web_server/utils/recommendation_utils.py @@ -34,7 +34,7 @@ def get_followed_categories_recommendations(user_id: int, no_streams: int = 4) - return streams -def get_streams_based_on_category(category_id: int, no_streams: int = 4) -> Optional[List[dict]]: +def get_streams_based_on_category(category_id: int, no_streams: int = 4, offset: int = 0) -> Optional[List[dict]]: """ Queries stream database to get top most viewed streams based on given category """ @@ -46,8 +46,8 @@ def get_streams_based_on_category(category_id: int, no_streams: int = 4) -> Opti JOIN categories c ON s.category_id = c.category_id WHERE c.category_id = ? ORDER BY num_viewers DESC - LIMIT ? - """, (category_id, no_streams)) + LIMIT ? OFFSET ? + """, (category_id, no_streams, offset)) return streams @@ -66,9 +66,9 @@ def get_highest_view_streams(no_streams: int = 4) -> Optional[List[dict]]: """, (no_streams,)) return data -def get_highest_view_categories(no_categories: int = 4) -> Optional[List[dict]]: +def get_highest_view_categories(no_categories: int = 4, offset: int = 0) -> Optional[List[dict]]: """ - Returns a list of top most popular categories + Returns a list of top most popular categories given offset """ with Database() as db: categories = db.fetchall(""" @@ -77,8 +77,8 @@ def get_highest_view_categories(no_categories: int = 4) -> Optional[List[dict]]: JOIN categories ON streams.category_id = categories.category_id GROUP BY categories.category_name ORDER BY SUM(streams.num_viewers) DESC - LIMIT ?; - """, (no_categories,)) + LIMIT ? OFFSET ?; + """, (no_categories, offset)) return categories def get_user_category_recommendations(user_id: int, no_categories: int = 4) -> Optional[List[dict]]: