diff --git a/catalog/management/commands/discover.py b/catalog/management/commands/discover.py index f787aac2..7029889a 100644 --- a/catalog/management/commands/discover.py +++ b/catalog/management/commands/discover.py @@ -5,11 +5,11 @@ from catalog.models import * 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 +from django.db.models import Count, F MAX_ITEMS_PER_PERIOD = 12 -MIN_MARKS = 3 +MIN_MARKS = 2 MAX_DAYS_FOR_PERIOD = 96 MIN_DAYS_FOR_PERIOD = 6 @@ -37,18 +37,19 @@ class Command(BaseCommand): ] 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)) + def get_popular_commented_podcast_ids(self, days, exisiting_ids): + return list( + Comment.objects.filter(query_item_category(ItemCategory.Podcast)) .filter(created_time__gt=timezone.now() - timedelta(days=days)) - .exclude(item_id__in=exisiting_ids) - .values("item_id") - .annotate(num=Count("item_id")) + .annotate(p=F("item__podcastepisode__program")) + .filter(p__isnull=False) + .exclude(p__in=exisiting_ids) + .values("p") + .annotate(num=Count("p")) .filter(num__gte=MIN_MARKS) - .order_by("-num")[:MAX_ITEMS_PER_PERIOD] - ] - return item_ids + .order_by("-num") + .values_list("p", flat=True)[:MAX_ITEMS_PER_PERIOD] + ) def cleanup_shows(self, items): seasons = [i for i in items if i.__class__ == TVSeason] @@ -78,16 +79,14 @@ class Command(BaseCommand): f"Marked {category} for last {days} days: {len(ids)}" ) item_ids = ids + item_ids - if category == ItemCategory.Podcast: - extra_ids = self.get_popular_commented_item_ids( - ItemCategory.Podcast, days, item_ids - ) - self.stdout.write( - f"Commented podcast for last {days} days: {len(extra_ids)}" - ) - item_ids = extra_ids + item_ids days //= 2 - + if category == ItemCategory.Podcast: + days = MAX_DAYS_FOR_PERIOD // 4 + extra_ids = self.get_popular_commented_podcast_ids(days, item_ids) + self.stdout.write( + f"Most commented podcast for last {days} 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)