本文整理汇总了Python中r2.models.Link._byID方法的典型用法代码示例。如果您正苦于以下问题:Python Link._byID方法的具体用法?Python Link._byID怎么用?Python Link._byID使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类r2.models.Link
的用法示例。
在下文中一共展示了Link._byID方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_comment_tree
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def add_comment_tree(comments):
# update the comment cache
add_comments(comments)
# update last modified
links = Link._byID(list(set(com.link_id for com in tup(comments))), data=True, return_dict=False)
for link in links:
set_last_modified(link, "comments")
示例2: run
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def run(verbose=True, sleep_time = 60, num_items = 1):
key = "indextank_cursor"
cursor = g.cache.get(key)
if cursor is None:
raise ValueError("%s is not set!" % key)
cursor = int(cursor)
while True:
if verbose:
print "Looking for %d items with _id < %d" % (num_items, cursor)
q = Link._query(sort = desc('_id'),
limit = num_items)
q._after(Link._byID(cursor))
last_date = None
for item in q:
cursor = item._id
last_date = item._date
amqp.add_item('indextank_changes', item._fullname,
message_id = item._fullname,
delivery_mode = amqp.DELIVERY_TRANSIENT)
g.cache.set(key, cursor)
if verbose:
if last_date:
last_date = last_date.strftime("%Y-%m-%d")
print ("Just enqueued %d items. New cursor=%s (%s). Sleeping %d seconds."
% (num_items, cursor, last_date, sleep_time))
sleep(sleep_time)
示例3: get_promos
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def get_promos(date, sr_names=None, link=None):
campaign_ids = PromotionWeights.get_campaign_ids(date, sr_names=sr_names, link=link)
campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False)
link_ids = {camp.link_id for camp in campaigns}
links = Link._byID(link_ids, data=True)
for camp in campaigns:
yield camp, links[camp.link_id]
示例4: test_run
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def test_run(start_link, count=1000):
'''Inject `count` number of links, starting with `start_link`'''
if isinstance(start_link, basestring):
start_link = int(start_link, 36)
links = Link._byID(range(start_link - count, start_link), data=True,
return_dict=False)
return inject(links)
示例5: _use_adserver_reporting
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def _use_adserver_reporting(thing):
if not feature.is_enabled("adserver_reporting"):
return False
if not g.adserver_reporting_cutoff:
return False
try:
cutoff = parse_date(g.adserver_reporting_cutoff)
except ValueError:
return False
if isinstance(thing, PromoCampaign):
link = Link._byID(thing.link_id)
else:
link = thing
campaigns = list(PromoCampaign._by_link(link._id))
# No campaigns, so nothing to report. Show the new
# view anyway.
if not campaigns:
return True
end_date = max(campaign.end_date for campaign in campaigns)
end_date = end_date.replace(tzinfo=g.tz)
cutoff = cutoff.replace(tzinfo=g.tz)
if end_date < cutoff:
return False
return not feature.is_enabled("legacy_ad_reporting")
示例6: GET_report
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def GET_report(self, start, end, link_text=None, owner=None):
now = datetime.now(g.tz).replace(hour=0, minute=0, second=0,
microsecond=0)
end = end or now - timedelta(days=1)
start = start or end - timedelta(days=7)
links = []
bad_links = []
owner_name = owner.name if owner else ''
if owner:
promo_weights = PromotionWeights.get_campaigns(start, end,
author_id=owner._id)
campaign_ids = [pw.promo_idx for pw in promo_weights]
campaigns = PromoCampaign._byID(campaign_ids, data=True)
link_ids = {camp.link_id for camp in campaigns.itervalues()}
links.extend(Link._byID(link_ids, data=True, return_dict=False))
if link_text is not None:
id36s = link_text.replace(',', ' ').split()
try:
links_from_text = Link._byID36(id36s, data=True)
except NotFound:
links_from_text = {}
bad_links = [id36 for id36 in id36s if id36 not in links_from_text]
links.extend(links_from_text.values())
content = PromoteReport(links, link_text, owner_name, bad_links, start,
end)
if c.render_style == 'csv':
return content.as_csv()
else:
return PromotePage(title=_("sponsored link report"),
content=content).render()
示例7: test_run_link
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def test_run_link(start_link, count=1000):
"""Inject `count` number of links, starting with `start_link`"""
if isinstance(start_link, basestring):
start_link = int(start_link, 36)
links = Link._byID(range(start_link - count, start_link), data=True, return_dict=False)
uploader = LinkUploader(g.CLOUDSEARCH_DOC_API, things=links)
return uploader.inject()
示例8: new_comment
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def new_comment(comment, inbox_rels):
author = Account._byID(comment.author_id)
job = [get_comments(author, "new", "all")]
if comment._deleted:
job.append(get_all_comments())
add_queries(job, delete_items=comment)
else:
# if comment._spam:
# sr = Subreddit._byID(comment.sr_id)
# job.append(get_spam_comments(sr))
add_queries(job, insert_items=comment)
amqp.add_item("new_comment", comment._fullname)
if not g.amqp_host:
l = Link._byID(comment.link_id, data=True)
add_comment_tree(comment, l)
# note that get_all_comments() is updated by the amqp process
# r2.lib.db.queries.run_new_comments
if inbox_rels:
for inbox_rel in tup(inbox_rels):
inbox_owner = inbox_rel._thing1
if inbox_rel._name == "inbox":
add_queries([get_inbox_comments(inbox_owner)], insert_items=inbox_rel)
else:
add_queries([get_inbox_selfreply(inbox_owner)], insert_items=inbox_rel)
set_unread(comment, inbox_owner, True)
示例9: get_links
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def get_links(cls, event_id):
link_ids = cls._get_related_link_ids(event_id)
links = Link._byID(link_ids, data=True, return_dict=False)
links.sort(key=lambda L: L.num_comments, reverse=True)
sr_ids = set(L.sr_id for L in links)
subreddits = Subreddit._byID(sr_ids, data=True)
wrapped = []
for link in links:
w = Wrapped(link)
if w._spam or w._deleted:
continue
if not getattr(w, "allow_liveupdate", True):
continue
w.subreddit = subreddits[link.sr_id]
# ideally we'd check if the user can see the subreddit, but by
# doing this we keep everything user unspecific which makes caching
# easier.
if w.subreddit.type == "private":
continue
comment_label = ungettext("comment", "comments", link.num_comments)
w.comments_label = strings.number_label % dict(
num=link.num_comments, thing=comment_label)
wrapped.append(w)
return wrapped
示例10: _handle_generate_daily_link_reports
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def _handle_generate_daily_link_reports(link_ids, campaign_ids):
now = datetime.utcnow()
links = Link._byID(link_ids, data=True, return_dict=False)
campaigns = PromoCampaign._byID(campaign_ids, data=True, return_dict=False)
if not campaigns:
return
links_start, links_end = _get_campaigns_date_range(campaigns)
now = now.replace(tzinfo=pytz.utc)
links_start = links_start.replace(tzinfo=pytz.utc)
links_end = links_end.replace(tzinfo=pytz.utc)
# if data has already been processed then there's no need
# to redo it. use the last time the report was run as a
# starting point, but subtract 24hrs since initial numbers
# are preliminary.
last_run = min(getattr(l, "last_daily_report_run", links_start) for l in links)
start = max(
last_run - timedelta(hours=24),
links_start,
)
# in cases where we may be running a report well after a link
# has completed ensure we always use the actual start.
if start > links_end:
start = links_start
end = min([now, links_end])
link_fullnames = ",".join([l._fullname for l in links])
g.log.info("generating report for link %s (%s-%s)" % (
link_fullnames, start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d')))
report_id = report.queue_report(
start=start,
end=end,
groups=["optionId", "day"],
parameters=[{
"campaignId": l.external_campaign_id,
} for l in links],
)
g.log.info("processing report for link (%s/%s)" %
(link_fullnames, report_id))
try:
_process_daily_link_reports(
links=links,
report_id=report_id,
queued_date=now,
)
g.log.info("successfully processed report for link (%s/%s)" %
(link_fullnames, report_id))
except report.ReportFailedException as e:
g.log.error(e)
# retry if report failed
_generate_link_reports(links)
示例11: test_run_link
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def test_run_link(start_link, count=1000):
'''Inject `count` number of links, starting with `start_link`'''
if isinstance(start_link, basestring):
start_link = int(start_link, 36)
links = Link._byID(range(start_link - count, start_link), data=True,
return_dict=False)
uploader = SolrLinkUploader(things=links)
return uploader.inject()
示例12: finalize_completed_campaigns
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def finalize_completed_campaigns(daysago=1):
# PromoCampaign.end_date is utc datetime with year, month, day only
now = datetime.datetime.now(g.tz)
date = now - datetime.timedelta(days=daysago)
date = date.replace(hour=0, minute=0, second=0, microsecond=0)
q = PromoCampaign._query(PromoCampaign.c.end_date == date,
# exclude no transaction and freebies
PromoCampaign.c.trans_id > 0,
data=True)
campaigns = list(q)
# check that traffic is up to date
earliest_campaign = min(campaigns, key=lambda camp: camp.start_date)
start, end = promote.get_total_run(earliest_campaign)
missing_traffic = get_missing_traffic(start.replace(tzinfo=None),
date.replace(tzinfo=None))
if missing_traffic:
raise ValueError("Can't finalize campaigns finished on %s."
"Missing traffic from %s" % (date, missing_traffic))
links = Link._byID([camp.link_id for link in links], data=True)
for camp in campaigns:
if hasattr(camp, 'refund_amount'):
continue
link = links[camp.link_id]
billable_impressions = promote.get_billable_impressions(camp)
billable_amount = promote.get_billable_amount(camp,
billable_impressions)
if billable_amount >= camp.bid:
text = ('%s completed with $%s billable (%s impressions @ $%s).'
% (camp, billable_amount, billable_impressions, camp.cpm))
PromotionLog.add(link, text)
refund_amount = 0.
else:
refund_amount = camp.bid - billable_amount
user = Account._byID(link.author_id, data=True)
try:
success = authorize.refund_transaction(user, camp.trans_id,
camp._id, refund_amount)
except authorize.AuthorizeNetException as e:
text = ('%s $%s refund failed' % (camp, refund_amount))
PromotionLog.add(link, text)
g.log.debug(text + ' (response: %s)' % e)
continue
text = ('%s completed with $%s billable (%s impressions @ $%s).'
' %s refunded.' % (camp, billable_amount,
billable_impressions, camp.cpm,
refund_amount))
PromotionLog.add(link, text)
camp.refund_amount = refund_amount
camp._commit()
示例13: comment_reply_effect
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def comment_reply_effect(comment):
if comment.parent_id is not None:
parent = Comment._byID(comment.parent_id, data=True)
else:
parent = Link._byID(comment.link_id, data=True)
all_effects = effects.get_all_effects([parent._fullname])
parent_effects = all_effects.get(parent._fullname, [])
for item_name in parent_effects:
item = items.get_item(item_name)
item.on_reply(c.user, parent)
示例14: fix_about_post
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def fix_about_post():
user = Account._by_name('Eliezer_Yudkowsky')
l = Link._byID(1, data=True)
# l = Link._byID(int('1i', 36))
if l.url.lower() == 'self':
l.url = l.make_permalink_slow()
l.is_self = True
l._commit()
l.set_url_cache()
v = Vote.vote(user, l, True, l.ip, False)
示例15: fields
# 需要导入模块: from r2.models import Link [as 别名]
# 或者: from r2.models.Link import _byID [as 别名]
def fields(self, thing):
'''Return fields relevant to a Link search index'''
account = self.accounts[thing.author_id]
sr = self.srs[thing.sr_id]
if isinstance(thing, Comment):
comment = thing
link = Link._byID(thing.link_id, data=True, return_dict=False)
else:
comment = None
link = thing
return LinkFields(link, account, sr, comment).fields()