Voxxium commited on
Commit
b06af5b
·
verified ·
1 Parent(s): 4b2085c

Update proxy_pool.py

Browse files
Files changed (1) hide show
  1. proxy_pool.py +38 -79
proxy_pool.py CHANGED
@@ -1,7 +1,3 @@
1
- """
2
- Proxy Pool — avec merge intelligent
3
- """
4
-
5
  import random
6
  import threading
7
  import time
@@ -12,7 +8,6 @@ class ProxyEntry:
12
  __slots__ = (
13
  'proxy', 'protocol', 'proxy_url', 'latency',
14
  'proxy_ip', 'verified', 'fail_count', 'total_used',
15
- 'last_seen', 'test_count',
16
  )
17
 
18
  def __init__(self, proxy, protocol, proxy_url,
@@ -25,8 +20,6 @@ class ProxyEntry:
25
  self.verified = verified
26
  self.fail_count = 0
27
  self.total_used = 0
28
- self.last_seen = time.time()
29
- self.test_count = 1
30
 
31
  def to_dict(self):
32
  return {
@@ -37,8 +30,6 @@ class ProxyEntry:
37
  "proxy_ip": self.proxy_ip,
38
  "verified": self.verified,
39
  "total_used": self.total_used,
40
- "age_s": round(time.time() - self.last_seen, 1),
41
- "test_count": self.test_count,
42
  }
43
 
44
 
@@ -50,69 +41,28 @@ class ProxyPool:
50
  self._rr = defaultdict(int)
51
  self._lr = 0.0
52
  self._rc = 0
53
- # config
54
- self.max_age = 600 # 10 min max sans retest
55
- self.max_pool_size = 500 # pas plus de 500 proxies
56
 
57
  def refresh(self, new_proxies):
58
- """
59
- MERGE les nouveaux proxies avec les existants.
60
- - Nouveaux: ajoutés/mis à jour
61
- - Anciens pas retestés: gardés si < max_age
62
- - Très vieux: supprimés
63
- """
64
  with self._lk:
65
- now = time.time()
66
-
67
- # 1. Update/add les nouveaux
68
  for px in new_proxies:
69
- url = px["proxy_url"]
70
-
71
- if url in self._um:
72
- # existe déjà → update
73
- e = self._um[url]
74
- e.latency = px["latency"]
75
- e.proxy_ip = px.get("proxy_ip", e.proxy_ip)
76
- e.verified = px.get("verified", e.verified)
77
- e.last_seen = now
78
- e.test_count += 1
79
- e.fail_count = max(0, e.fail_count - 1)
80
- else:
81
- # nouveau
82
- e = ProxyEntry(
83
- px["proxy"], px["protocol"],
84
- px["proxy_url"], px["latency"],
85
- px.get("proxy_ip", ""),
86
- px.get("verified", False),
87
- )
88
- e.last_seen = now
89
- self._um[url] = e
90
-
91
- # 2. Nettoyer les vieux proxies
92
- to_remove = []
93
- for url, e in self._um.items():
94
- age = now - e.last_seen
95
- if age > self.max_age:
96
- to_remove.append(url)
97
-
98
- for url in to_remove:
99
- del self._um[url]
100
-
101
- # 3. Reconstruire la liste triée
102
- self._px = sorted(
103
- self._um.values(),
104
- key=lambda x: x.latency
105
- )
106
-
107
- # 4. Limiter la taille
108
- if len(self._px) > self.max_pool_size:
109
- # garder les plus rapides
110
- removed = self._px[self.max_pool_size:]
111
- self._px = self._px[:self.max_pool_size]
112
- for e in removed:
113
- del self._um[e.proxy_url]
114
-
115
- self._lr = now
116
  self._rc += 1
117
 
118
  @property
@@ -175,7 +125,6 @@ class ProxyPool:
175
  p = self._um.get(url)
176
  if p:
177
  p.fail_count = 0
178
- p.last_seen = time.time()
179
 
180
  def get_all(self, protocol=None, verified=False, limit=500):
181
  with self._lk:
@@ -186,26 +135,36 @@ class ProxyPool:
186
 
187
  def get_stats(self):
188
  with self._lk:
189
- now = time.time()
190
  bp = defaultdict(lambda: {"total": 0, "alive": 0})
191
- ages = []
192
  for p in self._px:
193
  bp[p.protocol]["total"] += 1
194
  if p.fail_count < 3:
195
  bp[p.protocol]["alive"] += 1
196
- ages.append(now - p.last_seen)
197
-
198
  alive = sum(d["alive"] for d in bp.values())
199
- avg_age = round(sum(ages) / len(ages), 1) if ages else 0
200
-
201
  return {
202
  "total": len(self._px),
203
  "alive": alive,
204
  "by_protocol": dict(bp),
205
- "avg_age_s": avg_age,
206
- "max_age_config": self.max_age,
207
  "refresh_count": self._rc,
208
  "last_refresh_ago": round(
209
- now - self._lr, 1
210
  ) if self._lr else None,
211
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import random
2
  import threading
3
  import time
 
8
  __slots__ = (
9
  'proxy', 'protocol', 'proxy_url', 'latency',
10
  'proxy_ip', 'verified', 'fail_count', 'total_used',
 
11
  )
12
 
13
  def __init__(self, proxy, protocol, proxy_url,
 
20
  self.verified = verified
21
  self.fail_count = 0
22
  self.total_used = 0
 
 
23
 
24
  def to_dict(self):
25
  return {
 
30
  "proxy_ip": self.proxy_ip,
31
  "verified": self.verified,
32
  "total_used": self.total_used,
 
 
33
  }
34
 
35
 
 
41
  self._rr = defaultdict(int)
42
  self._lr = 0.0
43
  self._rc = 0
 
 
 
44
 
45
  def refresh(self, new_proxies):
 
 
 
 
 
 
46
  with self._lk:
47
+ old = dict(self._um)
48
+ fresh, um = [], {}
 
49
  for px in new_proxies:
50
+ e = ProxyEntry(
51
+ px["proxy"], px["protocol"],
52
+ px["proxy_url"], px["latency"],
53
+ px.get("proxy_ip", ""),
54
+ px.get("verified", False),
55
+ )
56
+ o = old.get(e.proxy_url)
57
+ if o:
58
+ e.total_used = o.total_used
59
+ e.fail_count = max(0, o.fail_count - 1)
60
+ fresh.append(e)
61
+ um[e.proxy_url] = e
62
+ fresh.sort(key=lambda x: x.latency)
63
+ self._px = fresh
64
+ self._um = um
65
+ self._lr = time.time()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  self._rc += 1
67
 
68
  @property
 
125
  p = self._um.get(url)
126
  if p:
127
  p.fail_count = 0
 
128
 
129
  def get_all(self, protocol=None, verified=False, limit=500):
130
  with self._lk:
 
135
 
136
  def get_stats(self):
137
  with self._lk:
 
138
  bp = defaultdict(lambda: {"total": 0, "alive": 0})
 
139
  for p in self._px:
140
  bp[p.protocol]["total"] += 1
141
  if p.fail_count < 3:
142
  bp[p.protocol]["alive"] += 1
 
 
143
  alive = sum(d["alive"] for d in bp.values())
 
 
144
  return {
145
  "total": len(self._px),
146
  "alive": alive,
147
  "by_protocol": dict(bp),
 
 
148
  "refresh_count": self._rc,
149
  "last_refresh_ago": round(
150
+ time.time() - self._lr, 1
151
  ) if self._lr else None,
152
+ }
153
+
154
+ def export_json(self):
155
+ with self._lk:
156
+ return [p.to_dict() for p in self._px]
157
+
158
+ def import_json(self, data):
159
+ proxies = []
160
+ for p in data:
161
+ proxies.append({
162
+ "proxy": p["proxy"],
163
+ "protocol": p["protocol"],
164
+ "proxy_url": p["proxy_url"],
165
+ "latency": p["latency"],
166
+ "proxy_ip": p.get("proxy_ip", ""),
167
+ "verified": p.get("verified", False),
168
+ })
169
+ if proxies:
170
+ self.refresh(proxies)