def add_original_sub_comment(relevant_post: Submission, cj_post: Submission) -> None: """ Adds a comment to the original_sub post. :param relevant_post: original_sub post :param cj_post: circlejerk_sub post :return: None """ rpid = str(relevant_post.id) cjpid = str(cj_post.id) if not db.get(rpid): db.set(rpid, [cjpid]) else: rid_list = db.get(rpid) rid_list = list(set(rid_list)) if cjpid not in rid_list: rid_list.append(cjpid) db.set(rpid, rid_list) posts = [reddit.submission(id=p) for p in db.get(rpid)] posts.sort(key=lambda x: x.score, reverse=True) post_tags = [] for post in posts: if post and post.author: nsfw_tag = "" if post.over_18: nsfw_tag = "[NSFW] " post_tags.append( "[{}{}](https://www.reddit.com{}) by {}\n\n".format( nsfw_tag, post.title, post.permalink, post.author)) posts_string = "".join(post_tags) reply_template = ("This post has been parodied on r/{0}.\n\n" "Relevant r/{0} posts: \n\n{1}".format( cj_post.subreddit.display_name, posts_string)) comment_string = reply_template + GITHUB_TAG authors = [] if relevant_post.comments: for comment in relevant_post.comments: if comment.author: authors.append(comment.author.name) if USERNAME not in authors and len(post_tags) > 0: relevant_post.reply(comment_string) logger.debug(comment_string) logger.info(f"Added comment to {relevant_post.subreddit.display_name}") else: for comment in relevant_post.comments: if comment.author: if comment.author.name == USERNAME: logger.debug(comment.body) if comment_string != comment.body: if len(post_tags) > 0: comment.edit(comment_string) logger.info("edited") else: comment.delete() logger.info("Comment deleted") else: logger.info( "Comment is the same as last time, not editing")
def _make_reply(self, submission: Submission, reply_text: str = None) -> None: url = submission.url title = submission.title selftext = submission.selftext if reply_text is None: reply_text, generated_text = self._prepare_reply(title, selftext) reply_text, generated_text = reply_text[0], generated_text[0] if not reply_text or reply_text == ' ': logger.debug( f'FAIL: generated reply to submission={title} was very bad ({generated_text})' ) return if self.test_submission: submission = self.reddit.submission( self.reddit.config.custom['test_submission']) if not self.is_silent: submission.reply( f'{url}\n\n{title}\n\n{selftext}\n\n\nREPLY:\n\n' + reply_text) else: if not self.is_silent: submission.reply(reply_text) logger.debug(f'made reply: submission={title} reply={reply_text}')
def add_circlejerk_comment(cj_post: Submission, relevant_post: Submission, certainty: float) -> None: """ Adds a comment to the circlejerk_sub post. If anyone knows how to format it so my username is also superscripted, please submit a PR. :param cj_post: circlejerk_sub post :param relevant_post: original_sub post :param certainty: Certainty metric :return: None """ nsfw_tag = "" if relevant_post.over_18: nsfw_tag = "[NSFW] " reply_template = "Relevant r/{} post: [{}{}](https://www.reddit.com{})\n\n".format( relevant_post.subreddit.display_name, nsfw_tag, relevant_post.title, relevant_post.permalink, ) certainty_tag = "Certainty: {}%\n\n".format(round(certainty * 100, 2)) comment = reply_template + certainty_tag + GITHUB_TAG cj_post.reply(comment) logger.debug(comment) logger.info(f"Added comment to {cj_post.subreddit.display_name}")
def analyze_submission(submission: Submission): logger.info("Found %s app", submission.url) url = parse.urlsplit(submission.url) scraper = SUPPORTED_STORES.get(url.hostname) info = scraper(url.geturl()) logger.info("Fetched information for %s from %s", info.title, info.store) submission.reply(body=str(info)) logger.info("Replied to %s", submission.permalink)
def add_original_sub_comment( relevant_post: Submission, cj_post: Submission, my_comments: ListingGenerator ) -> None: """ Adds a comment to the original_sub post. :param relevant_post: original_sub post :param cj_post: circlejerk_sub post :param my_comments: Iterator of RP bots recent comments :return: None """ rpid = str(relevant_post.id) cjpid = str(cj_post.id) if not db.get(rpid): db.set(rpid, [cjpid]) else: rid_list = db.get(rpid) rid_list = list(set(rid_list)) if cjpid not in rid_list: rid_list.append(cjpid) db.set(rpid, rid_list) posts = [reddit.submission(id=p) for p in db.get(rpid)] posts.sort(key=lambda x: x.score, reverse=True) post_tags = [] for post in posts: if post and post.author: nsfw_tag = "" if post.over_18: nsfw_tag = "[NSFW] " post_tags.append( "[{}{}](https://www.reddit.com{}) by {}\n\n".format( nsfw_tag, post.title, post.permalink, post.author ) ) posts_string = "".join(post_tags) reply_template = ( "This post has been parodied on r/{0}.\n\n" "Relevant r/{0} posts: \n\n{1}".format( cj_post.subreddit.display_name, posts_string ) ) comment_string = reply_template + BOT_TAG + GITHUB_TAG for my_comment in my_comments: if my_comment.link_id == relevant_post.id: modify_exisiting_comment(my_comment, comment_string, post_tags) return if len(post_tags) > 0: relevant_post.reply(comment_string) logger.debug(comment_string) logger.info(f"Added comment to {relevant_post.subreddit.display_name}")
async def process_post(submission: Submission): print("New post: " + submission.title) # Do not respond to memes! # Or non-image posts. # god I miss nullish coalescing if (hasattr(submission, 'link_flair_text') and submission.link_flair_text and ('meme' in submission.link_flair_text.lower() or 'news' in submission.link_flair_text.lower())) or submission.is_self: print("Either this is flaired Meme or this is a self-post.") return # Never ask for sauce twice on the same post. c.execute('SELECT * FROM allposts WHERE id=?', (submission.id, )) if c.fetchone(): print("Duplicate post. Not sure how this happened, but it did.") return # Make sure they have an author in the post. author_matcher = re.compile(r'.*\[.*\].*') if not author_matcher.match(submission.title): comment = submission.reply( "**Post titles must have the author in square brackets.**\n\n To avoid getting your post removed, make sure the author is in the " "title (i.e. [Author] Title).\n\n" + config['suffix']) if comment is not None: comment.mod.distinguish(how='yes', sticky=True) submission.mod.remove(mod_note="No author provided") return print("This is an actual post, asking for sauce...") await ask_for_sauce(submission)
def test_reply(self): self.reddit.read_only = False with self.recorder.use_cassette('TestSubmission.test_reply'): submission = Submission(self.reddit, '4b1tfm') comment = submission.reply('Test reply') assert comment.author == self.reddit.config.username assert comment.body == 'Test reply' assert comment.parent_id == submission.fullname
def test_reply(self): self.reddit.read_only = False with self.recorder.use_cassette("TestSubmission.test_reply"): submission = Submission(self.reddit, "4b1tfm") comment = submission.reply("Test reply") assert comment.author == self.reddit.config.username assert comment.body == "Test reply" assert comment.parent_id == submission.fullname
def test_reply(self): self.reddit.read_only = False with self.use_cassette(): submission = Submission(self.reddit, "4b1tfm") comment = submission.reply("Test reply") assert comment.author == self.reddit.config.username assert comment.body == "Test reply" assert comment.parent_id == submission.fullname
def handle_replies(reddit): while(1): if(not_replied): for i in not_replied: complaint_uri = '/twittercomplaints/' + str(i) result = firebase.get(complaint_uri, None) #complaint = json.loads(result) complaint = result if(complaint['approved'] == 'true'): try: post = Submission(reddit, complaint['postID']) post = reddit.submission(post) except: firebase.delete('/twittercomplaints/', str(i)) not_replied.remove(i) continue reply_text = 'Your complaint has been approved by the complaints manager. Here\'s the link to track it: https://complaintsapp-cfa95.firebaseapp.com/track/' + str(i) post.reply(reply_text) print('replied accepted') not_replied.remove(i) userName = post.author elif(complaint['approved'] == 'false'): try: post = Submission(reddit, complaint['postID']) post = reddit.submission(post) except: firebase.delete('/twittercomplaints/', str(i)) not_replied.remove(i) continue post = Submission(reddit, complaint['postID']) post = reddit.submission(post) userName = post.author reddit.redditor(userName.name).message('Regarding Complaint', 'We\'re sorry to inform you that your complaint posted on r/ComplaintsApp has been rejected by the complaints manager. To have your complaint approved, please post it again and make sure you use the proper format.') post.mod.remove() not_replied.remove(i) firebase.delete('/twittercomplaints/',str(i)) print('dm rejected') else: time.sleep(3)
def reply_to_submission(submission: Submission) -> bool: """Replies to a single submission""" for reply in submission.comments.list(): if reply.author.name == args.username: log.info(f'Already replied to submission {submission.id}') return False if args.dry_run: log.info(f'Dry-run, skipping submission: {submission.shortlink}') else: replies_per_thread = cache.get( f'replies_per_submission:{submission.id}') or 0 cache.set(f'replies_per_submission:{submission.id}', replies_per_thread + 1) log.info(f'Replying to submission: {submission.shortlink}') submission.reply(COMMENT.format(signature=submission.id)) submission.upvote() return True return False
async def ask_for_sauce(submission: Submission): comment = submission.reply('**Reply to this comment** with the source, in regular link format, ' 'such as \n```\nhttps://nhentai.net/g/(numbers).\n```\nIf you feel like your post ' 'has no applicable source, reply with "None".\n\n' + config['suffix']) if comment is None: print('Something wacky happened') return comment.mod.distinguish(how='yes', sticky=True) c.execute('INSERT INTO allposts VALUES (?)', (submission.id,)) c.execute('INSERT INTO pendingposts VALUES (?, ?, ?)', (submission.id, submission.author.name, comment.id)) conn.commit() # Remove it until a source reply is found submission.mod.remove() return
def post_removal_comment(submission: Submission, removal_action: RemovalAction) -> Comment: message = "" if removal_action.use_comment_reply_prefix: message += removal_action.subreddit.comment_reply_prefix + "\n\n" message += removal_action.comment_reply if removal_action.use_comment_reply_suffix: message += "\n\n" + removal_action.subreddit.comment_reply_suffix message = message.format(submission) removal_comment = submission.reply(message) removal_comment.mod.approve() removal_comment.mod.distinguish(how="yes", sticky=True) return removal_comment
def test_reply__none(self): self.reddit.read_only = False submission = Submission(self.reddit, "ah19vv") with self.recorder.use_cassette("TestSubmission.test_reply__none"): reply = submission.reply("TEST") assert reply is None
def remove_submission(self, submission: models.Submission, rule: Rule): self.log.debug( f'[Core] Submission would have been removed! {submission.permalink}' ) submission.reply(str(rule)).mod.distinguish(sticky=False) submission.mod.remove()