Spaces:
Paused
Paused
Update app.py
Browse files
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 =
|
| 12 |
TIMEOUT_SEC = 5 # Reject if slower than 5 seconds
|
| 13 |
-
CHECK_INTERVAL =
|
| 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
|
| 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
|
| 32 |
except:
|
| 33 |
-
|
| 34 |
-
return
|
| 35 |
|
| 36 |
def worker_loop():
|
| 37 |
while True:
|
| 38 |
-
print(f"\n[{datetime.now().strftime('%H:%M')}] β»οΈ Starting
|
| 39 |
|
| 40 |
-
# 1. RE-VALIDATE EXISTING (
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 67 |
except: pass
|
| 68 |
|
| 69 |
-
# Shuffle
|
|
|
|
| 70 |
random.shuffle(raw_proxies)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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
|
| 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)
|