add some exception handling for problems

This commit is contained in:
Tobias Diekershoff 2023-01-07 23:46:28 +01:00
parent e0b30df10a
commit f71425528e

View file

@ -32,6 +32,7 @@ class BrewBlocklist():
self.auto_accept = auto_accept self.auto_accept = auto_accept
self.auto_accept_direction = auto_accept_direction self.auto_accept_direction = auto_accept_direction
self.safe_harbor = [] self.safe_harbor = []
self.error = []
config = configparser.RawConfigParser() config = configparser.RawConfigParser()
config.read(configfile) config.read(configfile)
for section in config.sections(): for section in config.sections():
@ -62,6 +63,9 @@ class BrewBlocklist():
if source['type'] == 'friendica': if source['type'] == 'friendica':
# Friendica publishes the blocklist as CSV file # Friendica publishes the blocklist as CSV file
requ = requests.get('https://{}/blocklist/domain/download'.format(source['url'])) requ = requests.get('https://{}/blocklist/domain/download'.format(source['url']))
if not requ.status_code == requests.codes.ok:
self.error.append('The request to {} failed'.format(sources['url']))
break
for line in requ.text.split('\n'): for line in requ.text.split('\n'):
try: try:
pattern, reason = line.split(',') pattern, reason = line.split(',')
@ -75,9 +79,15 @@ class BrewBlocklist():
elif source['type'] == 'mastodon': elif source['type'] == 'mastodon':
# Mastodon has an API endpoint that contains the information # Mastodon has an API endpoint that contains the information
requ = requests.get('https://{}//api/v1/instance/domain_blocks'.format(source['url'])) requ = requests.get('https://{}//api/v1/instance/domain_blocks'.format(source['url']))
for item in requ.json(): if not requ.status_code == requests.codes.ok:
self.blocklist[item['domain']] = self.blocklist.get(item['domain'], 0) + source['trust'] self.error.append('The request to {} failed'.format(sources['url']))
self.reasons[item['domain']] = self.reasons.get(item['domain'], item['comment']) break
try:
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'])
except:
self.error.append('{} returned no valid json to the API call'.format(source['url']))
else: else:
raise ValueError('{} is not a supported node type, check your config file'.format(source['type'])) raise ValueError('{} is not a supported node type, check your config file'.format(source['type']))
@ -129,6 +139,9 @@ class BrewBlocklist():
if self.outputfile: if self.outputfile:
sys.stdout = orig_stdout sys.stdout = orig_stdout
out_file.close() out_file.close()
if len(self.error):
print("\n\nWhile creating the blocklist the following problems occured:")
print("\n".join(self.error))
def main(): def main():
""" """