1
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2
|
|
3
|
"""
|
4
|
Browser profiles and Selenium driver initialization
|
5
|
"""
|
6
|
|
7
|
# This file is part of Haketilo.
|
8
|
#
|
9
|
# Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
|
10
|
#
|
11
|
# This program is free software: you can redistribute it and/or modify
|
12
|
# it under the terms of the GNU General Public License as published by
|
13
|
# the Free Software Foundation, either version 3 of the License, or
|
14
|
# (at your option) any later version.
|
15
|
#
|
16
|
# This program is distributed in the hope that it will be useful,
|
17
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
# GNU General Public License for more details.
|
20
|
#
|
21
|
# You should have received a copy of the GNU General Public License
|
22
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
23
|
#
|
24
|
# I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
25
|
# license. Although I request that you do not make use this code in a
|
26
|
# proprietary program, I am not going to enforce this in court.
|
27
|
|
28
|
from selenium import webdriver
|
29
|
from selenium.webdriver.firefox.options import Options
|
30
|
import time
|
31
|
|
32
|
from .misc_constants import *
|
33
|
|
34
|
def set_profile_proxy(profile, proxy_host, proxy_port):
|
35
|
# proxy type 1 designates "manual"
|
36
|
profile.set_preference('network.proxy.type', 1)
|
37
|
profile.set_preference('network.proxy.no_proxies_on', '')
|
38
|
profile.set_preference('network.proxy.share_proxy_settings', True)
|
39
|
|
40
|
for proto in ['http', 'ftp', 'socks', 'ssl']:
|
41
|
profile.set_preference(f'network.proxy.{proto}', proxy_host)
|
42
|
profile.set_preference(f'network.proxy.{proto}_port', proxy_port)
|
43
|
profile.set_preference(f'network.proxy.backup.{proto}', '')
|
44
|
profile.set_preference(f'network.proxy.backup.{proto}_port', 0)
|
45
|
|
46
|
def firefox_safe_mode(firefox_binary=default_firefox_binary,
|
47
|
proxy_host=default_proxy_host,
|
48
|
proxy_port=default_proxy_port):
|
49
|
profile = webdriver.FirefoxProfile()
|
50
|
set_profile_proxy(profile, proxy_host, proxy_port)
|
51
|
|
52
|
options = Options()
|
53
|
options.add_argument('--safe-mode')
|
54
|
|
55
|
return webdriver.Firefox(options=options, firefox_profile=profile,
|
56
|
firefox_binary=firefox_binary)
|
57
|
|
58
|
def firefox_with_profile(firefox_binary=default_firefox_binary,
|
59
|
profile_dir=default_clean_profile_dir,
|
60
|
proxy_host=default_proxy_host,
|
61
|
proxy_port=default_proxy_port):
|
62
|
profile = webdriver.FirefoxProfile(profile_dir)
|
63
|
set_profile_proxy(profile, proxy_host, proxy_port)
|
64
|
|
65
|
return webdriver.Firefox(firefox_profile=profile,
|
66
|
firefox_binary=firefox_binary)
|