From fc7d3003dc7ef64ac69b313a0a873247110cd9e4 Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Sun, 12 Nov 2023 05:21:59 -0800 Subject: [PATCH] sleeper: simplify args --- automon/helpers/sleeper.py | 56 ++++++++----------- automon/helpers/tests/test_sleeper.py | 29 +++------- automon/integrations/facebook/groups.py | 2 +- .../google/sheets/tests/test_google_sheets.py | 2 +- .../integrations/instagram/client_browser.py | 4 +- automon/integrations/instagram/stories.py | 16 +++--- automon/integrations/openvpn/openvpn.py | 2 +- .../integrations/seleniumWrapper/browser.py | 2 +- 8 files changed, 45 insertions(+), 68 deletions(-) diff --git a/automon/helpers/sleeper.py b/automon/helpers/sleeper.py index cb449468..2014d1db 100644 --- a/automon/helpers/sleeper.py +++ b/automon/helpers/sleeper.py @@ -4,95 +4,83 @@ from automon.log import logger log = logger.logging.getLogger(__name__) -log.setLevel(logger.INFO) +log.setLevel(logger.DEBUG) class Sleeper: @staticmethod - def seconds(caller: object or str, seconds: int) -> time.sleep: + def seconds(seconds: int) -> time.sleep: """Sleep for this many seconds""" sleep = seconds - if sleep < 2: - log.debug(f'[{Sleeper.seconds.__name__}] ' - f'[{caller}] sleeping for {sleep} second') - else: - log.debug(f'[{Sleeper.seconds.__name__}] ' - f'[{caller}] sleeping for {sleep} seconds') + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def minute(caller: object or str, sleep: int = 60) -> time.sleep: + def minute(minutes: int = 1) -> time.sleep: """Sleep for a minute""" - log.debug(f'[{Sleeper.minute.__name__}] ' - f'[{caller}] sleeping for {sleep} seconds') + sleep = round(minutes * 60) + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def within_a_minute(caller, sleep: int = None): + def within_a_minute(sleep: int = None): """Sleep for a random minute""" sleep = sleep if isinstance(sleep, int) else \ random.choice(range(1, 1 * 60)) - log.debug(f'[{Sleeper.within_a_minute.__name__}] ' - f'[{caller}] sleeping for {sleep} seconds') + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def minutes(caller, minutes: int): + def minutes(minutes: int): """Sleep for this many minutes""" sleep = minutes * 60 - log.debug(f'[{Sleeper.minutes.__name__}] ' - f'[{caller}] sleeping for {sleep} minutes') + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def hour(caller, hour: int = 1): + def hour(hour: int = 1): """At some time within an hour, this will run""" sleep = hour if not hour else random.choice( range(1, hour * 60 * 60)) - log.debug(f'[{Sleeper.hour.__name__}] ' - f'[{caller}] sleeping for {sleep} seconds') + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def hours(caller, hours): + def hours(hours): """Sleep for this many hours""" sleep = hours * 60 * 60 - log.debug(f'[{Sleeper.hours.__name__}] ' - f'[{caller}] sleeping for {hours} hours') + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def day(caller, hours: int = 24): + def day(hours: int = 24): """At some time within 24 hours, this will run""" sleep = hours if not hours else random.choice( range(1, hours * 60 * 60)) - log.debug(f'[{Sleeper.day.__name__}] ' - f'[{caller}] sleeping for {sleep} seconds') + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def daily(caller, hours: int = 24): + def daily(hours: int = 24): """Sleep for one day""" sleep = hours if not hours else hours * 60 * 60 - log.debug(f'[{Sleeper.daily.__name__}] ' - f'[{caller}] sleeping for {sleep} seconds') + log.debug(f'{sleep}') return time.sleep(sleep) @staticmethod - def time_range(caller, seconds: int): - """Sleep for a random range - """ + def time_range(seconds: int): + """Sleep for a random range""" + sleep = seconds if not seconds else random.choice( range(1, seconds)) - log.debug(f'[{Sleeper.time_range.__name__}] ' - f'[{caller}] sleeping for {sleep} seconds') + log.debug(f'{sleep}') return time.sleep(sleep) diff --git a/automon/helpers/tests/test_sleeper.py b/automon/helpers/tests/test_sleeper.py index defc09fb..5ee265cf 100644 --- a/automon/helpers/tests/test_sleeper.py +++ b/automon/helpers/tests/test_sleeper.py @@ -16,26 +16,15 @@ def test_Sleeper(self): self.assertTrue(Sleeper.daily) self.assertTrue(Sleeper.time_range) - self.assertRaises(TypeError, Sleeper.seconds) - self.assertRaises(TypeError, Sleeper.minute) - self.assertRaises(TypeError, Sleeper.within_a_minute) - self.assertRaises(TypeError, Sleeper.minutes) - self.assertRaises(TypeError, Sleeper.hour) - self.assertRaises(TypeError, Sleeper.hours) - self.assertRaises(TypeError, Sleeper.day) - self.assertRaises(TypeError, Sleeper.daily) - self.assertRaises(TypeError, Sleeper.time_range) - self.assertRaises(TypeError, Sleeper.seconds, '1') - - self.assertIsNone(Sleeper.seconds(SleeperTest, 0)) - self.assertIsNone(Sleeper.minute(SleeperTest, 0)) - self.assertIsNone(Sleeper.within_a_minute(SleeperTest, 0)) - self.assertIsNone(Sleeper.minutes(SleeperTest, 0)) - self.assertIsNone(Sleeper.hour(SleeperTest, 0)) - self.assertIsNone(Sleeper.hours(SleeperTest, 0)) - self.assertIsNone(Sleeper.day(SleeperTest, 0)) - self.assertIsNone(Sleeper.daily(SleeperTest, 0)) - self.assertIsNone(Sleeper.time_range(SleeperTest, 0)) + self.assertIsNone(Sleeper.seconds(0)) + self.assertIsNone(Sleeper.minute(0)) + self.assertIsNone(Sleeper.within_a_minute(0)) + self.assertIsNone(Sleeper.minutes(0)) + self.assertIsNone(Sleeper.hour(0)) + self.assertIsNone(Sleeper.hours(0)) + self.assertIsNone(Sleeper.day(0)) + self.assertIsNone(Sleeper.daily(0)) + self.assertIsNone(Sleeper.time_range(0)) if __name__ == '__main__': diff --git a/automon/integrations/facebook/groups.py b/automon/integrations/facebook/groups.py index 8c335203..4db20bb0 100644 --- a/automon/integrations/facebook/groups.py +++ b/automon/integrations/facebook/groups.py @@ -443,7 +443,7 @@ def get_with_rate_limiter( result = self.get(url=url) if self.rate_limited(): - Sleeper.time_range(caller=__name__, seconds=self._timeout) + Sleeper.time_range(seconds=self._timeout) self._timeout = round(self._timeout * 1.6) log.info(str(dict( url=url, diff --git a/automon/integrations/google/sheets/tests/test_google_sheets.py b/automon/integrations/google/sheets/tests/test_google_sheets.py index 7d5aaba9..4363cf8b 100644 --- a/automon/integrations/google/sheets/tests/test_google_sheets.py +++ b/automon/integrations/google/sheets/tests/test_google_sheets.py @@ -246,7 +246,7 @@ def main(): range = f'{SHEET_NAME}!{duplicate_index}:{duplicate_index}' result = sheets_client.clear(range=range) # max 60/min - Sleeper.seconds(f'WriteRequestsPerMinutePerUser', seconds=1) + Sleeper.seconds(seconds=1) log.info(result) df = df.drop(duplicate_index) diff --git a/automon/integrations/instagram/client_browser.py b/automon/integrations/instagram/client_browser.py index 487e880b..9bcaa3ef 100644 --- a/automon/integrations/instagram/client_browser.py +++ b/automon/integrations/instagram/client_browser.py @@ -83,7 +83,7 @@ def get_stories(self, account: str): log.debug(f'[get_stories] no stories for {account}') return num_of_stories - Sleeper.seconds('instagram', 2) + Sleeper.seconds(2) while True: try: @@ -97,7 +97,7 @@ def get_stories(self, account: str): num_of_stories += 1 browser.save_screenshot_to_minio(bucket_name='screenshots', prefix='instagram/' + account) - Sleeper.seconds('watch the story for a bit', 1) + Sleeper.seconds(1) browser.save_screenshot_to_minio(bucket_name='screenshots', prefix='instagram/' + account) except Exception as error: diff --git a/automon/integrations/instagram/stories.py b/automon/integrations/instagram/stories.py index 90725858..06f2cc89 100644 --- a/automon/integrations/instagram/stories.py +++ b/automon/integrations/instagram/stories.py @@ -41,14 +41,14 @@ def authenticate(username, password, minio_client=None, retries=None): log.logging.debug('[authenticate] {}'.format(login_page)) - Sleeper.seconds('instagram get page', 1) + Sleeper.seconds(1) actions = ActionChains(browser.browser) actions.send_keys(Keys.TAB) actions.send_keys(username) actions.perform() - Sleeper.seconds('instagram get page', 1) + Sleeper.seconds(1) # the password field is sometimes div[3] and div[4] login_pass_xpaths = [ @@ -70,7 +70,7 @@ def authenticate(username, password, minio_client=None, retries=None): except: pass - Sleeper.seconds('instagram get page', 2) + Sleeper.seconds(2) found_btn = False for xpath in login_btn_xpaths: @@ -90,12 +90,12 @@ def authenticate(username, password, minio_client=None, retries=None): '[browser] Found password field: {} Found login button: {}'.format(browser.browser.name, found_pass, found_btn)) - Sleeper.minute("instagram can't authenticate") + Sleeper.minute() login_pass.send_keys(password) login_btn.click() - Sleeper.seconds('wait for instagram to log in', 5) + Sleeper.seconds(5) log.logging.debug( '[authenticated browser] [{}] {} session: {}'.format(browser.browser.name, browser.browser.title, @@ -123,7 +123,7 @@ def get_stories(authenticated_browser, account): log.logging.debug('[get_stories] no stories for {}'.format(account)) return num_of_stories - Sleeper.seconds('instagram', 2) + Sleeper.seconds(2) while True: try: @@ -134,7 +134,7 @@ def get_stories(authenticated_browser, account): log.logging.debug(('[get_stories] {} end of stories'.format(account))) raise Exception num_of_stories += 1 - Sleeper.seconds('watch the story for a bit', 1) + Sleeper.seconds(1) browser.save_screenshot_to_minio(prefix=account) except: # TODO: disable browser proxy when done @@ -204,7 +204,7 @@ def run(config): else: browser = authenticate(login, password, client) - Sleeper.hour('instagram') + Sleeper.hour() def runrun(browser, account): diff --git a/automon/integrations/openvpn/openvpn.py b/automon/integrations/openvpn/openvpn.py index ebdb63df..dd634526 100644 --- a/automon/integrations/openvpn/openvpn.py +++ b/automon/integrations/openvpn/openvpn.py @@ -208,7 +208,7 @@ def run(minio_config: MinioConfig, openvpn_config): log.logging.info('[build client configs] Finshed building all OpenVPN clients') log.logging.debug('[ClientConfig] sleeping') - Sleeper.day('openvpn') + Sleeper.day() def test_run(minio_config: MinioConfig, openvpn_config): diff --git a/automon/integrations/seleniumWrapper/browser.py b/automon/integrations/seleniumWrapper/browser.py index 92f8a07e..91648010 100644 --- a/automon/integrations/seleniumWrapper/browser.py +++ b/automon/integrations/seleniumWrapper/browser.py @@ -549,7 +549,7 @@ def wait_for( value=value, error=error, ))) - Sleeper.seconds(__name__, 0.2) + Sleeper.seconds(0.2) retry += 1