fixed parsing bug

This commit is contained in:
Simone Robutti 2022-10-24 17:13:40 +02:00
parent cf9ffd2149
commit f35f3c78a9
1 changed files with 5 additions and 2 deletions

View File

@ -35,11 +35,13 @@ class TelegramFormatter(AbstractEventFormatter):
_conf = ("publisher", "telegram") _conf = ("publisher", "telegram")
def _validate_event(self, event: MobilizonEvent) -> None: def _validate_event(self, event: MobilizonEvent) -> None:
description = event.description description = event.description
if not (description and description.strip()): if not (description and description.strip()):
self._log_error("No description was found", raise_error=InvalidEvent) self._log_error("No description was found", raise_error=InvalidEvent)
def _validate_message(self, message: str) -> None: def _validate_message(self, message: str) -> None:
if ( if (
len("".join(BeautifulSoup(message, "html.parser").findAll(text=True))) len("".join(BeautifulSoup(message, "html.parser").findAll(text=True)))
>= 4096 >= 4096
@ -74,7 +76,8 @@ class TelegramFormatter(AbstractEventFormatter):
tag.unwrap() tag.unwrap()
# cleaning html trailing whitespace # cleaning html trailing whitespace
for tag in html.findAll("a"): for tag in html.findAll("a"):
tag["href"] = tag["href"].replace(" ", "").strip().lstrip() if "href" in tag:
tag["href"] = tag["href"].replace(" ", "").strip().lstrip()
s = str(html) s = str(html)
return re.sub(r"\n{2,}", "\n\n", s).strip() # remove multiple newlines return re.sub(r"\n{2,}", "\n\n", s).strip() # remove multiple newlines
@ -103,7 +106,6 @@ class TelegramPlatform(AbstractPlatform):
def _validate_response(self, res): def _validate_response(self, res):
try: try:
res.raise_for_status() res.raise_for_status()
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
self._log_error( self._log_error(
@ -113,6 +115,7 @@ class TelegramPlatform(AbstractPlatform):
try: try:
data = res.json() data = res.json()
except Exception as e: except Exception as e:
self._log_error( self._log_error(
f"Server returned invalid json data: {str(e)}", f"Server returned invalid json data: {str(e)}",