forked from reddit-archive/reddit
/
promote.py
890 lines (751 loc) · 31 KB
/
promote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is reddit Inc.
#
# All portions of the code written by reddit are Copyright (c) 2006-2012 reddit
# Inc. All Rights Reserved.
###############################################################################
from __future__ import with_statement
from r2.models import *
from r2.lib.wrapped import Wrapped
from r2.lib import authorize
from r2.lib import emailer, filters
from r2.lib.memoize import memoize
from r2.lib.template_helpers import get_domain
from r2.lib.utils import Enum, UniqueIterator
from organic import keep_fresh_links
from pylons import g, c
from datetime import datetime, timedelta
from r2.lib.db.queries import make_results, db_sort, add_queries, merge_results
import itertools
import random
promoted_memo_lifetime = 30
promoted_memo_key = 'cached_promoted_links2'
promoted_lock_key = 'cached_promoted_links_lock2'
STATUS = Enum("unpaid", "unseen", "accepted", "rejected",
"pending", "promoted", "finished")
CAMPAIGN = Enum("start", "end", "bid", "sr", "trans_id")
@memoize("get_promote_srid")
def get_promote_srid(name = 'promos'):
try:
sr = Subreddit._by_name(name, stale=True)
except NotFound:
sr = Subreddit._new(name = name,
title = "promoted links",
# negative author_ids make this unlisable
author_id = -1,
type = "public",
ip = '0.0.0.0')
return sr._id
# attrs
def promo_traffic_url(l):
domain = get_domain(cname = False, subreddit = False)
return "http://%s/traffic/%s/" % (domain, l._id36)
def promo_edit_url(l):
domain = get_domain(cname = False, subreddit = False)
return "http://%s/promoted/edit_promo/%s" % (domain, l._id36)
def pay_url(l, indx):
return "%spromoted/pay/%s/%d" % (g.payment_domain, l._id36, indx)
def view_live_url(l, srname):
url = get_domain(cname=False, subreddit=False)
if srname:
url += '/r/%s' % srname
return 'http://%s/?ad=%s' % (url, l._fullname)
# booleans
def is_promo(link):
return (link and not link._deleted and link.promoted is not None
and hasattr(link, "promote_status"))
def is_accepted(link):
return is_promo(link) and (link.promote_status != STATUS.rejected and
link.promote_status >= STATUS.accepted)
def is_unpaid(link):
return is_promo(link) and link.promote_status == STATUS.unpaid
def is_unapproved(link):
return is_promo(link) and link.promote_status <= STATUS.unseen
def is_rejected(link):
return is_promo(link) and link.promote_status == STATUS.rejected
def is_promoted(link):
return is_promo(link) and link.promote_status == STATUS.promoted
def is_valid_campaign(link, campaign_id):
# check for campaign in link data (old way)
if link and campaign_id in getattr(link, "campaigns", {}):
return True
# check for campaign in Thing data (new way)
try:
PromoCampaign._byID(campaign_id)
return True
except NotFound:
return False
def is_live_on_sr(link, srname):
campaigns = link.campaigns
if not campaigns:
return False
live = scheduled_campaigns_by_link(link)
srname = srname.lower()
srname = srname if srname != DefaultSR.name.lower() else ''
for index in live:
campaign = campaigns.get(index)
if campaign and campaign[CAMPAIGN.sr].lower() == srname:
return True
return False
# no references to promote_status below this function, pls
def set_status(l, status, onchange = None):
# keep this out here. Useful for updating the queue if there is a bug
# and for initial migration
add_queries([_sponsored_link_query(None, l.author_id),
_sponsored_link_query(None),
_sponsored_link_query(status, l.author_id),
_sponsored_link_query(status)], insert_items = [l])
# no need to delete or commit of the status is unchanged
if status != getattr(l, "promote_status", None):
# new links won't even have a promote_status yet
if hasattr(l, "promote_status"):
add_queries([_sponsored_link_query(l.promote_status, l.author_id),
_sponsored_link_query(l.promote_status)],
delete_items = [l])
l.promote_status = status
l._commit()
if onchange:
onchange()
# query queue updates below
def _sponsored_link_query(status, author_id = None):
q = Link._query(Link.c.sr_id == get_promote_srid(),
Link.c._spam == (True, False),
Link.c._deleted == (True,False),
sort = db_sort('new'))
if status is not None:
q._filter(Link.c.promote_status == status)
if author_id is not None:
q._filter(Link.c.author_id == author_id)
return make_results(q)
def get_unpaid_links(author_id = None):
return _sponsored_link_query(STATUS.unpaid, author_id = author_id)
def get_unapproved_links(author_id = None):
return _sponsored_link_query(STATUS.unseen, author_id = author_id)
def get_rejected_links(author_id = None):
return _sponsored_link_query(STATUS.rejected, author_id = author_id)
def get_live_links(author_id = None):
return _sponsored_link_query(STATUS.promoted, author_id = author_id)
def get_accepted_links(author_id = None):
return merge_results(_sponsored_link_query(STATUS.accepted,
author_id = author_id),
_sponsored_link_query(STATUS.pending,
author_id = author_id),
_sponsored_link_query(STATUS.finished,
author_id = author_id))
def get_all_links(author_id = None):
return _sponsored_link_query(None, author_id = author_id)
# subreddit roadblocking functions
roadblock_prefix = "promotion_roadblock"
def roadblock_key(sr_name, d):
return "%s-%s_%s" % (roadblock_prefix,
sr_name, d.strftime("%Y_%m_%d"))
def roadblock_reddit(sr_name, start_date, end_date):
d = start_date
now = promo_datetime_now().date()
# set the expire to be 1 week after the roadblock end date
expire = ((end_date - now).days + 7) * 86400
while d < end_date:
g.hardcache.add(roadblock_key(sr_name, d),
"%s-%s" % (start_date.strftime("%Y_%m_%d"),
end_date.strftime("%Y_%m_%d")),
time = expire)
d += timedelta(1)
def unroadblock_reddit(sr_name, start_date, end_date):
d = start_date
while d < end_date:
g.hardcache.delete(roadblock_key(sr_name, d))
d += timedelta(1)
def is_roadblocked(sr_name, start_date, end_date):
d = start_date
while d < end_date:
res = g.hardcache.get(roadblock_key(sr_name, d))
if res:
start_date, end_date = res.split('-')
start_date = datetime.strptime(start_date, "%Y_%m_%d").date()
end_date = datetime.strptime(end_date, "%Y_%m_%d").date()
return (start_date, end_date)
d += timedelta(1)
def get_roadblocks():
rbs = g.hardcache.backend.ids_by_category(roadblock_prefix)
by_sr = {}
for rb in rbs:
rb = rb.split('_')
date = datetime.strptime('_'.join(rb[1:]), "%Y_%m_%d").date()
by_sr.setdefault(rb[0], []).append((date, date + timedelta(1)))
blobs = []
for k, v in by_sr.iteritems():
for sd, ed in sorted(v):
if blobs and blobs[-1][0] == k and blobs[-1][-1] == sd:
blobs[-1] = (k, blobs[-1][1], ed)
else:
blobs.append((k, sd, ed))
blobs.sort(key = lambda x: x[1])
return blobs
# control functions
class RenderableCampaign():
__slots__ = ["indx", "start_date", "end_date", "duration",
"bid", "sr", "status"]
def __init__(self, link, indx, raw_campaign, transaction):
sd, ed, bid, sr, trans_id = raw_campaign
self.indx = indx
self.start_date = sd.strftime("%m/%d/%Y")
self.end_date = ed.strftime("%m/%d/%Y")
ndays = (ed - sd).days
self.duration = strings.time_label % dict(num = ndays,
time = ungettext("day", "days", ndays))
self.bid = "%.2f" % bid
self.sr = sr
live = is_live_on_sr(link, sr)
self.status = dict(paid = bool(transaction),
complete = False,
free = (trans_id < 0),
pay_url = pay_url(link, indx),
view_live_url = view_live_url(link, sr),
sponsor = c.user_is_sponsor,
live = live)
if transaction:
if transaction.is_void():
self.status['paid'] = False
self.status['free'] = False
elif transaction.is_charged():
self.status['complete'] = True
def get(self, key, default):
return getattr(self, key, default)
def __iter__(self):
for s in self.__slots__:
yield getattr(self, s)
def editable_add_props(l):
if not isinstance(l, Wrapped):
l = Wrapped(l)
l.bids = get_transactions(l)
l.campaigns = dict((indx, RenderableCampaign(l, indx, campaign, l.bids.get(indx)))
for indx, campaign in
getattr(l, "campaigns", {}).iteritems())
return l
# These could be done with relationships, but that seeks overkill as
# we never query based on user and only check per-thing
def is_traffic_viewer(thing, user):
return (c.user_is_sponsor or user._id == thing.author_id or
user._id in getattr(thing, "promo_traffic_viewers", set()))
def add_traffic_viewer(thing, user):
viewers = getattr(thing, "promo_traffic_viewers", set()).copy()
if user._id not in viewers:
viewers.add(user._id)
thing.promo_traffic_viewers = viewers
thing._commit()
return True
return False
def rm_traffic_viewer(thing, user):
viewers = getattr(thing, "promo_traffic_viewers", set()).copy()
if user._id in viewers:
viewers.remove(user._id)
thing.promo_traffic_viewers = viewers
thing._commit()
return True
return False
def traffic_viewers(thing):
return sorted(getattr(thing, "promo_traffic_viewers", set()))
# logging routine for keeping track of diffs
def promotion_log(thing, text, commit = False):
"""
For logging all sorts of things
"""
name = c.user.name if c.user_is_loggedin else "<MAGIC>"
log = list(getattr(thing, "promotion_log", []))
now = datetime.now(g.tz).strftime("%Y-%m-%d %H:%M:%S")
text = "[%s: %s] %s" % (name, now, text)
log.append(text)
# copy (and fix encoding) to make _dirty
thing.promotion_log = map(filters._force_utf8, log)
if commit:
thing._commit()
return text
def new_promotion(title, url, user, ip):
"""
Creates a new promotion with the provided title, etc, and sets it
status to be 'unpaid'.
"""
sr = Subreddit._byID(get_promote_srid())
l = Link._submit(title, url, user, sr, ip)
l.promoted = True
l.disable_comments = False
l.campaigns = {}
promotion_log(l, "promotion created")
l._commit()
# set the status of the link, populating the query queue
if c.user_is_sponsor or user.trusted_sponsor:
set_status(l, STATUS.accepted)
else:
set_status(l, STATUS.unpaid)
# the user has posted a promotion, so enable the promote menu unless
# they have already opted out
if user.pref_show_promote is not False:
user.pref_show_promote = True
user._commit()
# notify of new promo
emailer.new_promo(l)
return l
def sponsor_wrapper(link):
w = Wrapped(link)
w.render_class = PromotedLink
w.rowstyle = "promoted link"
return w
def campaign_lock(link):
return "edit_promo_campaign_lock_" + str(link._id)
def get_transactions(link):
# tuple of (transaction_id, key)
trans_tuples = [(v[CAMPAIGN.trans_id], k)
for k, v in getattr(link, "campaigns", {}).iteritems()
if v[CAMPAIGN.trans_id] != 0]
bids = authorize.get_transactions(*trans_tuples)
return dict((indx, bids.get((t, indx))) for t, indx in trans_tuples)
def new_campaign(link, dates, bid, sr):
# empty string for sr_name means target to all
sr_name = sr.name if sr else ""
# dual-write campaigns as data Things
campaign = PromoCampaign._new(link, sr_name, bid, dates[0], dates[1])
# note indx in link.campaigns is the Thing id now
indx = campaign._id
with g.make_lock(campaign_lock(link)):
# get a copy of the attr so that it'll be
# marked as dirty on the next write.
campaigns = getattr(link, "campaigns", {}).copy()
# add the campaign
campaigns[indx] = list(dates) + [bid, sr_name, 0]
PromotionWeights.add(link, indx, sr_name, dates[0], dates[1], bid)
link.campaigns = {}
link.campaigns = campaigns
promotion_log(link, "campaign %s created" % campaign._id)
link._commit()
author = Account._byID(link.author_id, True)
if getattr(author, "complimentary_promos", False):
free_campaign(link, indx, c.user)
return indx
def free_campaign(link, index, user):
auth_campaign(link, index, user, -1)
def edit_campaign(link, index, dates, bid, sr):
sr_name = sr.name if sr else ""
with g.make_lock(campaign_lock(link)):
campaigns = getattr(link, "campaigns", {}).copy()
if index in campaigns:
trans_id = campaigns[index][CAMPAIGN.trans_id]
prev_bid = campaigns[index][CAMPAIGN.bid]
# store the name not the reddit
campaigns[index] = list(dates) + [bid, sr_name, trans_id]
PromotionWeights.reschedule(link, index,
sr_name, dates[0], dates[1], bid)
link.campaigns = {}
link.campaigns = campaigns
promotion_log(link, "updated campaign %s. (bid: %0.2f)" % (index, bid))
link._commit()
#TODO cancel any existing charges if the bid has changed
if prev_bid != bid:
void_campaign(link, index, c.user)
# dual-write update to campaign Thing if it exists
try:
campaign = PromoCampaign._byID(index)
campaign.set_bid(sr_name, bid, dates[0], dates[1])
campaign._commit()
except NotFound:
g.log.debug("Skipping update of non-existent PromoCampaign [link:%d, index:%d]" %
(link._id, index))
author = Account._byID(link.author_id, True)
if getattr(author, "complimentary_promos", False):
free_campaign(link, index, c.user)
def complimentary(username, value = True):
a = Account._by_name(username, True)
a.complimentary_promos = value
a._commit()
def delete_campaign(link, index):
with g.make_lock(campaign_lock(link)):
campaigns = getattr(link, "campaigns", {}).copy()
if index in campaigns:
PromotionWeights.delete_unfinished(link, index)
del campaigns[index]
link.campaigns = {}
link.campaigns = campaigns
promotion_log(link, "deleted campaign %s" % index)
link._commit()
#TODO cancel any existing charges
void_campaign(link, index, c.user)
# dual-write update to campaign Thing if it exists
try:
campaign = PromoCampaign._byID(index)
campaign.delete()
except NotFound:
g.log.debug("Skipping deletion of non-existent PromoCampaign [link:%d, index:%d]" %
(link._id, index))
def void_campaign(link, index, user):
campaigns = getattr(link, "campaigns", {}).copy()
if index in campaigns:
sd, ed, bid, sr, trans_id = campaigns[index]
transactions = get_transactions(link)
if transactions.get(index):
# void the existing transaction
a = Account._byID(link.author_id)
authorize.void_transaction(a, trans_id, index)
def auth_campaign(link, index, user, pay_id):
"""
for setting up a campaign as a real bid with authorize.net
"""
with g.make_lock(campaign_lock(link)):
campaigns = getattr(link, "campaigns", {}).copy()
if index in campaigns:
# void any existing campaign
void_campaign(link, index, user)
sd, ed, bid, sr, trans_id = campaigns[index]
# create a new transaction and update the bid
test = 1 if g.debug else None
trans_id, reason = authorize.auth_transaction(bid, user,
pay_id, link,
index,
test = test)
if not reason and trans_id is not None and int(trans_id) != 0:
promotion_log(link, "updated payment and/or bid for campaign %s: "
"SUCCESS (trans_id: %d, amt: %0.2f)"
% (index, trans_id, bid))
if trans_id < 0:
promotion_log(link, "FREEBIE (campaign: %s)" % index)
set_status(link,
max(STATUS.unseen if trans_id else STATUS.unpaid,
link.promote_status))
# notify of campaign creation
# update the query queue
if user._id == link.author_id and trans_id > 0:
emailer.promo_bid(link, bid, sd)
else:
# something bad happend.
promotion_log(link, "updated payment and/or bid for campaign %s: FAILED ('%s')"
% (index, reason))
trans_id = 0
campaigns[index] = sd, ed, bid, sr, trans_id
link.campaigns = {}
link.campaigns = campaigns
link._commit()
# dual-write update to campaign Thing
campaign = PromoCampaign._byID(index)
if campaign:
if trans_id > 0:
campaign.mark_paid(trans_id)
elif trans_id < 0:
campaign.mark_freebie(trans_id)
else:
campaign.mark_payment_error(reason)
campaign._commit()
return bool(trans_id), reason
return False, ""
# dates are referenced to UTC, while we want promos to change at (roughly)
# midnight eastern-US.
# TODO: make this a config parameter
timezone_offset = -5 # hours
timezone_offset = timedelta(0, timezone_offset * 3600)
def promo_datetime_now(offset = None):
now = datetime.now(g.tz) + timezone_offset
if offset is not None:
now += timedelta(offset)
return now
def accept_promotion(link):
"""
Accepting is campaign agnostic. Accepting the ad just means that
it is allowed to run if payment has been processed.
If a campagn is able to run, this also requeues it.
"""
promotion_log(link, "status update: accepted")
# update the query queue
set_status(link, STATUS.accepted)
now = promo_datetime_now(0)
if link._fullname in set(l.thing_name for l in
PromotionWeights.get_campaigns(now)):
promotion_log(link, "requeued")
charge_pending(0) # campaign must be charged before it will go live
make_daily_promotions()
if link._spam:
link._spam = False
link._commit()
emailer.accept_promo(link)
def reject_promotion(link, reason = None):
promotion_log(link, "status update: rejected")
# update the query queue
set_status(link, STATUS.rejected)
# check to see if this link is a member of the current live list
links, weighted = get_live_promotions()
if link._fullname in links:
links.remove(link._fullname)
for k in list(weighted.keys()):
weighted[k] = [(lid, w) for lid, w in weighted[k]
if lid != link._fullname]
if not weighted[k]:
del weighted[k]
set_live_promotions((links, weighted))
promotion_log(link, "dequeued")
# don't send a rejection email when the rejection was user initiated.
if not c.user or c.user._id != link.author_id:
emailer.reject_promo(link, reason = reason)
def unapprove_promotion(link):
promotion_log(link, "status update: unapproved")
# update the query queue
set_status(link, STATUS.unseen)
links, weghts = get_live_promotions()
def accepted_campaigns(offset=0):
now = promo_datetime_now(offset=offset)
campaigns = PromotionWeights.get_campaigns(now)
links = Link._by_fullname(set(x.thing_name for x in campaigns),
data=True, return_dict=True)
for x in campaigns:
l = links[x.thing_name]
if not is_accepted(l):
continue
camp = getattr(l, "campaigns", {}).get(x.promo_idx)
if not camp or not camp[CAMPAIGN.trans_id]:
continue
yield (l, x.promo_idx, camp)
def get_scheduled(offset=0):
by_sr = {}
for l, index, camp in accepted_campaigns(offset=offset):
sd, ed, bid, sr, trans_id = camp
if authorize.is_charged_transaction(trans_id, index):
by_sr.setdefault(sr, []).append((l, bid))
return by_sr
def charge_pending(offset=1):
for l, index, camp in accepted_campaigns(offset=offset):
user = Account._byID(l.author_id)
sd, ed, bid, sr, trans_id = camp
try:
if (authorize.is_charged_transaction(trans_id, index) or not
authorize.charge_transaction(user, trans_id, index)):
continue
if is_promoted(l):
emailer.queue_promo(l, bid, trans_id)
else:
set_status(l, STATUS.pending,
onchange=lambda: emailer.queue_promo(l, bid, trans_id))
promotion_log(l, "auth charge for campaign %s, trans_id: %d" % (index, trans_id), True)
except:
print "Error on %s, campaign %s" % (l, index)
def scheduled_campaigns_by_link(l, date=None):
# A promotion/campaign is scheduled/live if it's in
# PromotionWeights.get_campaigns(now) and
# authorize.is_charged_transaction()
date = date or promo_datetime_now()
if not is_accepted(l):
return []
if not l.campaigns:
return []
scheduled = PromotionWeights.get_campaigns(date)
campaigns = [c.promo_idx for c in scheduled if c.thing_name == l._fullname]
# Check authorize
accepted = []
for index in campaigns:
if not index in l.campaigns:
continue
sd, ed, bid, sr, trans_id = l.campaigns[index]
if authorize.is_charged_transaction(trans_id, index):
accepted.append(index)
return accepted
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 and len(t) > 1:
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
def get_weighted_schedule(offset = 0):
by_sr = get_scheduled(offset = offset)
weight_dict = get_traffic_weights(by_sr.keys())
weighted = {}
links = set()
for sr_name, t_tuples in by_sr.iteritems():
weighted[sr_name] = []
for l, weight in t_tuples:
links.add(l._fullname)
weighted[sr_name].append((l._fullname,
weight * weight_dict[sr_name]))
return links, weighted
def promotion_key():
return "current_promotions"
def get_live_promotions():
return g.permacache.get(promotion_key()) or (set(), {})
def set_live_promotions(x):
return g.permacache.set(promotion_key(), x)
# Gotcha: even if links are scheduled and authorized, they won't be added to
# current promotions until they're actually charged, so make sure to call
# charge_pending() before make_daily_promotions()
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)
def get_promotion_list(user, site):
# site is specified, pick an ad from that site
if not isinstance(site, FakeSubreddit):
srids = set([site._id])
elif isinstance(site, MultiReddit):
srids = set(site.sr_ids)
# site is Fake, user is not. Pick based on their subscriptions.
elif user and not isinstance(user, FakeAccount):
srids = set(Subreddit.reverse_subscriber_ids(user) + [""])
# both site and user are "fake" -- get the default subscription list
else:
srids = set(Subreddit.user_subreddits(None, True) + [""])
return get_promotions_cached(srids)
#@memoize('get_promotions_cached', time = 10 * 60)
def get_promotions_cached(sites):
p = get_live_promotions()
if p:
links, promo_dict = p
available = {}
for k, links in promo_dict.iteritems():
if k in sites:
for l, w in links:
available[l] = available.get(l, 0) + w
# sort the available list by weight
links = available.keys()
links.sort(key = lambda x: -available[x])
norm = sum(available.values())
# return a sorted list of (link, norm_weight)
return [(l, available[l] / norm) for l in links]
return []
def randomized_promotion_list(user, site):
promos = get_promotion_list(user, site)
# no promos, no problem
if not promos:
return []
# more than two: randomize
elif len(promos) > 1:
n = random.uniform(0, 1)
for i, (l, w) in enumerate(promos):
n -= w
if n < 0:
promos = promos[i:] + promos[:i]
break
# fall thru for the length 1 case here as well
return [l for l, w in promos]
def insert_promoted(link_names, pos, promoted_every_n = 5):
"""
Inserts promoted links into an existing organic list. Destructive
on `link_names'
"""
promoted_items = randomized_promotion_list(c.user, c.site)
if not promoted_items:
return link_names, pos
# no point in running the builder over more promoted links than
# we'll even use
max_promoted = max(1,len(link_names)/promoted_every_n)
builder = IDBuilder(promoted_items, keep_fn = keep_fresh_links,
skip = True)
promoted_items = builder.get_items()[0]
focus = None
if promoted_items:
focus = promoted_items[0]._fullname
# insert one promoted item for every N items
for i, item in enumerate(promoted_items):
p = i * (promoted_every_n + 1)
if p > len(link_names):
break
p += pos
if p > len(link_names):
p = p % len(link_names)
link_names.insert(p, item._fullname)
link_names = filter(None, link_names)
if focus:
try:
pos = link_names.index(focus)
except ValueError:
pass
# don't insert one at the head of the list 50% of the time for
# logged in users, and 50% of the time for logged-off users when
# the pool of promoted links is less than 3 (to avoid showing the
# same promoted link to the same person too often)
if ((c.user_is_loggedin or len(promoted_items) < 3) and
random.choice((True,False))):
pos = (pos + 1) % len(link_names)
return list(UniqueIterator(link_names)), pos
def benchmark_promoted(user, site, pos = 0, link_sample = 50, attempts = 100):
c.user = user
c.site = site
link_names = ["blah%s" % i for i in xrange(link_sample)]
res = {}
for i in xrange(attempts):
names, p = insert_promoted(link_names[::], pos)
name = names[p]
res[name] = res.get(name, 0) + 1
res = list(res.iteritems())
res.sort(key = lambda x : x[1], reverse = True)
expected = dict(get_promotion_list(user, site))
for l, v in res:
print "%s: %5.3f %3.5f" % (l,float(v)/attempts, expected.get(l, 0))
def Run(offset = 0):
charge_pending(offset = offset + 1)
charge_pending(offset = offset)
make_daily_promotions(offset = offset)