Esempio n. 1
0
def get_traffic_weights(srnames):
    from r2.lib import traffic

    # the weight is just the last 7 days of impressions (averaged)
    def weigh(t, npoints=7):
        if t:
            t = [y[1] for x, y in t[-npoints - 1:-1]]
            return max(float(sum(t)) / len(t), 1)
        return 1

    default_traffic = [
        weigh(traffic.load_traffic("day", "reddit", sr.name))
        for sr in Subreddit.top_lang_srs('all', 10)
    ]
    default_traffic = (float(max(sum(default_traffic), 1)) /
                       max(len(default_traffic), 1))

    res = {}
    for srname in srnames:
        if srname:
            res[srname] = (
                default_traffic /
                weigh(traffic.load_traffic("day", "reddit", srname)))
        else:
            res[srname] = 1
    return res
Esempio n. 2
0
def get_traffic_weights(srnames):
    from r2.lib import traffic

    # the weight is just the last 7 days of impressions (averaged)
    def weigh(t, npoints = 7):
        if t:
            t = [y[1] for x, y in t[-npoints-1:-1]]
            return max(float(sum(t)) / len(t), 1)
        return 1

    default_traffic = [weigh(traffic.load_traffic("day", "reddit", sr.name))
                             for sr in Subreddit.top_lang_srs('all', 10)]
    default_traffic = (float(max(sum(default_traffic),1)) /
                       max(len(default_traffic), 1))

    res = {}
    for srname in srnames:
        if srname:
            res[srname] = (default_traffic /
                          weigh(traffic.load_traffic("day", "reddit", srname)) )
        else:
            res[srname] = 1
    return res
Esempio n. 3
0
def promote_promoted(test = False):
    """
    make promotions that are no longer supposed to be active
    'finished' and find all pending promotions that are supposed to be
    promoted and promote them.
    """
    from r2.lib.traffic import load_traffic
    with g.make_lock(promoted_lock_key):
        now = promo_datetime_now()

        promoted =  Link._by_fullname(get_promoted_direct().keys(),
                                      data = True, return_dict = False)
        promos = {}
        for l in promoted:
            keep = True
            if l.promote_until < now:
                keep = False
            maximum_clicks = getattr(l, "maximum_clicks", None)
            maximum_views = getattr(l, "maximum_views", None)
            if maximum_clicks or maximum_views:
                # grab the traffic
                traffic = load_traffic("day", "thing", l._fullname)
                if traffic:
                    # (unique impressions, number impressions, 
                    #  unique clicks, number of clicks)
                    traffic = [y for x, y in traffic]
                    traffic = map(sum, zip(*traffic))
                    uimp, nimp, ucli, ncli = traffic
                    if maximum_clicks and maximum_clicks < ncli:
                        keep = False
                    if maximum_views and maximum_views < nimp:
                        keep = False

            if not keep:
                if test:
                    print "Would have unpromoted: (%s, %s)" % \
                          (l, l.make_permalink(None))
                else:
                    unpromote(l, batch = True)

        new_promos = Link._query(Link.c.promote_status == (STATUS.pending,
                                                           STATUS.promoted),
                                 Link.c.promoted == True,
                                 data = True)
        for l in new_promos:
            if l.promote_until > now and l._date <= now:
                if test:
                    print "Would have promoted: %s" % l
                else:
                    promote(l, batch = True)
                promos[l._fullname] = auction_weight(l)
            elif l.promote_until <= now:
                if test:
                    print "Would have unpromoted: (%s, %s)" % \
                          (l, l.make_permalink(None))
                else:
                    unpromote(l, batch = True)

        # remove unpaid promos that are scheduled to run on today or before
        unpaid_promos = Link._query(Link.c.promoted == True,
                                    Link.c.promote_status == STATUS.unpaid,
                                    Link.c._date < now,
                                    Link.c._deleted == False, 
                                    data = True)
        for l in unpaid_promos:
            if test:
                print "Would have rejected: %s" % promo_edit_url(l)
            else:
                reject_promo(l, reason = "We're sorry, but this sponsored link was not set up for payment before the appointed date.  Please add payment info and move the date into the future if you would like to resubmit.  Also please feel free to email us at [email protected] if you believe this email is in error.")


        if test:
            print promos
        else:
            set_promoted(promos)
        return promos