def cached_organic_links(user_id, langs): if user_id is None: sr_ids = Subreddit.default_subreddits() else: user = Account._byID(user_id, data=True) sr_ids = Subreddit.user_subreddits(user) sr_count = count.get_link_counts() #only use links from reddits that you're subscribed to link_names = filter(lambda n: sr_count[n][1] in sr_ids, sr_count.keys()) link_names.sort(key = lambda n: sr_count[n][0]) #potentially add a up and coming link if random.choice((True, False)) and sr_ids: sr = Subreddit._byID(random.choice(sr_ids)) items = only_recent(get_hot(sr)) if items: if len(items) == 1: new_item = items[0] else: new_item = random.choice(items[1:4]) link_names.insert(0, new_item._fullname) # remove any that the user has acted on builder = IDBuilder(link_names, skip = True, keep_fn = keep_link, num = organic_length) link_names = [ x._fullname for x in builder.get_items()[0] ] #if not logged in, don't reset the count. if we did that we might get in a #cycle where the cache will return the same link over and over if user_id: update_pos(0) insert_promoted(link_names, sr_ids, user_id is not None) # remove any duplicates caused by insert_promoted if the user is logged in if user_id: link_names = list(UniqueIterator(link_names)) return link_names
def cached_organic_links(user_id, langs): if user_id is None: sr_ids = Subreddit.default_srs(langs, ids = True) else: user = Account._byID(user_id, data=True) sr_ids = Subreddit.user_subreddits(user) sr_count = count.get_link_counts() #only use links from reddits that you're subscribed to link_names = filter(lambda n: sr_count[n][1] in sr_ids, sr_count.keys()) link_names.sort(key = lambda n: sr_count[n][0]) #potentially add a up and coming link if random.choice((True, False)): sr = Subreddit._byID(random.choice(sr_ids)) items = only_recent(get_hot(sr)) if items: if len(items) == 1: new_item = items[0] else: new_item = random.choice(items[1:4]) link_names.insert(0, new_item._fullname) # remove any that the user has acted on builder = IDBuilder(link_names, skip = True, keep_fn = keep_link, num = organic_length) link_names = [ x._fullname for x in builder.get_items()[0] ] calculation_key = str(time()) update_pos(0, calculation_key) insert_promoted(link_names, sr_ids, user_id is not None) # remove any duplicates caused by insert_promoted ret = [ l for l in UniqueIterator(link_names) ] return (calculation_key, ret)