# coding: utf-8
import requests
import random
from faker import Factory


CITIES = ['北京市', '天津市', '重庆市', '上海市',
          '河北省', '山西省', '辽宁省', '吉林省',
          '黑龙江省', '江苏省', '浙江省', '安徽省',
          '福建省', '江西省', '山东省', '河南省',
          '湖北省', '湖南省', '广东省', '海南省',
          '四川省', '贵州省', '云南省', '陕西省',
          '甘肃省', '青海省', '台湾省', '内蒙古自治区',
          '广西壮族自治区', '宁夏回族自治区', '新疆维吾尔自治区',
          '西藏自治区', '香港特别行政区', '澳门特别行政区',]

JOBS = ['销售|客服|市场', '财务|人力资源|行政', '项目|质量|高级管理',
        'IT|互联网|通信', '房产|建筑|物业管理', '金融',
        '采购|贸易|交通|物流', '生产|制造', '传媒|印刷|艺术|设计',
        '咨询|法律|教育|翻译', '服务业', '能源|环保|农业|科研',
        '兼职|实习|社工|其他',]


def random_username(trunc=True):
    nameparts = Factory.create().name().split()
    for i in range(len(nameparts)):
        if random.randint(0, 1):
            randnum = random.randint(0, 100)
            nameparts.append(str(randnum))
    random.shuffle(nameparts)

    username = list(filter(str.isalnum, ''.join(nameparts)))
    username = ''.join(username)

    length = random.randint(3, len(username))
    username = username[:length]

    funcs = [str.lower, str.upper, str]
    return random.choice(funcs)(username)

def random_password():
    password = random_username(trunc=False)
    if len(password) < 6:
        password = password * 2
    return password


fake = Factory.create(locale='zh_CN')

username = random_username()
password = random_password()
realname = fake.name()
email    = Factory.create().email()
city     = random.choice(CITIES)
job      = random.choice(JOBS)
mobile   = fake.phone_number() if random.randint(0, 1) else ""
address  = fake.address() if random.randint(0, 1) else ""

register_data = {
    "userName": username,
    "email": email,
    "password": password,
    "password1": password,
    "realName": realname,
    "province": city,
    "job": job,
    "mobile": mobile,
    "address": address,
}

register_data = {
    '_ZVING_METHOD': 'Register.doRegister',
    '_ZVING_URL': '%2Fregister',
    '_ZVING_DATA': str(register_data),
    '_ZVING_DATA_FORMAT': 'json',
}

url = "http://zqyj.chinalaw.gov.cn/ajax/invoke"

r1 = requests.post(url, data=register_data)

if r1.status_code == 200 and '成功' in r1.text:
    vote_data = {
        '_ZVING_METHOD': 'SupportOppose.voteOppose',
        '_ZVING_URL': '%2FdraftDetail',
        '_ZVING_DATA': '{"ID":"45115"}',
        '_ZVING_DATA_FORMAT': 'json',
    }

    r2 = requests.post(url, data=vote_data, cookies=r1.cookies)
    if r2.status_code != 200:
        print(r2.status_code, r2.text)
    else:
        print(r2.text)