1
+ from seleniumwire import webdriver
2
+ from selenium .webdriver .chrome .options import Options
3
+ from selenium .webdriver .support .ui import WebDriverWait
4
+ from selenium .webdriver .support import expected_conditions as EC
5
+ from webdriver_manager .chrome import ChromeDriverManager
6
+ from selenium .webdriver .chrome .service import Service
7
+ from selenium .webdriver .common .by import By
8
+ from selenium .common .exceptions import NoSuchElementException , TimeoutException
9
+ import chromedriver_autoinstaller
10
+ import os
11
+ import re
12
+ import random
13
+ import json
14
+ from os .path import isfile
15
+ import sys
16
+ from clear_screen import clear
17
+
18
+ # Function to load settings from a JSON file
19
+ def load_settings ():
20
+ # Check if proxy file exists
21
+ if not isfile ("proxy.txt" ):
22
+ # If not, create one with default settings
23
+ with open ("proxy.txt" , 'w' ) as proxy_file :
24
+ proxy_file .write ("DEFAULT_PROXY" )
25
+ print ("Proxy file created." )
26
+ # Return the settings
27
+ return
28
+
29
+ # Function to load proxies from a file
30
+ def load_proxies (file_path ):
31
+ with open (file_path , 'r' ) as proxy_file :
32
+ proxies = proxy_file .readlines ()
33
+ return proxies
34
+
35
+ def initialize_driver (proxy ):
36
+ chromedriver_autoinstaller .install ()
37
+
38
+ # Setup ChromeOptions
39
+ options = webdriver .ChromeOptions ()
40
+ options .add_argument ('--window-size=1920,1080' )
41
+ options .add_argument ('--headless' )
42
+ options .add_argument ('--no-sandbox' )
43
+ options .add_argument ('--disable-gpu' )
44
+ options .add_argument ('--disable-extensions' )
45
+ options .add_argument ('--disable-dev-shm-usage' )
46
+ options .add_argument ('--disable-blink-features=AutomationControlled' )
47
+ options .add_argument ('--ignore-certificate-errors' )
48
+ options .add_argument ('--ignore-ssl-errors' )
49
+ options .add_argument ('--disable-dev-shm-usage' )
50
+ options .add_argument ("--disable-logging" )
51
+ options .add_argument ("--log-level=3" )
52
+ options .add_experimental_option ("excludeSwitches" , ["enable-logging" ])
53
+
54
+ # Add arguments to make headless browser appear as regular one
55
+ options .add_argument ("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" )
56
+ options .add_argument ("--start-maximized" )
57
+ options .add_argument ("--disable-blink-features" )
58
+ options .add_argument ("--disable-blink-features=AutomationControlled" )
59
+
60
+ # Split the proxy string into components
61
+ proxy_parts = proxy .split (':' )
62
+ formatted_proxy = f'{ proxy_parts [2 ]} :{ proxy_parts [3 ]} @{ proxy_parts [0 ]} :{ proxy_parts [1 ]} '
63
+
64
+ # Proxy setup
65
+ proxy_options = {
66
+ 'proxy' : {
67
+ 'https' : f'https://{ formatted_proxy } ' ,
68
+ }
69
+ }
70
+
71
+ # Initialize driver
72
+ driver = webdriver .Chrome (seleniumwire_options = proxy_options , options = options )
73
+ clear ()
74
+ return driver
75
+
76
+ def remove_proxy (file_path , proxy ):
77
+ with open (file_path , 'r' ) as f :
78
+ proxies = f .readlines ()
79
+ with open (file_path , 'w' ) as f :
80
+ for p in proxies :
81
+ if p .strip () != proxy :
82
+ f .write (p )
83
+
84
+ # Initialize driver to None before the try block
85
+ driver = None
86
+
87
+ if __name__ == "__main__" :
88
+ try :
89
+ if len (sys .argv ) > 1 :
90
+ path = sys .argv [1 ]
91
+ else :
92
+ path = str (input ("Please enter a filename: " ))
93
+
94
+ # Check if the file exists in the same directory
95
+ if not os .path .isfile (os .path .join (os .path .dirname (__file__ ), path )):
96
+ print ("File not found in the current directory!" )
97
+ os ._exit (1 )
98
+
99
+ # Load settings
100
+ settings = load_settings ()
101
+
102
+ # Load proxies if use_proxy is True
103
+ proxies = load_proxies ("proxy.txt" )
104
+ proxy = random .choice (proxies ).strip ()
105
+
106
+ if (proxy == "DEFAULT_PROXY" ):
107
+ print ("No proxy found in proxy.txt! Please add a proxy." )
108
+ os ._exit (1 )
109
+
110
+ # Initialize driver
111
+ driver = initialize_driver (proxy )
112
+ wait = WebDriverWait (driver , 60 )
113
+ wait_Error = WebDriverWait (driver , 1 )
114
+
115
+ driver .get ("https://tineye.com" )
116
+
117
+ try :
118
+ upload = wait .until (EC .presence_of_element_located ((By .XPATH , '//*[@id="upload_box"]' )))
119
+ upload .send_keys (os .path .join (os .path .dirname (__file__ ), path ))
120
+ except TimeoutException :
121
+ print ("Upload button not found!" )
122
+ driver .quit ()
123
+ os ._exit (1 )
124
+
125
+ try :
126
+ results = wait .until (EC .presence_of_element_located ((By .XPATH , '//*[@id="result_count"]' )))
127
+ results_text = results .text .replace (',' , '' ) # Remove commas
128
+ num_results = re .search (r'\d+' , results_text ).group ()
129
+ print ("Proxy: " , proxy )
130
+ print ("Results: " , num_results )
131
+ print ("Search url: " , driver .current_url )
132
+ except TimeoutException :
133
+ print ("Results not found" )
134
+ driver .quit ()
135
+
136
+ try :
137
+ os .remove (os .path .join (os .path .dirname (__file__ ), path ))
138
+ except :
139
+ print ("Error deleting file!" )
140
+ driver .quit ()
141
+ os ._exit (1 )
142
+
143
+ except Exception as e :
144
+ print (f"An error occurred: { e } !" )
145
+ finally :
146
+ # Check if driver is not None before calling quit
147
+ if driver is not None :
148
+ driver .quit ()
0 commit comments