[O] Encapsulate
This commit is contained in:
+3
-13
@@ -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)
|
||||
|
||||
+3
-12
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user