lexicalspace commited on
Commit
9a6119e
Β·
verified Β·
1 Parent(s): bb783fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -34
app.py CHANGED
@@ -4,13 +4,13 @@ import threading
4
  import time
5
  import random
6
  from datetime import datetime
7
-
8
  # ==============================================================================
9
  # βš™οΈ CONFIGURATION
10
  # ==============================================================================
11
- TARGET_COUNT = 10 # We want exactly 10 working proxies
12
  TIMEOUT_SEC = 5 # Reject if slower than 5 seconds
13
- CHECK_INTERVAL = 300 # Re-check every 5 minutes (300 seconds)
14
 
15
  # Shared Memory (The "Live File")
16
  proxy_storage = {
@@ -22,33 +22,38 @@ proxy_storage = {
22
  # πŸ•΅οΈβ€β™‚οΈ PROXY WORKER (Background Thread)
23
  # ==============================================================================
24
  def check_proxy(ip):
25
- """Returns True if proxy connects to YouTube in < 5 seconds."""
26
  proxies = {"http": f"http://{ip}", "https": f"http://{ip}"}
27
  try:
28
- # We test against YouTube specifically because that's our target
29
  r = requests.get("https://www.youtube.com", proxies=proxies, timeout=TIMEOUT_SEC)
30
  if r.status_code == 200:
31
- return True
32
  except:
33
- return False
34
- return False
35
 
36
  def worker_loop():
37
  while True:
38
- print(f"\n[{datetime.now().strftime('%H:%M')}] ♻️ Starting Validation Cycle...")
39
 
40
- # 1. RE-VALIDATE EXISTING (Keep the good ones)
41
  current_list = proxy_storage["valid_proxies"]
42
  still_good = []
43
- for ip in current_list:
44
- if check_proxy(ip):
45
- still_good.append(ip)
46
- else:
47
- print(f" ❌ Dropped dead proxy: {ip}")
48
-
49
- proxy_storage["valid_proxies"] = still_good
50
 
51
- # 2. FILL THE POOL (If we have less than 10)
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  if len(proxy_storage["valid_proxies"]) < TARGET_COUNT:
53
  needed = TARGET_COUNT - len(proxy_storage["valid_proxies"])
54
  print(f" πŸ“‰ Pool low. Need {needed} more. Fetching fresh list...")
@@ -63,28 +68,42 @@ def worker_loop():
63
  for s in sources:
64
  try:
65
  r = requests.get(s, timeout=10)
66
- raw_proxies += r.text.strip().split("\n")
 
67
  except: pass
68
 
69
- # Shuffle to get random ones
 
70
  random.shuffle(raw_proxies)
71
-
72
- # Test until we hit 10
73
- for ip in raw_proxies:
74
- ip = ip.strip()
75
- if not ip or ip in proxy_storage["valid_proxies"]: continue
76
-
77
- # Stop if we hit our target
78
- if len(proxy_storage["valid_proxies"]) >= TARGET_COUNT:
79
- break
 
 
80
 
81
- print(f" Testing {ip}...", end="\r")
82
- if check_proxy(ip):
83
- print(f" βœ… FOUND NEW: {ip}")
84
- proxy_storage["valid_proxies"].append(ip)
85
 
 
 
 
 
 
 
 
 
 
 
 
86
  proxy_storage["last_updated"] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
87
- print(f" πŸ’€ Sleeping for {CHECK_INTERVAL/60} mins. Current Pool: {len(proxy_storage['valid_proxies'])}")
88
 
89
  # 3. SLEEP
90
  time.sleep(CHECK_INTERVAL)
 
4
  import time
5
  import random
6
  from datetime import datetime
7
+ from concurrent.futures import ThreadPoolExecutor, as_completed
8
  # ==============================================================================
9
  # βš™οΈ CONFIGURATION
10
  # ==============================================================================
11
+ TARGET_COUNT = 80 # We want exactly 10 working proxies
12
  TIMEOUT_SEC = 5 # Reject if slower than 5 seconds
13
+ CHECK_INTERVAL = 120 # Re-check every 5 minutes (300 seconds)
14
 
15
  # Shared Memory (The "Live File")
16
  proxy_storage = {
 
22
  # πŸ•΅οΈβ€β™‚οΈ PROXY WORKER (Background Thread)
23
  # ==============================================================================
24
  def check_proxy(ip):
25
+ """Returns the IP if valid, else None."""
26
  proxies = {"http": f"http://{ip}", "https": f"http://{ip}"}
27
  try:
 
28
  r = requests.get("https://www.youtube.com", proxies=proxies, timeout=TIMEOUT_SEC)
29
  if r.status_code == 200:
30
+ return ip
31
  except:
32
+ pass
33
+ return None
34
 
35
  def worker_loop():
36
  while True:
37
+ print(f"\n[{datetime.now().strftime('%H:%M:%S')}] ♻️ Starting High-Speed Validation...")
38
 
39
+ # 1. RE-VALIDATE EXISTING (In Parallel)
40
  current_list = proxy_storage["valid_proxies"]
41
  still_good = []
 
 
 
 
 
 
 
42
 
43
+ if current_list:
44
+ print(f" πŸ”Ž Re-checking {len(current_list)} active proxies...")
45
+ with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
46
+ # Submit all checks at once
47
+ future_to_ip = {executor.submit(check_proxy, ip): ip for ip in current_list}
48
+ for future in as_completed(future_to_ip):
49
+ result = future.result()
50
+ if result:
51
+ still_good.append(result)
52
+
53
+ print(f" βœ… Retained {len(still_good)} working proxies.")
54
+ proxy_storage["valid_proxies"] = still_good
55
+
56
+ # 2. FILL THE POOL (If low)
57
  if len(proxy_storage["valid_proxies"]) < TARGET_COUNT:
58
  needed = TARGET_COUNT - len(proxy_storage["valid_proxies"])
59
  print(f" πŸ“‰ Pool low. Need {needed} more. Fetching fresh list...")
 
68
  for s in sources:
69
  try:
70
  r = requests.get(s, timeout=10)
71
+ if r.status_code == 200:
72
+ raw_proxies += r.text.strip().split("\n")
73
  except: pass
74
 
75
+ # Shuffle and Clean
76
+ raw_proxies = list(set(raw_proxies)) # Remove duplicates
77
  random.shuffle(raw_proxies)
78
+ print(f" ⚑ Loaded {len(raw_proxies)} raw candidates. Checking in parallel...")
79
+
80
+ # Mass Check in Batches
81
+ with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
82
+ # Submit a large batch to the executor
83
+ futures = []
84
+ for ip in raw_proxies:
85
+ ip = ip.strip()
86
+ if not ip or ip in proxy_storage["valid_proxies"]: continue
87
+
88
+ futures.append(executor.submit(check_proxy, ip))
89
 
90
+ # Optimization: Don't queue 10,000 tasks. Queue enough to fill workers + buffer
91
+ if len(futures) >= needed * 5:
92
+ break
 
93
 
94
+ # Process results as they finish (Real-time filling)
95
+ for future in as_completed(futures):
96
+ # Stop early if we hit the target while checking
97
+ if len(proxy_storage["valid_proxies"]) >= TARGET_COUNT:
98
+ break
99
+
100
+ result = future.result()
101
+ if result:
102
+ print(f" βœ… FOUND NEW: {result}")
103
+ proxy_storage["valid_proxies"].append(result)
104
+
105
  proxy_storage["last_updated"] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
106
+ print(f" πŸ’€ Sleeping for {CHECK_INTERVAL}s. Current Pool: {len(proxy_storage['valid_proxies'])}")
107
 
108
  # 3. SLEEP
109
  time.sleep(CHECK_INTERVAL)