From 8187bb54782fd6f33ad04c6772b71365a68222d7 Mon Sep 17 00:00:00 2001 From: Tatu Wikman Date: Sat, 10 Oct 2020 20:19:30 +0300 Subject: [PATCH] blacken url module --- modules/url.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/modules/url.py b/modules/url.py index 2382491..d69fced 100644 --- a/modules/url.py +++ b/modules/url.py @@ -54,8 +54,8 @@ class MatrixModule(BotModule): return # skip edited content to prevent spamming the same thing multiple times - if('content' in event.source): - if('m.new_content' in event.source['content']): + if "content" in event.source: + if "m.new_content" in event.source["content"]: self.logger.debug(f"Skipping edited event to prevent spam") return @@ -78,11 +78,10 @@ class MatrixModule(BotModule): # fix for #98 a bit ugly, but skip all matrix.to urls # those are 99.99% pills and should not # spam the channel with matrix.to titles - if(url.startswith("https://matrix.to/#/")): + if url.startswith("https://matrix.to/#/"): self.logger.debug(f"Skipping matrix.to url (#98): {url}") continue - try: title, description = self.get_content_from_url(url) except Exception as e: @@ -123,14 +122,16 @@ class MatrixModule(BotModule): return (title, description) if r.status_code != 200: - self.logger.warning(f"Failed fetching url {url}. Status code: {r.status_code}") + self.logger.warning( + f"Failed fetching url {url}. Status code: {r.status_code}" + ) return (title, description) # try parse and get the title try: soup = BeautifulSoup(r.text, "html.parser") # Prefer og:title first (for example Youtube uses this) - ogtitle = soup.find("meta", property="og:title") + ogtitle = soup.find("meta", property="og:title") if ogtitle: title = ogtitle["content"] elif soup.head and soup.head.title: @@ -145,8 +146,8 @@ class MatrixModule(BotModule): # Issue 63 patch - Title should not contain newlines or tabs if title is not None: assert isinstance(title, str) - title = title.replace('\n', '') - title = title.replace('\t', '') + title = title.replace("\n", "") + title = title.replace("\t", "") return (title, description) async def matrix_message(self, bot, room, event): @@ -179,9 +180,7 @@ class MatrixModule(BotModule): bot.must_be_owner(event) self.type = "m.notice" bot.save_settings() - await bot.send_text( - room, "Sending titles as notices from now on." - ) + await bot.send_text(room, "Sending titles as notices from now on.") return # show status @@ -189,9 +188,7 @@ class MatrixModule(BotModule): bot.must_be_owner(event) self.type = "m.text" bot.save_settings() - await bot.send_text( - room, "Sending titles as text from now on." - ) + await bot.send_text(room, "Sending titles as text from now on.") return # invalid command @@ -204,8 +201,8 @@ class MatrixModule(BotModule): def get_settings(self): data = super().get_settings() - data['status'] = self.status - data['type'] = self.type + data["status"] = self.status + data["type"] = self.type return data def set_settings(self, data):