Skip to content

Commit 722897e

Browse files
committed
feat: preparation of cloakbrowser and undetected-chromedriver for use with Selenium-wrapper from other languages like VBA
routines to download cloakbrowser and download+patch undetected-chroemdriver, backpassing location values via ini-files
1 parent fdb28b8 commit 722897e

3 files changed

Lines changed: 194 additions & 0 deletions

File tree

src/utils_msoffice/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- submodule with utilities for Microsoft Office in general
1111
- submodule with utilities for Microsoft Excel including "pythonic" access to ExcelAPI
1212
- submodule for interfacing calling COM scripting with Microsoft Office applications
13+
- submodule for making cloakbrowser available
1314
"""
1415

1516

@@ -30,3 +31,5 @@
3031
import utils_msoffice.utils_office as UtilsOffice
3132
import utils_msoffice.utils_excel as UtilsExcel
3233
import utils_msoffice.runner_VBA as RunnerVBA
34+
from utils_msoffice.prepare_cloakbrowser import executeVBAcallee as prepare_cloakbrowser
35+
from utils_msoffice.prepare_undetectedchrome import executeVBAcallee as prepare_undetectedchrome
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# prepare cloakbrowser for calling via VBA
2+
3+
"""
4+
Download cloakbrowser to be called from other languages like VBA
5+
return location and call parameters via INI-file
6+
"""
7+
8+
9+
# ruff and mypy per file settings
10+
#
11+
# empty lines
12+
# ruff: noqa: E302, E303
13+
# naming conventions
14+
# ruff: noqa: N802, N812
15+
# others
16+
# ruff: noqa: PLW1514
17+
18+
# fmt: off
19+
20+
21+
22+
import os
23+
import tempfile
24+
import configparser
25+
26+
from tap import Tap as TypedArgParse
27+
28+
from cloakbrowser.config import get_default_stealth_args
29+
from cloakbrowser.download import ensure_binary
30+
31+
import utils_mystuff as Utils
32+
33+
34+
35+
# TAP for parameters
36+
class ParamsClass(TypedArgParse):
37+
cloakbrowser_cache_dir: str | None = None
38+
params_inifile: str | None = None
39+
40+
41+
42+
# caller routines
43+
44+
# test routine for PythonIDE debugging - parameters are normally set via CLI
45+
# hint: quotation marks surrounding parameter values are swallowed by CLI param transfer mechanism
46+
def executeStandaloneTest() -> None:
47+
48+
paramsparser = ParamsClass()
49+
params = paramsparser.parse_args()
50+
executeMain(params)
51+
52+
53+
# VBA caller with direct CLI parameters (basically an CLI caller)
54+
def executeVBAcallee() -> None:
55+
56+
Utils.log_cli_args()
57+
paramsparser = ParamsClass(explicit_bool=True)
58+
params = paramsparser.parse_args()
59+
Utils.log_cli_params(params)
60+
61+
executeMain(params)
62+
63+
64+
65+
# main program logic
66+
67+
# main caller
68+
def executeMain(params: ParamsClass) -> None:
69+
70+
# determine cache path for cloakbrowser
71+
if params.cloakbrowser_cache_dir is None:
72+
params.cloakbrowser_cache_dir = os.path.join(tempfile.gettempdir(), "cloakbrowser")
73+
os.environ["CLOAKBROWSER_CACHE_DIR"] = params.cloakbrowser_cache_dir
74+
# determine ini file for parameters calling cloakbrowser externally
75+
if params.params_inifile is None:
76+
params.params_inifile = os.path.join(tempfile.gettempdir(), "cloakbrowser.ini")
77+
78+
# determine content of ini file
79+
config = configparser.ConfigParser()
80+
config.add_section("cloakbrowser")
81+
config["cloakbrowser"]["binary_path"] = ensure_binary()
82+
config["cloakbrowser"]["stealth_args"] = ",".join(get_default_stealth_args())
83+
84+
# write back ini file
85+
with open(params.params_inifile, 'w') as configfile:
86+
config.write(configfile)
87+
88+
89+
90+
# standalone call / test
91+
92+
if __name__ == '__main__':
93+
94+
executeStandaloneTest()
95+
96+
# completion message
97+
Utils.exitFinished("prepare_cloakbrowser")
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# prepare cloakbrowser for calling via VBA
2+
3+
4+
"""
5+
Download chromedriver and apply undetected-chromedriver patch to be called from other languages like VBA
6+
return location via INI-file
7+
"""
8+
9+
10+
# ruff and mypy per file settings
11+
#
12+
# empty lines
13+
# ruff: noqa: E302, E303
14+
# naming conventions
15+
# ruff: noqa: N802, N812
16+
# others
17+
# ruff: noqa: PLW1514
18+
19+
# fmt: off
20+
21+
22+
23+
import os
24+
import tempfile
25+
import configparser
26+
27+
from tap import Tap as TypedArgParse
28+
29+
from undetected_chromedriver.patcher import Patcher
30+
31+
import utils_mystuff as Utils
32+
33+
34+
35+
# TAP for parameters
36+
class ParamsClass(TypedArgParse):
37+
params_inifile: str | None = None
38+
39+
40+
41+
# caller routines
42+
43+
# test routine for PythonIDE debugging - parameters are normally set via CLI
44+
# hint: quotation marks surrounding parameter values are swallowed by CLI param transfer mechanism
45+
def executeStandaloneTest() -> None:
46+
47+
paramsparser = ParamsClass()
48+
params = paramsparser.parse_args()
49+
executeMain(params)
50+
51+
52+
# VBA caller with direct CLI parameters (basically an CLI caller)
53+
def executeVBAcallee() -> None:
54+
55+
Utils.log_cli_args()
56+
paramsparser = ParamsClass(explicit_bool=True)
57+
params = paramsparser.parse_args()
58+
Utils.log_cli_params(params)
59+
60+
executeMain(params)
61+
62+
63+
64+
# main program logic
65+
66+
# main caller
67+
def executeMain(params: ParamsClass) -> None:
68+
69+
# determine ini file for parameters calling cloakbrowser externally
70+
if params.params_inifile is None:
71+
params.params_inifile = os.path.join(tempfile.gettempdir(), "undetectedchrome.ini")
72+
73+
patcher = Patcher()
74+
patcher.auto()
75+
76+
# determine content of ini file
77+
config = configparser.ConfigParser()
78+
config.add_section("undetectedchrome")
79+
config["undetectedchrome"]["binary_path"] = patcher.executable_path
80+
81+
# write back ini file
82+
with open(params.params_inifile, 'w') as configfile:
83+
config.write(configfile)
84+
85+
86+
87+
# standalone call / test
88+
89+
if __name__ == '__main__':
90+
91+
executeStandaloneTest()
92+
93+
# completion message
94+
Utils.exitFinished("prepare_cloakbrowser")

0 commit comments

Comments
 (0)