From 7f4fad4d61caa700f85bb03cc86ba7873d17c7e5 Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Mon, 14 Oct 2024 05:34:03 +0800 Subject: [PATCH] facebook: add conditional check if text exists --- automon/integrations/facebook/groups.py | 79 +++++++++++++------------ 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/automon/integrations/facebook/groups.py b/automon/integrations/facebook/groups.py index 58cefc16..571f6fc1 100644 --- a/automon/integrations/facebook/groups.py +++ b/automon/integrations/facebook/groups.py @@ -51,7 +51,8 @@ def content_unavailable(self): try: text = self._browser.wait_for_xpath(self._xpath_content_unavailable, timeout=5) - text = text.text + if text: + text = text.text logger.debug(text) return text except Exception as error: @@ -66,9 +67,10 @@ def creation_date(self): value='span', by=self._browser.by.TAG_NAME ) - text = text[0] - text = text.text.split('See more')[0] - text = text.strip() + if text: + text = text[0] + text = text.text.split('See more')[0] + text = text.strip() logger.debug(text) return text except Exception as error: @@ -115,11 +117,12 @@ def history(self): value='span', by=self._browser.by.TAG_NAME ) - text = text[-1] - text = text.text - if 'See more' in text: - text = text.split('See more') - text = text[0] + if text: + text = text[-1] + text = text.text + if 'See more' in text: + text = text.split('See more') + text = text[0] logger.debug(text) return text except Exception as error: @@ -129,7 +132,8 @@ def history(self): def temporarily_blocked(self): try: text = self._browser.wait_for_xpath(self._xpath_temporarily_blocked, timeout=5) - text = text.text + if text: + text = text.text logger.debug(text) return text except Exception as error: @@ -145,8 +149,9 @@ def members(self): value='span', by=self._browser.by.TAG_NAME ) - text = text[-1] - text = text.text + if text: + text = text[-1] + text = text.text logger.debug(text) return text except Exception as error: @@ -167,7 +172,8 @@ def members_count(self): def must_login(self): try: text = self._browser.wait_for_anything(self._xpath_must_login) - text = text.text + if text: + text = text.text logger.debug(text) return text except Exception as error: @@ -182,8 +188,9 @@ def posts_monthly(self): value='span', by=self._browser.by.TAG_NAME ) - text = text[-1] - text = text.text + if text: + text = text[-1] + text = text.text logger.debug(text) return text except Exception as error: @@ -209,8 +216,9 @@ def posts_today(self): value='span', by=self._browser.by.TAG_NAME ) - text = text[-1] - text = text.text + if text: + text = text[-1] + text = text.text logger.debug(text) return text except Exception as error: @@ -246,12 +254,10 @@ def privacy(self): return_first=True, ) if text: - break - - text = text[-1] - text = text.text - logger.debug(text) - return text + text = text[-1] + text = text.text + logger.debug(text) + return text except Exception as error: message, session, stacktrace = self.error_parsing(error) logger.error(f'{self.url} :: {message=} :: {session=} :: {stacktrace=}') @@ -272,12 +278,10 @@ def privacy_details(self): exact_match=True ) if text: - break - - text = text[0] - text = text.text - logger.debug(text) - return text + text = text[0] + text = text.text + logger.debug(text) + return text except Exception as error: message, session, stacktrace = self.error_parsing(error) logger.error(f'{self.url} :: {message=} :: {session=} :: {stacktrace=}') @@ -291,9 +295,10 @@ def title(self) -> str: logger.error(text) return '' - text = text.split('|') - text = text[0] - text = text.strip() + if text: + text = text.split('|') + text = text[0] + text = text.strip() logger.debug(text) return text except Exception as error: @@ -328,12 +333,10 @@ def visible(self) -> str: exact_match=True ) if text: - break - - text = text[-1] - text = text.text - logger.debug(text) - return text + text = text[-1] + text = text.text + logger.debug(text) + return text except Exception as error: message, session, stacktrace = self.error_parsing(error) logger.error(f'{self.url} :: {message=} :: {session=} :: {stacktrace=}')