File size: 4,879 Bytes
3301039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# proxy_checker.py (Final Recommended Version)

import requests
import concurrent.futures
from tqdm import tqdm # Import tqdm for the progress bar

# --- CONFIGURATION ---
PROXY_SOURCES = [
    'https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt',
    'https://api.proxyscrape.com/v4/free-proxy-list/get?request=display_proxies&proxy_format=protocolipport&format=text',
    'https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks5.txt',
    'https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks4.txt',
    'https://raw.githubusercontent.com/jetkai/proxy-list/main/online-proxies/txt/proxies-https.txt',
    'https://raw.githubusercontent.com/jetkai/proxy-list/main/online-proxies/txt/proxies-http.txt',
]

OUTPUT_FILE = 'working_proxies.txt'
TEST_URL = 'https://httpbin.org/ip' # Using HTTPS is a slightly better test

# --- TUNING PARAMETERS ---
TIMEOUT = 5 # Don't wait more than 5 seconds for a proxy.
MAX_WORKERS = 100 # Increased for faster checking, adjust based on your network.

# --- IMPROVEMENT: Add a common browser User-Agent ---
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

def fetch_proxies():
    """Fetches proxies from all sources and returns a unique list."""
    all_proxies = set()
    for url in PROXY_SOURCES:
        try:
            print(f"Fetching proxies from: {url}")
            response = requests.get(url, timeout=15, headers=HEADERS)
            response.raise_for_status()
            
            proxies = response.text.strip().split('\n')
            
            # Determine protocol based on URL content if not explicitly provided
            protocol = 'http' # Default
            if 'socks5' in url: protocol = 'socks5'
            elif 'socks4' in url: protocol = 'socks4'
            elif 'https' in url: protocol = 'https'

            for p in proxies:
                p = p.strip()
                if not p: continue
                # Handle proxies that already include the protocol
                if '://' in p:
                    all_proxies.add(p)
                else:
                    all_proxies.add(f"{protocol}://{p}")
        except Exception as e:
            print(f"Failed to fetch or parse from {url}: {e}")
    return list(all_proxies)

def check_proxy(proxy):
    """

    Checks a single proxy. Returns the proxy string if working, else None.

    CRITICAL: Requires 'pip install requests[socks]' to test SOCKS proxies.

    """
    proxy = proxy.strip()
    proxies = {'http': proxy, 'https': proxy}
    try:
        # Make the request using the proxy and the custom user-agent
        response = requests.get(TEST_URL, proxies=proxies, timeout=TIMEOUT, headers=HEADERS)
        if response.status_code == 200:
            return proxy # Success
    except Exception:
        pass # Any exception means the proxy has failed
    return None

def main():
    """Main function to orchestrate fetching, checking, and saving."""
    print("--- Starting Proxy Fetch and Check ---")
    
    # CRITICAL: Remind user to install SOCKS support
    try:
        import socks
    except ImportError:
        print("\n[WARNING] PySocks is not installed. SOCKS proxies cannot be checked.")
        print("Please run: pip install 'requests[socks]'\n")

    proxies_to_check = fetch_proxies()
    if not proxies_to_check:
        print("Could not fetch any proxies. Exiting.")
        return

    print(f"\nFetched {len(proxies_to_check)} unique proxies. Now testing with a {TIMEOUT}s timeout...")
    working_proxies = []
    
    # Using ThreadPoolExecutor for concurrent checking
    with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
        # Create a future for each proxy check
        future_to_proxy = {executor.submit(check_proxy, p): p for p in proxies_to_check}
        
        # Use tqdm to create a progress bar
        for future in tqdm(concurrent.futures.as_completed(future_to_proxy), total=len(proxies_to_check), desc="Checking Proxies"):
            result = future.result()
            if result:
                working_proxies.append(result)

    print(f"\n--- Check Complete ---")
    print(f"Found {len(working_proxies)} working proxies.")

    if working_proxies:
        with open(OUTPUT_FILE, 'w') as f:
            for proxy in working_proxies:
                f.write(f"{proxy}\n")
        print(f"Successfully saved working proxies to '{OUTPUT_FILE}'.")
        print("You can now copy the contents of this file into the PROXY_LIST in scraper.py")
    else:
        print("\nWARNING: No working proxies were found. The parallel scraper will be ineffective.")

if __name__ == "__main__":
    main()