-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapsbrowser.py
100 lines (77 loc) · 3.21 KB
/
mapsbrowser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import datetime
import pathlib
import sys
import os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class ScreenshotBrowser:
def __init__(self, driver):
self.driver = driver
def setup(self, x_size: int, y_size: int) -> None:
"""Sets up the browser window size and adds consent"""
# self.config['window_x'], self.config['window_y']
self.driver.set_window_size(x_size, y_size)
self.driver.get('https://www.google.be/404page')
self.driver.add_cookie(
{'name': 'CONSENT',
'value': 'YES+cb.20220111+BE.nl'})
def get_maps_page(self, url: str, image_path: pathlib.Path) -> None:
"""Gets the google maps page from url, saves it at image_path"""
self.driver.get(url)
# Remove omnibox
js_string = 'var element = document.getElementById("omnibox-container"); element.remove();'
self.driver.execute_script(js_string)
# Remove username and icons
js_string = 'var element = document.getElementById("vasquette"); element.remove();'
self.driver.execute_script(js_string)
# Remove bottom scaling bar
js_string = 'var element = document.getElementsByClassName("app-viewcard-strip"); element[0].remove();'
self.driver.execute_script(js_string)
print('[INFO]: Saving screenshot to', str(image_path))
self.driver.save_screenshot(str(image_path))
def __enter__(self):
print('[DEBUG] Entering ScreenshotBrowser class...')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('[DEBUG] Exiting ScreenshotBrowser class...')
self.driver.quit()
def gecko_path():
"""
Returns path to the geckodriver.
Detects if it is running as a frozen instance by pyinstaller or not.
"""
relative_path = './driver/geckodriver.exe'
try:
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
class FirefoxBrowser(ScreenshotBrowser):
def __init__(self, visual: bool = False):
if visual:
self.driver = webdriver.Firefox(executable_path=gecko_path())
else:
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['marionette'] = True
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
self.driver = webdriver.Firefox(
executable_path=gecko_path(),
capabilities=capabilities,
options=options,
)
super().__init__(self.driver)
class ChromeBrowser(ScreenshotBrowser):
def __init__(self, visual=False):
chromedriver_path = '/usr/lib/chromium-browser/chromedriver'
if visual:
self.driver = webdriver.Chrome(chromedriver_path)
else:
options = webdriver.ChromeOptions()
options.add_argument('--headless')
self.driver = webdriver.Chrome(
executable_path=chromedriver_path,
options=options,
)
super().__init__(self.driver)