API for trending items and posts

This commit is contained in:
Your Name 2025-01-08 00:28:30 -05:00 committed by Henri Dickson
parent 445efb2509
commit 8d83b5c8ad
3 changed files with 30 additions and 2 deletions

View file

@ -1,7 +1,9 @@
from enum import Enum
from typing import Any, Callable, List, Optional, Tuple, Type
from django.core.cache import cache
from django.http import HttpResponse
from django.utils import timezone
from ninja import Schema
from common.api import *
@ -38,6 +40,11 @@ class SearchableItemCategory(Enum):
Performance = "performance"
class Gallery(Schema):
name: str
items: List[ItemSchema]
@api.get(
"/catalog/search",
response={200: SearchResult, 400: Result},
@ -96,6 +103,27 @@ def fetch_item(request, url: str):
return 202, {"message": "Fetch in progress"}
@api.get(
"/catalog/gallery/",
response={200: list[Gallery]},
summary="Trending items in catalog",
auth=None,
tags=["catalog"],
)
def trending_items(request):
"""
Returns a list of galleries, each gallery is a list of items
"""
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]
return 200, gallery_list
def _get_item(cls, uuid, response):
item = Item.get_by_url(uuid)
if not item:

View file

@ -117,7 +117,7 @@ class DiscoverGenerator(BaseJob):
items = self.cleanup_shows(items)
gallery_list.append(
{
"name": "popular_" + category.value,
"name": "trending_" + category.value,
"category": category,
"items": items,
}
@ -202,6 +202,7 @@ class DiscoverGenerator(BaseJob):
cache.set("featured_collections", collection_ids, timeout=None)
cache.set("popular_tags", list(tags), timeout=None)
cache.set("popular_posts", list(post_ids), timeout=None)
cache.set("trends_statuses", list(post_ids), timeout=None)
logger.info(
f"Discover data updated, excluded: {len(excluding_identities)}, trends: {len(trends)}, collections: {len(collection_ids)}, tags: {len(tags)}, posts: {len(post_ids)}."
)

View file

@ -1,6 +1,5 @@
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.paginator import Paginator
from django.db.models import Count
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect, render