def monitor_edits(): while True: logging.info('Monitoring r/%s edits', subreddit.display_name) edited_stream = stream_generator(subreddit.mod.edited, pause_after=0) try: for item in edited_stream: if isinstance(item, comment.Comment): logging.info( 'Comment [%s] on submission "%s" was edited by %s', item.id, item.submission.title, item.author.name) handle_comment(item, edit=True) elif isinstance(item, submission.Submission): logging.info('Submission "%s" was edited by %s', item.title, item.author.name) pass elif item is not None: logging.error('Unknown edited item type: %s', type(item)) except ServerError: logging.error('Reddit server is down: %s', sys.exc_info()[0], exc_info=True) except Exception: logging.error('Caught exception: %s', sys.exc_info()[0], exc_info=True)
def listen(self, skip_existing: bool = True): handler = Activity(self.client) for mention in stream_generator(self.client.inbox.mentions, skip_existing=skip_existing): try: if isinstance(mention, Comment): self.__process(handler, mention) except Exception as e: logging.error(e)
def handle_inbox(): global exit_flag while True: try: for mail in stream_generator(reddit_inbox.inbox.unread): if exit_flag: exit(-1) if isinstance(mail, Message): if mail.subject.lower() == "blacklist": logger.info( f"Found a blacklist request from {mail.author.name}" ) with open("blacklist", 'a') as f: f.write(mail.author.name + "\n") logger.debug( f"Added {mail.author.name} to the blacklist.") else: # Potentially someone wanting to mirror via DMs urls = parse(mail.body) if urls: logger.info( f"Got PM Mirror Request from {mail.author.name}" ) logger.debug( f"Found the following URLs in DM Request: {urls}" ) posts = convert(urls) if posts: response = MAIL_START_MESSAGE for post in posts: try: response += upload_and_format( post, f"images/{uuid.uuid4().hex}") except Exception: continue response += END_MESSAGE if response != MAIL_START_MESSAGE + END_MESSAGE: mail.reply(response) logger.info( f"Replied to {mail.author.name}'s DM request.") else: logger.debug( f"Got PM From {mail.author.name}, but it's irrelevant" ) mail.mark_read() except: pass
def test_stream(self, _): Thing = namedtuple("Thing", ["fullname"]) initial_things = [Thing(n) for n in reversed(range(100))] counter = 99 def generate(limit, **kwargs): nonlocal counter counter += 1 if counter % 2 == 0: return initial_things return [Thing(counter)] + initial_things[:-1] stream = stream_generator(generate) seen = set() for _ in range(400): thing = next(stream) assert thing not in seen seen.add(thing)
def mentions(): reddit = get_reddit() redis_client = get_redis_client() logging.info('Listening to mentions') for mention in stream_generator(reddit.inbox.mentions, skip_existing=True): # type: Comment mention.mark_read() submission = mention.submission # type: Submission replied_key = f"replied:comment:{mention.id}" if redis_client.exists(replied_key): logging.info(f'Already replied to mention {mention.id}') continue url = get_url(submission) if url: title, summary = extract_summary(url) if len(summary): while True: try: message = message_format.format(title=title, summary=summary) comment = mention.reply(message) redis_client.set(replied_key, time.time()) redis_client.sadd('comments', comment.id) logging.info( f'Replied summary to mention {mention.id}') break except praw.exceptions.APIException as e: if e.error_type == 'RATELIMIT': logging.info('RATELIMIT detected') handle_rate_limit(e.message) else: logging.exception(e) break else: logging.info(f'Cannot find contents in URL: {url}') time.sleep(60)
def get_edited_comments_stream(self): return stream_generator(self.subreddit.mod.edited, pause_after=-1)
password='******') botAlt = praw.Reddit(user_agent='EternalCards 0.1', client_id='XzEVaUokS7Rq9w', client_secret='bIHZMZ3UR-a-VCR9IeQLLobSBfk', username='******', password='******') subreddit = bot.subreddit('Abeneezer') writerName = 'EternalCards' comments = subreddit.stream.comments() edited = subreddit.mod.edited edited_stream = stream_generator(edited, pause_after=-1) for x in edited_stream: if x is None: continue if type(x) == Comment: print('yay found an edited commet' + ' ' + x.body) fullNames = [] names = [] with open('eternal-cards.json') as f: data = json.load(f) for x in range(0, len(data)): fullNames.append( [data[x]["Name"], data[x]["DetailsUrl"], data[x]["ImageUrl"]])