fix podcast merging
[mygpo.git] / mygpo / admin / group.py
blob8f5b6b6d2bbc37545e233b3b4a604a43cabebf5f
1 from datetime import datetime
2 from collections import defaultdict
5 DEFAULT_RELEASE = datetime(1970, 1, 1)
6 _SORT_KEY = lambda eps: eps[0].released or DEFAULT_RELEASE
8 class PodcastGrouper(object):
9 """ Groups episodes of two podcasts based on certain features
11 The results are sorted by release timestamp """
13 DEFAULT_RELEASE = datetime(1970, 1, 1)
15 def __init__(self, podcasts):
17 if not podcasts or (None in podcasts):
18 raise ValueError('podcasts must not be None')
20 self.podcasts = podcasts
23 def __get_episodes(self):
24 episodes = {}
25 for podcast in self.podcasts:
26 episodes.update(dict((e._id, e) for e in podcast.get_episodes()))
28 return episodes
31 def group(self, get_features):
33 episodes = self.__get_episodes()
35 episode_groups = defaultdict(list)
37 episode_features = map(get_features, episodes.items())
39 for features, episode_id in episode_features:
40 episode = episodes[episode_id]
41 episode_groups[features].append(episode)
43 groups = sorted(episode_groups.values(), key=_SORT_KEY)
45 return enumerate(groups)