diff --git a/automon/integrations/instagram/tests/test_instagram_browser_auth.py b/automon/integrations/instagram/tests/test_instagram_browser_auth.py index b21e15f8..4ecbdfa0 100644 --- a/automon/integrations/instagram/tests/test_instagram_browser_auth.py +++ b/automon/integrations/instagram/tests/test_instagram_browser_auth.py @@ -9,7 +9,7 @@ class InstagramClientTest(unittest.TestCase): if c.is_running(): c.browser.get(c.urls.domain) - c.browser.add_cookie_from_file() + c.browser.add_cookie_from_base64() c.browser.refresh() if c.is_authenticated(): def test_authenticate(self): diff --git a/automon/integrations/seleniumWrapper/browser.py b/automon/integrations/seleniumWrapper/browser.py index 160ac408..4db91e81 100644 --- a/automon/integrations/seleniumWrapper/browser.py +++ b/automon/integrations/seleniumWrapper/browser.py @@ -1,5 +1,6 @@ import os import json +import base64 import datetime import tempfile import functools @@ -195,24 +196,56 @@ def action_type(self, key: str or Keys, secret: bool = True): ))) return False - def add_cookie(self, cookie_dict: dict): + def add_cookie(self, cookie_dict: dict) -> bool: result = self.webdriver.add_cookie(cookie_dict=cookie_dict) - log.info(f'{result}') - return result - def add_cookie_from_file(self, file: str = 'cookies.txt') -> [dict]: + if result is None: + log.debug(f'{True}') + return True + + log.error(f'{False}') + return False + + def add_cookies(self, cookies_list: list): + for cookie in cookies_list: + self.add_cookie(cookie_dict=cookie) + self.get_cookies_summary() + + log.debug(f'{True}') + return True + + def add_cookie_from_file(self, file: str = None) -> bool: + if not file and self.config.cookies_file: + file = self.config.cookies_file + else: + file = 'cookies.txt' + if os.path.exists(file): log.debug(f'{file}') with open(file, 'r') as cookies_file: - for cookie in json.loads(cookies_file.read()): - self.add_cookie(cookie_dict=cookie) - self.get_cookies() + self.add_cookies( + json.loads(cookies_file.read()) + ) return True else: log.error(f'{file}') return False + def add_cookie_from_base64(self, base64_str: str = None): + if not base64_str and self.config.cookies_base64: + base64_str = self.config.cookies_base64 + + if base64_str: + self.add_cookies( + json.loads(base64.b64decode(base64_str)) + ) + log.debug(f'{True}') + return True + + log.error(f'{False}') + return False + def delete_all_cookies(self) -> None: result = self.webdriver.delete_all_cookies() log.info(f'{True}') @@ -225,6 +258,18 @@ def get_cookie(self, name: str) -> dict: def get_cookies(self) -> [dict]: result = self.webdriver.get_cookies() + log.debug(f'{True}') + return result + + def get_cookies_base64(self) -> base64: + result = self.get_cookies() + log.debug(f'{True}') + return base64.b64encode( + json.dumps(result).encode() + ).decode() + + def get_cookies_summary(self): + result = self.get_cookies() summary = {} if result: for cookie in result: @@ -243,8 +288,8 @@ def get_cookies(self) -> [dict]: else: summary[domain] = [name] - log.info(f'{summary}') - return result + log.debug(f'{summary}') + return summary @_is_running def close(self): diff --git a/automon/integrations/seleniumWrapper/config.py b/automon/integrations/seleniumWrapper/config.py index c14f032a..04a50bc8 100644 --- a/automon/integrations/seleniumWrapper/config.py +++ b/automon/integrations/seleniumWrapper/config.py @@ -1,6 +1,7 @@ import selenium.webdriver from automon.log import logger +from automon.helpers.osWrapper import environ from .config_webdriver_chrome import ConfigChrome @@ -13,6 +14,9 @@ def __init__(self): self._webdriver = None self._webdriver_wrapper = None + self._cookies_base64 = environ('SELENIUM_COOKIES_BASE64') + self._cookies_file = environ('SELENIUM_COOKIES_FILE') + self._chrome = ConfigChrome() self._edge = NotImplemented self._firefox = NotImplemented @@ -36,6 +40,16 @@ def window_size(self): if self.webdriver_wrapper: return self.webdriver_wrapper.window_size + @property + def cookies_base64(self): + log.debug(f'{len(self._cookies_base64)}') + return self._cookies_base64 + + @property + def cookies_file(self): + log.info(f'{self._cookies_file}') + return self._cookies_file + def Chrome(self): """selenium Chrome webdriver diff --git a/env-example.sh b/env-example.sh index f2741878..c0b40921 100644 --- a/env-example.sh +++ b/env-example.sh @@ -72,6 +72,8 @@ TWINE_PASSWORD= # Selenium SELENIUM_CHROMEDRIVER_PATH= SELENIUM_OPT= +SELENIUM_COOKIES_BASE64= +SELENIUM_COOKIES_FILE= # Sentry.io SENTRY_DSN=