diff --git a/r2/r2/lib/normalized_hot.py b/r2/r2/lib/normalized_hot.py index f793b7627..e4028987d 100644 --- a/r2/r2/lib/normalized_hot.py +++ b/r2/r2/lib/normalized_hot.py @@ -83,30 +83,23 @@ def get_hot(sr): return res +def only_recent(items): + return filter(lambda l: l._date > utils.timeago('%d day' % g.HOT_PAGE_AGE), + items) + @memoize('normalize_hot', time = g.page_cache_time) def normalized_hot_cached(sr_ids): results = [] srs = Subreddit._byID(sr_ids, data = True, return_dict = False) for sr in srs: - #items = get_hot(sr) - items = filter(lambda l: l._date > utils.timeago('%d day' % g.HOT_PAGE_AGE), - get_hot(sr)) + items = only_recent(get_hot(sr)) if not items: continue top_score = max(items[0]._hot, 1) - - top, rest = items[:2], items[2:] - - if top: - normals = [l._hot / top_score for l in top] - results.extend((l, random.choice(normals)) for l in top) - #random.shuffle(normals) - #results.extend((l, normals.pop()) for l in top) - - if rest: - results.extend((l, l._hot / top_score) for l in rest) + if items: + results.extend((l, l._hot / top_score) for l in items) results.sort(key = lambda x: (x[1], x[0]._hot), reverse = True) return [l[0]._fullname for l in results] diff --git a/r2/r2/lib/organic.py b/r2/r2/lib/organic.py index 9d19a17f9..ccf4a96ed 100644 --- a/r2/r2/lib/organic.py +++ b/r2/r2/lib/organic.py @@ -21,9 +21,11 @@ ################################################################################ from r2.models import * from r2.lib.memoize import memoize -from r2.lib.normalized_hot import is_top_link +from r2.lib.normalized_hot import is_top_link, get_hot, only_recent from r2.lib import count +import random + from pylons import g cache = g.cache @@ -43,9 +45,22 @@ def cached_organic_links(username): sr_count = count.get_link_counts() srs = Subreddit.user_subreddits(user) + + #only use links from reddits that you're subscribed to link_names = filter(lambda n: sr_count[n][1] in srs, 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(srs)) + 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) + builder = IDBuilder(link_names, num = 30, skip = True, keep_fn = keep_link) links = builder.get_items()[0] cache.set(pos_key(user), 0)