support blocklists from Mastodon API compatible nodes

This commit is contained in:
Tobias Diekershoff 2023-01-07 22:46:20 +01:00
parent a30b809cbf
commit 4fa20216f9
2 changed files with 31 additions and 11 deletions

View File

@ -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

View File

@ -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):
"""