avoid conflict when confirming pubsub sbuscription
[mygpo.git] / mygpo / core / podcasts.py
blob96d9ab06c317ba662c0f6d07eb8216590872993e
1 from itertools import chain, islice
3 from mygpo.core.models import Podcast, PodcastGroup
4 from mygpo.core.proxy import proxy_object
5 from mygpo.db.couchdb.episode import episodes_for_podcast
6 from mygpo.utils import sorted_chain
9 # the default sort order for podcasts
10 PODCAST_SORT=lambda p: p.display_title
13 class PodcastSet(set):
14 """ Represents a set of podcasts """
16 def __init__(self, podcasts=None):
17 self.podcasts = podcasts or []
20 def get_newest_episodes(self, max_date, num_episodes, max_per_podcast=5):
21 """ Returns the newest episodes for a set of podcasts """
23 podcast_key = lambda p: p.latest_episode_timestamp
25 podcasts = filter(lambda p: p.latest_episode_timestamp, self.podcasts)
26 podcasts = sorted(podcasts, key=podcast_key, reverse=True)
28 # we need at most num_episodes podcasts
29 podcasts = podcasts[:num_episodes]
31 podcast_dict = dict((p.get_id(), p) for p in podcasts)
33 links = [(p.latest_episode_timestamp, lazy_call(episodes_for_podcast,
34 p, since=1, until=max_date, descending=True,
35 limit=max_per_podcast) ) for p in podcasts]
37 episodes = sorted_chain(links, lambda e: e.released, reverse=True)
39 for episode in islice(episodes, num_episodes):
40 p = podcast_dict.get(episode.podcast, None)
41 yield proxy_object(episode, podcast=p)
44 def lazy_call(f, *args, **kwargs):
45 for x in f(*args, **kwargs):
46 yield x
49 def individual_podcasts(pg):
50 """ returns individual podcasts for an iter of Podcast(Group) objects """
52 for p in pg:
53 if isinstance(p, Podcast):
54 yield p
56 elif isinstance(p, PodcastGroup):
57 for x in p.podcasts:
58 yield x