diff --git a/README.md b/README.md index ba06222..ef19c12 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,13 @@ For example domains = friendica.example.com ``` +You can also add Mastodon instances you trust. In addition to the configuration +needed for Friendica nodes you have to add the `type = mastodon` entry to the +config section. + +Please note only suspended entries from the Mastodon blocklist will be added to +the blocklist. Silenced entries will be ignored. + ### Running the script You have to supply the file name of the configuration file on the command diff --git a/src/brewserverblocklist/brewserverblocklist.py b/src/brewserverblocklist/brewserverblocklist.py index aa79ffe..4e69d7f 100644 --- a/src/brewserverblocklist/brewserverblocklist.py +++ b/src/brewserverblocklist/brewserverblocklist.py @@ -37,9 +37,12 @@ class BrewBlocklist(): for section in config.sections(): section_values = dict(config.items(section)) if not section == 'safe harbor': + if not 'type' in section_values.keys(): + section_values['type'] = 'friendica' self.sources.append({ 'url': section, 'trust': int(section_values['trust']), + 'type': section_values['type'] }) else: for item in section_values['domains'].split(','): @@ -57,17 +60,27 @@ class BrewBlocklist(): mention of the server wins) and sum the trust levels of the blocks. """ for source in self.sources: - requ = requests.get('https://{}/blocklist/domain/download'.format(source['url'])) - for line in requ.text.split('\n'): - try: - pattern, reason = line.split(',') - except ValueError: - # happens in an empty line in the source CSV file, which seems - # to be the last line of the file so we can just break the loop - # one step early and ignore the exception silently. - break - self.blocklist[pattern] = self.blocklist.get(pattern, 0) + source['trust'] - self.reasons[pattern] = self.reasons.get(pattern, reason) + if source['type'] == 'friendica': + # Friendica publishes the blocklist as CSV file + requ = requests.get('https://{}/blocklist/domain/download'.format(source['url'])) + for line in requ.text.split('\n'): + try: + pattern, reason = line.split(',') + except ValueError: + # happens in an empty line in the source CSV file, which seems + # to be the last line of the file so we can just break the loop + # one step early and ignore the exception silently. + break + self.blocklist[pattern] = self.blocklist.get(pattern, 0) + source['trust'] + self.reasons[pattern] = self.reasons.get(pattern, reason) + elif source['type'] == 'mastodon': + # Mastodon has an API endpoint that contains the information + requ = requests.get('https://{}//api/v1/instance/domain_blocks'.format(source['url'])) + for item in requ.json(): + self.blocklist[item['domain']] = self.blocklist.get(item['domain'], 0) + source['trust'] + self.reasons[item['domain']] = self.reasons.get(item['domain'], item['comment']) + else: + raise ValueError('{} is not a supported node type, check your config file'.format(source['type'])) def clean_list(self): """