[Migration] use "new" Podcast in type checks
[mygpo.git] / mygpo / core / podcasts.py
blobdfbd4934f3c364002e2eecad055e4f75d56c4984
1 from itertools import chain, islice
3 from mygpo.podcasts.models import Podcast, PodcastGroup
4 from mygpo.core.proxy import proxy_object
5 from mygpo.utils import sorted_chain
8 # the default sort order for podcasts
9 PODCAST_SORT=lambda p: p.display_title
12 class PodcastSet(set):
13 """ Represents a set of podcasts """
15 def __init__(self, podcasts=None):
16 self.podcasts = podcasts or []
19 def get_newest_episodes(self, max_date, num_episodes, max_per_podcast=5):
20 """ Returns the newest episodes for a set of podcasts """
22 podcast_key = lambda p: p.latest_episode_timestamp
24 podcasts = filter(lambda p: p.latest_episode_timestamp, self.podcasts)
25 podcasts = sorted(podcasts, key=podcast_key, reverse=True)
27 # we need at most num_episodes podcasts
28 podcasts = podcasts[:num_episodes]
30 podcast_dict = dict((p.get_id(), p) for p in podcasts)
32 # TODO: max_per_podcast
33 episodes = Episode.objects.filter(podcast__in=podcasts,
34 released__isnull=False,
35 released__lt=max_date,
38 for episode in islice(episodes, num_episodes):
39 p = podcast_dict.get(episode.podcast, None)
40 yield proxy_object(episode, podcast=p)
43 def lazy_call(f, *args, **kwargs):
44 for x in f(*args, **kwargs):
45 yield x
48 def individual_podcasts(pg):
49 """ returns individual podcasts for an iter of Podcast(Group) objects """
51 for p in pg:
52 if isinstance(p, Podcast):
53 yield p
55 elif isinstance(p, PodcastGroup):
56 for x in p.podcast_set.all():
57 yield x