update trending apis

This commit is contained in:
mein Name 2025-02-03 18:03:55 -05:00 committed by Henri Dickson
parent 471a8cba8d
commit cf773069f2
4 changed files with 107 additions and 7 deletions

View file

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

View file

@ -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 = (

View file

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

View file

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