File size: 2,818 Bytes
f4d52c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from selenium import webdriver
import urllib.request
import os
import logging

def create_driver(profile=0):
    # Options to make it more human-like
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_argument("--disable-notifications")
    options.add_argument("--disable-popup-blocking")
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-gpu")
    options.add_argument("--disable-infobars")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--no-sandbox")

    # May be required to change this later on
    options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")


    user_data_dir = os.path.abspath(os.getcwd())+"/commands/profile"
    if profile:
        options.add_argument(f"user-data-dir={user_data_dir}")
    prefs = {"credentials_enable_service": False,
             "profile.password_manager_enabled": False}
    options.add_experimental_option("prefs", prefs)
    options.add_argument("--headless")

    # to supress the error messages/logs?
    options.add_argument("--log-level=3")
    options.add_experimental_option("excludeSwitches", ["enable-logging"])
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    
    # Not recommended to change from default value (decides how the page is loaded)
    options.page_load_strategy = 'normal' 

    # Remove logs from console
    selenium_logger = logging.getLogger('selenium')
    selenium_logger.setLevel(logging.ERROR)

    # Create driver instance (using service is not required)
    driver = webdriver.Chrome(options=options)
    return driver

def create_url_headers(tempImg, site=0):
    opener = urllib.request.build_opener()
    if not site:
        site = tempImg
    opener.addheaders = [
            ('Accept', 'application/json, text/javascript, */*; q=0.01'),
            ('X-Requested-With', 'XMLHttpRequest'),
            ('Referer', f'{site}'),
            ('Host', f'https//{tempImg.split("/")[2]}'),
            ('Content-Type', 'application/json; charset=UTF-8'),
            ('Connection', 'keep-alive'),
            ('user-agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36')
        ]
    return opener

def tab_handler(driver, image=0):
    if image:
        tempImg = image.get_attribute("href")
        driver.execute_script("window.open('');")
        driver.switch_to.window(driver.window_handles[1])
        driver.get(f"{tempImg}")
        return driver, tempImg

    driver.close()
    driver.switch_to.window(driver.window_handles[0])
    return driver