|
2 | 2 | from selenium.webdriver.chrome.options import Options as ChromeOptions
|
3 | 3 | from selenium.webdriver.firefox.options import Options as FirefoxOptions
|
4 | 4 | from sys import platform
|
| 5 | +from pathlib import Path |
5 | 6 |
|
| 7 | +ERROR_MSG_BASE = ( |
| 8 | + "Scrapping only the name and the number of contributions " |
| 9 | + "from Google Maps public profile" |
| 10 | +) |
6 | 11 |
|
7 | 12 | class SeleniumWrapper:
|
| 13 | + |
8 | 14 | def __init__(self, browser):
|
9 |
| - self.browser = browser |
| 15 | + # TODO check browser validity |
| 16 | + self.browser = browser[0].upper() + browser[1:] |
| 17 | + |
10 | 18 | self.driver = None
|
11 |
| - DRIVER_PATH = "" |
| 19 | + self._set_driver() |
| 20 | + |
12 | 21 |
|
13 |
| - # Detect OS |
| 22 | + def _get_driver_extension(self): |
14 | 23 | if platform.startswith("linux"):
|
15 |
| - EXT = "" |
| 24 | + return "" |
16 | 25 | elif platform.startswith("win32"):
|
17 |
| - EXT = ".exe" |
18 |
| - else: |
19 |
| - raise Exception( |
20 |
| - "The use of selenium is not supported for this OS. " |
21 |
| - 'Only "linux" and "win32" are possible\n', |
22 |
| - "Scrapping only the name and the number of contributions " |
23 |
| - "from Google Maps public profile", |
24 |
| - sep="\n", |
25 |
| - ) |
26 |
| - |
27 |
| - # Choose the good driver |
28 |
| - if self.browser == "chrome": |
29 |
| - options = ChromeOptions() |
30 |
| - DRIVER_PATH = f"./drivers/chromedriver{EXT}" |
31 |
| - self.driver = webdriver.Chrome(options=options, executable_path=DRIVER_PATH) |
| 26 | + return ".exe" |
| 27 | + raise Exception( |
| 28 | + "Mailfogle with selenium is not supported for this OS. " |
| 29 | + 'Only "linux" and "windows" are possible\n' + ERROR_MSG_BASE |
| 30 | + ) |
| 31 | + |
| 32 | + def _set_driver(self): |
| 33 | + msg = ( |
| 34 | + "Your {browser} profile cannot be loaded. " |
| 35 | + "It may be missing or inaccessible.\n" + ERROR_MSG_BASE |
| 36 | + ) |
32 | 37 |
|
33 |
| - elif self.browser == "firefox": |
| 38 | + extension = self._get_driver_extension() |
| 39 | + |
| 40 | + if self.browser == "Chrome": |
| 41 | + options = ChromeOptions() |
| 42 | + driver_filename = "chromedriver" + extension |
| 43 | + elif self.browser == "Firefox": |
34 | 44 | options = FirefoxOptions()
|
35 |
| - DRIVER_PATH = f"./drivers/geckodriver{EXT}" |
36 |
| - self.driver = webdriver.Firefox( |
37 |
| - options=options, executable_path=DRIVER_PATH |
38 |
| - ) |
| 45 | + driver_filename = "geckodriver" + extension |
| 46 | + |
| 47 | + driver_path = f"./drivers/{driver_filename}" |
| 48 | + if not Path(driver_path).is_file(): |
| 49 | + raise Exception(msg.format(browser=self.browser)) |
| 50 | + |
| 51 | + |
| 52 | + self.driver = getattr(webdriver, self.browser)( |
| 53 | + options=options, executable_path=driver_path |
| 54 | + ) |
0 commit comments