Compare commits

...

6 Commits
0.1.0 ... main

Author SHA1 Message Date
mmalter 3512843e10 different approach for har 2025-02-13 10:16:24 +01:00
mmalter d7bfe4ed40 add method to retrieve har 2025-02-12 05:18:32 +01:00
mmalter 8d9ba4d663 better install procedure 2025-02-11 04:58:09 +01:00
mmalter 88511b62aa add har_export_trigger 2025-02-11 04:11:39 +01:00
mmalter 647a7744fb get rid of superfluous init 2025-02-10 21:31:55 +01:00
mmalter 69df93b81a improve _driver cleanup 2025-01-20 08:16:41 +01:00
2 changed files with 81 additions and 28 deletions

View File

@ -1,10 +1,43 @@
from selenium.webdriver import firefox
from fake_useragent import UserAgent
from pathlib import Path
from selenium.webdriver import firefox
import importlib
import os
import tempfile
import types
import uuid
def tor_profile():
def unique_name(prefix):
return prefix + "-" + str(uuid.uuid4())
class Grattoir(object):
def __init__(self, name, with_har=False):
self.name = name
self.with_har = with_har
if self.with_har:
self.har_filename = unique_name(self.name)
self.har_export_path = tempfile.TemporaryDirectory()
self.export_path = (
self.har_export_path.name + "/" + self.har_filename
)
def tor_browser_binary_path(self):
for pe in os.environ["PATH"].split(os.pathsep):
checkname = os.path.join(pe, "tor-browser")
if os.access(checkname, os.X_OK) and not os.path.isdir(checkname):
return checkname
return None
def install_har_export_trigger(self):
with importlib.resources.path(
__package__ + ".assets",
"har_export_trigger-0.6.2resigned1.xpi",
) as p:
self.driver.install_addon(p.as_uri()[7:])
def tor_profile_path(self):
tor_firefox_path = str(Path("~/.tor project/firefox").expanduser())
ini_path = tor_firefox_path + "/profiles.ini"
with open(ini_path) as ini:
@ -12,29 +45,49 @@ def tor_profile():
if "Path=" in line:
return tor_firefox_path + '/' + line.split('=')[1][:-1]
def har_enabled_profile(self, profile):
prefs = {
"extensions.netmonitor.har.enableAutomation": True,
"extensions.netmonitor.har.contentAPIToken": "test",
"extensions.netmonitor.har.autoConnect": True,
"devtools.netmonitor.har.compress": False,
"devtools.netmonitor.har.defaultFileName": self.har_filename,
"devtools.netmonitor.har.defaultLogDir": self.har_export_path.name,
"devtools.netmonitor.har.enableAutoExportToFile": True,
"devtools.netmonitor.har.forceExport": True,
"devtools.netmonitor.har.includeResponseBodies": True,
"devtools.netmonitor.har.jsonp": True,
"devtools.netmonitor.har.jsonpCallback": True,
"devtools.netmonitor.har.pageLoadedTimeout": "2500",
"devtools.toolbox.selectedTool": "netmonitor",
"devtools.netmonitor.persistlog": True,
}
for k, v in prefs.items():
profile.set_preference(k, v)
return profile
def tor_browser_binary_path():
for pe in os.environ["PATH"].split(os.pathsep):
checkname = os.path.join(pe, "tor-browser")
if os.access(checkname, os.X_OK) and not os.path.isdir(checkname):
return checkname
return None
def firefox_profile(self):
profile = firefox.firefox_profile.FirefoxProfile(profile_directory=self.tor_profile_path())
if self.with_har:
profile = self.har_enabled_profile(profile)
return profile
class Grattoir(object):
def __init__(self):
self._driver = None
def init_driver(self):
self.driver = None
b_path = self.tor_browser_binary_path()
binary = firefox.firefox_binary.FirefoxBinary(firefox_path=b_path)
profile = self.firefox_profile()
options = firefox.options.Options()
options.profile = profile
options.binary = binary
self.driver = firefox.webdriver.WebDriver(options=options)
self.install_har_export_trigger()
def __enter__(self):
p_path = tor_profile()
profile = firefox.firefox_profile.FirefoxProfile(profile_directory=p_path)
b_path = tor_browser_binary_path()
binary = firefox.firefox_binary.FirefoxBinary(firefox_path=b_path)
o = firefox.options.Options()
o.profile = profile
o.binary = binary
self._driver = firefox.webdriver.WebDriver(options=o)
return self._driver
self.init_driver()
return self
def __exit__(self, exc_type, exc_value, exc_tb):
self._driver.quit()
self.driver.quit()
#if self.with_har:
#self.har_export_path.cleanup()

Binary file not shown.