def organic(self): o_links, pos = organic.organic_links(c.user) if o_links: # get links in proximity to pos l = min(len(o_links) - 3, 8) disp_links = [o_links[(i + pos) % len(o_links)] for i in xrange(-2, l)] def keep_fn(item): return item.likes is None and item.keep_item(item) b = IDBuilder(disp_links, wrap=self.builder_wrapper, skip=True, keep_fn=keep_fn) o = OrganicListing( b, org_links=o_links, visible_link=o_links[pos], max_num=self.listing_obj.max_num, max_score=self.listing_obj.max_score, ).listing() if len(o.things) > 0: # only pass through a listing if the links made it # through our builder organic.update_pos(pos + 1) return o
def make_spotlight(self): """Build the Spotlight. The frontpage gets a Spotlight box that contains promoted and organic links from the user's subscribed subreddits and promoted links targeted to the frontpage. If the user has disabled ads promoted links will not be shown. The content of the Spotlight box is a bit tricky because a single version of the frontpage is cached and displayed to all logged out users. Because of the caching we must include many promoted links and select one to display on the client side. Otherwise, each logged out user would see the same promoted link and we would not get the desired distribution of promoted link views. Most of the promoted links are included as stubs to reduce the size of the page. When a promoted link stub is selected by the lottery the full link is fetched and displayed. There are only ~1000 cache resets per day so it is necessary to use a large subset of the eligible promoted links when choosing stubs for the Spotlight box. Using 100 stubs works great when there are fewer than 100 possible promoted links and allows room for growth. """ organic_fullnames = organic.organic_links(c.user) promoted_links = [] # If prefs allow it, mix in promoted links and sr discovery content if (c.user.pref_show_sponsors or not c.user.gold) and g.live_config["sr_discovery_links"]: organic_fullnames.extend(g.live_config["sr_discovery_links"]) show_promo = False srnames = [] if c.user.pref_show_sponsors or not c.user.gold: srs = promote.srs_with_live_promos(c.user, c.site) if srs: if (c.user_is_loggedin and random.random() > 0.5) or not c.user_is_loggedin: srnames = [sr.name for sr in srs] show_promo = True random.shuffle(organic_fullnames) organic_fullnames = organic_fullnames[:10] b = IDBuilder(organic_fullnames, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, skip=True) organic_links = b.get_items()[0] has_subscribed = c.user.has_subscribed interestbar_prob = g.live_config["spotlight_interest_sub_p" if has_subscribed else "spotlight_interest_nosub_p"] interestbar = InterestBar(has_subscribed) s = SpotlightListing( organic_links=organic_links, interestbar=interestbar, interestbar_prob=interestbar_prob, show_promo=show_promo, srnames=srnames, max_num=self.listing_obj.max_num, max_score=self.listing_obj.max_score, ).listing() return s
def organic(self): o_links, pos = organic.organic_links(c.user) if o_links: # get links in proximity to pos disp_links = [o_links[(i + pos) % len(o_links)] for i in xrange(-2, 8)] b = IDBuilder(disp_links, wrap = self.builder_wrapper) o = OrganicListing(b, org_links = o_links, visible_link = o_links[pos], max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score) organic.update_pos(c.user, (pos + 1) % len(o_links)) return o.listing()
def make_spotlight(self): """Build the Spotlight. The frontpage gets a Spotlight box that contains promoted and organic links from the user's subscribed subreddits and promoted links targeted to the frontpage. If the user has disabled ads promoted links will not be shown. Promoted links are requested from the adserver client-side. """ organic_fullnames = organic.organic_links(c.user) promoted_links = [] show_promo = False srnames = [] can_show_promo = c.user.pref_show_sponsors or not c.user.gold try_show_promo = ((c.user_is_loggedin and random.random() > 0.5) or not c.user_is_loggedin) if can_show_promo and try_show_promo: srnames = promote.srnames_with_live_promos(c.user, c.site) if srnames: show_promo = True random.shuffle(organic_fullnames) organic_fullnames = organic_fullnames[:10] b = IDBuilder(organic_fullnames, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, skip=True) organic_links = b.get_items()[0] has_subscribed = c.user.has_subscribed interestbar_prob = g.live_config['spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] interestbar = InterestBar(has_subscribed) s = SpotlightListing(organic_links=organic_links, interestbar=interestbar, interestbar_prob=interestbar_prob, show_promo=show_promo, srnames=srnames, max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score).listing() return s
def make_spotlight(self): """Build the Spotlight. The frontpage gets a Spotlight box that contains promoted and organic links from the user's subscribed subreddits and promoted links targeted to the frontpage. If the user has disabled ads promoted links will not be shown. Promoted links are requested from the adserver client-side. """ organic_fullnames = organic.organic_links(c.user) promoted_links = [] show_promo = False srnames = [] can_show_promo = c.user.pref_show_sponsors or not c.user.gold try_show_promo = ((c.user_is_loggedin and random.random() > 0.5) or not c.user_is_loggedin) if can_show_promo and try_show_promo: srnames = promote.srnames_with_live_promos(c.user, c.site) if srnames: show_promo = True random.shuffle(organic_fullnames) organic_fullnames = organic_fullnames[:10] b = IDBuilder(organic_fullnames, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, skip=True) organic_links = b.get_items()[0] has_subscribed = c.user.has_subscribed interestbar_prob = g.live_config[ 'spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] interestbar = InterestBar(has_subscribed) s = SpotlightListing(organic_links=organic_links, interestbar=interestbar, interestbar_prob=interestbar_prob, show_promo=show_promo, srnames=srnames, max_num=self.listing_obj.max_num, max_score=self.listing_obj.max_score).listing() return s
def organic(self): o_links, pos, calculation_key = organic.organic_links(c.user) if o_links: # get links in proximity to pos l = min(len(o_links) - 3, 8) disp_links = [o_links[(i + pos) % len(o_links)] for i in xrange(-2, l)] b = IDBuilder(disp_links, wrap = self.builder_wrapper) o = OrganicListing(b, org_links = o_links, visible_link = o_links[pos], max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score).listing() if len(o.things) > 0: # only pass through a listing if the links made it # through our builder organic.update_pos(pos+1, calculation_key) return o
def make_spotlight(self): """Build the Spotlight. The frontpage gets a Spotlight box that contains promoted and organic links from the user's subscribed subreddits and promoted links targeted to the frontpage. If the user has disabled ads promoted links will not be shown. The content of the Spotlight box is a bit tricky because a single version of the frontpage is cached and displayed to all logged out users. Because of the caching we must include many promoted links and select one to display on the client side. Otherwise, each logged out user would see the same promoted link and we would not get the desired distribution of promoted link views. Most of the promoted links are included as stubs to reduce the size of the page. When a promoted link stub is selected by the lottery the full link is fetched and displayed. There are only ~1000 cache resets per day so it is necessary to use a large subset of the eligible promoted links when choosing stubs for the Spotlight box. Using 100 stubs works great when there are fewer than 100 possible promoted links and allows room for growth. """ organic_fullnames = organic.organic_links(c.user) promoted_links = [] # If prefs allow it, mix in promoted links and sr discovery content if ((c.user.pref_show_sponsors or not c.user.gold) and g.live_config['sr_discovery_links']): organic_fullnames.extend(g.live_config['sr_discovery_links']) show_promo = False srnames = [] if c.user.pref_show_sponsors or not c.user.gold: srs = promote.srs_with_live_promos(c.user, c.site) if srs: if ((c.user_is_loggedin and random.random() > 0.5) or not c.user_is_loggedin): srnames = [sr.name for sr in srs] show_promo = True random.shuffle(organic_fullnames) organic_fullnames = organic_fullnames[:10] b = IDBuilder(organic_fullnames, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, skip=True) organic_links = b.get_items()[0] has_subscribed = c.user.has_subscribed interestbar_prob = g.live_config[ 'spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] interestbar = InterestBar(has_subscribed) s = SpotlightListing(organic_links=organic_links, interestbar=interestbar, interestbar_prob=interestbar_prob, show_promo=show_promo, srnames=srnames, max_num=self.listing_obj.max_num, max_score=self.listing_obj.max_score).listing() return s
def spotlight(self): if (isinstance(c.site, DefaultSR) and (not c.user_is_loggedin or (c.user_is_loggedin and c.user.pref_organic))): spotlight_links = organic.organic_links(c.user) pos = organic_pos() if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if c.user.pref_show_sponsors or not c.user.gold: spotlight_links, pos = promote.insert_promoted(spotlight_links, pos) trial = populate_spotlight() # Need to do this again, because if there was a duplicate removed, # pos might be pointing outside the list. if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if trial: spotlight_links.insert(pos, trial._fullname) if not spotlight_links: return None # get links in proximity to pos num_tl = len(spotlight_links) if num_tl <= 3: disp_links = spotlight_links else: left_side = max(-1, min(num_tl - 3, 8)) disp_links = [spotlight_links[(i + pos) % num_tl] for i in xrange(-2, left_side)] def trial_keep_fn(item): if trial and trial._fullname == item._fullname: return True return organic.keep_fresh_links(item) def trial_wrap(item): if item is trial: w = Wrapped(item) w.trial_mode = True w.render_class = LinkOnTrial return w return self.builder_wrapper(item) b = IDBuilder(disp_links, wrap = trial_wrap if trial else self.builder_wrapper, num = organic.organic_length, skip = True, keep_fn = trial_keep_fn if trial else organic.keep_fresh_links) try: vislink = spotlight_links[pos] except IndexError: g.log.error("spotlight_links = %r" % spotlight_links) g.log.error("pos = %d" % pos) raise s = SpotlightListing(b, spotlight_links = spotlight_links, visible_link = vislink, max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score).listing() if len(s.things) > 0: # only pass through a listing if the links made it # through our builder organic.update_pos(pos+1) return s # no organic box on a hot page, then show a random promoted link elif not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors: link_ids = randomized_promotion_list(c.user, c.site) if link_ids: res = wrap_links(link_ids, wrapper = self.builder_wrapper, num = 1, keep_fn = lambda x: x.fresh, skip = True) if res.things: return res
def spotlight(self): if (isinstance(c.site, DefaultSR) and (not c.user_is_loggedin or (c.user_is_loggedin and c.user.pref_organic))): spotlight_links = organic.organic_links(c.user) pos = organic_pos() if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if c.user.pref_show_sponsors or not c.user.gold: spotlight_links, pos = promote.insert_promoted( spotlight_links, pos) trial = populate_spotlight() # Need to do this again, because if there was a duplicate removed, # pos might be pointing outside the list. if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if trial: spotlight_links.insert(pos, trial._fullname) if not spotlight_links: return None # get links in proximity to pos num_tl = len(spotlight_links) if num_tl <= 3: disp_links = spotlight_links else: left_side = max(-1, min(num_tl - 3, 8)) disp_links = [ spotlight_links[(i + pos) % num_tl] for i in xrange(-2, left_side) ] def trial_keep_fn(item): if trial and trial._fullname == item._fullname: return True return organic.keep_fresh_links(item) def trial_wrap(item): if item is trial: w = Wrapped(item) w.trial_mode = True w.render_class = LinkOnTrial return w return self.builder_wrapper(item) b = IDBuilder( disp_links, wrap=trial_wrap if trial else self.builder_wrapper, num=organic.organic_length, skip=True, keep_fn=trial_keep_fn if trial else organic.keep_fresh_links) try: vislink = spotlight_links[pos] except IndexError: g.log.error("spotlight_links = %r" % spotlight_links) g.log.error("pos = %d" % pos) raise s = SpotlightListing( b, spotlight_links=spotlight_links, visible_link=vislink, max_num=self.listing_obj.max_num, max_score=self.listing_obj.max_score).listing() if len(s.things) > 0: # only pass through a listing if the links made it # through our builder organic.update_pos(pos + 1) return s # no organic box on a hot page, then show a random promoted link elif not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors: link_ids = randomized_promotion_list(c.user, c.site) if link_ids: res = wrap_links(link_ids, wrapper=self.builder_wrapper, num=1, keep_fn=lambda x: x.fresh, skip=True) res.parent_name = "promoted" if res.things: return res
def make_spotlight(self): """Build the Spotlight. The frontpage gets a Spotlight box that contains promoted and organic links from the user's subscribed subreddits and promoted links targeted to the frontpage. If the user has disabled ads promoted links will not be shown. The content of the Spotlight box is a bit tricky because a single version of the frontpage is cached and displayed to all logged out users. Because of the caching we must include many promoted links and select one to display on the client side. Otherwise, each logged out user would see the same promoted link and we would not get the desired distribution of promoted link views. Most of the promoted links are included as stubs to reduce the size of the page. When a promoted link stub is selected by the lottery the full link is fetched and displayed. There are only ~1000 cache resets per day so it is necessary to use a large subset of the eligible promoted links when choosing stubs for the Spotlight box. Using 100 stubs works great when there are fewer than 100 possible promoted links and allows room for growth. """ organic_fullnames = organic.organic_links(c.user) promoted_links = [] # If prefs allow it, mix in promoted links and sr discovery content if c.user.pref_show_sponsors or not c.user.gold: if g.live_config['sr_discovery_links']: organic_fullnames.extend(g.live_config['sr_discovery_links']) n_promoted = 100 n_build = 1 if c.user_is_loggedin else 10 picker = (promote.lottery_promoted_links if c.user_is_loggedin else promote.sample_promoted_links) promo_tuples = picker(c.user, c.site, n=n_promoted) if not c.user_is_loggedin: promo_tuples.sort(key=lambda t: t.weight, reverse=True) b = CampaignBuilder( promo_tuples, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, num=n_build, skip=True, ) promoted_links, first, last, before, after = b.get_items() if promoted_links and last: lookup = {t.campaign: i for i, t in enumerate(promo_tuples)} last_index = lookup[last.campaign] stubs = promo_tuples[last_index + 1:] promoted_links.extend(stubs) if not (organic_fullnames or promoted_links): return None random.shuffle(organic_fullnames) organic_fullnames = organic_fullnames[:10] b = IDBuilder(organic_fullnames, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, skip=True) organic_links = b.get_items()[0] has_subscribed = c.user.has_subscribed interestbar_prob = g.live_config['spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] interestbar = InterestBar(has_subscribed) promotion_prob = 0.5 if c.user_is_loggedin else 1. s = SpotlightListing(organic_links=organic_links, promoted_links=promoted_links, interestbar=interestbar, interestbar_prob=interestbar_prob, promotion_prob=promotion_prob, max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score, predetermined_winner=c.user_is_loggedin).listing() return s
def make_spotlight(self): """Build the Spotlight. The frontpage gets a Spotlight box that contains promoted and organic links from the user's subscribed subreddits and promoted links targeted to the frontpage. If the user has disabled ads promoted links will not be shown. The content of the Spotlight box is a bit tricky because a single version of the frontpage is cached and displayed to all logged out users. Because of the caching we must include many promoted links and select one to display on the client side. Otherwise, each logged out user would see the same promoted link and we would not get the desired distribution of promoted link views. Most of the promoted links are included as stubs to reduce the size of the page. When a promoted link stub is selected by the lottery the full link is fetched and displayed. There are only ~1000 cache resets per day so it is necessary to use a large subset of the eligible promoted links when choosing stubs for the Spotlight box. Using 100 stubs works great when there are fewer than 100 possible promoted links and allows room for growth. """ organic_fullnames = organic.organic_links(c.user) promoted_links = [] # If prefs allow it, mix in promoted links and sr discovery content if c.user.pref_show_sponsors or not c.user.gold: if g.live_config['sr_discovery_links']: organic_fullnames.extend(g.live_config['sr_discovery_links']) n_promoted = 100 n_build = 1 if c.user_is_loggedin else 10 picker = (promote.lottery_promoted_links if c.user_is_loggedin else promote.sample_promoted_links) promo_tuples = picker(c.user, c.site, n=n_promoted) if not c.user_is_loggedin: promo_tuples.sort(key=lambda t: t.weight, reverse=True) b = CampaignBuilder( promo_tuples, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, skip=True, ) promoted_links, first, last, before, after = b.get_items() if promoted_links: stubs = promoted_links[n_build:] stubs = [ promote.PromoTuple(item._fullname, item.weight, item.campaign) for item in stubs ] promoted_links = promoted_links[:n_build] + stubs if not (organic_fullnames or promoted_links): return None random.shuffle(organic_fullnames) organic_fullnames = organic_fullnames[:10] b = IDBuilder(organic_fullnames, wrap=self.builder_wrapper, keep_fn=organic.keep_fresh_links, skip=True) organic_links = b.get_items()[0] has_subscribed = c.user.has_subscribed interestbar_prob = g.live_config[ 'spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] interestbar = InterestBar(has_subscribed) promotion_prob = 0.5 if c.user_is_loggedin else 1. s = SpotlightListing( organic_links=organic_links, promoted_links=promoted_links, interestbar=interestbar, interestbar_prob=interestbar_prob, promotion_prob=promotion_prob, max_num=self.listing_obj.max_num, max_score=self.listing_obj.max_score, predetermined_winner=c.user_is_loggedin).listing() return s
def spotlight(self): if (self.requested_ad or not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors): link_ids = None if self.requested_ad: link = None try: link = Link._by_fullname(self.requested_ad) except NotFound: pass if not (link and link.promoted and (c.user_is_sponsor or c.user_is_loggedin and link.author_id == c.user._id)): return self.abort404() # check if we can show the requested ad if promote.is_live_on_sr(link, c.site.name): link_ids = [link._fullname] else: return _("requested campaign not eligible for display") else: # no organic box on a hot page, then show a random promoted link link_ids = randomized_promotion_list(c.user, c.site) if link_ids: res = wrap_links(link_ids, wrapper=self.builder_wrapper, num=1, keep_fn=lambda x: x.fresh, skip=True) res.parent_name = "promoted" if res.things: return res elif (isinstance(c.site, DefaultSR) and (not c.user_is_loggedin or (c.user_is_loggedin and c.user.pref_organic))): # Spotlight shows rising links. If available, mix in subreddit # discovery links as well. (These don't count towards ad bids) spotlight_links = organic.organic_links(c.user) if hasattr(g, 'sr_discovery_links'): spotlight_links.extend(g.sr_discovery_links) random.shuffle(spotlight_links) spotlight_keep_fn = lambda l: promote.is_promo(l) or organic.keep_fresh_links(l) num_links = len(spotlight_links) else: spotlight_keep_fn = organic.keep_fresh_links num_links = organic.organic_length pos = organic_pos() if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if c.user.pref_show_sponsors or not c.user.gold: spotlight_links, pos = promote.insert_promoted(spotlight_links, pos) # Need to do this again, because if there was a duplicate removed, # pos might be pointing outside the list. if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if not spotlight_links: return None # get links in proximity to pos num_tl = len(spotlight_links) if num_tl <= 3: disp_links = spotlight_links else: left_side = max(-1, min(num_tl - 3, 8)) disp_links = [spotlight_links[(i + pos) % num_tl] for i in xrange(-2, left_side)] b = IDBuilder(disp_links, wrap = self.builder_wrapper, num = num_links, keep_fn = spotlight_keep_fn, skip = True) try: vislink = spotlight_links[pos] except IndexError: g.log.error("spotlight_links = %r" % spotlight_links) g.log.error("pos = %d" % pos) raise s = SpotlightListing(b, spotlight_links = spotlight_links, visible_link = vislink, max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score).listing() if len(s.things) > 0: # only pass through a listing if the links made it # through our builder organic.update_pos(pos+1) return s
def spotlight(self): campaigns_by_link = {} if (self.requested_ad or not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors): link_ids = None if self.requested_ad: link = None try: link = Link._by_fullname(self.requested_ad) except NotFound: pass if not (link and link.promoted and (c.user_is_sponsor or c.user_is_loggedin and link.author_id == c.user._id)): return self.abort404() # check if we can show the requested ad if promote.is_live_on_sr(link, c.site.name): link_ids = [link._fullname] else: return _("requested campaign not eligible for display") else: # no organic box on a hot page, then show a random promoted link promo_tuples = randomized_promotion_list(c.user, c.site) link_ids, camp_ids = zip(*promo_tuples) if promo_tuples else ([],[]) # save campaign-to-link mapping so campaign can be added to # link data later (for tracking.) Gotcha: assumes each link # appears for only campaign campaigns_by_link = dict(promo_tuples) if link_ids: res = wrap_links(link_ids, wrapper=self.builder_wrapper, num=1, keep_fn=lambda x: x.fresh, skip=True) res.parent_name = "promoted" if res.things: # store campaign id for tracking for thing in res.things: thing.campaign = campaigns_by_link.get(thing._fullname, None) return res elif (isinstance(c.site, DefaultSR) and (not c.user_is_loggedin or (c.user_is_loggedin and c.user.pref_organic))): spotlight_links = organic.organic_links(c.user) pos = organic_pos() if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) spotlight_keep_fn = organic.keep_fresh_links num_links = organic.organic_length # If prefs allow it, mix in promoted links and sr discovery content if c.user.pref_show_sponsors or not c.user.gold: if g.live_config['sr_discovery_links']: spotlight_links.extend(g.live_config['sr_discovery_links']) random.shuffle(spotlight_links) spotlight_keep_fn = lambda l: promote.is_promo(l) or organic.keep_fresh_links(l) num_links = len(spotlight_links) spotlight_links, pos, campaigns_by_link = promote.insert_promoted(spotlight_links, pos) # Need to do this again, because if there was a duplicate removed, # pos might be pointing outside the list. if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if not spotlight_links: return None # get links in proximity to pos num_tl = len(spotlight_links) if num_tl <= 3: disp_links = spotlight_links else: left_side = max(-1, min(num_tl - 3, 8)) disp_links = [spotlight_links[(i + pos) % num_tl] for i in xrange(-2, left_side)] b = IDBuilder(disp_links, wrap = self.builder_wrapper, num = num_links, keep_fn = spotlight_keep_fn, skip = True) try: vislink = spotlight_links[pos] except IndexError: g.log.error("spotlight_links = %r" % spotlight_links) g.log.error("pos = %d" % pos) raise s = SpotlightListing(b, spotlight_items = spotlight_links, visible_item = vislink, max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score).listing() has_subscribed = c.user.has_subscribed promo_visible = promote.is_promo(s.lookup[vislink]) if not promo_visible: prob = g.live_config['spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] if random.random() < prob: bar = InterestBar(has_subscribed) s.spotlight_items.insert(pos, bar) s.visible_item = bar if len(s.things) > 0: # only pass through a listing if the links made it # through our builder organic.update_pos(pos+1) # add campaign id to promoted links for tracking for thing in s.things: thing.campaign = campaigns_by_link.get(thing._fullname, None) return s
def spotlight(self): """Build the Spotlight or a single promoted link. The frontpage gets a Spotlight box that contains promoted and organic links from the user's subscribed subreddits and promoted links targeted to the frontpage. Other subreddits get a single promoted link. In either case if the user has disabled ads promoted links will not be shown. The content of the Spotlight box is a bit tricky because a single version of the frontpage is cached and displayed to all logged out users. Because of the caching we must include many promoted links and select one to display on the client side. Otherwise, each logged out user would see the same promoted link and we would not get the desired distribution of promoted link views. Most of the promoted links are included as stubs to reduce the size of the page. When a promoted link stub is selected by the lottery the full link is fetched and displayed. There are only ~1000 cache resets per day so it is necessary to use a large subset of the eligible promoted links when choosing stubs for the Spotlight box. Using 100 stubs works great when there are fewer than 100 possible promoted links and allows room for growth. """ campaigns_by_link = {} if (self.requested_ad or not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors): link_ids = None if self.requested_ad: link = None try: link = Link._by_fullname(self.requested_ad) except NotFound: pass if not (link and link.promoted and (c.user_is_sponsor or c.user_is_loggedin and link.author_id == c.user._id)): return self.abort404() # check if we can show the requested ad if promote.is_live_on_sr(link, c.site.name): link_ids = [link._fullname] else: return _("requested campaign not eligible for display") else: # no organic box on a hot page, then show a random promoted link promo_tuples = randomized_promotion_list(c.user, c.site) link_ids, camp_ids = zip(*promo_tuples) if promo_tuples else ([],[]) # save campaign-to-link mapping so campaign can be added to # link data later (for tracking.) Gotcha: assumes each link # appears for only campaign campaigns_by_link = dict(promo_tuples) if link_ids: res = wrap_links(link_ids, wrapper=self.builder_wrapper, num=1, keep_fn=organic.keep_fresh_links, skip=True) res.parent_name = "promoted" if res.things: # store campaign id for tracking for thing in res.things: thing.campaign = campaigns_by_link.get(thing._fullname, None) return res elif (isinstance(c.site, DefaultSR) and (not c.user_is_loggedin or (c.user_is_loggedin and c.user.pref_organic))): organic_fullnames = organic.organic_links(c.user) promoted_links = [] # If prefs allow it, mix in promoted links and sr discovery content if c.user.pref_show_sponsors or not c.user.gold: if g.live_config['sr_discovery_links']: organic_fullnames.extend(g.live_config['sr_discovery_links']) n_promoted = 100 n_build = 10 promo_tuples = promote.get_promoted_links(c.user, c.site, n_promoted) promo_tuples = sorted(promo_tuples, key=lambda p: p.weight, reverse=True) promo_build = promo_tuples[:n_build] promo_stub = promo_tuples[n_build:] b = CampaignBuilder(promo_build, wrap=self.builder_wrapper, keep_fn=promote.is_promoted) promoted_links = b.get_items()[0] promoted_links.extend(promo_stub) if not (organic_fullnames or promoted_links): return None random.shuffle(organic_fullnames) organic_fullnames = organic_fullnames[:10] b = IDBuilder(organic_fullnames, wrap = self.builder_wrapper, keep_fn = organic.keep_fresh_links, skip = True) organic_links = b.get_items()[0] has_subscribed = c.user.has_subscribed interestbar_prob = g.live_config['spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] interestbar = InterestBar(has_subscribed) promotion_prob = 0.5 if c.user_is_loggedin else 1. s = SpotlightListing(organic_links=organic_links, promoted_links=promoted_links, interestbar=interestbar, interestbar_prob=interestbar_prob, promotion_prob=promotion_prob, max_num = self.listing_obj.max_num, max_score = self.listing_obj.max_score).listing() return s
def spotlight(self): campaigns_by_link = {} if (self.requested_ad or not isinstance(c.site, DefaultSR) and c.user.pref_show_sponsors): link_ids = None if self.requested_ad: link = None try: link = Link._by_fullname(self.requested_ad) except NotFound: pass if not (link and link.promoted and (c.user_is_sponsor or c.user_is_loggedin and link.author_id == c.user._id)): return self.abort404() # check if we can show the requested ad if promote.is_live_on_sr(link, c.site.name): link_ids = [link._fullname] else: return _("requested campaign not eligible for display") else: # no organic box on a hot page, then show a random promoted link promo_tuples = randomized_promotion_list(c.user, c.site) link_ids, camp_ids = zip( *promo_tuples) if promo_tuples else ([], []) # save campaign-to-link mapping so campaign can be added to # link data later (for tracking.) Gotcha: assumes each link # appears for only campaign campaigns_by_link = dict(promo_tuples) if link_ids: res = wrap_links(link_ids, wrapper=self.builder_wrapper, num=1, keep_fn=lambda x: x.fresh, skip=True) res.parent_name = "promoted" if res.things: # store campaign id for tracking for thing in res.things: thing.campaign = campaigns_by_link.get( thing._fullname, None) return res elif (isinstance(c.site, DefaultSR) and (not c.user_is_loggedin or (c.user_is_loggedin and c.user.pref_organic))): spotlight_links = organic.organic_links(c.user) pos = organic_pos() if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) spotlight_keep_fn = organic.keep_fresh_links num_links = organic.organic_length # If prefs allow it, mix in promoted links and sr discovery content if c.user.pref_show_sponsors or not c.user.gold: if g.live_config['sr_discovery_links']: spotlight_links.extend(g.live_config['sr_discovery_links']) random.shuffle(spotlight_links) spotlight_keep_fn = lambda l: promote.is_promo( l) or organic.keep_fresh_links(l) num_links = len(spotlight_links) spotlight_links, pos, campaigns_by_link = promote.insert_promoted( spotlight_links, pos) # Need to do this again, because if there was a duplicate removed, # pos might be pointing outside the list. if not spotlight_links: pos = 0 elif pos != 0: pos = pos % len(spotlight_links) if not spotlight_links: return None # get links in proximity to pos num_tl = len(spotlight_links) if num_tl <= 3: disp_links = spotlight_links else: left_side = max(-1, min(num_tl - 3, 8)) disp_links = [ spotlight_links[(i + pos) % num_tl] for i in xrange(-2, left_side) ] b = IDBuilder(disp_links, wrap=self.builder_wrapper, num=num_links, keep_fn=spotlight_keep_fn, skip=True) try: vislink = spotlight_links[pos] except IndexError: g.log.error("spotlight_links = %r" % spotlight_links) g.log.error("pos = %d" % pos) raise s = SpotlightListing( b, spotlight_items=spotlight_links, visible_item=vislink, max_num=self.listing_obj.max_num, max_score=self.listing_obj.max_score).listing() has_subscribed = c.user.has_subscribed promo_visible = promote.is_promo(s.lookup[vislink]) if not promo_visible: prob = g.live_config[ 'spotlight_interest_sub_p' if has_subscribed else 'spotlight_interest_nosub_p'] if random.random() < prob: bar = InterestBar(has_subscribed) s.spotlight_items.insert(pos, bar) s.visible_item = bar if len(s.things) > 0: # only pass through a listing if the links made it # through our builder organic.update_pos(pos + 1) # add campaign id to promoted links for tracking for thing in s.things: thing.campaign = campaigns_by_link.get( thing._fullname, None) return s