File size: 4,868 Bytes
f4d52c1
 
 
 
 
 
 
 
 
 
 
 
 
 
5db5fe3
f4d52c1
 
 
 
 
 
 
 
 
 
 
 
 
 
5db5fe3
f4d52c1
 
 
 
 
 
5db5fe3
f4d52c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5db5fe3
 
 
 
 
 
f4d52c1
5db5fe3
f4d52c1
 
5db5fe3
f4d52c1
5db5fe3
 
 
 
 
 
 
 
 
 
 
 
f4d52c1
 
5db5fe3
f4d52c1
5db5fe3
 
 
 
 
 
 
 
f4d52c1
 
5db5fe3
f4d52c1
 
 
 
 
5db5fe3
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

def searchQuery(user_search, driver, elem, mode=0, score="", isLoggedIn=True):
    if isLoggedIn == False:
        anchors = driver.find_elements(By.XPATH, '//*[@class="sc-93qi7v-2 hbGpVM"]//a')
        for n_iter,anchor in enumerate(anchors):
            if anchor.get_attribute("lang") =="en":
                driver.execute_script("arguments[0].click();", anchor)
        
    user_search = user_search.lower()
    WebDriverWait(driver, timeout=15).until(EC.presence_of_element_located((By.XPATH, elem)))
    search_bar = driver.find_element(By.XPATH, elem)

    try:
        search_bar.click()
        if not mode:
            time.sleep(1.8)
        driver.execute_script('arguments[0].value=arguments[1]', search_bar, user_search)
    except:
        time.sleep(3)
        driver.execute_script('arguments[0].value=arguments[1]', search_bar, user_search)
        
    if not mode:
        specific_Query(driver=driver, search_bar=search_bar, user_search=user_search)
    else:
        time.sleep(1)
        search_bar.send_keys(Keys.ARROW_DOWN)
        time.sleep(2)
    
    if mode:
        search_bar.send_keys(Keys.ARROW_DOWN)
        time.sleep(1.2)
        search_bar.send_keys(Keys.ENTER)
    time.sleep(0.3)

    try:
        driver.execute_script("arguments[0].value+=arguments[1]", search_bar, score)
        search_bar.send_keys(Keys.ENTER)
    except:
        pass


def save_Search(driver, mode=0):
    try:
        with open("./commands/url.txt", "r+") as file:
            lines = file.readlines()

            line_to_modify = mode + 1  # Line number to modify based on mode
            new_line_content = driver.current_url  # Content to write to the line

            if line_to_modify > len(lines):
                lines.extend(['\n'] * (line_to_modify - len(lines)))
            elif lines[line_to_modify - 1].strip() == new_line_content:
                # If the line already has the same content, no action is needed
                return

            lines[line_to_modify - 1] = new_line_content + '\n'

            file.seek(0)
            file.writelines(lines)
            file.truncate()
    except:
        line_to_modify = mode + 1  # Line number to modify based on mode
        new_line_content = driver.current_url  # Content to write to the line

        with open("./commands/url.txt", "w") as file:
            lines = ['\n'] * line_to_modify
            lines[line_to_modify - 1] = new_line_content + '\n'
            file.writelines(lines)



def continue_Search(driver, link, mode=0):
    try:
        with open("./commands/url.txt", "r") as file:
            lines = file.readlines()

        line_to_read = mode + 1  # Line number to read based on mode

        if len(lines) >= line_to_read and lines[line_to_read - 1].strip() != '':
            url = lines[line_to_read - 1].strip()
            driver.get(url)
        else:
            driver.get(link)
    except:
        driver.get(link)


def specific_Query(driver, search_bar, user_search):
    # Replace underscores with spaces and append the full search term
    user_search_words = user_search.replace("_", " ").split()
    user_search_words.append(''.join(user_search_words))
    full_query_found = False
    query_to_click = None

    try:
        for user_word in user_search_words:
            time.sleep(1.2)
            driver.execute_script('arguments[0].value=arguments[1]', search_bar, user_word)
            time.sleep(1)
            search_bar.send_keys(Keys.SPACE)
            time.sleep(2.5)

            search_bar_queries = driver.find_elements(By.XPATH, '//*[@class="sc-1974j38-2 kjWkkt"]//a')
            for query in search_bar_queries:
                query_text = query.find_element(By.XPATH, './/div').text.lower()
                if len(query.find_elements(By.XPATH, './/div//div[2]')) > 0:
                    query_text = query.find_element(By.XPATH, './/div//div[2]').text.lower()

                if all(word in query_text for word in user_search_words[:-1]):
                    query_to_click = query
                    full_query_found = True
                    time.sleep(0.3)
                    break

            if full_query_found:
                break

    except Exception as e:
        pass

    if full_query_found:
        driver.execute_script("arguments[0].click();", query_to_click)
    else:
        # If full query not found, just press Enter
        search_bar.send_keys(Keys.ENTER)


def contains_works(driver, elem):
    try:
        WebDriverWait(driver, timeout=9).until(EC.presence_of_element_located((By.XPATH, elem)))
        return True
    except:
        return False