Example #1
0
    def _do(self, comment):
        # Get all urls
        urls = self._get_urls(comment.body)
        if not urls:
            write_line(comment.body)
            write_line(' => Skipping..was a false positive (A).')
            return True

        if self.use_threaded:
            delay_processing = False
            tmp_id = hashlib.md5(str(urls)).hexdigest()
            self.tmp_flags[tmp_id] = [len(urls), 0]  # total, current count

            for url in urls:
                if 'imgur.com' in url:
                    def tmp_done(result):
                        self.imgur_lookup[url] = result

                        if tmp_id in self.tmp_flags:
                            self.tmp_flags[tmp_id][1] += 1
                            if self.tmp_flags[tmp_id][1] >= self.tmp_flags[tmp_id][0]:
                                self._process_urls(comment, urls)
                                del self.tmp_flags[tmp_id]

                    self.pool.apply_async(get_image_hash, args=(url,), callback=tmp_done)
                    delay_processing = True

            if delay_processing:
                return True
            else:
                del self.tmp_flags[tmp_id]
                return self._process_urls(comment, urls)
        else:
            return self._process_urls(comment, urls)
Example #2
0
 def _attempt_edit(self, message, new_message):
     # Edit the comment
     try:
         message.edit(new_message)
         return True
     except Exception as e:
         write_line(' => Exception while editing in CommentXkcdBot.')
         write_err(e)
         return False
Example #3
0
    def _do(self, mail):
        if mail.body == "ignore me" and mail.author:
            self.data_store.add_ignore(mail.author.name.lower())

            # Reply to the user
            try:
                reply_msg = "You have been added to the ignore list. If this bot continues to respond, PM /u/LunarMist2."
                self.bot.reply(mail.name, reply_msg)
                mail.mark_as_read()
                #write_line(reply_msg)
                write_line(' => Reply Sent!')
            except Exception as e:
                write_line(' => Exception while replying in PMXkcdBot.')
                write_err(e)
                return False

        mail.mark_as_read()
        return True
Example #4
0
    def _do(self, submission):
        data = self.xkcd.get_json(submission.url)
        if not data:
            write_line(' => Data could not be fetched for {url}'.format(url=submission.url))
            return True

        if data.get('num'):
            timestamp = int(time.time())
            author = submission.author.name if submission.author else '[deleted]'
            sub = submission.subreddit.display_name
            link = submission.permalink
            self.data_store.insert_xkcd_event(data.get('num'), timestamp, sub, author, link, False)

        if data.get('transcript', '') == '':
            write_line(' => Skipping...transcript is blank.')
            return True

        reply_msg_body = ''
        reply_msg_sig = '---\n' + ' ^| '.join(['^' + a for a in XKCD_SIG_LINKS])

        if data.get('img'):
            reply_msg_body += u'[Image]({image})\n\n'.format(image=data.get('img').replace('(', '\\(').replace(')', '\\)'))
        if submission.url.find('imgs') != -1:
            reply_msg_body += u'[Original Source](http://xkcd.com/{num}/)\n\n'.format(num=data.get('num', ''))
        if data.get('title'):
            reply_msg_body += u'**Title:** {title}\n\n'.format(title=data.get('title', '').replace('\n', '\n\n'))
        if data.get('transcript'):
            reply_msg_body += u'**Transcript:** {transcript}\n\n'.format(transcript=re.sub('\n{{.+}}', '', data.get('transcript', '')).replace('\n', '\n\n'))
        if data.get('alt'):
            reply_msg_body += u'**Title-text:** {alt}\n\n'.format(alt=data.get('alt', '').replace('\n', '\n\n'))

        explained = self.xkcd.get_explained_link(data.get('num'))
        if explained:
            reply_msg_body += u'[Comic Explanation]({link})\n\n'.format(link=explained)
        stats = self.data_store.get_stats(data.get('num'))
        if stats:
            reply_msg_body += u'**Stats:** This comic has been referenced {0} time(s), representing {1:.4f}% of referenced xkcds.\n\n'.format(stats[0], stats[1])

        reply_msg = reply_msg_body + reply_msg_sig

        # Reply to the user and mark it as read
        try:
            self.bot.reply(submission.name, reply_msg)
            #write_line(reply_msg)
            write_line(' => Reply Sent!')
        except Exception as e:
            write_line(' => Exception while replying in SubmissionXkcdBot.')
            write_err(e)
            return False

        return True
Example #5
0
    def _do(self, mail):
        if mail.body == "ignore me" and mail.author:
            self.data_store.add_ignore(mail.author.name.lower())

            # Reply to the user
            try:
                reply_msg = "You have been added to the ignore list. If this bot continues to respond, PM /u/LunarMist2."
                self.bot.reply(mail.name, reply_msg)
                mail.mark_as_read()
                #write_line(reply_msg)
                write_line(' => Reply Sent!')
            except Exception as e:
                write_line(' => Exception while replying in PMXkcdBot.')
                write_err(e)
                return False

        if mail.body.startswith('delete') and mail.author:
            parts = mail.body.split(' ')
            if len(parts) == 2:
                thing_id = parts[1]
                obj = self.bot.get_refreshed(thing_id)
                if obj:
                    parent = self.bot.get_refreshed(obj.parent_id)
                    if parent and parent.author and parent.author.name == mail.author.name:
                        obj.delete()
                        write_line(' => Comment Deleted!')

        mail.mark_as_read()
        return True
Example #6
0
    def _attempt_reply(self, message, reply_msg):
        # Ensure I can respond to the user
        if message.author and message.author.name.lower() in self.data_store.get_ignores():
            write_line('Skipping message {id}. Reason: Author on ignore list.'.format(id=message.id))
            return True

        # Reply to the user
        try:
            r = self.bot.reply(message.name, reply_msg)
            #write_line(reply_msg)
            write_line(' => Reply Sent!')
            return r
        except Exception as e:
            write_line(' => Exception while replying in CommentXkcdBot.')
            write_err(e)
            return False
Example #7
0
    def _do(self, mail):
        try:
            comments = mail.author.get_comments(limit=None)
        except Exception as e:
            write_line(' => Exception while getting comments in TopEmotesBot.')
            write_err(e)
            return False

        emotes_dict = defaultdict(int)

        # Parse each comment for emotes
        for comment in comments:
            matches = re.findall(FULL_EMOTE_REGEX, comment.body)
            if matches:
                for match in matches:
                    emote = re.search(EMOTE_REGEX, match)
                    if emote:
                        emotes_dict[emote.group(0).lower()] += 1

        # Remove [](/sp)
        if '/sp' in emotes_dict:
            del emotes_dict['/sp']

        # Remove [](/spoiler)
        if '/spoiler' in emotes_dict:
            del emotes_dict['/spoiler']

        # Sort and reverse
        sorted_emotes = list(reversed(sorted(emotes_dict.iteritems(), key=operator.itemgetter(1))))

        # Build the reply message
        if len(sorted_emotes) == 0:
            reply_msg = "[](/rdcry) You haven't yet used any emotes. You should use them.\n" \
                        '[](/sp)\n' \
                        '\n---\n' \
                        '[](/scootacheer) ^Report ^all ^problems ^to ^/u/LunarMist2 ^| ^[Source](https://github.com/JeremySimpson/redditbot)'
        else:
            reply_msg_header = 'Your emote counts:\n\n' \
                               'Emote | Count\n' \
                               ':--:|:--:\n'

            reply_msg_sig = '\n---\n' \
                            '[](/scootacheer) ^Report ^all ^problems ^to ^/u/LunarMist2 ^| ^[Source](https://github.com/JeremySimpson/redditbot)'

            table_content = ''
            for emote, count in sorted_emotes:
                tr = '{emote}|{count}\n'.format(emote=emote, count=count)
                if len(reply_msg_header) + len(table_content) + len(tr) + len(reply_msg_sig) >= MAX_MESSAGE_LENGTH:
                    break
                else:
                    table_content += tr

            reply_msg = reply_msg_header + table_content + reply_msg_sig

        # Reply to the user and mark it as read
        try:
            self.bot.reply(mail.name, reply_msg)
            mail.mark_as_read()
            #write_line(reply_msg)
            write_line(' => Reply Sent!')
        except Exception as e:
            write_line(' => Exception while replying in TopEmotesBot.')
            write_err(e)
            return False

        return True