def generate_feed(uid): subscriptions = Subscription.query(Subscription.uid == uid).fetch(200) subscription_urls = [sub.url for sub in subscriptions if sub.url] if len(subscription_urls) > 0: sources = Source.query(Source.url.IN(subscription_urls)).order(-Source.most_recent_article_added_date).fetch(len(subscription_urls)) source_jsons = {} for source_json in memcache.get_multi([source.feed_cache_key() for source in sources]).itervalues(): source_jsons[source_json['id']] = source_json to_fetch = [source for source in sources if source.key.id() not in source_jsons] print 'HITS {0} TO_FETCH {1}'.format(len(source_jsons), len(to_fetch)) if len(to_fetch): source_promises = [src.json(include_articles=True, article_limit=FEED_ARTICLE_LIMIT, return_promise=True) for src in to_fetch] for promise in source_promises: data = promise() source_jsons[data['id']] = data # put the cache keys: if len(to_fetch): memcache.set_multi({source.feed_cache_key(): source_jsons[source.key.id()] for source in to_fetch if (source.key.id() in source_jsons)}) source_json = [source_jsons[source.key.id()] for source in sources if source.key.id() in source_jsons] else: source_json = [] return { "sources": source_json }
def sources_subscribed_by_id(uid, just_inserted=None): subs = Subscription.query(Subscription.uid == uid).fetch(limit=100) if just_inserted and just_inserted.key.id() not in [sub.key.id() for sub in subs]: subs = [just_inserted] + subs json_futures = [sub.json(return_promise=True) for sub in subs] jsons = [j() for j in json_futures] jsons = [j for j in jsons if j['source']] return jsons