<commit_before>#!/usr/bin/env python3

from bot import Bot
import logging
from mastodon import Mastodon
import re
from report import Report


logger = logging.getLogger(__name__)


class MastodonBot(Bot):
    def crawl(self, user):
        """
        Crawl mentions from Mastodon.

        :return: list of statuses
        """
        mentions = []
        try:
            m = Mastodon(*user.get_masto_credentials())
        except TypeError:
            # logger.error("No Mastodon Credentials in database.", exc_info=True)
            return mentions
        try:
            notifications = m.notifications()
        except Exception:
            logger.error("Unknown Mastodon API Error.", exc_info=True)
            return mentions
        for status in notifications:
            if (status['type'] == 'mention' and
                    not user.toot_is_seen(status['status']['uri'])):
                # save state
                user.toot_witness(status['status']['uri'])
                # add mention to mentions
                text = re.sub(r'<[^>]*>', '', status['status']['content'])
                text = re.sub(
                    "(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
                    "", text)
                if status['status']['visibility'] == 'public':
                    mentions.append(Report(status['account']['acct'],
                                           self,
                                           text,
                                           status['status']['id'],<commit_msg>don't log Mastodon 502 errors.<commit_after>#!/usr/bin/env python3

from bot import Bot
import logging
from mastodon import Mastodon, MastodonServerError
import re
from report import Report


logger = logging.getLogger(__name__)


class MastodonBot(Bot):
    def crawl(self, user):
        """
        Crawl mentions from Mastodon.

        :return: list of statuses
        """
        mentions = []
        try:
            m = Mastodon(*user.get_masto_credentials())
        except TypeError:
            # logger.error("No Mastodon Credentials in database.", exc_info=True)
            return mentions
        try:
            notifications = m.notifications()
        except MastodonServerError:
            logger.error("Unknown Mastodon API Error: 502")
            return mentions
        for status in notifications:
            if (status['type'] == 'mention' and
                    not user.toot_is_seen(status['status']['uri'])):
                # save state
                user.toot_witness(status['status']['uri'])
                # add mention to mentions
                text = re.sub(r'<[^>]*>', '', status['status']['content'])
                text = re.sub(
                    "(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
                    "", text)
                if status['status']['visibility'] == 'public':
                    mentions.append(Report(status['account']['acct'],
                                           self,
                                           text,
                                           status['status']['id'],
{'fraction_non_alphanumeric': 0.13135186960690318, 'fraction_numerical': 0.005113454777884308, 'mean_word_length': 1.5488599348534202, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3595347', 'n_tokens_mistral': 866, 'n_tokens_neox': 791, 'n_words': 184}