FEAT: Added offset to routes when searching for categories

This commit is contained in:
Oscar
2025-02-13 14:24:05 +00:00
parent e5e92db400
commit 9bd1e249ed
2 changed files with 13 additions and 11 deletions

View File

@@ -36,14 +36,15 @@ def popular_streams(no_streams) -> list[dict]:
return jsonify(streams)
@stream_bp.route('/streams/popular/<string:category_name>')
def popular_streams_by_category(category_name) -> list[dict]:
@stream_bp.route('/streams/popular/<string:category_name>/<int:no_streams>/<int:offset>')
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/<int:no_categories>')
def popular_categories(no_categories) -> list[dict]:
@stream_bp.route('/categories/popular/<int:no_categories>/<int:offset>')
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

View File

@@ -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]]: