File size: 5,087 Bytes
ffa51e6 b06af5b ffa51e6 b06af5b ffa51e6 a59cacd ffa51e6 b06af5b ffa51e6 b06af5b | 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import random
import threading
import time
from collections import defaultdict
class ProxyEntry:
__slots__ = (
'proxy', 'protocol', 'proxy_url', 'latency',
'proxy_ip', 'verified', 'fail_count', 'total_used',
)
def __init__(self, proxy, protocol, proxy_url,
latency, proxy_ip, verified):
self.proxy = proxy
self.protocol = protocol
self.proxy_url = proxy_url
self.latency = latency
self.proxy_ip = proxy_ip
self.verified = verified
self.fail_count = 0
self.total_used = 0
def to_dict(self):
return {
"proxy": self.proxy,
"protocol": self.protocol,
"proxy_url": self.proxy_url,
"latency": self.latency,
"proxy_ip": self.proxy_ip,
"verified": self.verified,
"total_used": self.total_used,
}
class ProxyPool:
def __init__(self):
self._px = []
self._um = {}
self._lk = threading.Lock()
self._rr = defaultdict(int)
self._lr = 0.0
self._rc = 0
def refresh(self, new_proxies):
with self._lk:
old = dict(self._um)
fresh, um = [], {}
for px in new_proxies:
e = ProxyEntry(
px["proxy"], px["protocol"],
px["proxy_url"], px["latency"],
px.get("proxy_ip", ""),
px.get("verified", False),
)
o = old.get(e.proxy_url)
if o:
e.total_used = o.total_used
e.fail_count = max(0, o.fail_count - 1)
fresh.append(e)
um[e.proxy_url] = e
fresh.sort(key=lambda x: x.latency)
self._px = fresh
self._um = um
self._lr = time.time()
self._rc += 1
@property
def size(self):
return len(self._px)
def _alive(self, protocol=None, verified=False):
r = [p for p in self._px if p.fail_count < 3]
if protocol:
r = [p for p in r if p.protocol == protocol]
if verified:
r = [p for p in r if p.verified]
return r
def get_round_robin(self, protocol=None, verified=False):
with self._lk:
a = self._alive(protocol, verified)
if not a:
return None
k = f"{protocol}_{verified}"
i = self._rr[k] % len(a)
self._rr[k] = i + 1
a[i].total_used += 1
return a[i]
def get_random(self, protocol=None, verified=False):
with self._lk:
a = self._alive(protocol, verified)
if not a:
return None
p = random.choice(a)
p.total_used += 1
return p
def get_fastest(self, protocol=None, verified=False):
with self._lk:
a = self._alive(protocol, verified)
if not a:
return None
a[0].total_used += 1
return a[0]
def get_least_used(self, protocol=None, verified=False):
with self._lk:
a = self._alive(protocol, verified)
if not a:
return None
p = min(a, key=lambda x: x.total_used)
p.total_used += 1
return p
def report_failure(self, url):
with self._lk:
p = self._um.get(url)
if p:
p.fail_count += 1
def report_success(self, url):
with self._lk:
p = self._um.get(url)
if p:
p.fail_count = 0
def get_all(self, protocol=None, verified=False, limit=500):
with self._lk:
return [
p.to_dict()
for p in self._alive(protocol, verified)[:limit]
]
def get_stats(self):
with self._lk:
bp = defaultdict(lambda: {"total": 0, "alive": 0})
for p in self._px:
bp[p.protocol]["total"] += 1
if p.fail_count < 3:
bp[p.protocol]["alive"] += 1
alive = sum(d["alive"] for d in bp.values())
return {
"total": len(self._px),
"alive": alive,
"by_protocol": dict(bp),
"refresh_count": self._rc,
"last_refresh_ago": round(
time.time() - self._lr, 1
) if self._lr else None,
}
def export_json(self):
with self._lk:
return [p.to_dict() for p in self._px]
def import_json(self, data):
proxies = []
for p in data:
proxies.append({
"proxy": p["proxy"],
"protocol": p["protocol"],
"proxy_url": p["proxy_url"],
"latency": p["latency"],
"proxy_ip": p.get("proxy_ip", ""),
"verified": p.get("verified", False),
})
if proxies:
self.refresh(proxies) |