Пример #1
0
def make_daily_promotions():
    # charge campaigns so they can go live
    charge_pending(offset=0)
    charge_pending(offset=1)

    # promote links and record ids of promoted links
    link_ids = set()
    for campaign, link in get_scheduled_promos(offset=0):
        link_ids.add(link._id)
        promote_link(link, campaign)

    # expire finished links
    q = Link._query(Link.c.promote_status == PROMOTE_STATUS.promoted,
                    data=True)
    q = q._filter(not_(Link.c._id.in_(link_ids)))
    for link in q:
        update_promote_status(link, PROMOTE_STATUS.finished)
        emailer.finished_promo(link)

    # update subreddits with promos
    all_live_promo_srnames(_update=True)

    _mark_promos_updated()
    finalize_completed_campaigns(daysago=1)
    hooks.get_hook('promote.make_daily_promotions').call(offset=0)
Пример #2
0
def unpromote(thing, batch = False, status = STATUS.finished):
    """
    unpromote a link with provided status, removing it from the
    current promotional queue.
    """
    if status == STATUS.finished:
        PromoteDates.log_end(thing)
        emailer.finished_promo(thing)
        thing.unpromoted_on = datetime.now(g.tz)
        promotion_log(thing, "status update: finished")
    thing.promote_status = status
    thing._commit()
    if not batch:
        with g.make_lock(promoted_lock_key):
            promoted = get_promoted_direct()
            if thing._fullname in promoted:
                del promoted[thing._fullname]
                set_promoted(promoted)
Пример #3
0
def make_daily_promotions(offset=0, test=False):
    old_links = set([])
    all_links, weighted = get_weighted_schedule(offset)
    # over18 check
    for sr, links in weighted.iteritems():
        if sr:
            sr = Subreddit._by_name(sr)
            if sr.over_18:
                for l in Link._by_fullname([l[0] for l in links],
                                           return_dict=False):
                    l.over_18 = True
                    l._commit()

    x = get_live_promotions()
    if x:
        old_links, old_weights = x
        # links that need to be promoted
        new_links = all_links - old_links
        # links that have already been promoted
        old_links = old_links - all_links
    else:
        new_links = links

    links = Link._by_fullname(new_links.union(old_links),
                              data=True,
                              return_dict=True)
    for l in old_links:
        if is_promoted(links[l]):
            if test:
                print "unpromote", l
            else:
                # update the query queue
                set_status(links[l],
                           STATUS.finished,
                           onchange=lambda: emailer.finished_promo(links[l]))

    for l in new_links:
        if is_accepted(links[l]):
            if test:
                print "promote2", l
            else:
                # update the query queue
                set_status(links[l],
                           STATUS.promoted,
                           onchange=lambda: emailer.live_promo(links[l]))

    # convert the weighted dict to use sr_ids which are more useful
    srs = {"": ""}
    for srname in weighted.keys():
        if srname:
            srs[srname] = Subreddit._by_name(srname)._id
    weighted = dict((srs[k], v) for k, v in weighted.iteritems())

    if not test:
        set_live_promotions((all_links, weighted))
    else:
        print(all_links, weighted)
Пример #4
0
def make_daily_promotions(offset = 0, test = False):
    old_links = set([])
    all_links, weighted = get_weighted_schedule(offset)
    # over18 check
    for sr, links in weighted.iteritems():
        if sr:
            sr = Subreddit._by_name(sr)
            if sr.over_18:
                for l in Link._by_fullname([l[0] for l in links], return_dict = False):
                    l.over_18 = True
                    l._commit()

    x = get_live_promotions()
    if x:
        old_links, old_weights = x
        # links that need to be promoted
        new_links = all_links - old_links
        # links that have already been promoted
        old_links = old_links - all_links
    else:
        new_links = links

    links = Link._by_fullname(new_links.union(old_links), data = True,
                              return_dict = True)
    for l in old_links:
        if is_promoted(links[l]):
            if test:
                print "unpromote", l
            else:
                # update the query queue
                set_status(links[l], STATUS.finished, 
                           onchange = lambda: emailer.finished_promo(links[l]))

    for l in new_links:
        if is_accepted(links[l]):
            if test:
                print "promote2", l
            else:
                # update the query queue
                set_status(links[l], STATUS.promoted,
                           onchange = lambda: emailer.live_promo(links[l]))

    # convert the weighted dict to use sr_ids which are more useful
    srs = {"":""}
    for srname in weighted.keys():
        if srname:
            srs[srname] = Subreddit._by_name(srname)._id
    weighted = dict((srs[k], v) for k, v in weighted.iteritems())

    if not test:
        set_live_promotions((all_links, weighted))
    else:
        print (all_links, weighted)
Пример #5
0
def make_daily_promotions():
    # charge campaigns so they can go live
    charge_pending(offset=0)
    charge_pending(offset=1)

    # promote links and record ids of promoted links
    link_ids = set()
    for campaign, link in get_scheduled_promos(offset=0):
        link_ids.add(link._id)
        promote_link(link, campaign)

    # expire finished links
    q = Link._query(Link.c.promote_status == PROMOTE_STATUS.promoted, data=True)
    q = q._filter(not_(Link.c._id.in_(link_ids)))
    for link in q:
        update_promote_status(link, PROMOTE_STATUS.finished)
        emailer.finished_promo(link)

    # update subreddits with promos
    all_live_promo_srnames(_update=True)

    _mark_promos_updated()
    finalize_completed_campaigns(daysago=1)
    hooks.get_hook('promote.make_daily_promotions').call(offset=0)
Пример #6
0
def make_daily_promotions(offset=0, test=False):
    """
    Arguments:
      offset - number of days after today to get the schedule for
      test - if True, new schedule will be generated but not launched
    Raises Exception with list of campaigns that had errors if there were any
    """
    by_srname, links, error_campaigns = get_scheduled(offset)
    all_links = set([l._fullname for l in links])
    srs = Subreddit._by_name(by_srname.keys())

    # over18 check
    for srname, adweights in by_srname.iteritems():
        if srname:
            sr = srs[srname]
            if sr.over_18:
                sr_links = Link._by_fullname([a.link for a in adweights],
                                             return_dict=False)
                for l in sr_links:
                    l.over_18 = True
                    if not test:
                        l._commit()

    old_ads = get_live_promotions([LiveAdWeights.ALL_ADS])
    old_links = set(x.link for x in old_ads[LiveAdWeights.ALL_ADS])

    # links that need to be promoted
    new_links = all_links - old_links
    # links that have already been promoted
    old_links = old_links - all_links

    links = Link._by_fullname(new_links.union(old_links),
                              data=True,
                              return_dict=True)

    for l in old_links:
        if is_promoted(links[l]):
            if test:
                print "unpromote", l
            else:
                # update the query queue
                set_promote_status(links[l], PROMOTE_STATUS.finished)
                emailer.finished_promo(links[l])

    for l in new_links:
        if is_accepted(links[l]):
            if test:
                print "promote2", l
            else:
                # update the query queue
                set_promote_status(links[l], PROMOTE_STATUS.promoted)
                emailer.live_promo(links[l])

    # convert the weighted dict to use sr_ids which are more useful
    by_srid = {
        srs[srname]._id: adweights
        for srname, adweights in by_srname.iteritems() if srname != ''
    }
    if '' in by_srname:
        by_srid[''] = by_srname['']

    if not test:
        set_live_promotions(by_srid)
        _mark_promos_updated()
    else:
        print by_srid

    # after launching as many campaigns as possible, raise an exception to
    #   report any error campaigns. (useful for triggering alerts in irc)
    if error_campaigns:
        raise Exception("Some scheduled campaigns could not be added to daily "
                        "promotions: %r" % error_campaigns)
Пример #7
0
def make_daily_promotions(offset=0, test=False):
    """
    Arguments:
      offset - number of days after today to get the schedule for
      test - if True, new schedule will be generated but not launched
    Raises Exception with list of campaigns that had errors if there were any
    """

    scheduled_adweights, error_campaigns = get_scheduled(offset)
    current_adweights_byid = get_live_promotions([LiveAdWeights.ALL_ADS])
    current_adweights = current_adweights_byid[LiveAdWeights.ALL_ADS]

    link_names = [aw.link for aw in itertools.chain(scheduled_adweights,
                                                    current_adweights)]
    links = Link._by_fullname(link_names, data=True)

    camp_names = [aw.campaign for aw in itertools.chain(scheduled_adweights,
                                                        current_adweights)]
    campaigns = PromoCampaign._by_fullname(camp_names, data=True)
    srs = Subreddit._by_name([camp.sr_name for camp in campaigns.itervalues()
                              if camp.sr_name])

    expired_links = ({aw.link for aw in current_adweights} -
                     {aw.link for aw in scheduled_adweights})
    for link_name in expired_links:
        link = links[link_name]
        if is_promoted(link):
            if test:
                print "unpromote", link_name
            else:
                # update the query queue
                set_promote_status(link, PROMOTE_STATUS.finished)
                emailer.finished_promo(link)

    by_srid = defaultdict(list)
    for adweight in scheduled_adweights:
        link = links[adweight.link]
        campaign = campaigns[adweight.campaign]
        if campaign.sr_name:
            sr = srs[campaign.sr_name]
            sr_id = sr._id
            sr_over_18 = sr.over_18
        else:
            sr_id = ''
            sr_over_18 = False

        if sr_over_18:
            if test:
                print "over18", link._fullname
            else:
                link.over_18 = True
                link._commit()

        if is_accepted(link) and not is_promoted(link):
            if test:
                print "promote2", link._fullname
            else:
                # update the query queue
                set_promote_status(link, PROMOTE_STATUS.promoted)
                emailer.live_promo(link)

        by_srid[sr_id].append(adweight)

    if not test:
        set_live_promotions(by_srid)
        _mark_promos_updated()
    else:
        print by_srid

    finalize_completed_campaigns(daysago=offset+1)
    hooks.get_hook('promote.make_daily_promotions').call(offset=offset)

    # after launching as many campaigns as possible, raise an exception to 
    #   report any error campaigns. (useful for triggering alerts in irc)
    if error_campaigns:
        raise Exception("Some scheduled campaigns could not be added to daily "
                        "promotions: %r" % error_campaigns)
Пример #8
0
def make_daily_promotions(offset = 0, test = False):
    """
    Arguments:
      offset - number of days after today to get the schedule for
      test - if True, new schedule will be generated but not launched
    Raises Exception with list of campaigns that had errors if there were any
    """
    schedule = get_scheduled(offset)
    all_links = set([l._fullname for l in schedule['links']])
    error_campaigns = schedule['error_campaigns']
    weighted = weight_schedule(schedule['by_sr'])

    # over18 check
    for sr, links in weighted.iteritems():
        if sr:
            sr = Subreddit._by_name(sr)
            if sr.over_18:
                for l in Link._by_fullname([l[0] for l in links], return_dict = False):
                    l.over_18 = True
                    if not test:
                        l._commit()

    old_links = get_live_promotions([SponsorBoxWeightings.ALL_ADS_ID])[0]
    
    # links that need to be promoted
    new_links = all_links - old_links
    # links that have already been promoted
    old_links = old_links - all_links

    links = Link._by_fullname(new_links.union(old_links), data = True,
                              return_dict = True)
    
    for l in old_links:
        if is_promoted(links[l]):
            if test:
                print "unpromote", l
            else:
                # update the query queue
                set_promote_status(links[l], PROMOTE_STATUS.finished)
                emailer.finished_promo(links[l])

    for l in new_links:
        if is_accepted(links[l]):
            if test:
                print "promote2", l
            else:
                # update the query queue
                set_promote_status(links[l], PROMOTE_STATUS.promoted)
                emailer.live_promo(links[l])

    # convert the weighted dict to use sr_ids which are more useful
    srs = {"":""}
    for srname in weighted.keys():
        if srname:
            srs[srname] = Subreddit._by_name(srname)._id
    weighted = dict((srs[k], v) for k, v in weighted.iteritems())

    if not test:
        set_live_promotions(all_links, weighted)
        _mark_promos_updated()
    else:
        print (all_links, weighted)

    # after launching as many campaigns as possible, raise an exception to 
    #   report any error campaigns. (useful for triggering alerts in irc)
    if error_campaigns:
        raise Exception("Some scheduled campaigns could not be added to daily "
                        "promotions: %r" % error_campaigns)
Пример #9
0
def make_daily_promotions(offset=0, test=False):
    """
    Arguments:
      offset - number of days after today to get the schedule for
      test - if True, new schedule will be generated but not launched
    Raises Exception with list of campaigns that had errors if there were any
    """
    by_srname, links, error_campaigns = get_scheduled(offset)
    all_links = set([l._fullname for l in links])
    srs = Subreddit._by_name(by_srname.keys())

    # over18 check
    for srname, adweights in by_srname.iteritems():
        if srname:
            sr = srs[srname]
            if sr.over_18:
                sr_links = Link._by_fullname([a.link for a in adweights],
                                             return_dict=False)
                for l in sr_links:
                    l.over_18 = True
                    if not test:
                        l._commit()

    old_ads = get_live_promotions([LiveAdWeights.ALL_ADS])
    old_links = set(x.link for x in old_ads[LiveAdWeights.ALL_ADS])

    # links that need to be promoted
    new_links = all_links - old_links
    # links that have already been promoted
    old_links = old_links - all_links

    links = Link._by_fullname(new_links.union(old_links), data=True,
                              return_dict=True)

    for l in old_links:
        if is_promoted(links[l]):
            if test:
                print "unpromote", l
            else:
                # update the query queue
                set_promote_status(links[l], PROMOTE_STATUS.finished)
                emailer.finished_promo(links[l])

    for l in new_links:
        if is_accepted(links[l]):
            if test:
                print "promote2", l
            else:
                # update the query queue
                set_promote_status(links[l], PROMOTE_STATUS.promoted)
                emailer.live_promo(links[l])

    # convert the weighted dict to use sr_ids which are more useful
    by_srid = {srs[srname]._id: adweights for srname, adweights
                                          in by_srname.iteritems()
                                          if srname != ''}
    if '' in by_srname:
        by_srid[''] = by_srname['']

    if not test:
        set_live_promotions(by_srid)
        _mark_promos_updated()
    else:
        print by_srid

    # after launching as many campaigns as possible, raise an exception to 
    #   report any error campaigns. (useful for triggering alerts in irc)
    if error_campaigns:
        raise Exception("Some scheduled campaigns could not be added to daily "
                        "promotions: %r" % error_campaigns)
Пример #10
0
def make_daily_promotions(offset=0, test=False):
    '''
    Arguments:
      offset - number of days after today to get the schedule for
      test - if True, new schedule will be generated but not launched
    Raises Exception with list of campaigns that had errors if there were any
    '''
    old_links = set([])

    schedule = get_scheduled(offset)
    all_links = set([l._fullname for l in schedule['links']])
    error_campaigns = schedule['error_campaigns']
    weighted = weight_schedule(schedule['by_sr'])

    # over18 check
    for sr, links in weighted.iteritems():
        if sr:
            sr = Subreddit._by_name(sr)
            if sr.over_18:
                for l in Link._by_fullname([l[0] for l in links],
                                           return_dict=False):
                    l.over_18 = True
                    if not test:
                        l._commit()

    x = get_live_promotions()
    if x:
        old_links, old_weights = x
        # links that need to be promoted
        new_links = all_links - old_links
        # links that have already been promoted
        old_links = old_links - all_links
    else:
        new_links = links

    links = Link._by_fullname(new_links.union(old_links),
                              data=True,
                              return_dict=True)
    for l in old_links:
        if is_promoted(links[l]):
            if test:
                print "unpromote", l
            else:
                # update the query queue
                set_status(links[l],
                           STATUS.finished,
                           onchange=lambda: emailer.finished_promo(links[l]))

    for l in new_links:
        if is_accepted(links[l]):
            if test:
                print "promote2", l
            else:
                # update the query queue
                set_status(links[l],
                           STATUS.promoted,
                           onchange=lambda: emailer.live_promo(links[l]))

    # convert the weighted dict to use sr_ids which are more useful
    srs = {"": ""}
    for srname in weighted.keys():
        if srname:
            srs[srname] = Subreddit._by_name(srname)._id
    weighted = dict((srs[k], v) for k, v in weighted.iteritems())

    if not test:
        set_live_promotions((all_links, weighted))
    else:
        print(all_links, weighted)

    # after launching as many campaigns as possible, raise an exception to
    #   report any error campaigns. (useful for triggering alerts in irc)
    if error_campaigns:
        raise Exception("Some scheduled campaigns could not be added to daily "
                        "promotions: %r" % error_campaigns)
Пример #11
0
def make_daily_promotions(offset=0, test=False):
    """
    Arguments:
      offset - number of days after today to get the schedule for
      test - if True, new schedule will be generated but not launched
    Raises Exception with list of campaigns that had errors if there were any
    """

    scheduled_adweights, error_campaigns = get_scheduled(offset)
    current_adweights_byid = get_live_promotions([LiveAdWeights.ALL_ADS])
    current_adweights = current_adweights_byid[LiveAdWeights.ALL_ADS]

    link_names = [aw.link for aw in itertools.chain(scheduled_adweights,
                                                    current_adweights)]
    links = Link._by_fullname(link_names, data=True)

    camp_names = [aw.campaign for aw in itertools.chain(scheduled_adweights,
                                                        current_adweights)]
    campaigns = PromoCampaign._by_fullname(camp_names, data=True)
    srs = Subreddit._by_name([camp.sr_name for camp in campaigns.itervalues()
                              if camp.sr_name])

    expired_links = ({aw.link for aw in current_adweights} -
                     {aw.link for aw in scheduled_adweights})
    for link_name in expired_links:
        link = links[link_name]
        if is_promoted(link):
            if test:
                print "unpromote", link_name
            else:
                # update the query queue
                set_promote_status(link, PROMOTE_STATUS.finished)
                emailer.finished_promo(link)

    by_srid = defaultdict(list)
    for adweight in scheduled_adweights:
        link = links[adweight.link]
        campaign = campaigns[adweight.campaign]
        if campaign.sr_name:
            sr = srs[campaign.sr_name]
            sr_id = sr._id
            sr_over_18 = sr.over_18
        else:
            sr_id = ''
            sr_over_18 = False

        if sr_over_18:
            if test:
                print "over18", link._fullname
            else:
                link.over_18 = True
                link._commit()

        if is_accepted(link) and not is_promoted(link):
            if test:
                print "promote2", link._fullname
            else:
                # update the query queue
                set_promote_status(link, PROMOTE_STATUS.promoted)
                emailer.live_promo(link)

        by_srid[sr_id].append(adweight)

    if not test:
        set_live_promotions(by_srid)
        _mark_promos_updated()
    else:
        print by_srid

    finalize_completed_campaigns(daysago=offset+1)
    hooks.get_hook('promote.make_daily_promotions').call(offset=offset)

    # after launching as many campaigns as possible, raise an exception to 
    #   report any error campaigns. (useful for triggering alerts in irc)
    if error_campaigns:
        raise Exception("Some scheduled campaigns could not be added to daily "
                        "promotions: %r" % error_campaigns)