Skip to content

Commit

Permalink
API for trending items and posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and alphatownsman committed Jan 8, 2025
1 parent 445efb2 commit 8d83b5c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions catalog/api.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion catalog/jobs/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def run(self):
items = self.cleanup_shows(items)
gallery_list.append(
{
"name": "popular_" + category.value,
"name": "trending_" + category.value,
"category": category,
"items": items,
}
Expand Down Expand Up @@ -202,6 +202,7 @@ def run(self):
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)}."
)
1 change: 0 additions & 1 deletion catalog/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8d83b5c

Please sign in to comment.