1 from datetime
import datetime
2 from collections
import defaultdict
4 from mygpo
.db
.couchdb
.episode
import episodes_for_podcast
7 DEFAULT_RELEASE
= datetime(1970, 1, 1)
8 _SORT_KEY
= lambda eps
: eps
[0].released
or DEFAULT_RELEASE
10 class PodcastGrouper(object):
11 """ Groups episodes of two podcasts based on certain features
13 The results are sorted by release timestamp """
15 DEFAULT_RELEASE
= datetime(1970, 1, 1)
17 def __init__(self
, podcasts
):
19 if not podcasts
or (None in podcasts
):
20 raise ValueError('podcasts must not be None')
22 self
.podcasts
= podcasts
25 def __get_episodes(self
):
27 for podcast
in self
.podcasts
:
28 episodes
.update(dict((e
._id
, e
) for e
in episodes_for_podcast(podcast
)))
33 def group(self
, get_features
):
35 episodes
= self
.__get
_episodes
()
37 episode_groups
= defaultdict(list)
39 episode_features
= map(get_features
, episodes
.items())
41 for features
, episode_id
in episode_features
:
42 episode
= episodes
[episode_id
]
43 episode_groups
[features
].append(episode
)
45 groups
= sorted(episode_groups
.values(), key
=_SORT_KEY
)
47 return enumerate(groups
)