From e0e4c6691c739b436e861c4d53b4a54c6dcd5d2b Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Wed, 2 Mar 2022 23:29:25 -0500 Subject: [PATCH] [O] Encapsulate --- src/PS5TrackingBot.py | 16 +++------------- src/StockTrackingBot.py | 15 +++------------ src/utils.py | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 src/utils.py diff --git a/src/PS5TrackingBot.py b/src/PS5TrackingBot.py index 20a3161..9d014b4 100644 --- a/src/PS5TrackingBot.py +++ b/src/PS5TrackingBot.py @@ -1,14 +1,13 @@ import os -import re import time -import traceback import requests -from selenium.common.exceptions import StaleElementReferenceException from selenium.webdriver import Chrome from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By +from utils import parse_retry + # Config # Telegram chat ID that receives update messages (could be a channel in @channel_id format) # TG_RECEIVER = 1770239825 @@ -78,18 +77,9 @@ if __name__ == '__main__': # parse_page(browser) # browser.close() - def parse(tries: int = 0): - try: - parse_page(browser) - except StaleElementReferenceException: - if tries < 3: - parse(tries + 1) - except Exception as e: - traceback.print_exc() - # Refresh indefinitely while True: time.sleep(5) - parse() + parse_retry(parse_page, browser) browser.refresh() time.sleep(2) diff --git a/src/StockTrackingBot.py b/src/StockTrackingBot.py index a720b19..7dd4ad3 100644 --- a/src/StockTrackingBot.py +++ b/src/StockTrackingBot.py @@ -1,14 +1,14 @@ import os import re import time -import traceback import requests -from selenium.common.exceptions import StaleElementReferenceException from selenium.webdriver import Chrome from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By +from utils import parse_retry + # Config # Price increase ratio threshold (ignore everything higher than this ratio) INCR_MAX = 0.2 @@ -120,18 +120,9 @@ if __name__ == '__main__': # parse_page(browser) # browser.close() - def parse(tries: int = 0): - try: - parse_page(browser) - except StaleElementReferenceException: - if tries < 3: - parse(tries + 1) - except Exception as e: - traceback.print_exc() - # Refresh indefinitely while True: time.sleep(5) - parse() + parse_retry(parse_page, browser) browser.refresh() time.sleep(2) diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..0d45826 --- /dev/null +++ b/src/utils.py @@ -0,0 +1,15 @@ +import traceback +from typing import Callable + +from selenium.common.exceptions import StaleElementReferenceException +from selenium.webdriver.chrome.webdriver import WebDriver + + +def parse_retry(parser: Callable, browser: WebDriver, tries: int = 0): + try: + parser(browser) + except StaleElementReferenceException: + if tries < 3: + parse_retry(parser, browser, tries + 1) + except Exception as e: + traceback.print_exc()