|
1
|
|
|
from blink2png_bridge import Blink2pngBridge |
|
2
|
|
|
from selenium import webdriver |
|
3
|
|
|
|
|
4
|
|
|
""" |
|
5
|
|
|
Copyright (c) 2020 Star Inc.(https://starinc.xyz) |
|
6
|
|
|
|
|
7
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public |
|
8
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this |
|
9
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
# Browser Simulation |
|
14
|
|
|
|
|
15
|
|
|
class BrowserRender: |
|
16
|
|
|
""" |
|
17
|
|
|
The main solution. |
|
18
|
|
|
|
|
19
|
|
|
To render web page from QTWebEngine with blink2png, |
|
20
|
|
|
but we plan using Gecko/Servo to replace someday. |
|
21
|
|
|
""" |
|
22
|
|
|
|
|
23
|
|
|
driver = None |
|
24
|
|
|
|
|
25
|
|
|
def __init__(self, capture_browser: str): |
|
26
|
|
|
self.using = capture_browser # Not used |
|
27
|
|
|
self.driver = Blink2pngBridge() |
|
28
|
|
|
|
|
29
|
|
|
def capture(self, url: str, path: str, size: str = "1920,1080"): |
|
30
|
|
|
assert self.driver, "Web Driver Not Existed." |
|
31
|
|
|
(width, height) = size.split(",") |
|
32
|
|
|
self.driver.set_window_size(width, height) |
|
33
|
|
|
self.driver.set_wait(1) |
|
34
|
|
|
self.driver.set_timeout(3) |
|
35
|
|
|
self.driver.save_screenshot(url, path) |
|
36
|
|
|
|
|
37
|
|
|
def close(self): |
|
38
|
|
|
pass |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
class BrowserAgent: |
|
42
|
|
|
""" |
|
43
|
|
|
As a backup solution. |
|
44
|
|
|
|
|
45
|
|
|
To capture web page via Selenium with webdriver. |
|
46
|
|
|
The class will allow you to use your browser as the agent to take a screenshot from it. |
|
47
|
|
|
""" |
|
48
|
|
|
|
|
49
|
|
|
driver = None |
|
50
|
|
|
|
|
51
|
|
|
def __init__(self, capture_browser: str): |
|
52
|
|
|
self.using = capture_browser |
|
53
|
|
|
self.driver = self._set_browser() |
|
54
|
|
|
|
|
55
|
|
|
def _set_browser(self): |
|
56
|
|
|
if self.using == "firefox": |
|
57
|
|
|
options = webdriver.FirefoxOptions() |
|
58
|
|
|
options.add_argument('--headless') |
|
59
|
|
|
options.add_argument('--private-window') |
|
60
|
|
|
return webdriver.Firefox(firefox_options=options) |
|
61
|
|
|
elif self.using == "chrome": |
|
62
|
|
|
options = webdriver.ChromeOptions() |
|
63
|
|
|
options.add_argument('--headless') |
|
64
|
|
|
options.add_argument('--incognito') |
|
65
|
|
|
options.add_argument('--disable-gpu') |
|
66
|
|
|
return webdriver.Chrome(chrome_options=options) |
|
67
|
|
|
raise BrowserException("BrowserAgent", "No Supported Browser Selected") |
|
68
|
|
|
|
|
69
|
|
|
def capture(self, url: str, path: str, size: str = "1920,1080"): |
|
70
|
|
|
assert self.driver, "Web Driver Not Existed." |
|
71
|
|
|
(width, height) = size.split(",") |
|
72
|
|
|
self.driver.set_window_size(width, height) |
|
73
|
|
|
self.driver.get(url) |
|
74
|
|
|
self.driver.save_screenshot(path) |
|
75
|
|
|
|
|
76
|
|
|
def close(self): |
|
77
|
|
|
self.driver.close() |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
class BrowserException(Exception): |
|
81
|
|
|
def __init__(self, cause, message): |
|
82
|
|
|
self.cause = cause |
|
83
|
|
|
self.message = message |
|
84
|
|
|
|
|
85
|
|
|
def __str__(self): |
|
86
|
|
|
return self.cause + ': ' + self.message |
|
87
|
|
|
|