constverum / ProxyBroker

Proxy [Finder | Checker | Server]. HTTP(S) & SOCKS :performing_arts:
http://proxybroker.readthedocs.io
Apache License 2.0
3.88k stars 1.09k forks source link

How to automatic loop run again when finish ? #149

Open DarKWinGTM opened 5 years ago

DarKWinGTM commented 5 years ago
import re
import os
import pip
import sys
import time
import random
import _thread
import asyncio
import datetime
import threading

try:
    __import__('proxybroker')
    __import__('requests')
except:
    print("Trying to Install required module: requireds\n")
    os.system('python -m pip install requireds')
    print("Trying to Install required module: proxybroker\n")
    os.system('python -m pip install --upgrade proxybroker')
finally:
    import requests
    from proxybroker import Broker

async def post(proxies, need):
    while True:

        proxy   =   await proxies.get() 

        try:
            query       +=  1
        except:
            query       =+  1
        if proxy is None:
            break
        elif proxy.avg_resp_time <= 1.50:
            port('https' if 'HTTPS' in proxy.types else 'http', proxy.host, proxy.port, 'type', proxy.types, query, need)
        else:
            print('%s - %s://%s:%d - %s:%s' % ('--', 'https' if 'HTTPS' in proxy.types else 'http', proxy.host, proxy.port, query, main.flow(-1)))

class port():
    def __init__(self, protocol, host, port, mode, types, query, need):
        self.protocol   =   protocol
        self.host       =   host
        self.port       =   port
        self.mode       =   mode
        self.types      =   types
        self.query      =   query
        self.need       =   need
        self.proxyes    =   {
            "http"  : '{0}://{1}:{2}'.format('socks5' if 'SOCKS5' in list(self.types)[0] else 'http', self.host, self.port), 
            "https" : '{0}://{1}:{2}'.format('socks5' if 'SOCKS5' in list(self.types)[0] else 'https', self.host, self.port), 
            "ftp"   : '{0}://{1}:{2}'.format('socks5' if 'SOCKS5' in list(self.types)[0] else 'ftp', self.host, self.port)
        }
        if self.mode == 'type':
            self.thread = _thread.start_new_thread(self.type, ())
        else:
            return
    def type(self):

        #   time.sleep(random.choice([2.5, 5.0, 7.5, 10.0, 12.5]))

        try:
            open('D:\\proxies.hig', 'x+')
        except FileExistsError:
            open('D:\\proxies.hig', 'r+')
        try:
            open('D:\\proxies.ihi', 'x+')
        except FileExistsError:
            open('D:\\proxies.ihi', 'r+')
        try:
            open('D:\\proxies.pci', 'x+')
        except FileExistsError:
            open('D:\\proxies.pci', 'r+')
        try:
            open('D:\\proxies.low', 'x+')
        except FileExistsError:
            open('D:\\proxies.low', 'r+')

        try:
            self.jadata =   requests.get('https://v2.api.iphub.info/guest/ip/{0}?c=Fae9gi8a'.format(self.host), proxies=self.proxyes).json()
        except:
            self.jadata =   {}
            pass
        try:
            self.jbdata =   requests.get('https://proxycheck.io/v2/{0}'.format(self.host), proxies=self.proxyes).json()
        except:
            self.jbdata =   {}
            pass

        try:
            if (not self.jadata['block'] == 1 and self.jbdata[self.host]['proxy'] == 'no') and not re.search(self.host, open('D:\\proxies.hig', 'r').read()):
                with open('D:\\proxies.hig', 'a+') as self.h:
                    self.h.write(
                        (
                            '%s_%s_%s://%s:%d\n' % (
                                datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S').replace(' ', '_'), 
                                self.types, 
                                self.protocol, 
                                self.host, 
                                self.port
                            )
                        ).replace(' ', '')
                    )
            elif not self.jadata['block'] == 1 and not re.search(self.host, open('D:\\proxies.ihi', 'r').read()):
                with open('D:\\proxies.ihi', 'a+') as self.m:
                    self.m.write(
                        (
                            '%s_%s_%s://%s:%d\n' % (
                                datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S').replace(' ', '_'), 
                                self.types, 
                                self.protocol, 
                                self.host, 
                                self.port
                            )
                        ).replace(' ', '')
                    )
            elif self.jbdata[self.host]['proxy'] == 'no' and not re.search(self.host, open('D:\\proxies.pci', 'r').read()):
                with open('D:\\proxies.pci', 'a+') as self.m:
                    self.m.write(
                        (
                            '%s_%s_%s://%s:%d\n' % (
                                datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S').replace(' ', '_'), 
                                self.types, 
                                self.protocol, 
                                self.host, 
                                self.port
                            )
                        ).replace(' ', '')
                    )
            elif not self.jadata == {} or not self.jbdata == {}:
                with open('D:\\proxies.low', 'a+') as self.l:
                    self.l.write(
                        (
                            '%s_%s_%s://%s:%d\n' % (
                                datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S').replace(' ', '_'), 
                                self.types, 
                                self.protocol, 
                                self.host, 
                                self.port
                            )
                        ).replace(' ', '')
                    )
            print('%d%s - %s://%s:%d - %s:%s' % (self.jadata['block'], '0' if self.jbdata[self.host]['proxy'] == 'no' else '1', self.protocol, self.host, self.port, self.query, main.flow(-1)))
        except:
            print('%d - %s://%s:%d - %s:%s' % (-1, self.protocol, self.host, self.port, self.query, main.flow(-1)))
        finally:
            return

class main():
    def __init__(self, need, time):
        self.need   =   need
        self.time   =   time
        self.loop   =   asyncio.get_event_loop()
        self.queu   =   asyncio.Queue()
        self.brok   =   Broker(
            self.queu, 
            timeout     =   5, 
            max_conn    =   need, 
            max_tries   =   2
        )
        self.task   =   asyncio.gather(
            self.brok.find(
                types   =   [
                    'SOCKS5', 
                    'SOCKS4', 
                    'HTTPS'
                ], 
                limit   =   self.need
            ),
            post(self.queu, self.need), 
        )
        self.thread =   _thread.start_new_thread(
            self.gone, ()
        )
        main.flow(self.need)
        main.repo()
    def gone(self):
        try:
            self.loop.run_until_complete(self.task)
        except:
            pass
        finally:
            while roll >= 1: time.sleep(self.time)
            self.loop   =   None
            self.queu   =   None
            self.brok   =   None
            self.task   =   None
            main(self.need, self.time)
    def flow(g):
        global roll
        try:
            if g >= 1:
                roll = g
            else:
                roll += g
        except:
            pass
        finally:
            return roll
    def repo():
        print(time.ctime())

if __name__ == '__main__':
    main(20, 10)
DarKWinGTM commented 5 years ago

Unhandled exception in thread started by <bound method main.gone of <main.main object at 0x0000025DDD89B588>> Traceback (most recent call last): File "C:\Users\User\Desktop\PB.py", line 191, in gone main(self.need, self.time) File "C:\Users\User\Desktop\PB.py", line 156, in init self.loop = asyncio.get_event_loop() File "C:\Python\Python37\lib\asyncio\events.py", line 644, in get_event_loop % threading.current_thread().name) RuntimeError: There is no current event loop in thread 'Dummy-1'.