import sys
import os

sys.path.append(os.pardir)

import random
import time
import requests
from contextlib import closing
from help import utils
from threading import Thread


def get_train_set_path(path: str):
    create_path = utils.join_root_path(path)
    return create_path


def create_train_set_dir(path='auth-set'):
    create_path = get_train_set_path(path)
    is_existed = os.path.exists(create_path)
    if not is_existed:
        os.mkdir(create_path)


def gen_image_name(char_pool):
    prefix = ''
    for i in range(4):
        prefix += random.choice(char_pool)
    suffix = str(time.time()).replace('.', '')
    return "{}_{}".format(prefix, suffix)


def gen_image_all_url(path):
    rule = '0123456789'
    return '{}/{}.png'.format(path, gen_image_name(rule))


def get_image(url, count=20000, path='auth-set'):
    create_train_set_dir(path)
    for loop in range(count):
        response = requests.get(url, verify=False, stream=True)
        with closing(response) as response:
            with open(gen_image_all_url(get_train_set_path(path)), 'wb') as f:
                for i in response.iter_content(chunk_size=512):
                    f.write(i)
        print('第{}张图片保存成功'.format(loop + 1))


def main():
    get_image('https://gray.930pm.cn/home.php/Login/verify_c', path='auth-set')


if __name__ == '__main__':
    t1 = Thread(target=main)
    t2 = Thread(target=main)
    t3 = Thread(target=main)
    t4 = Thread(target=main)
    t1.start()
    t2.start()
    t3.start()
    t4.start()

[((4, 0, 4, 26), 'sys.path.append', 'sys.path.append', ({(4, 16, 4, 25): 'os.pardir'}, {}), '(os.pardir)', False, 'import sys\n'), ((15, 18, 15, 44), 'help.utils.join_root_path', 'utils.join_root_path', ({(15, 39, 15, 43): 'path'}, {}), '(path)', False, 'from help import utils\n'), ((21, 17, 21, 44), 'os.path.exists', 'os.path.exists', ({(21, 32, 21, 43): 'create_path'}, {}), '(create_path)', False, 'import os\n'), ((55, 9, 55, 28), 'threading.Thread', 'Thread', (), '', False, 'from threading import Thread\n'), ((56, 9, 56, 28), 'threading.Thread', 'Thread', (), '', False, 'from threading import Thread\n'), ((57, 9, 57, 28), 'threading.Thread', 'Thread', (), '', False, 'from threading import Thread\n'), ((58, 9, 58, 28), 'threading.Thread', 'Thread', (), '', False, 'from threading import Thread\n'), ((23, 8, 23, 29), 'os.mkdir', 'os.mkdir', ({(23, 17, 23, 28): 'create_path'}, {}), '(create_path)', False, 'import os\n'), ((29, 18, 29, 42), 'random.choice', 'random.choice', ({(29, 32, 29, 41): 'char_pool'}, {}), '(char_pool)', False, 'import random\n'), ((42, 19, 42, 63), 'requests.get', 'requests.get', (), '', False, 'import requests\n'), ((43, 13, 43, 30), 'contextlib.closing', 'closing', ({(43, 21, 43, 29): 'response'}, {}), '(response)', False, 'from contextlib import closing\n'), ((30, 17, 30, 28), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n')]