def get_newest_episodes(self, max_date, max_per_podcast=5): """ Returns the newest episodes of all subscribed podcasts Only max_per_podcast episodes per podcast are loaded. Episodes with release dates above max_date are discarded. This method returns a generator that produces the newest episodes. The number of required DB queries is equal to the number of (distinct) podcasts of all consumed episodes (max: number of subscribed podcasts), plus a constant number of initial queries (when the first episode is consumed). """ cmp_key = lambda episode: episode.released or datetime(2000, 01, 01) podcasts = list(self.get_subscribed_podcasts()) podcasts = filter(lambda p: p.latest_episode_timestamp, podcasts) podcasts = sorted(podcasts, key=lambda p: p.latest_episode_timestamp, reverse=True) podcast_dict = dict((p.get_id(), p) for p in podcasts) # contains the un-yielded episodes, newest first episodes = [] for podcast in podcasts: yielded_episodes = 0 for episode in episodes: # determine for which episodes there won't be a new episodes # that is newer; those can be yielded if episode.released > podcast.latest_episode_timestamp: p = podcast_dict.get(episode.podcast, None) yield proxy_object(episode, podcast=p) yielded_episodes += 1 else: break # remove the episodes that have been yielded before episodes = episodes[yielded_episodes:] # fetch and merge episodes for the next podcast from mygpo.db.couchdb.episode import episodes_for_podcast new_episodes = episodes_for_podcast(podcast, since=1, until=max_date, descending=True, limit=max_per_podcast) episodes = sorted(episodes+new_episodes, key=cmp_key, reverse=True) # yield the remaining episodes for episode in episodes: podcast = podcast_dict.get(episode.podcast, None) yield proxy_object(episode, podcast=podcast)
def _prepare_list(self, l): podcasts = Podcast.get_multi(l.podcasts[:self.podcasts_per_topic]) user = User.get(l.user) l = proxy_object(l) l.podcasts = podcasts l.username = user.username l.cls = "PodcastList" return l
def get_random_list(self, podcasts_per_list=5): random_list = next(random_podcastlists(), None) list_owner = None if random_list: random_list = proxy_object(random_list) random_list.more_podcasts = max(0, len(random_list.podcasts) - podcasts_per_list) random_list.podcasts = podcasts_by_id(random_list.podcasts[:podcasts_per_list]) random_list.user = get_user_by_id(random_list.user) yield random_list
def podcasts_for_states(podcast_states): """ returns the podcasts corresponding to the podcast states """ podcast_ids = [state.podcast for state in podcast_states] podcasts = podcasts_to_dict(podcast_ids) for state in podcast_states: podcast = proxy_object(podcasts[state.podcast], url=state.ref_url) podcasts[state.podcast] = podcast return podcasts.values()
def get_subscribed_podcasts(self, public=None): """ Returns all subscribed podcasts for the user The attribute "url" contains the URL that was used when subscribing to the podcast """ from mygpo.db.couchdb.podcast_state import get_subscribed_podcast_states_by_user states = get_subscribed_podcast_states_by_user(self, public) podcast_ids = [state.podcast for state in states] podcasts = podcasts_to_dict(podcast_ids) for state in states: podcast = podcasts.get(state.podcast, None) if podcast is None: continue podcast = proxy_object(podcast, url=state.ref_url) podcasts[state.podcast] = podcast return set(podcasts.values())
def get_newest_episodes(self, max_date, num_episodes, max_per_podcast=5): """ Returns the newest episodes for a set of podcasts """ podcast_key = lambda p: p.latest_episode_timestamp podcasts = filter(lambda p: p.latest_episode_timestamp, self.podcasts) podcasts = sorted(podcasts, key=podcast_key, reverse=True) # we need at most num_episodes podcasts podcasts = podcasts[:num_episodes] podcast_dict = dict((p.get_id(), p) for p in podcasts) links = [(p.latest_episode_timestamp, lazy_call(episodes_for_podcast, p, since=1, until=max_date, descending=True, limit=max_per_podcast) ) for p in podcasts] episodes = sorted_chain(links, lambda e: e.released, reverse=True) for episode in islice(episodes, num_episodes): p = podcast_dict.get(episode.podcast, None) yield proxy_object(episode, podcast=p)
def list_show(request, plist, owner): is_own = owner == request.user site = RequestSite(request) plist = proxy_object(plist) podcasts = list(podcasts_groups_by_id(plist.podcasts)) plist.podcasts = podcasts max_subscribers = max([p.subscriber_count() for p in podcasts] + [0]) thing = plist.get_flattr_thing(site.domain, owner.username) flattr = Flattr(owner, site.domain, request.is_secure()) flattr_autosubmit = flattr.get_autosubmit_url(thing) return render(request, 'list.html', { 'podcastlist': plist, 'max_subscribers': max_subscribers, 'owner': owner, 'flattr_autosubmit': flattr_autosubmit, 'domain': site.domain, 'is_own': is_own, })
def list_show(request, plist, owner): is_own = owner == request.user plist = proxy_object(plist) podcasts = list(Podcast.get_multi(plist.podcasts)) plist.podcasts = podcasts max_subscribers = max([p.subscriber_count() for p in podcasts] + [0]) site = RequestSite(request) return render( request, "list.html", { "podcastlist": plist, "max_subscribers": max_subscribers, "owner": owner, "domain": site.domain, "is_own": is_own, }, )
def annotate_episode(episode): listener_count = listeners.get(episode._id, None) return proxy_object(episode, listeners=listener_count)
def _prepare_list(l): user = get_user_by_id(l.user) l = proxy_object(l) l.username = user.username if user else '' return l
def _prepare_category(self, resp): category = Category.wrap(resp['doc']) category = proxy_object(category) category.podcasts = category.get_podcasts(0, self.podcasts_per_cat) return category
def set_podcast(episode): episode = proxy_object(episode) episode.podcast = podcasts.get(episode.podcast, None) return episode
def _prepare_category(self, category): category = proxy_object(category) category.podcasts = category.get_podcasts(0, self.podcasts_per_topic) category.cls = "Category" return category
def _annotate_episode(listeners, episode_actions, episode): listener_count = listeners.pop(episode._id, None) action = episode_actions.pop(episode._id, None) return proxy_object(episode, listeners=listener_count, action=action)
def _set_objects(h): dev = user.get_device(h.device) return proxy_object(h, device=dev)
def _prepare_list(l): user = User.get(l.user) l = proxy_object(l) l.username = user.username return l
def _set_objects(h): #TODO: optimize by indexing devices by id dev = request.user.get_device(h.device) return proxy_object(h, device=dev)