more discover mix
This commit is contained in:
parent
ac0c74f03f
commit
a1fc4b3fe6
1 changed files with 36 additions and 17 deletions
|
@ -2,14 +2,16 @@ from django.core.management.base import BaseCommand
|
|||
from django.core.cache import cache
|
||||
import pprint
|
||||
from catalog.models import *
|
||||
from journal.models import ShelfMember, query_item_category, ItemCategory
|
||||
from journal.models import ShelfMember, query_item_category, ItemCategory, Comment
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
from django.db.models import Count
|
||||
|
||||
|
||||
MAX_GALLERY_ITEMS = 64
|
||||
MIN_MARKS = 3
|
||||
MAX_ITEMS_PER_PERIOD = 12
|
||||
MIN_MARKS = 2
|
||||
MAX_DAYS_FOR_PERIOD = 64
|
||||
MIN_DAYS_FOR_PERIOD = 4
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
@ -22,16 +24,29 @@ class Command(BaseCommand):
|
|||
help="generate discover data",
|
||||
)
|
||||
|
||||
def get_popular_item_ids(self, category, days):
|
||||
# self.stdout.write(f"Generating popular {category} items for {days} days...")
|
||||
def get_popular_marked_item_ids(self, category, days, exisiting_ids):
|
||||
item_ids = [
|
||||
m["item_id"]
|
||||
for m in ShelfMember.objects.filter(query_item_category(category))
|
||||
.filter(created_time__gt=timezone.now() - timedelta(days=days))
|
||||
.exclude(item_id__in=exisiting_ids)
|
||||
.values("item_id")
|
||||
.annotate(num=Count("item_id"))
|
||||
.filter(num__gte=MIN_MARKS)
|
||||
.order_by("-num")[:MAX_GALLERY_ITEMS]
|
||||
.order_by("-num")[:MAX_ITEMS_PER_PERIOD]
|
||||
]
|
||||
return item_ids
|
||||
|
||||
def get_popular_commented_item_ids(self, category, days, exisiting_ids):
|
||||
item_ids = [
|
||||
m["item_id"]
|
||||
for m in Comment.objects.filter(query_item_category(category))
|
||||
.filter(created_time__gt=timezone.now() - timedelta(days=days))
|
||||
.exclude(item_id__in=exisiting_ids)
|
||||
.values("item_id")
|
||||
.annotate(num=Count("item_id"))
|
||||
.filter(num__gte=MIN_MARKS)
|
||||
.order_by("-num")[:MAX_ITEMS_PER_PERIOD]
|
||||
]
|
||||
return item_ids
|
||||
|
||||
|
@ -55,20 +70,24 @@ class Command(BaseCommand):
|
|||
]
|
||||
gallery_list = []
|
||||
for category in gallery_categories:
|
||||
days = 128
|
||||
days = MAX_DAYS_FOR_PERIOD
|
||||
item_ids = []
|
||||
while days > 4:
|
||||
ids = [
|
||||
i
|
||||
for i in self.get_popular_item_ids(category, days)
|
||||
if i not in item_ids
|
||||
]
|
||||
if len(ids) > MAX_GALLERY_ITEMS // 5:
|
||||
ids = ids[: MAX_GALLERY_ITEMS // 5]
|
||||
self.stdout.write(f"{category} for last {days} days: {len(ids)}")
|
||||
while days >= MIN_DAYS_FOR_PERIOD:
|
||||
ids = self.get_popular_marked_item_ids(category, days, item_ids)
|
||||
self.stdout.write(
|
||||
f"Marked {category} for last {days} days: {len(ids)}"
|
||||
)
|
||||
item_ids = ids + item_ids
|
||||
days //= 2
|
||||
items = list(Item.objects.filter(id__in=item_ids))
|
||||
if category == ItemCategory.Podcast:
|
||||
extra_ids = self.get_popular_commented_item_ids(
|
||||
ItemCategory.Podcast, MAX_DAYS_FOR_PERIOD, item_ids
|
||||
)
|
||||
self.stdout.write(
|
||||
f"Commented podcast for last {MAX_DAYS_FOR_PERIOD} days: {len(extra_ids)}"
|
||||
)
|
||||
item_ids = extra_ids + item_ids
|
||||
items = [Item.objects.get(pk=i) for i in item_ids]
|
||||
if category == ItemCategory.TV:
|
||||
items = self.cleanup_shows(items)
|
||||
gallery_list.append(
|
||||
|
|
Loading…
Add table
Reference in a new issue