# -*- coding: utf-8 -*-
__author__ = 'florije'

import re
import json
import time
import random
import requests
import datetime
import requesocks
from bs4 import BeautifulSoup


def test_url_available(url):
    proxies = {
        'http': 'http://{url}'.format(url=url),
        # 'http': 'http://222.88.142.51:8000',
    }
    try:
        res = requests.get('http://www.luoo.net/', proxies=proxies)
    except Exception as e:
        return False
    return res.status_code == 200

available_url = 'http://pachong.org/test/single/id/{id}'

proxies = {
    "http": "socks5://127.0.0.1:1080",
    "https": "socks5://127.0.0.1:1080"
}
user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"
headers = {
    "User-Agent": user_agent,
    "Host": "pachong.org",
    "Referer": "http://pachong.org/"
}


def get_var_list(page_content):
    # res = 'var chick=958+2894;var bee=6981+6600^chick;var cock=8243+890^bee;var calf=8872+4397^cock;var fish=3571+2760^calf;'
    # var dog=7544+4134;var worm=3554+5240^dog;var goat=1408+5265^worm;var cat=1981+8542^goat;var fish=7827+5906^cat;
    # print page_content
    pattern = re.compile(r'var \w+=\S+;var \w+=\S+;var \w+=\S+;var \w+=\S+;var \w+=\S+;')
    match = pattern.search(page_content)
    if match:
        print match.group()
    content = match.group()[:-1]
    var_list = content.split(';')  # ['var chick=958+2894']

    map_dict = {}

    # one
    raw_one = var_list[0]  # 'var chick=958+2894'
    left_raw_one, right_raw_one = raw_one.split('=')[0], raw_one.split('=')[1]
    ont_var = left_raw_one.split(' ')[1]
    one_value = sum([int(value) for value in right_raw_one.split('+')])
    print ont_var, one_value
    map_dict[ont_var] = one_value

    # two
    raw_two = var_list[1]  # 'var bee=6981+6600^chick'
    left_raw_two, right_raw_two = raw_two.split('=')[0], raw_two.split('=')[1]
    two_var = left_raw_two.split(' ')[1]
    two_value = int(right_raw_two.split('+')[0]) + int(right_raw_two.split('+')[1].split('^')[0]) ^ map_dict.get(right_raw_two.split('+')[1].split('^')[1])
    print two_var, two_value
    map_dict[two_var] = two_value

    # three
    raw_three = var_list[2]  # 'var cock=8243+890^bee'
    left_raw_three, right_raw_three = raw_three.split('=')[0], raw_three.split('=')[1]
    three_var = left_raw_three.split(' ')[1]
    three_value = int(right_raw_three.split('+')[0]) + int(right_raw_three.split('+')[1].split('^')[0]) ^ map_dict.get(right_raw_three.split('+')[1].split('^')[1])
    print three_var, three_value
    map_dict[three_var] = three_value

    # four
    raw_four = var_list[3]  # var calf=8872+4397^cock;
    left_raw_four, right_raw_four = raw_four.split('=')[0], raw_four.split('=')[1]
    four_var = left_raw_four.split(' ')[1]
    four_value = int(right_raw_four.split('+')[0]) + int(right_raw_four.split('+')[1].split('^')[0]) ^ map_dict.get(right_raw_four.split('^')[1])
    print four_var, four_value
    map_dict[four_var] = four_value

    # five
    raw_five = var_list[4]  # 'var fish=3571+2760^calf'
    left_raw_five, right_raw_five = raw_five.split('=')[0], raw_five.split('=')[1]
    five_var = left_raw_five.split(' ')[1]
    five_value = int(right_raw_five.split('+')[0]) + int(right_raw_five.split('+')[1].split('^')[0]) ^ map_dict.get(right_raw_five.split('^')[1])
    print five_var, five_value
    map_dict[five_var] = five_value

    return map_dict


def get_urls():

    session = requesocks.Session(headers=headers, proxies=proxies)
    page_content = session.get('http://pachong.org/').content
    # print(page_content.content)

    soup = BeautifulSoup(page_content)
    # print soup.find("table", {"class": "tb"})
    # var chick=958+2894;var bee=6981+6600^chick;var cock=8243+890^bee;var calf=8872+4397^cock;var fish=3571+2760^calf;

    map_dict = get_var_list(page_content)

    table = soup.find('table', attrs={'class': 'tb'})
    table_body = table.find('tbody')
    rows = table_body.find_all('tr')
    data = []
    for row in rows:
        cols = row.find_all('td')

        # url_res = session.get(available_url.format(id=cols[6].find('a').get('name'))).content
        # json_url_res = json.loads(url_res)
        json_url_res = {'ret': 0, 'data': {'all': 0}}
        if json_url_res.get('ret') == 0:
            raw_port = cols[2].text.strip()  # document.write((11476^fish)+298);
            port = raw_port[raw_port.index('(') + 1: raw_port.index(';') - 1]  # (11476^fish)+298

            port = (int(port.split('^')[0][1:]) ^ map_dict.get(port[port.index('^') + 1: port.index(')')])) + int(port.split('+')[1])

            tmp = {'id': cols[0].text.strip(), 'ip': cols[1].text.strip(), 'port': port,
                   'url_id': cols[6].find('a').get('name'), 'con_time': json_url_res.get('data').get('all'), 'region': cols[3].text.strip()}
            data.append(tmp)
            print(tmp)
        # time.sleep(random.randint(3, 7))

    #     cols = [ele.text.strip() for ele in row.find_all('td')]
    #     data.append([ele for ele in cols if ele])

    return data


def save_data(json_data):
    with open('data_{date}.json'.format(date=datetime.datetime.now().strftime('%Y%m%d%H%M%S')), mode='w') as f:
        f.write(json_data)


if __name__ == '__main__':
    res = get_urls()
    data = []
    for item in res:
        if u'调试' not in item.get('region'):
            if test_url_available('{ip}:{port}'.format(ip=item.get('ip'), port=item.get('port'))):
                data.append(item)
    save_data(json.dumps(data))
