import requests
from bs4 import BeautifulSoup


class ProxyInfo:
    def __init__(self, ip, port, protocol, type, address):
        self.ip = ip
        self.port = port
        self.protocol = protocol
        self.type = type
        self.address = address
        self.delay = 0

    def __lt__(self, other):  # override <操作符
        if self.delay < other.delay:
            return True
        return False

    def set_delay(self, delay):
        self.delay = delay


class ProxyFetcher:
    host = "https://www.zdaye.com"
    start_url = host + "/dayProxy.html"
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
    }

    def find_proxys(self, max_num=5):
        resp = requests.get(self.start_url, headers=self.headers)
        soup = BeautifulSoup(resp.text, "lxml")
        thread_item_list = soup.find_all("div", attrs={"class": "thread_content"})
        if not thread_item_list:
            return None
        urls = []
        for th in thread_item_list:
            url = th.a.attrs["href"]
            urls.append(self.host + url)
        proxy_list = self.get_detail_proxys(urls[0])
        if not proxy_list:
            return None
        alive_proxy_list = []
        for proxy in proxy_list:
            ok, elapsed = self.test_proxy_status(proxy.ip, proxy.port, proxy.protocol)
            if not ok:
                continue
            if len(alive_proxy_list) >= max_num*2:
                break
            proxy.set_delay(elapsed)
            alive_proxy_list.append(proxy)
        alive_proxy_list.sort()
        return alive_proxy_list[0: min(max_num, len(alive_proxy_list))]

    def get_detail_proxys(self, url):
        resp = requests.get(url, headers=self.headers)
        soup = BeautifulSoup(resp.text, "lxml")
        ipc_table = soup.find("table", id='ipc')
        if not ipc_table:
            return None

        result_list = []
        ip_tag_list = ipc_table.find_all("tr")
        for ip_tag in ip_tag_list:
            tds = ip_tag.find_all("td")
            ip = tds[0].string
            port = tds[1].string
            protocol = tds[2].string
            type = tds[3].string
            address = tds[4].string
            result_list.append(ProxyInfo(ip, port, protocol, type, address))
        return result_list

    def test_proxy_status(self, ip, port, protocol):
        url = "https://www.taoba.com"
        proxies = {
            str(protocol).lower(): ip + ":" + port
        }
        r = requests.get(url, proxies=proxies)
        return r.status_code == 200 and r.text.find("taobao") > 0, r.elapsed.microseconds


proxy_finder = ProxyFetcher()

if __name__ == "__main__":
    f = ProxyFetcher()
    print(f.find_proxys())