Skip to content

Commit

Permalink
sleeper: simplify args
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Nov 12, 2023
1 parent 60bf211 commit fc7d300
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 68 deletions.
56 changes: 22 additions & 34 deletions automon/helpers/sleeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 9 additions & 20 deletions automon/helpers/tests/test_sleeper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
2 changes: 1 addition & 1 deletion automon/integrations/facebook/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions automon/integrations/instagram/client_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions automon/integrations/instagram/stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -204,7 +204,7 @@ def run(config):
else:
browser = authenticate(login, password, client)

Sleeper.hour('instagram')
Sleeper.hour()


def runrun(browser, account):
Expand Down
2 changes: 1 addition & 1 deletion automon/integrations/openvpn/openvpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion automon/integrations/seleniumWrapper/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def wait_for(
value=value,
error=error,
)))
Sleeper.seconds(__name__, 0.2)
Sleeper.seconds(0.2)

retry += 1

Expand Down

0 comments on commit fc7d300

Please sign in to comment.