1
+ # -*- coding: utf-8 -*-
2
+ from __future__ import absolute_import , unicode_literals
3
+
4
+ from elasticsearch import helpers
5
+
6
+ from time import time
7
+
8
+ from proxies .cache_server .config import es
9
+ from proxies .cache_server .config import fetch , parse_response
10
+ from proxies .cache_server .config import is_good_proxy
11
+ from proxies .cache_server .config import logger
12
+ from proxies .cache_server .config import BASE_URL , SSL_URL , SOCKS_URL
13
+
14
+ def _add (proxies ):
15
+ actions = [
16
+ {
17
+ "_index" : "proxies" ,
18
+ "_type" : "proxy" ,
19
+ "_id" : proxy ['ip address' ] + ':' + proxy ['port' ],
20
+ "doc" : proxy
21
+ }
22
+ for proxy in proxies
23
+ ]
24
+ if proxies :
25
+ try :
26
+ tic = time ()
27
+ helpers .bulk (es , actions )
28
+ tac = time ()
29
+ print ('Time taken to add to index:' , tac - tic )
30
+ except Exception as e :
31
+ template = 'An exception of type {0} occurred.\n Arguments: {1!r}'
32
+ message = template .format (type (e ).__name__ , e .args )
33
+ logger .error (message )
34
+
35
+ def _check ():
36
+ urls = [BASE_URL , SSL_URL , SOCKS_URL ]
37
+ proxies = []
38
+ # Fetch all the proxies from these urls
39
+ for url in urls :
40
+ res = fetch (url )
41
+ # Passing empty conditions so that
42
+ proxies .extend (parse_response (res , {}))
43
+
44
+ # Check if they work
45
+ working_proxies = []
46
+ for proxy in proxies :
47
+ ip = proxy ['ip address' ] + ':' + proxy ['port' ]
48
+ # Implies SOCKS proxy
49
+ if 'version' in proxy :
50
+ ip = version + '://' + ip
51
+ protocol = ('http' , 'https' )[proxy ['https' ] == 'yes' ]
52
+
53
+ try :
54
+ # Only if it works
55
+ if is_good_proxy (ip , protocol = protocol ):
56
+ working_proxies .append (proxy )
57
+ except Exception as e :
58
+ template = 'An exception of type {0} occurred.\n Arguments: {1!r}'
59
+ message = template .format (type (e ).__name__ , e .args )
60
+ logger .error (message )
61
+
62
+ return working_proxies
63
+
64
+ if __name__ == '__main__' :
65
+ tic = time ()
66
+ proxies = _check ()
67
+ # _add(proxies)
68
+ tac = time ()
69
+ print ('Total time:' , tac - tic )
0 commit comments