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()

[((22, 48), 'sys.path.append', 'sys.path.append', (['os.pardir'], {}), '(os.pardir)\n', (37, 48), False, 'import sys\n'), ((230, 256), 'help.utils.join_root_path', 'utils.join_root_path', (['path'], {}), '(path)\n', (250, 256), False, 'from help import utils\n'), ((385, 412), 'os.path.exists', 'os.path.exists', (['create_path'], {}), '(create_path)\n', (399, 412), False, 'import os\n'), ((1355, 1374), 'threading.Thread', 'Thread', ([], {'target': 'main'}), '(target=main)\n', (1361, 1374), False, 'from threading import Thread\n'), ((1384, 1403), 'threading.Thread', 'Thread', ([], {'target': 'main'}), '(target=main)\n', (1390, 1403), False, 'from threading import Thread\n'), ((1413, 1432), 'threading.Thread', 'Thread', ([], {'target': 'main'}), '(target=main)\n', (1419, 1432), False, 'from threading import Thread\n'), ((1442, 1461), 'threading.Thread', 'Thread', ([], {'target': 'main'}), '(target=main)\n', (1448, 1461), False, 'from threading import Thread\n'), ((444, 465), 'os.mkdir', 'os.mkdir', (['create_path'], {}), '(create_path)\n', (452, 465), False, 'import os\n'), ((556, 580), 'random.choice', 'random.choice', (['char_pool'], {}), '(char_pool)\n', (569, 580), False, 'import random\n'), ((915, 959), 'requests.get', 'requests.get', (['url'], {'verify': '(False)', 'stream': '(True)'}), '(url, verify=False, stream=True)\n', (927, 959), False, 'import requests\n'), ((973, 990), 'contextlib.closing', 'closing', (['response'], {}), '(response)\n', (980, 990), False, 'from contextlib import closing\n'), ((598, 609), 'time.time', 'time.time', ([], {}), '()\n', (607, 609), False, 'import time\n')]