# -*- coding: utf-8 -*-
# !/usr/bin/env python
"""
-------------------------------------------------
   File Name：     GetFreeProxy.py
   Description :  抓取免费代理
   Author :       JHao
   date：          2016/11/25
-------------------------------------------------
   Change Activity:
                   2016/11/25:
-------------------------------------------------
"""
import re
import time

import requests

try:
    from importlib import reload  # py3 实际不会实用，只是为了不显示语法错误
except:
    import sys  # py2

    reload(sys)
    sys.setdefaultencoding('utf-8')

from Util.utilFunction import getHtmlTree
from Util.WebRequest import WebRequest

# for debug to disable insecureWarning
requests.packages.urllib3.disable_warnings()


class GetFreeProxy(object):
    """
    proxy getter
    """

    def __init__(self):
        pass

    @staticmethod
    def freeProxyFirst(page=10):
        """
        抓取无忧代理 http://www.data5u.com/
        :param page: 页数
        :return:
        """
        url_list = ['http://www.data5u.com/',
                    'http://www.data5u.com/free/',
                    # 'http://www.data5u.com/free/gngn/index.shtml',
                    # 'http://www.data5u.com/free/gnpt/index.shtml',
                    'http://www.data5u.com/free/gwgn/index.shtml',
                    'http://www.data5u.com/free/gwpt/index.shtml']
        for url in url_list:
            html_tree = getHtmlTree(url)
            ul_list = html_tree.xpath('//ul[@class="l2"]')
            for ul in ul_list:
                try:
                    yield ':'.join(ul.xpath('.//li/text()')[0:2])
                except Exception as e:
                    pass

    @staticmethod
    def freeProxySecond(proxy_number=200):
        """
        抓取代理66 http://www.66ip.cn/
        :param proxy_number: 代理数量
        :return:
        """
        url_list = [
            "http://www.66ip.cn/mo.php?sxb=%CF%E3%B8%DB&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format(
                proxy_number),
            "http://www.66ip.cn/nmtq.php?getnum={}&isp=0&anonymoustype=3&start=&ports=&export=&ipaddress=&area=2&proxytype=0&api=66ip".format(
                proxy_number),
            "http://www.66ip.cn/nmtq.php?getnum={}&isp=0&anonymoustype=2&start=&ports=&export=&ipaddress=&area=2&proxytype=0&api=66ip".format(
                proxy_number),
            "http://www.66ip.cn/nmtq.php?getnum={}&isp=0&anonymoustype=3&start=&ports=&export=&ipaddress=&area=2&proxytype=0&api=66ip".format(
                proxy_number),
        ]


        for url in url_list:
            request = WebRequest()
            # html = request.get(url).content
            # content为未解码，text为解码后的字符串
            html = request.get(url).text
            for proxy in re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}', html):
                yield proxy
            time.sleep(2)

    @staticmethod
    def freeProxyThird(days=1):
        """
        抓取ip181 http://www.ip181.com/
        :param days:
        :return:
        """
        url = 'http://www.ip181.com/'
        html_tree = getHtmlTree(url)
        try:
            tr_list = html_tree.xpath('//tr')[1:]
            for tr in tr_list:
                yield ':'.join(tr.xpath('./td/text()')[0:2])
        except Exception as e:
            pass

    @staticmethod
    def freeProxyFourth():
        """
        抓取西刺代理 http://api.xicidaili.com/free2016.txt
        :return:
        """
        url_list = ['http://www.xicidaili.com/nn',  # 高匿
                    'http://www.xicidaili.com/nt',  # 透明
                    ]
        for each_url in url_list:
            tree = getHtmlTree(each_url)
            proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
            for proxy in proxy_list:
                try:
                    yield ':'.join(proxy.xpath('./td/text()')[0:2])
                except Exception as e:
                    pass

    @staticmethod
    def freeProxyFifth():
        """
        抓取guobanjia http://www.goubanjia.com/free/gngn/index.shtml
        :return:
        """
        urls = [
            "http://www.goubanjia.com/free/gwgn/index{page}.shtml",
            "http://www.goubanjia.com/free/gwpt/index{page}.shtml"
        ]

        for url in urls:
            for page in range(1, 7):
                page_url = url.format(page=page)
                tree = getHtmlTree(page_url)
                proxy_list = tree.xpath('//td[@class="ip"]')
                # 此网站有隐藏的数字干扰，或抓取到多余的数字或.符号
                # 需要过滤掉<p style="display:none;">的内容
                xpath_str = """.//*[not(contains(@style, 'display: none'))
                                    and not(contains(@style, 'display:none'))
                                    and not(contains(@class, 'port'))
                                    ]/text()
                            """
                for each_proxy in proxy_list:
                    try:
                        # :符号裸放在td下，其他放在div span p中，先分割找出ip，再找port
                        ip_addr = ''.join(each_proxy.xpath(xpath_str))
                        port = each_proxy.xpath(".//span[contains(@class, 'port')]/text()")[0]
                        yield '{}:{}'.format(ip_addr, port)
                    except Exception as e:
                        pass
            time.sleep(2)

    @staticmethod
    def freeProxySixth():
        """
        抓取讯代理免费proxy http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10
        :return:
        """
        url = 'http://www.xdaili.cn/ipagent/freeip/getFreeIps?page=1&rows=10'
        request = WebRequest()
        try:
            res = request.get(url).json()
            for row in res['RESULT']['rows']:
                yield '{}:{}'.format(row['ip'], row['port'])
        except Exception as e:
            pass

    @staticmethod
    def freeProxyKuaidaili():
        # 快代理http://www.kuaidaili.com/free/inha/1/
        url = "http://www.kuaidaili.com/free/inha/{}/"
        for page in range(1, 10):
            page_url = url.format(page)
            tree = getHtmlTree(page_url)
            ip_list = tree.xpath('//td[@data-title="IP"]/text()')
            port_list = tree.xpath('//td[@data-title="PORT"]/text()')
            for index, ip in enumerate(ip_list):
                yield '{}:{}'.format(ip, port_list[index])

    @staticmethod
    def freeProxyProxydb():
        # Proxydb  http://proxydb.net/?protocol=http&protocol=https&country=&offset=0
        url = "http://proxydb.net/?protocol=http&country=&offset={}"
        for offset in range(0, 150, 15):
            page_url = url.format(offset)
            tree = getHtmlTree(page_url)
            proxy_list = tree.xpath('//table//script/text()')
            for item in proxy_list:
                list = re.split('=|;|\'', "".join(item.split()))
                ip = list[2][::-1] + list[10]
                port = eval(list[13])
                yield '{}:{}'.format(ip, port)
            time.sleep(2)

    @staticmethod
    def freeProxyMimiIP():
        # 秘密IP
        url = "http://www.mimiip.com/hw/{}"
        for page in range(1, 11):
            page_url = url.format(page)
            tree = getHtmlTree(page_url)
            proxy_list = tree.xpath('//table[@class="list"]//tr')
            for proxy in proxy_list:
                try:
                    content = proxy.xpath('./td/text()')
                    if content[4] == 'HTTP':
                        yield ':'.join(content[0:2])
                except Exception as e:
                    pass
            time.sleep(2)

    @staticmethod
    def freeProxyPingRui():
        # PingRui
        url_list = [
            'http://pingrui.net/wn/',
            'http://pingrui.net/wt/'
        ]

        for each_url in url_list:
            tree = getHtmlTree(each_url)
            proxy_list = tree.xpath('.//table[@id="ip_list"]//tr')
            for proxy in proxy_list:
                try:
                    yield ':'.join(proxy.xpath('./td/text()')[0:2])
                except Exception as e:
                    pass
            time.sleep(2)


if __name__ == '__main__':
    gg = GetFreeProxy()
    for e in gg.freeProxyPingRui():
        print(e)
