|
1 |
| -import json |
2 | 1 | import logging
|
3 | 2 | import queue
|
4 | 3 | import random
|
5 | 4 | import threading
|
6 | 5 | import time
|
7 | 6 |
|
| 7 | +import chanfig |
8 | 8 | import requests
|
9 | 9 | from bs4 import BeautifulSoup
|
10 | 10 |
|
@@ -44,7 +44,12 @@ def to_dict(self):
|
44 | 44 | dict: A dict with four keys: ``addr``, ``protocol``,
|
45 | 45 | ``weight`` and ``last_checked``
|
46 | 46 | """
|
47 |
| - return dict(addr=self.addr, protocol=self.protocol, weight=self.weight, last_checked=self.last_checked) |
| 47 | + return { |
| 48 | + "addr": self.addr, |
| 49 | + "protocol": self.protocol, |
| 50 | + "weight": self.weight, |
| 51 | + "last_checked": self.last_checked, |
| 52 | + } |
48 | 53 |
|
49 | 54 |
|
50 | 55 | class ProxyPool:
|
@@ -146,17 +151,15 @@ def save(self, filename):
|
146 | 151 | for proxy in self.proxies[protocol]:
|
147 | 152 | serializable_proxy = self.proxies[protocol][proxy].to_dict()
|
148 | 153 | proxies[protocol].append(serializable_proxy)
|
149 |
| - with open(filename, "w") as fout: |
150 |
| - json.dump(proxies, fout) |
| 154 | + chanfig.save(proxies, filename) |
151 | 155 |
|
152 | 156 | def load(self, filename):
|
153 | 157 | """Load proxies from file"""
|
154 |
| - with open(filename) as fin: |
155 |
| - proxies = json.load(fin) |
156 |
| - for protocol in proxies: |
157 |
| - for proxy in proxies[protocol]: |
| 158 | + proxies = chanfig.load(filename) |
| 159 | + for protocol, protocol_proxies in proxies.items(): |
| 160 | + for proxy in protocol_proxies: |
158 | 161 | self.proxies[protocol][proxy["addr"]] = Proxy(
|
159 |
| - proxy["addr"], proxy["protocol"], proxy["weight"], proxy["last_checked"] |
| 162 | + proxy["addr"], protocol, proxy.get("weight", 1.0), proxy.get("last_checked") |
160 | 163 | )
|
161 | 164 | self.addr_list[protocol].append(proxy["addr"])
|
162 | 165 |
|
@@ -215,7 +218,7 @@ def is_valid(self, addr, protocol="http", timeout=5):
|
215 | 218 | raise
|
216 | 219 | except requests.exceptions.Timeout:
|
217 | 220 | return {"valid": False, "msg": "timeout"}
|
218 |
| - except: |
| 221 | + except BaseException: # noqa: B036 |
219 | 222 | return {"valid": False, "msg": "exception"}
|
220 | 223 | else:
|
221 | 224 | if r.status_code == 200:
|
@@ -278,20 +281,20 @@ def scan(
|
278 | 281 | t = threading.Thread(
|
279 | 282 | name=f"val-{i + 1:0>2d}",
|
280 | 283 | target=self.validate,
|
281 |
| - kwargs=dict( |
282 |
| - proxy_scanner=proxy_scanner, |
283 |
| - expected_num=expected_num, |
284 |
| - queue_timeout=queue_timeout, |
285 |
| - val_timeout=val_timeout, |
286 |
| - ), |
| 284 | + kwargs={ |
| 285 | + "proxy_scanner": proxy_scanner, |
| 286 | + "expected_num": expected_num, |
| 287 | + "queue_timeout": queue_timeout, |
| 288 | + "val_timeout": val_timeout, |
| 289 | + }, |
287 | 290 | )
|
288 | 291 | t.daemon = True
|
289 | 292 | val_threads.append(t)
|
290 | 293 | t.start()
|
291 | 294 | for t in val_threads:
|
292 | 295 | t.join()
|
293 | 296 | self.logger.info("Proxy scanning done!")
|
294 |
| - except: |
| 297 | + except BaseException: |
295 | 298 | raise
|
296 | 299 | finally:
|
297 | 300 | if out_file is not None:
|
@@ -466,18 +469,14 @@ def scan_free_proxy_list(self):
|
466 | 469 | def scan_file(self, src_file):
|
467 | 470 | """Scan candidate proxies from an existing file"""
|
468 | 471 | self.logger.info(f"start scanning file {src_file} for proxy list...")
|
469 |
| - with open(src_file) as fin: |
470 |
| - proxies = json.load(fin) |
| 472 | + proxies = chanfig.load(src_file) |
471 | 473 | for protocol in proxies.keys():
|
472 | 474 | for proxy in proxies[protocol]:
|
473 | 475 | self.proxy_queue.put({"addr": proxy["addr"], "protocol": protocol})
|
474 | 476 |
|
475 | 477 | def is_scanning(self):
|
476 | 478 | """Return whether at least one scanning thread is alive"""
|
477 |
| - for t in self.scan_threads: |
478 |
| - if t.is_alive(): |
479 |
| - return True |
480 |
| - return False |
| 479 | + return any(t.is_alive() for t in self.scan_threads) |
481 | 480 |
|
482 | 481 | def scan(self):
|
483 | 482 | """Start a thread for each registered scan function to scan proxy lists"""
|
|
0 commit comments