From cf773069f27fb1f39a2f60dad0c51a1c16af1ec2 Mon Sep 17 00:00:00 2001 From: mein Name Date: Mon, 3 Feb 2025 18:03:55 -0500 Subject: [PATCH] update trending apis --- catalog/apis.py | 81 ++++++++++++++++++++++++++++++++++++-- catalog/jobs/discover.py | 7 +++- catalog/views.py | 5 ++- journal/apis/collection.py | 21 ++++++++++ 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/catalog/apis.py b/catalog/apis.py index a3ebb15f..71987ddb 100644 --- a/catalog/apis.py +++ b/catalog/apis.py @@ -142,21 +142,96 @@ def fetch_item(request, url: str): summary="Trending items in catalog", auth=None, tags=["catalog"], + deprecated=True, ) def trending_items(request): """ - Returns a list of galleries, each gallery is a list of items + Returns a list of galleries, each gallery is a list of items, deprecated, removing by May 1 2025 """ gallery_list = cache.get("public_gallery", []) # rotate every 6 minutes rot = timezone.now().minute // 6 for gallery in gallery_list: - i = rot * len(gallery["items"]) // 10 - gallery["items"] = gallery["items"][i:] + gallery["items"][:i] + items = cache.get(gallery["name"], []) + i = rot * len(items) // 10 + gallery["items"] = items[i:] + items[:i] return 200, gallery_list +def _get_trending(name): + rot = timezone.now().minute // 6 + items = cache.get(name, []) + i = rot * len(items) // 10 + return items[i:] + items[:i] + + +@api.get( + "/catalog/trending/book/", + response={200: list[ItemSchema]}, + summary="Trending books in catalog", + auth=None, + tags=["catalog"], +) +def trending_book(request): + return _get_trending("trending_book") + + +@api.get( + "/catalog/trending/movie/", + response={200: list[ItemSchema]}, + summary="Trending movies in catalog", + auth=None, + tags=["catalog"], +) +def trending_movie(request): + return _get_trending("trending_movie") + + +@api.get( + "/catalog/trending/tv/", + response={200: list[ItemSchema]}, + summary="Trending tv in catalog", + auth=None, + tags=["catalog"], +) +def trending_tv(request): + return _get_trending("trending_tv") + + +@api.get( + "/catalog/trending/music/", + response={200: list[ItemSchema]}, + summary="Trending music in catalog", + auth=None, + tags=["catalog"], +) +def trending_music(request): + return _get_trending("trending_music") + + +@api.get( + "/catalog/trending/game/", + response={200: list[ItemSchema]}, + summary="Trending games in catalog", + auth=None, + tags=["catalog"], +) +def trending_game(request): + return _get_trending("trending_game") + + +@api.get( + "/catalog/trending/podcast/", + response={200: list[ItemSchema]}, + summary="Trending podcasts in catalog", + auth=None, + tags=["catalog"], +) +def trending_podcast(request): + return _get_trending("trending_podcast") + + def _get_item(cls, uuid, response): item = Item.get_by_url(uuid) if not item: diff --git a/catalog/jobs/discover.py b/catalog/jobs/discover.py index 5e366df8..f0f0d275 100644 --- a/catalog/jobs/discover.py +++ b/catalog/jobs/discover.py @@ -113,13 +113,15 @@ class DiscoverGenerator(BaseJob): items = [Item.objects.get(pk=i) for i in item_ids] if category == ItemCategory.TV: items = self.cleanup_shows(items) + key = "trending_" + category.value gallery_list.append( { - "name": "trending_" + category.value, + "name": key, "category": category, - "items": items, } ) + cache.set(key, items, timeout=None) + item_ids = self.get_popular_marked_item_ids(category, DAYS_FOR_TRENDS, [])[ :5 ] @@ -147,6 +149,7 @@ class DiscoverGenerator(BaseJob): ], } ) + trends.sort(key=lambda x: int(x["history"][0]["accounts"]), reverse=True) collections = ( diff --git a/catalog/views.py b/catalog/views.py index 6b21a24e..ebf30cb8 100644 --- a/catalog/views.py +++ b/catalog/views.py @@ -258,8 +258,9 @@ def discover(request): # rotate every 6 minutes rot = timezone.now().minute // 6 for gallery in gallery_list: - i = rot * len(gallery["items"]) // 10 - gallery["items"] = gallery["items"][i:] + gallery["items"][:i] + items = cache.get(gallery["name"], []) + i = rot * len(items) // 10 + gallery["items"] = items[i:] + items[:i] if request.user.is_authenticated: if not request.user.registration_complete: diff --git a/journal/apis/collection.py b/journal/apis/collection.py index db67ff90..ac5f2f5f 100644 --- a/journal/apis/collection.py +++ b/journal/apis/collection.py @@ -1,7 +1,11 @@ from datetime import datetime from typing import List +from django.core.cache import cache +from django.utils import timezone +from django.views.decorators.cache import cache_page from ninja import Field, Schema +from ninja.decorators import decorate_view from ninja.pagination import paginate from catalog.common.models import Item, ItemSchema @@ -190,3 +194,20 @@ def collection_delete_item(request, collection_uuid: str, item_uuid: str): return 404, {"message": "Item not found"} c.remove_item(item) return 200, {"message": "OK"} + + +@api.get( + "/catalog/trending/collection/", + response={200: list[CollectionSchema]}, + summary="Trending collection in catalog", + auth=None, + tags=["catalog"], +) +@decorate_view(cache_page(600)) +def trending_collection(request): + rot = timezone.now().minute // 6 + collection_ids = cache.get("featured_collections", []) + i = rot * len(collection_ids) // 10 + collection_ids = collection_ids[i:] + collection_ids[:i] + featured_collections = Collection.objects.filter(pk__in=collection_ids) + return featured_collections