예제 #1
0
    def __init__(self):
        BaseAds.__init__(self)

        keywords = promote.keywords_from_context(
            c.user, c.site,
            include_subscriptions=False,
        )

        properties = adzerkpromote.properties_from_context(
            context=c,
            site=c.site,
            exclude=(None if c.user_is_loggedin else ["age_hours"]),
        )

        data = {
            "keywords": list(keywords),
            "properties": properties,
            "origin": c.request_origin,
        }

        placements = request.GET.get("placements", None)

        if c.user_is_sponsor and placements:
            data["placements"] = placements

        self.ad_url = g.adzerk_url.format(data=json.dumps(data))
        self.frame_id = "ad_main"
예제 #2
0
    def __init__(self):
        BaseAds.__init__(self)

        keywords = promote.keywords_from_context(
            c.user, c.site,
            include_subscriptions=False,
            live_promos_only=False,
        )

        # adzerk reporting is easier when not using a space in the tag
        if Frontpage.name in keywords:
            keywords = keywords - {Frontpage.name} | {FRONTPAGE_NAME}

        data = {
            "keywords": list(keywords),
            "origin": c.request_origin,
        }

        placements = request.GET.get("placements", None)

        if c.user_is_sponsor and placements:
            data["placements"] = placements

        self.ad_url = g.adzerk_url.format(data=json.dumps(data))
        self.frame_id = "ad_main"
예제 #3
0
    def __init__(self, displayed_things=[], link=None):
        BaseAds.__init__(self)

        keywords = promote.keywords_from_context(
            c.user, c.site,
            include_subscriptions=False,
            displayed_things=displayed_things,
            block_programmatic=getattr(link, 'block_programmatic', False)
        )

        properties = adzerkpromote.properties_from_context(
            context=c,
            site=c.site,
            exclude=(None if c.user_is_loggedin else ["age_hours"]),
        )

        if link:
            properties["page"] = link._fullname

        interests = [w[2:] for w in keywords if w.startswith('a.')]
        if len(interests) > 0:
            properties["interests"] = interests

        data = {
            "keywords": list(keywords),
            "properties": properties,
            "origin": c.request_origin,
        }

        placements = request.GET.get("placements", None)
        percentage = request.GET.get("percentage", None)

        if c.user_is_sponsor:
            if placements is not None:
                data["placements"] = placements

            if percentage is not None:
                data["properties"]["percentage"] = percentage

        self.ad_url = g.adzerk_url.format(data=json.dumps(data))
        self.frame_id = "ad_main"
예제 #4
0
    def POST_request_promo(
        self,
        site,
        srnames,
        is_mobile_web,
        platform,
        loid,
        is_refresh,
        displayed_things,
        referrer,
    ):
        self.OPTIONS_request_promo()

        if (errors.INVALID_SITE_PATH, "site") in c.errors:
            return abort(reddit_http_error(400, errors.INVALID_SITE_PATH))

        displayed_list = displayed_things.split(',') if displayed_things else []
        if site:
            keywords = promote.keywords_from_context(
                c.user,
                site,
                displayed_things=displayed_list,
            )
        elif srnames:
            keywords = srnames.split('+')
        else:
            return

        properties = properties_from_context(c, site)

        # backwards compat
        if platform is None:
            platform = "mobile_web" if is_mobile_web else "desktop"

        # request multiple ads in case some are hidden by the builder due
        # to the user's hides/preferences
        response = adzerk_request(
            keywords=keywords,
            properties=properties,
            user_id=self.get_uid(loid),
            platform=platform,
            is_refresh=is_refresh,
            referrer=referrer,
        )

        if not response:
            g.stats.simple_event('adzerk.request.no_promo')
            return

        # for adservers, adzerk returns markup so we pass it to the client
        if isinstance(response, AdserverResponse):
            g.stats.simple_event('adzerk.request.adserver')
            return responsive(response.body)

        res_by_campaign = {r.campaign: r for r in response}
        adserver_click_urls = {r.campaign: r.click_url for r in response}
        tuples = [promote.PromoTuple(r.link, 1., r.campaign) for r in response]
        builder = CampaignBuilder(tuples, wrap=default_thing_wrapper(),
                                  keep_fn=promote.promo_keep_fn,
                                  num=1,
                                  skip=True)
        listing = LinkListing(builder, nextprev=False).listing()
        promote.add_trackers(listing.things, c.site, adserver_click_urls=adserver_click_urls)
        promote.update_served(listing.things)
        if listing.things:
            g.stats.simple_event('adzerk.request.valid_promo')
            if is_refresh:
                g.stats.simple_event('adzerk.request.auto_refresh')

            w = listing.things[0]
            r = res_by_campaign[w.campaign]

            up = UrlParser(r.imp_pixel)
            up.hostname = "pixel.redditmedia.com"
            w.adserver_imp_pixel = up.unparse()
            w.adserver_upvote_pixel = r.upvote_pixel
            w.adserver_downvote_pixel = r.downvote_pixel
            w.adserver_click_url = r.click_url
            w.ecpm = r.ecpm
            w.moat_query = r.moat_query
            w.num = ""
            return responsive(w.render(), space_compress=True)
        else:
            g.stats.simple_event('adzerk.request.skip_promo')
예제 #5
0
    def POST_request_promo(
        self,
        site,
        srnames,
        is_mobile_web,
        platform,
        loid,
        is_refresh,
        placements,
        displayed_things,
        referrer,
        user_day,
        user_hour,
        adblock,
    ):
        self.OPTIONS_request_promo()

        if (errors.INVALID_SITE_PATH, "site") in c.errors:
            return abort(reddit_http_error(400, errors.INVALID_SITE_PATH))

        displayed_list = displayed_things.split(',') if displayed_things else []
        if site:
            keywords = promote.keywords_from_context(
                c.user,
                site,
                displayed_things=displayed_list,
            )
        elif srnames:
            keywords = srnames.split('+')
        else:
            return

        properties = promote.properties_from_context(
            context=c,
            keywords=keywords,
            placement="sponsored_headline",
            displayed_things=displayed_list,
            exclude=(None if c.user_is_loggedin else ["age_hours"]),
        )

        properties['user_day'] = user_day
        properties['user_hour'] = user_hour

        if adblock is not None:
            properties["adblock"] = adblock

        # backwards compat
        if platform is None:
            platform = "mobile_web" if is_mobile_web else "desktop"

        if not placements:
            placements = ["div0"]

        # request multiple ads in case some are hidden by the builder due
        # to the user's hides/preferences
        response = adzerk_request(
            keywords=keywords,
            properties=properties,
            user_id=self.get_uid(loid),
            placement_names=placements,
            platform=platform,
            is_refresh=is_refresh,
            referrer=referrer,
            timeout=int(g.live_config.get("ads_loading_timeout_ms", 1000) / 1000.),
        )

        if not response:
            g.stats.simple_event('adzerk.request.no_promo')
            return

        # for blanks, adzerk returns markup so we pass it to the client
        if isinstance(response, BlankCreativeResponse):
            g.stats.simple_event('adzerk.request.blank')
            return responsive(response.body)

        res_by_campaign = {r.campaign: r for r in response}
        adserver_click_urls = {r.campaign: r.click_url for r in response}
        priorities = {r.campaign: r.priority for r in response}
        tuples = [promote.PromoTuple(r.link, 1., r.campaign) for r in response]
        builder = CampaignBuilder(tuples, wrap=default_thing_wrapper(),
                                  keep_fn=promote.promo_keep_fn,
                                  num=1,
                                  skip=True)
        listing = LinkListing(builder, nextprev=False).listing()
        promote.add_trackers(
            listing.things, c.site,
            adserver_click_urls=adserver_click_urls, priorities=priorities)
        promote.update_served(listing.things)
        if listing.things:
            g.stats.simple_event('adzerk.request.valid_promo')
            if is_refresh:
                g.stats.simple_event('adzerk.request.auto_refresh')

            w = listing.things[0]
            r = res_by_campaign[w.campaign]

            up = UrlParser(r.imp_pixel)
            up.hostname = "pixel.redditmedia.com"
            w.adserver_imp_pixel = up.unparse()
            w.adserver_upvote_pixel = r.upvote_pixel
            w.adserver_downvote_pixel = r.downvote_pixel
            w.adserver_click_url = r.click_url
            w.ecpm = r.ecpm
            w.moat_query = r.moat_query
            w.num = ""
            return responsive(w.render(), space_compress=True)
        else:
            g.stats.simple_event('adzerk.request.skip_promo')