Skip to content

Commit

Permalink
selenium: explicit import ChromeWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Nov 27, 2023
1 parent a358fa1 commit 373c5b3
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 95 deletions.
42 changes: 8 additions & 34 deletions automon/integrations/facebook/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from automon.log import logger
from automon.helpers.sleeper import Sleeper
from automon.integrations.seleniumWrapper import SeleniumBrowser
from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper

log = logger.logging.getLogger(__name__)
log.setLevel(logger.DEBUG)
Expand Down Expand Up @@ -64,16 +65,14 @@ def __init__(self, url: str = None):
Depends on Selenium"""
self._url = url

self._browser = None
self._browser = SeleniumBrowser()

self._rate_per_minute = 2
self._rate_counter = []
self._wait_between_retries = random.choice(range(1, 60))

def content_unavailable(self):
"""This content isn't available right now"""
if not self._browser:
return

try:
xpath_content_unavailble = self._browser.wait_for_xpath(self._xpath_content_unavailble)
Expand All @@ -91,8 +90,6 @@ def content_unavailable(self):
self.screenshot_error()

def creation_date(self):
if not self._browser:
return

try:
xpath_creation_date = self._browser.wait_for_xpath(self._xpath_creation_date)
Expand Down Expand Up @@ -142,8 +139,6 @@ def average_rate(self):
return 0

def history(self):
if not self._browser:
return

try:
xpath_history = self._browser.wait_for_xpath(self._xpath_history)
Expand Down Expand Up @@ -181,8 +176,6 @@ def temporarily_blocked(self):
self.screenshot_error()

def members(self):
if not self._browser:
return

try:
xpath_members = self._browser.wait_for_xpath(self._xpath_members)
Expand All @@ -201,8 +194,6 @@ def members(self):
self.screenshot_error()

def members_count(self):
if not self._browser:
return

if self.members:
count = [x for x in self.members()]
Expand Down Expand Up @@ -234,8 +225,6 @@ def must_login(self):
self.screenshot_error()

def posts_monthly(self):
if not self._browser:
return

try:
xpath_monthly_posts = self._browser.wait_for_xpath(self._xpath_posts_monthly)
Expand All @@ -253,8 +242,6 @@ def posts_monthly(self):
self.screenshot_error()

def posts_monthly_count(self):
if not self._browser:
return

if self.posts_monthly:
count = [x for x in self.posts_monthly()]
Expand All @@ -266,8 +253,6 @@ def posts_monthly_count(self):
return posts_monthly_count

def posts_today(self):
if not self._browser:
return

try:
xpath_posts_today = self._browser.wait_for_xpath(self._xpath_posts_today)
Expand All @@ -285,8 +270,6 @@ def posts_today(self):
self.screenshot_error()

def posts_today_count(self):
if not self._browser:
return

if self.posts_today:
count = [x for x in self.posts_today()]
Expand All @@ -298,8 +281,6 @@ def posts_today_count(self):
return posts_today_count

def privacy(self):
if not self._browser:
return

try:
xpath_privacy = self._browser.wait_for_xpath(self._xpath_privacy)
Expand All @@ -317,8 +298,6 @@ def privacy(self):
self.screenshot_error()

def privacy_details(self):
if not self._browser:
return

try:
xpath_privacy_details = self._browser.wait_for_xpath(self._xpath_privacy_details)
Expand All @@ -336,8 +315,6 @@ def privacy_details(self):
self.screenshot_error()

def title(self) -> str:
if not self._browser:
return

try:
xpath_title = self._browser.wait_for_xpath(self._xpath_title)
Expand Down Expand Up @@ -368,8 +345,6 @@ def url_cleaner(url: str):
return url

def visible(self) -> str:
if not self._browser:
return

try:
xpath_visible = self._browser.wait_for_xpath(self._xpath_visible)
Expand Down Expand Up @@ -566,28 +541,27 @@ def set_url(self, url: str) -> str:

def start(self, headless: bool = True, random_user_agent: bool = False, set_user_agent: str = None):
"""start new instance of selenium"""
self._browser = SeleniumBrowser()
self._browser.config.webdriver_wrapper = ChromeWrapper()

if headless:
self._browser.set_webdriver().Chrome().in_headless().set_locale_experimental()
# self._browser.set_webdriver().Chrome().enable_headless().set_locale_experimental()
self._browser.config.webdriver_wrapper.enable_headless().set_locale_experimental()
else:
self._browser.set_webdriver().Chrome().set_locale_experimental()
self._browser.config.webdriver_wrapper.set_locale_experimental()

if random_user_agent:
self._browser.set_webdriver().Chrome().set_user_agent(
self._browser.config.webdriver_wrapper.set_user_agent(
self._browser.get_random_user_agent()
)
elif set_user_agent:
self._browser.set_webdriver().Chrome().set_user_agent(
self._browser.config.webdriver_wrapper.set_user_agent(
set_user_agent
)

log.info(str(dict(
browser=self._browser
)))
browser = self._browser.run()
self._browser.set_webdriver().Chrome().set_window_size(width=1920 * 0.6, height=1080)
self._browser.config.webdriver_wrapper.set_window_size(width=1920 * 0.6, height=1080)
return browser

def stop(self):
Expand Down
6 changes: 4 additions & 2 deletions automon/integrations/instagram/client_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from automon.log import logger
from automon.integrations.seleniumWrapper.browser import SeleniumBrowser
from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper

from automon.helpers.sleeper import Sleeper
# from automon.integrations.minioWrapper import MinioClient
Expand All @@ -28,13 +29,14 @@ def __init__(self,
"""Instagram Browser Client"""
self.config = config or InstagramConfig(login=login, password=password)
self.browser = SeleniumBrowser()
self.browser.config.webdriver_wrapper = ChromeWrapper()

self.useragent = self.browser.get_random_user_agent()

if headless:
self.browser.set_webdriver().Chrome().in_headless().set_user_agent(self.useragent)
self.browser.config.webdriver_wrapper.in_headless().set_user_agent(self.useragent)
else:
self.browser.set_webdriver().Chrome().set_user_agent(self.useragent)
self.browser.config.webdriver_wrapper.set_user_agent(self.useragent)

def __repr__(self):
return f'{self.__dict__}'
Expand Down
17 changes: 4 additions & 13 deletions automon/integrations/seleniumWrapper/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import base64
import datetime
import tempfile
import functools
import selenium
import selenium.webdriver

Expand All @@ -19,7 +18,6 @@
from automon.integrations.requestsWrapper import RequestsClient

from .config import SeleniumConfig
from .browser_types import SeleniumBrowserType
from .user_agents import SeleniumUserAgentBuilder

log = logger.logging.getLogger(__name__)
Expand Down Expand Up @@ -100,10 +98,6 @@ def request_status(self):
except:
pass

# @property
# def type(self) -> SeleniumBrowserType:
# return SeleniumBrowserType(self.config)

@property
def url(self):
return self.current_url
Expand All @@ -121,7 +115,7 @@ def current_url(self):

@property
def window_size(self):
return self.config.set_webdriver().window_size
return self.config.webdriver_wrapper.window_size

def _screenshot_name(self, prefix=None):
"""Generate a unique filename"""
Expand Down Expand Up @@ -365,7 +359,7 @@ def get_page_source_beautifulsoup(self, markdup: str = None, features: str = 'lx
markdup = self.get_page_source()
return BeautifulSoup(markup=markdup, features=features)

def get_random_user_agent(self, filter: list or str = None, case_sensitive: bool = False) -> list:
def get_random_user_agent(self, filter: list or str = None, case_sensitive: bool = False) -> str:
return SeleniumUserAgentBuilder().get_random(filter=filter, case_sensitive=case_sensitive)

def get_screenshot_as_base64(self, **kwargs):
Expand Down Expand Up @@ -464,15 +458,12 @@ def save_screenshot(

return False

def set_webdriver(self):
return self.config

def set_window_size(self, width=1920, height=1080, device_type=None) -> bool:
"""set browser resolution"""

try:
self.config.set_webdriver().webdriver_wrapper.set_window_size(width=width, height=height,
device_type=device_type)
self.config.webdriver_wrapper.set_window_size(width=width, height=height,
device_type=device_type)
except Exception as error:
message, session, stacktrace = self.error_parsing(error)
log.error(str(dict(
Expand Down
8 changes: 4 additions & 4 deletions automon/integrations/seleniumWrapper/browser_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def chrome(self, options: list = None) -> Chrome:

try:
if self.chromedriver:
return self.webdriver.Chrome(executable_path=self.chromedriver, options=chrome_options)
return self.webdriver.Chrome(options=chrome_options)
return self.webdriver.ChromeWrapper(executable_path=self.chromedriver, options=chrome_options)
return self.webdriver.ChromeWrapper(options=chrome_options)
except Exception as e:
log.error(f'Browser not set. {e}')

Expand All @@ -77,8 +77,8 @@ def chrome_headless(self, options: list = None, **kwargs) -> Chrome:

try:
if self.chromedriver:
return self.webdriver.Chrome(self.chromedriver, options=chrome_options, **kwargs)
return self.webdriver.Chrome(options=chrome_options, **kwargs)
return self.webdriver.ChromeWrapper(self.chromedriver, options=chrome_options, **kwargs)
return self.webdriver.ChromeWrapper(options=chrome_options, **kwargs)
except Exception as e:
log.error(f'Browser not set. {e}')

Expand Down
37 changes: 1 addition & 36 deletions automon/integrations/seleniumWrapper/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,25 @@
from automon.log import logger
from automon.helpers.osWrapper import environ

from .config_webdriver_chrome import ConfigChrome

log = logger.logging.getLogger(__name__)
log.setLevel(logger.DEBUG)


class SeleniumConfig(object):
def __init__(self):
self._webdriver = None
self._webdriver_wrapper = 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

@property
def webdriver(self):
try:
return self.webdriver_wrapper.webdriver
except:
return self._webdriver

@property
def webdriver_wrapper(self):
return self._webdriver_wrapper

@property
def window_size(self):
"""get window size
Expand All @@ -50,28 +40,6 @@ def cookies_file(self):
log.info(f'{self._cookies_file}')
return self._cookies_file

def Chrome(self):
"""selenium Chrome webdriver
"""
self._webdriver_wrapper = self._chrome
log.info(str(dict(
webdriver_wrapper=self._webdriver_wrapper
)))
return self.webdriver_wrapper

def Edge(self):
"""selenium Edge webdriver
"""
return self._edge

def Firefox(self):
"""selenium Firefox webdriver
"""
return self._firefox

def run(self):
"""run webdriver"""
run = self.webdriver_wrapper.run()
Expand All @@ -81,9 +49,6 @@ def run(self):
)))
return run

def set_webdriver(self):
return self

def start(self):
"""alias to run"""
return self.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
log.setLevel(logger.DEBUG)


class ConfigChrome(object):
class ChromeWrapper(object):

def __init__(self):
self._webdriver = None
Expand Down
4 changes: 3 additions & 1 deletion automon/integrations/seleniumWrapper/tests/test_browser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import unittest

from automon.integrations.seleniumWrapper.browser import SeleniumBrowser
from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper

browser = SeleniumBrowser()
browser.config.set_webdriver().Chrome().enable_defaults().enable_headless()
browser.config.webdriver_wrapper = ChromeWrapper()
browser.config.webdriver_wrapper.enable_defaults().enable_headless()


class SeleniumClientTest(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import unittest

from automon.integrations.seleniumWrapper.browser import SeleniumBrowser
from automon.integrations.seleniumWrapper.config_webdriver_chrome import ChromeWrapper

browser = SeleniumBrowser()
browser.config.set_webdriver().Chrome().enable_defaults().enable_headless()
browser.config.webdriver_wrapper = ChromeWrapper()
browser.config.webdriver_wrapper.enable_defaults().enable_headless()


class SeleniumClientTest(unittest.TestCase):
Expand Down
Loading

0 comments on commit 373c5b3

Please sign in to comment.