summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/webdriver.py
blob: da3b13ed7624932637078b3c2cc4edb93b14a8d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from __future__ import annotations

import time
from platformdirs import user_config_dir
try: 
    from selenium.webdriver.remote.webdriver import WebDriver 
except ImportError: 
    class WebDriver(): 
        pass
try:
    from undetected_chromedriver import Chrome, ChromeOptions
except ImportError:
    class Chrome():
        def __init__():
            raise RuntimeError('Please install the "undetected_chromedriver" package')
    class ChromeOptions():
        def add_argument():
            pass
try:
    from pyvirtualdisplay import Display
    has_pyvirtualdisplay = True
except ImportError:
    has_pyvirtualdisplay = False

def get_browser(
    user_data_dir: str = None,
    headless: bool = False,
    proxy: str = None,
    options: ChromeOptions = None
) -> Chrome:
    if user_data_dir == None:
        user_data_dir = user_config_dir("g4f")
    if proxy:
        if not options:
            options = ChromeOptions()
        options.add_argument(f'--proxy-server={proxy}')
    return Chrome(options=options, user_data_dir=user_data_dir, headless=headless)

class WebDriverSession():
    def __init__(
        self,
        webdriver: WebDriver = None,
        user_data_dir: str = None,
        headless: bool = False,
        virtual_display: bool = False,
        proxy: str = None,
        options: ChromeOptions = None
    ):
        self.webdriver = webdriver
        self.user_data_dir = user_data_dir
        self.headless = headless
        self.virtual_display = None
        if has_pyvirtualdisplay and virtual_display:
            self.virtual_display = Display(size=(1920,1080))
        self.proxy = proxy
        self.options = options
        self.default_driver = None
    
    def reopen(
        self,
        user_data_dir: str = None,
        headless: bool = False,
        virtual_display: bool = False
    ) -> WebDriver:
        if user_data_dir == None:
            user_data_dir = self.user_data_dir
        if self.default_driver:
            self.default_driver.quit()
        if not virtual_display and self.virtual_display:
            self.virtual_display.stop()
            self.virtual_display = None
        self.default_driver = get_browser(user_data_dir, headless, self.proxy)
        return self.default_driver

    def __enter__(self) -> WebDriver:
        if self.webdriver:
            return self.webdriver
        if self.virtual_display:
            self.virtual_display.start()
        self.default_driver = get_browser(self.user_data_dir, self.headless, self.proxy, self.options)
        return self.default_driver

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.default_driver:
            try:
                self.default_driver.close()
            except:
                pass
            time.sleep(0.1)
            self.default_driver.quit()
        if self.virtual_display:
            self.virtual_display.stop()