def __init__(self, source=None, time_secs=time.time, opts=None): self.feeds = {} self.opts = opts if opts is not None else choose_ad_options() log.info('feed trends chose option set: %r', self.opts) if source is None: source = default_ad_source(self.opts['campaign']) self.ads_by_ids = LRU(MAX_ADS) self.ad_queue = AdQueue(source, time_secs) self.new_ad_request_serial = 0 self.new_ad_request_count = 0
class FeedAds(object): def __init__(self, source=None, time_secs=time.time, opts=None): self.feeds = {} self.opts = opts if opts is not None else choose_ad_options() log.info('feed trends chose option set: %r', self.opts) if source is None: source = default_ad_source(self.opts['campaign']) self.ads_by_ids = LRU(MAX_ADS) self.ad_queue = AdQueue(source, time_secs) self.new_ad_request_serial = 0 self.new_ad_request_count = 0 def first_ad_index(self): idx = self.opts['startpos'] if isinstance(idx, tuple): # tuple means "choose random number in range" return random.randint(*idx) return idx source = property(lambda self: self.ad_queue.source, lambda self, src: setattr(self.ad_queue, 'source', src)) append_short_links = property(lambda self: self.ad_queue.append_short_links, lambda self, v: setattr(self.ad_queue, 'append_short_links', v)) def start_ad_request(self): self.ad_queue.once_have_ads(None) def ad_created(self, ad): self.ads_by_ids[hash(ad)] = ad def get_ad_by_id(self, id): return self.ads_by_ids.get(id) def feed_for_context(self, context): try: feed = self.feeds[context] except KeyError: feed = self.feeds[context] = AdPlacement(self, context) return feed def request_new_ads(self, new_ads, on_done): #print 'request_new_ads', len(new_ads) self.new_ad_request_serial += len(new_ads) ctx = dict() new_ads = new_ads[:] ctx['i'] = 0 new_ads[ctx['i']].ad = sentinel def on_ad(ad): if ad is not None: new_ads[ctx['i']].ad = ad self.ad_created(ad) ctx['i'] += 1 if ctx['i'] >= len(new_ads): on_done() else: self.get_new_ad(on_ad) else: on_done() self.get_new_ad(on_ad) def wrap_iterator(self, info): original_iterator = iter(info.iterator) #print #print #print '-'*80 #print 'wrapping iterator, afters:' #from pprint import pprint feed = self.feed_for_context(info.id) #pprint(afters) def feedad(ad, prev_item=None): return transform_ad(info, ad, prev_item, self.opts['linkstyle']) def newiter(): first_item = original_iterator.next() first_ad = feed.ad_first() if first_ad is not None: yield feedad(first_ad, first_item) prev_item, prev_id = first_item, first_item.id yield first_item for i, item in enumerate(original_iterator): id = item.id if prev_item is not None: ad = feed.ad_after_id(prev_id) if ad is not None: yield feedad(ad, prev_item) prev_item, prev_id = item, id yield item info.iterator = newiter() return info def get_new_ad(self, cb): self.new_ad_request_count += 1 self.ad_queue.next_ad(cb)
class FeedAds(object): def __init__(self, source=None, time_secs=time.time, opts=None): self.feeds = {} self.opts = opts if opts is not None else choose_ad_options() log.info('feed trends chose option set: %r', self.opts) if source is None: source = default_ad_source(self.opts['campaign']) self.ads_by_ids = LRU(MAX_ADS) self.ad_queue = AdQueue(source, time_secs) self.new_ad_request_serial = 0 self.new_ad_request_count = 0 def first_ad_index(self): idx = self.opts['startpos'] if isinstance(idx, tuple): # tuple means "choose random number in range" return random.randint(*idx) return idx source = property(lambda self: self.ad_queue.source, lambda self, src: setattr(self.ad_queue, 'source', src)) append_short_links = property( lambda self: self.ad_queue.append_short_links, lambda self, v: setattr(self.ad_queue, 'append_short_links', v)) def start_ad_request(self): self.ad_queue.once_have_ads(None) def ad_created(self, ad): self.ads_by_ids[hash(ad)] = ad def get_ad_by_id(self, id): return self.ads_by_ids.get(id) def feed_for_context(self, context): try: feed = self.feeds[context] except KeyError: feed = self.feeds[context] = AdPlacement(self, context) return feed def request_new_ads(self, new_ads, on_done): #print 'request_new_ads', len(new_ads) self.new_ad_request_serial += len(new_ads) ctx = dict() new_ads = new_ads[:] ctx['i'] = 0 new_ads[ctx['i']].ad = sentinel def on_ad(ad): if ad is not None: new_ads[ctx['i']].ad = ad self.ad_created(ad) ctx['i'] += 1 if ctx['i'] >= len(new_ads): on_done() else: self.get_new_ad(on_ad) else: on_done() self.get_new_ad(on_ad) def wrap_iterator(self, info): original_iterator = iter(info.iterator) #print #print #print '-'*80 #print 'wrapping iterator, afters:' #from pprint import pprint feed = self.feed_for_context(info.id) #pprint(afters) def feedad(ad, prev_item=None): return transform_ad(info, ad, prev_item, self.opts['linkstyle']) def newiter(): first_item = original_iterator.next() first_ad = feed.ad_first() if first_ad is not None: yield feedad(first_ad, first_item) prev_item, prev_id = first_item, first_item.id yield first_item for i, item in enumerate(original_iterator): id = item.id if prev_item is not None: ad = feed.ad_after_id(prev_id) if ad is not None: yield feedad(ad, prev_item) prev_item, prev_id = item, id yield item info.iterator = newiter() return info def get_new_ad(self, cb): self.new_ad_request_count += 1 self.ad_queue.next_ad(cb)