Minor refactoring

This commit is contained in:
sebthom
2022-11-22 19:40:30 +01:00
parent 811b8ec116
commit ea828efbfb

View File

@@ -2,23 +2,18 @@
Copyright (C) 2022 Sebastian Thomschke and contributors Copyright (C) 2022 Sebastian Thomschke and contributors
SPDX-License-Identifier: AGPL-3.0-or-later SPDX-License-Identifier: AGPL-3.0-or-later
""" """
import atexit, copy, getopt, importlib.metadata, json, logging, os, signal, sys, textwrap, time, urllib import atexit, copy, getopt, importlib.metadata, json, logging, os, re, signal, shutil, sys, textwrap, time, urllib
import re
import shutil
from collections.abc import Iterable from collections.abc import Iterable
from datetime import datetime from datetime import datetime
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
from typing import Any, Final from typing import Any, Final
from urllib import request
from wcmatch import glob from wcmatch import glob
from overrides import overrides from overrides import overrides
from ruamel.yaml import YAML from ruamel.yaml import YAML
from selenium.common.exceptions import NoSuchElementException, TimeoutException, WebDriverException, \ from selenium.common.exceptions import ElementClickInterceptedException, NoSuchElementException, TimeoutException, WebDriverException
ElementClickInterceptedException
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from . import utils, resources, extract # pylint: disable=W0406 from . import utils, resources, extract # pylint: disable=W0406
from .utils import abspath, apply_defaults, ensure, is_frozen, pause, pluralize, safe_get, parse_datetime from .utils import abspath, apply_defaults, ensure, is_frozen, pause, pluralize, safe_get, parse_datetime
@@ -365,7 +360,6 @@ class KleinanzeigenBot(SeleniumMixin):
self.handle_captcha_if_present("login-recaptcha", "but DON'T click 'Einloggen'.") self.handle_captcha_if_present("login-recaptcha", "but DON'T click 'Einloggen'.")
self.web_click(By.ID, "login-submit") self.web_click(By.ID, "login-submit")
pause(800, 3000) pause(800, 3000)
def handle_captcha_if_present(self, captcha_element_id:str, msg:str) -> None: def handle_captcha_if_present(self, captcha_element_id:str, msg:str) -> None:
@@ -670,7 +664,7 @@ class KleinanzeigenBot(SeleniumMixin):
self.web_input(By.XPATH, '//*[@id="site-search-query"]', str(id_)) self.web_input(By.XPATH, '//*[@id="site-search-query"]', str(id_))
# navigate to ad page and wait # navigate to ad page and wait
submit_button = self.webdriver.find_element(By.XPATH, '//*[@id="site-search-submit"]') submit_button = self.webdriver.find_element(By.XPATH, '//*[@id="site-search-submit"]')
WebDriverWait(self.webdriver, 15).until(EC.element_to_be_clickable(submit_button)) self.web_await(EC.element_to_be_clickable(submit_button), 15)
try: try:
submit_button.click() submit_button.click()
except ElementClickInterceptedException: # sometimes: special banner might pop up and intercept except ElementClickInterceptedException: # sometimes: special banner might pop up and intercept
@@ -734,7 +728,7 @@ class KleinanzeigenBot(SeleniumMixin):
file_ending = current_img_url.split('.')[-1].lower() file_ending = current_img_url.split('.')[-1].lower()
img_path = directory + '/' + img_fn_prefix + str(img_nr) + '.' + file_ending img_path = directory + '/' + img_fn_prefix + str(img_nr) + '.' + file_ending
if current_img_url.startswith('https'): # verify https (for Bandit linter) if current_img_url.startswith('https'): # verify https (for Bandit linter)
request.urlretrieve(current_img_url, img_path) # nosec B310 urllib.request.urlretrieve(current_img_url, img_path) # nosec B310
dl_counter += 1 dl_counter += 1
img_paths.append(img_path.split('/')[-1]) img_paths.append(img_path.split('/')[-1])