grattoir/grattoir/__init__.py

52 lines
1.7 KiB
Python

from selenium.webdriver import firefox
from fake_useragent import UserAgent
from pathlib import Path
import importlib
import os
def tor_profile():
tor_firefox_path = str(Path("~/.tor project/firefox").expanduser())
ini_path = tor_firefox_path + "/profiles.ini"
with open(ini_path) as ini:
for line in ini:
if "Path=" in line:
return tor_firefox_path + '/' + line.split('=')[1][:-1]
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 install_har_export_trigger(driver):
with importlib.resources.path(
__package__ + ".assets",
"har_export_trigger-0.6.2resigned1.xpi",
) as p:
driver.install_addon(p.as_uri()[7:])
return driver
class Grattoir(object):
def __enter__(self):
self._driver = None
p_path = tor_profile()
profile = firefox.firefox_profile.FirefoxProfile(profile_directory=p_path)
profile.set_preference("devtools.toolbox.selectedTool", "netmonitor")
profile.set_preference("devtools.netmonitor.persistlog", True)
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)
self._driver = install_har_export_trigger(self._driver)
return self._driver
def __exit__(self, exc_type, exc_value, exc_tb):
self._driver.quit()