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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import json
import logging
import queue
import random
import threading
import time

import requests
from bs4 import BeautifulSoup


class Proxy:
    """Proxy class

    Attributes:
        addr (str): A string with IP and port, for example '123.123.123.123:8080'
        protocol (str): 'http' or 'https'
        weight (float): A float point number indicating the probability of being
            selected, the weight is based on the connection time and stability
        last_checked (time): A UNIX timestamp indicating when the proxy was checked
    """

    def __init__(self, addr=None, protocol="http", weight=1.0, last_checked=None):
        self.addr = addr
        self.protocol = protocol
        self.weight = weight
        if last_checked is None:
            self.last_checked = int(time.time())
        else:
            self.last_checked = last_checked

    def format(self):
        """Return the proxy compatible with requests.Session parameters

        Returns:
            dict: A dict like {'http': '123.123.123.123:8080'}
        """
        return {self.protocol: self.addr}

    def to_dict(self):
        """convert detailed proxy info into a dict

        Returns:
            dict: A dict with four keys: ``addr``, ``protocol``,
                  ``weight`` and ``last_checked``
        """
        return dict(addr=self.addr, protocol=self.protocol, weight=self.weight, last_checked=self.last_checked)


class ProxyPool:
    """Proxy pool class

    ProxyPool provides friendly apis to manage proxies.

    Attributes:
        idx (dict): Index for http proxy list and https proxy list.
        test_url (dict): A dict containing two urls, when testing if a proxy
            is valid, test_url['http'] and test_url['https'] will be used
            according to the protocol.
        proxies (dict): All the http and https proxies.
        addr_list (dict): Address of proxies.
        dec_ratio (float): When decreasing the weight of some proxy, its weight
            is multiplied with `dec_ratio`.
        inc_ratio (float): Similar to `dec_ratio` but used for increasing
            weights, default the reciprocal of `dec_ratio`.
        weight_thr (float): The minimum weight of a valid proxy, if the weight
            of a proxy is lower than `weight_thr`, it will be removed.
        logger (Logger): A logging.Logger object used for logging.
    """

    def __init__(self, filename=None):
        """Init the pool from a json file.

        Args:
            filename (str, optional): if the filename is provided, proxies
                will be load from it.

        """
        self.idx = {"http": 0, "https": 0}
        self.test_url = {"http": "http://www.sina.com.cn", "https": "https://www.taobao.com"}
        self.proxies = {"http": {}, "https": {}}
        self.addr_list = {"http": [], "https": []}
        self.dec_ratio = 0.9
        self.inc_ratio = 1 / self.dec_ratio
        self.weight_thr = 0.2
        self.logger = logging.getLogger(__name__)
        if filename is not None:
            self.load(filename)

    def proxy_num(self, protocol=None):
        """Get the number of proxies in the pool

        Args:
            protocol (str, optional): 'http' or 'https' or None. (default None)

        Returns:
            If protocol is None, return the total number of proxies, otherwise,
            return the number of proxies of corresponding protocol.
        """
        http_num = len(self.proxies["http"])
        https_num = len(self.proxies["https"])
        if protocol == "http":
            return http_num
        elif protocol == "https":
            return https_num
        else:
            return http_num + https_num

    def get_next(self, protocol="http", format=False, policy="loop"):
        """Get the next proxy

        Args:
            protocol (str): 'http' or 'https'. (default 'http')
            format (bool): Whether to format the proxy. (default False)
            policy (str): Either 'loop' or 'random', indicating the policy of
                getting the next proxy. If set to 'loop', will return proxies
                in turn, otherwise will return a proxy randomly.

        Returns:
            Proxy or dict: If format is true, then return the formatted proxy
                which is compatible with requests.Session parameters,
                otherwise a Proxy object.
        """
        if not self.proxies[protocol]:
            return None
        if policy == "loop":
            idx = self.idx[protocol]
            self.idx[protocol] = (idx + 1) % len(self.proxies[protocol])
        elif policy == "random":
            idx = random.randint(0, self.proxy_num(protocol) - 1)
        else:
            self.logger.error(f"Unsupported get_next policy: {policy}")
            exit()
        proxy = self.proxies[protocol][self.addr_list[protocol][idx]]
        if proxy.weight < random.random():
            return self.get_next(protocol, format, policy)
        if format:
            return proxy.format()
        else:
            return proxy

    def save(self, filename):
        """Save proxies to file"""
        proxies = {"http": [], "https": []}
        for protocol in ["http", "https"]:
            for proxy in self.proxies[protocol]:
                serializable_proxy = self.proxies[protocol][proxy].to_dict()
                proxies[protocol].append(serializable_proxy)
        with open(filename, "w") as fout:
            json.dump(proxies, fout)

    def load(self, filename):
        """Load proxies from file"""
        with open(filename) as fin:
            proxies = json.load(fin)
        for protocol in proxies:
            for proxy in proxies[protocol]:
                self.proxies[protocol][proxy["addr"]] = Proxy(
                    proxy["addr"], proxy["protocol"], proxy["weight"], proxy["last_checked"]
                )
                self.addr_list[protocol].append(proxy["addr"])

    def add_proxy(self, proxy):
        """Add a valid proxy into pool

        You must call `add_proxy` method to add a proxy into pool instead of
        directly operate the `proxies` variable.
        """
        protocol = proxy.protocol
        addr = proxy.addr
        if addr in self.proxies:
            self.proxies[protocol][addr].last_checked = proxy.last_checked
        else:
            self.proxies[protocol][addr] = proxy
            self.addr_list[protocol].append(addr)

    def remove_proxy(self, proxy):
        """Remove a proxy out of the pool"""
        del self.search_flag[proxy.protocol][proxy.addr]
        del self.addr_list[proxy.protocol][proxy.addr]

    def increase_weight(self, proxy):
        """Increase the weight of a proxy by multiplying inc_ratio"""
        new_weight = proxy.weight * self.inc_ratio
        if new_weight < 1.0:
            proxy.weight = new_weight
        else:
            proxy.weight = 1.0

    def decrease_weight(self, proxy):
        """Decreasing the weight of a proxy by multiplying dec_ratio"""
        new_weight = proxy.weight * self.dec_ratio
        if new_weight < self.weight_thr:
            self.remove_proxy(proxy)
        else:
            proxy.weight = new_weight

    def is_valid(self, addr, protocol="http", timeout=5):
        """Check if a proxy is valid

        Args:
            addr: A string in the form of 'ip:port'
            protocol: Either 'http' or 'https', different test urls will be used
                      according to protocol.
            timeout: A integer indicating the timeout of connecting the test url.

        Returns:
            dict: If the proxy is valid, returns {'valid': True, 'response_time': xx}
                otherwise returns {'valid': False, 'msg': 'xxxxxx'}.
        """
        start = time.time()
        try:
            r = requests.get(self.test_url[protocol], timeout=timeout, proxies={protocol: "http://" + addr})
        except KeyboardInterrupt:
            raise
        except requests.exceptions.Timeout:
            return {"valid": False, "msg": "timeout"}
        except:
            return {"valid": False, "msg": "exception"}
        else:
            if r.status_code == 200:
                response_time = time.time() - start
                return {"valid": True, "response_time": response_time}
            else:
                return {"valid": False, "msg": f"status code: {r.status_code}"}

    def validate(self, proxy_scanner, expected_num=20, queue_timeout=3, val_timeout=5):
        """Target function of validation threads

        Args:
            proxy_scanner: A ProxyScanner object.
            expected_num: Max number of valid proxies to be scanned.
            queue_timeout: Timeout for getting a proxy from the queue.
            val_timeout: An integer passed to `is_valid` as argument `timeout`.
        """
        while self.proxy_num() < expected_num:
            try:
                candidate_proxy = proxy_scanner.proxy_queue.get(timeout=queue_timeout)
            except queue.Empty:
                if proxy_scanner.is_scanning():
                    continue
                else:
                    break
            addr = candidate_proxy["addr"]
            protocol = candidate_proxy["protocol"]
            ret = self.is_valid(addr, protocol, val_timeout)
            if self.proxy_num() >= expected_num:
                self.logger.info(f"Enough valid proxies, thread {threading.current_thread().name} exit.")
                break
            if ret["valid"]:
                self.add_proxy(Proxy(addr, protocol))
                self.logger.info("{} ok, {:.2f}s".format(addr, ret["response_time"]))
            else:
                self.logger.info("{} invalid, {}".format(addr, ret["msg"]))

    def scan(
        self, proxy_scanner, expected_num=20, val_thr_num=4, queue_timeout=3, val_timeout=5, out_file="proxies.json"
    ):
        """Scan and validate proxies

        Firstly, call the `scan` method of `proxy_scanner`, then using multiple
        threads to validate them.

        Args:
            proxy_scanner: A ProxyScanner object.
            expected_num: Max number of valid proxies to be scanned.
            val_thr_num: Number of threads used for validating proxies.
            queue_timeout: Timeout for getting a proxy from the queue.
            val_timeout: An integer passed to `is_valid` as argument `timeout`.
            out_file: A string or None. If not None, the proxies will be saved
                      into `out_file`.
        """
        try:
            proxy_scanner.scan()
            self.logger.info(f"starting {val_thr_num} threads to validating proxies...")
            val_threads = []
            for i in range(val_thr_num):
                t = threading.Thread(
                    name=f"val-{i + 1:0>2d}",
                    target=self.validate,
                    kwargs=dict(
                        proxy_scanner=proxy_scanner,
                        expected_num=expected_num,
                        queue_timeout=queue_timeout,
                        val_timeout=val_timeout,
                    ),
                )
                t.daemon = True
                val_threads.append(t)
                t.start()
            for t in val_threads:
                t.join()
            self.logger.info("Proxy scanning done!")
        except:
            raise
        finally:
            if out_file is not None:
                self.save(out_file)

    def default_scan(
        self,
        region="mainland",
        expected_num=20,
        val_thr_num=4,
        queue_timeout=3,
        val_timeout=5,
        out_file="proxies.json",
        src_files=None,
    ):
        """Default scan method, to simplify the usage of `scan` method.

        It will register following scan functions:
        1. scan_file
        2. scan_cnproxy (if region is mainland)
        3. scan_free_proxy_list (if region is overseas)
        4. scan_ip84
        5. scan_mimiip
        After scanning, all the proxy info will be saved in out_file.

        Args:
            region: Either 'mainland' or 'overseas'
            expected_num: An integer indicating the expected number of proxies,
                          if this argument is set too great, it may take long to
                          finish scanning process.
            val_thr_num: Number of threads used for validating proxies.
            queue_timeout: An integer indicating the timeout for getting a
                           candidate proxy from the queue.
            val_timeout: An integer indicating the timeout when connecting the
                         test url using a candidate proxy.
            out_file: the file name of the output file saving all the proxy info
            src_files: A list of file names to scan
        """
        if expected_num > 30:
            self.logger.warn(
                "The more proxy you expect, the more time it "
                "will take. It is highly recommended to limit the"
                " expected num under 30."
            )
        proxy_scanner = ProxyScanner()
        if src_files is None:
            src_files = []
        elif isinstance(src_files, str):
            src_files = [src_files]
        for filename in src_files:
            proxy_scanner.register_func(proxy_scanner.scan_file, {"src_file": filename})
        if region == "mainland":
            proxy_scanner.register_func(proxy_scanner.scan_cnproxy, {})
        elif region == "overseas":
            proxy_scanner.register_func(proxy_scanner.scan_free_proxy_list, {})
        proxy_scanner.register_func(proxy_scanner.scan_ip84, {"region": region, "page": 5})
        proxy_scanner.register_func(proxy_scanner.scan_mimiip, {"region": region, "page": 5})
        self.scan(proxy_scanner, expected_num, val_thr_num, queue_timeout, val_timeout, out_file)


class ProxyScanner:
    """Proxy scanner class

    ProxyScanner focuses on scanning proxy lists from different sources.

    Attributes:
        proxy_queue: The queue for storing proxies.
        scan_funcs: Name of functions to be used in `scan` method.
        scan_kwargs: Arguments of functions
        scan_threads: A list of `threading.thread` object.
        logger: A `logging.Logger` object used for logging.
    """

    def __init__(self):
        self.proxy_queue = queue.Queue()
        self.scan_funcs = []
        self.scan_kwargs = []
        self.scan_threads = []
        self.logger = logging.getLogger(__name__)

    def register_func(self, func_name, func_kwargs):
        """Register a scan function

        Args:
            func_name: The function name of a scan function.
            func_kwargs: A dict containing arguments of the scan function.
        """
        self.scan_funcs.append(func_name)
        self.scan_kwargs.append(func_kwargs)

    def scan_ip84(self, region="mainland", page=1):
        """Scan candidate proxies from http://ip84.com

        Args:
            region: Either 'mainland' or 'overseas'.
            page: An integer indicating how many pages to be scanned.
        """
        self.logger.info("start scanning http://ip84.com for proxy list...")
        for i in range(1, page + 1):
            if region == "mainland":
                url = f"http://ip84.com/dlgn/{i}"
            elif region == "overseas":
                url = f"http://ip84.com/gwgn/{i}"
            else:
                url = f"http://ip84.com/gn/{i}"
            response = requests.get(url)
            soup = BeautifulSoup(response.content, "lxml")
            table = soup.find("table", class_="list")
            for tr in table.find_all("tr"):
                if tr.th is not None:
                    continue
                info = tr.find_all("td")
                protocol = info[4].string.lower()
                addr = f"{info[0].string}:{info[1].string}"
                self.proxy_queue.put({"addr": addr, "protocol": protocol})

    def scan_mimiip(self, region="mainland", page=1):
        """Scan candidate proxies from http://mimiip.com

        Args:
            region: Either 'mainland' or 'overseas'.
            page: An integer indicating how many pages to be scanned.
        """
        self.logger.info("start scanning http://mimiip.com for proxy list...")
        for i in range(1, page + 1):
            if region == "mainland":
                url = f"http://www.mimiip.com/gngao/{i}"
            elif region == "overseas":
                url = f"http://www.mimiip.com/hw/{i}"
            else:
                url = f"http://www.mimiip.com/gngao/{i}"
            response = requests.get(url)
            soup = BeautifulSoup(response.content, "lxml")
            table = soup.find("table", class_="list")
            for tr in table.find_all("tr"):
                if tr.th is not None:
                    continue
                info = tr.find_all("td")
                protocol = info[4].string.lower()
                addr = f"{info[0].string}:{info[1].string}"
                self.proxy_queue.put({"addr": addr, "protocol": protocol})

    def scan_cnproxy(self):
        """Scan candidate (mainland) proxies from http://cn-proxy.com"""
        self.logger.info("start scanning http://cn-proxy.com for proxy list...")
        response = requests.get("http://cn-proxy.com")
        soup = BeautifulSoup(response.content, "lxml")
        tables = soup.find_all("table", class_="sortable")
        for table in tables:
            for tr in table.tbody.find_all("tr"):
                info = tr.find_all("td")
                addr = f"{info[0].string}:{info[1].string}"
                self.proxy_queue.put({"addr": addr, "protocol": "http"})

    def scan_free_proxy_list(self):
        """Scan candidate (overseas) proxies from http://free-proxy-list.net"""
        self.logger.info("start scanning http://free-proxy-list.net " "for proxy list...")
        response = requests.get("http://free-proxy-list.net")
        soup = BeautifulSoup(response.content, "lxml")
        table = soup.find("table", id="proxylisttable")
        for tr in table.tbody.find_all("tr"):
            info = tr.find_all("td")
            if info[4].string != "elite proxy":
                continue
            if info[6].string == "yes":
                protocol = "https"
            else:
                protocol = "http"
            addr = f"{info[0].string}:{info[1].string}"
            self.proxy_queue.put({"addr": addr, "protocol": protocol})

    def scan_file(self, src_file):
        """Scan candidate proxies from an existing file"""
        self.logger.info(f"start scanning file {src_file} for proxy list...")
        with open(src_file) as fin:
            proxies = json.load(fin)
        for protocol in proxies.keys():
            for proxy in proxies[protocol]:
                self.proxy_queue.put({"addr": proxy["addr"], "protocol": protocol})

    def is_scanning(self):
        """Return whether at least one scanning thread is alive"""
        for t in self.scan_threads:
            if t.is_alive():
                return True
        return False

    def scan(self):
        """Start a thread for each registered scan function to scan proxy lists"""
        self.logger.info(
            "{0} registered scan functions, starting {0} threads "
            "to scan candidate proxy lists...".format(len(self.scan_funcs))
        )
        for i in range(len(self.scan_funcs)):
            t = threading.Thread(
                name=self.scan_funcs[i].__name__, target=self.scan_funcs[i], kwargs=self.scan_kwargs[i]
            )
            t.daemon = True
            self.scan_threads.append(t)
            t.start()