Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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