API for trending items and posts
This commit is contained in:
parent
445efb2509
commit
8d83b5c8ad
3 changed files with 30 additions and 2 deletions
|
@ -1,7 +1,9 @@
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, Callable, List, Optional, Tuple, Type
|
from typing import Any, Callable, List, Optional, Tuple, Type
|
||||||
|
|
||||||
|
from django.core.cache import cache
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
from django.utils import timezone
|
||||||
from ninja import Schema
|
from ninja import Schema
|
||||||
|
|
||||||
from common.api import *
|
from common.api import *
|
||||||
|
@ -38,6 +40,11 @@ class SearchableItemCategory(Enum):
|
||||||
Performance = "performance"
|
Performance = "performance"
|
||||||
|
|
||||||
|
|
||||||
|
class Gallery(Schema):
|
||||||
|
name: str
|
||||||
|
items: List[ItemSchema]
|
||||||
|
|
||||||
|
|
||||||
@api.get(
|
@api.get(
|
||||||
"/catalog/search",
|
"/catalog/search",
|
||||||
response={200: SearchResult, 400: Result},
|
response={200: SearchResult, 400: Result},
|
||||||
|
@ -96,6 +103,27 @@ def fetch_item(request, url: str):
|
||||||
return 202, {"message": "Fetch in progress"}
|
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):
|
def _get_item(cls, uuid, response):
|
||||||
item = Item.get_by_url(uuid)
|
item = Item.get_by_url(uuid)
|
||||||
if not item:
|
if not item:
|
||||||
|
|
|
@ -117,7 +117,7 @@ class DiscoverGenerator(BaseJob):
|
||||||
items = self.cleanup_shows(items)
|
items = self.cleanup_shows(items)
|
||||||
gallery_list.append(
|
gallery_list.append(
|
||||||
{
|
{
|
||||||
"name": "popular_" + category.value,
|
"name": "trending_" + category.value,
|
||||||
"category": category,
|
"category": category,
|
||||||
"items": items,
|
"items": items,
|
||||||
}
|
}
|
||||||
|
@ -202,6 +202,7 @@ class DiscoverGenerator(BaseJob):
|
||||||
cache.set("featured_collections", collection_ids, timeout=None)
|
cache.set("featured_collections", collection_ids, timeout=None)
|
||||||
cache.set("popular_tags", list(tags), timeout=None)
|
cache.set("popular_tags", list(tags), timeout=None)
|
||||||
cache.set("popular_posts", list(post_ids), timeout=None)
|
cache.set("popular_posts", list(post_ids), timeout=None)
|
||||||
|
cache.set("trends_statuses", list(post_ids), timeout=None)
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Discover data updated, excluded: {len(excluding_identities)}, trends: {len(trends)}, collections: {len(collection_ids)}, tags: {len(tags)}, posts: {len(post_ids)}."
|
f"Discover data updated, excluded: {len(excluding_identities)}, trends: {len(trends)}, collections: {len(collection_ids)}, tags: {len(tags)}, posts: {len(post_ids)}."
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.paginator import Paginator
|
|
||||||
from django.db.models import Count
|
from django.db.models import Count
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.shortcuts import get_object_or_404, redirect, render
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
|
|
Loading…
Add table
Reference in a new issue