本文整理汇总了Python中praw.models.Comment方法的典型用法代码示例。如果您正苦于以下问题:Python models.Comment方法的具体用法?Python models.Comment怎么用?Python models.Comment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类praw.models
的用法示例。
在下文中一共展示了models.Comment方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mark_unread
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def mark_unread(self, items):
"""Unmark Comments or Messages as read.
:param items: A list containing instances of :class:`.Comment` and/or
:class:`.Message` to be be marked as unread relative to the
authorized user's inbox.
Requests are batched at 25 items (reddit limit).
For example, to mark the first 10 items as unread try:
.. code:: python
to_unread = list(reddit.inbox.all(limit=10))
reddit.inbox.mark_unread(to_unread)
.. seealso::
:meth:`.Comment.mark_unread` and :meth:`.Message.mark_unread`
"""
while items:
data = {'id': ','.join(x.fullname for x in items[:25])}
self._reddit.post(API_PATH['unread_message'], data=data)
items = items[25:]
示例2: mentions
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def mentions(self, **generator_kwargs):
r"""Return a ListingGenerator for mentions.
A mention is :class:`.Comment` in which the authorized redditor is
named in its body like ``/u/redditor_name``.
Additional keyword arguments are passed in the initialization of
:class:`.ListingGenerator`.
For example, to output the author and body of the first 25 mentions
try:
.. code:: python
for mention in reddit.inbox.mentions(limit=25):
print('{}\n{}\n'.format(mention.author, mention.body))
"""
return ListingGenerator(self._reddit, API_PATH['mentions'],
**generator_kwargs)
示例3: unread
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def unread(self, mark_read=False, **generator_kwargs):
"""Return a ListingGenerator for unread comments and messages.
:param mark_read: Marks the inbox as read (default: False).
.. note:: This only marks the inbox as read not the messages. Use
:meth:`.Inbox.mark_read` to mark the messages.
Additional keyword arguments are passed in the initialization of
:class:`.ListingGenerator`.
For example, to output the author of unread comments try:
.. code:: python
from praw.models import Comment
for item in reddit.inbox.unread(limit=None):
if isinstance(item, Comment):
print(item.author)
"""
self._safely_add_arguments(
generator_kwargs, 'params', mark=mark_read)
return ListingGenerator(self._reddit, API_PATH['unread'],
**generator_kwargs)
示例4: create_reply
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def create_reply(replyable, response_url, hero_id, img=None):
"""Method that creates a reply in reddit format.
The reply consists of a link to the response audio file, the response itself, a warning about the sound
and an ending added from the config file (post footer).
TODO Image is currently ignored due to new reddit redesign not rendering flairs properly.
:param replyable: The comment/submission on reddit
:param response_url: The url to the response audio file
:param hero_id: The hero_id to which the response belongs to.
:param img: The img path to be used for reply.
:return: The text for the comment reply.
"""
original_text = replyable.body if isinstance(replyable, Comment) else replyable.title
hero_name = db_api.get_hero_name(hero_id)
return "[{}]({}) (sound warning: {}){}".format(original_text, response_url, hero_name, config.COMMENT_ENDING)
示例5: item_in_subreddit
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def item_in_subreddit(item, subreddits):
''' Does the reddit Submission or Comment belong to one of the subreddits
to which the user is searching in
'''
subreddit = str(item['subreddit']).lower()
return subreddit in subreddits
示例6: extract_comment
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def extract_comment(comment: Comment, sub_id=None) -> Dict:
c = comment
author = c.author.name if c.author != None else ''
r = dict(
created_utc=c.created_utc,
text=c.body,
id=c.id,
score=c.score,
author=author
)
if sub_id != None:
r.update({'parent': sub_id})
return r
示例7: extract_comment
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def extract_comment(comment: Comment, sub_id=None):
c = comment
author = c.author.name if c.author != None else ''
r = dict(
created_utc=c.created_utc,
text=c.body,
id=c.id,
score=c.score,
author=author
)
if sub_id != None:
r.update({'parent': sub_id})
return r
示例8: mark_read
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def mark_read(self, items):
"""Mark Comments or Messages as read.
:param items: A list containing instances of :class:`.Comment` and/or
:class:`.Message` to be be marked as read relative to the
authorized user's inbox.
Requests are batched at 25 items (reddit limit).
For example, to mark all unread Messages as read, try:
.. code:: python
from praw.models import Message
unread_messages = []
for item in reddit.inbox.unread(limit=None):
if isinstance(item, Message):
unread_messages.append(item)
reddit.inbox.mark_read(unread_messages)
.. seealso::
:meth:`.Comment.mark_read` and :meth:`.Message.mark_read`
"""
while items:
data = {'id': ','.join(x.fullname for x in items[:25])}
self._reddit.post(API_PATH['read_message'], data=data)
items = items[25:]
示例9: add_custom_reply
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def add_custom_reply(replyable, custom_response):
"""Method to create a custom reply for specific cases that match the custom responses set.
:param replyable: The comment/submission on reddit
:param custom_response: The matching custom response
:return: None
"""
original_text = replyable.body if isinstance(replyable, Comment) else replyable.title
reply = custom_response.format(original_text, config.COMMENT_ENDING)
replyable.reply(reply)
示例10: user_saved
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def user_saved(reddit, **search_options):
matched = False
if 'search_type' in search_options and search_options['search_type'] == 'saved_posts':
source = reddit.user.me().saved(limit=None)
else:
source = reddit.user.me().new(limit=None)
for saved_item in source:
# we can have two types of objects here:
# 1. praw.models.reddit.comment.Comment
# 2. praw.models.reddit.submission.Submission
# 1. DATA MASSAGING
saved_item.id # trigger loading
item = vars(saved_item) # pull off properties
# determine item type
if isinstance(saved_item, Submission):
item['type'] = 'submission'
elif isinstance(saved_item, Comment):
item['type'] = 'comment'
else:
logger.error('Unexpected item type: %s', type(saved_item))
# 2. PERFORMING SEARCH
# if we have a "subreddits" critera, check if the item passes the criteria
if search_options['subreddits'] and not item_in_subreddit(item, search_options['subreddits']):
continue
# search through the text items
for text_key in ['link_title', 'link_permalink', 'link_url', 'selftext', 'title', 'body', 'permalink', 'url']:
if text_key not in item:
continue
if query_in_str(search_options['search_queries'], item[text_key]):
matched = True
break
if matched:
yield item
matched = False
# dump items to file (for development/debugging)
# for i, saved_item in enumerate(results):
# with open('results/' + str(i) + '.txt', 'w', encoding='utf-8') as f:
# f.write(pprint.pformat(vars(saved_item), indent=4))
示例11: process_replyable
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def process_replyable(reddit, replyable):
"""Method used to check all the comments in a submission and add replies if they are responses.
PRAW generates past ~100 comments/submissions on the first iteration. Then the loop only runs if there is a new
comment/submission added to the stream. This also means that once PRAW is up and running, after the initial comments
list it won't generate any duplicate comments.
However, just as a safeguard, Caching is used to store replyable ids as they are processed for the first time.
Otherwise, when the bot is restarted it might reply twice to same comments. If replyable id is in the already present
in the cache_api, then it is ignored, else processed and added to the cache_api.
* Self comments are ignored.
* It is prepared for comparision to the responses in dictionary.
* If the replyable is not on the excluded responses list (loaded from config) and if it is in the responses db or
specific responses list, a reply replyable is prepared and posted.
:param reddit: The reddit account instance
:param replyable: comment or submission
:return: None
"""
if cache_api.check(thing_id=replyable.fullname):
return
# Ignore thyself
if replyable.author == reddit.user.me:
return
logger.debug("Found new replyable: " + str(replyable.fullname))
processed_body = process_body(replyable.body if isinstance(replyable, Comment) else replyable.title)
# Don't reply to single word text (they're mostly common phrases).
if ' ' not in processed_body:
return
if processed_body in config.EXCLUDED_RESPONSES:
return
if processed_body in config.CUSTOM_RESPONSES:
add_custom_reply(replyable=replyable, custom_response=config.CUSTOM_RESPONSES[processed_body])
if not flair_specific_reply_added(replyable, processed_body):
add_regular_reply(replyable, processed_body)
示例12: register_user
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def register_user(msg):
user = models.User(msg.author.name)
if not user.is_registered():
user.get_new_address()
if user.address:
content_reply = Template(lang.message_register_success + lang.message_footer).render(
username=user.username,
address=user.address)
tittle_reply = 'you are registered'
user.register()
models.HistoryStorage.add_to_history(msg.author.name, "", "", "", "register")
# create a backup of wallet
crypto.backup_wallet()
else:
bot_logger.logger.warning('Error during register !')
else:
bot_logger.logger.info('%s are already registered ' % user.username)
# pending_tips is balance of tip send to unregistered users
pending_tips = user.get_balance_pending_tip()
unconfirmed_balance = user.get_balance_unconfirmed()
spendable_balance = user.get_balance()
bot_logger.logger.info('user %s spendable_balance = %s' % (user.username, spendable_balance))
unconfirmed_value_usd = utils.get_coin_value(unconfirmed_balance)
spendable_value_usd = utils.get_coin_value(spendable_balance)
pending_tips_value_usd = utils.get_coin_value(pending_tips)
content_reply = Template(
lang.message_already_registered + lang.message_account_details + lang.message_footer).render(
username=msg.author.name,
address=user.address,
spendable_balance=str(spendable_balance),
spendable_value_usd=str(spendable_value_usd),
unconfirmed_balance=str(unconfirmed_balance),
unconfirmed_value_usd=str(unconfirmed_value_usd),
pending_tips=str(pending_tips),
pending_tips_value_usd=str(pending_tips_value_usd)
)
tittle_reply = 'you are already registered'
# send PM so just reply
if type(msg) is Message:
msg.reply(content_reply)
# we have just comment so send info in PM
if type(msg) is Comment:
user.send_private_message(tittle_reply, content_reply)
示例13: replay_pending_tip
# 需要导入模块: from praw import models [as 别名]
# 或者: from praw.models import Comment [as 别名]
def replay_pending_tip(reddit, tx_queue, failover_time):
# check if user have pending tips
list_tips = user_function.get_unregistered_tip()
if list_tips:
for arr_tip in list_tips:
tip = models.Tip().create_from_array(arr_tip)
bot_logger.logger.info("replay tipping check for %s" % str(tip.id))
# check if it's not too old & replay tipping
if not tip.is_expired():
if tip.receiver.is_registered():
bot_logger.logger.info(
"replay tipping %s - %s send %s to %s " % (
str(tip.id), tip.sender.username, tip.amount, tip.receiver.username))
tip.tx_id = crypto.tip_user(tip.sender.address, tip.receiver.address, tip.amount, tx_queue,
failover_time)
if tip.tx_id:
tip.finish = True
user_function.remove_pending_tip(tip.id)
if tip.message_fullname is not None:
msg_id = re.sub(r't\d+_(?P<id>\w+)', r'\g<id>', tip.message_fullname)
msg = Comment(reddit, msg_id)
msg.reply(Template(lang.message_tip).render(
sender=tip.sender.username, receiver=tip.receiver.username, amount=str(tip.amount),
value_usd=str(tip.get_value_usd()), txid=tip.tx_id))
else:
tip.status = "waiting registration of receiver"
bot_logger.logger.info(
"replay check for %s - user %s not registered " % (str(tip.id), tip.receiver.username))
else:
tip.status = "receiver not registered in time"
tip.finish = ""
bot_logger.logger.info(
"delete old tipping - %s send %s to %s " % (
tip.sender.username, tip.amount, tip.receiver.username))
user_function.remove_pending_tip(tip.id)
# update tip status
models.HistoryStorage.update_tip(tip.sender.username, tip)
models.HistoryStorage.update_tip(tip.receiver.username, tip)
else:
bot_logger.logger.info("no pending tipping")