import datetime
import os
import shutil
import tempfile

from joblib import Parallel, delayed
from fastpic_upload import upload_file_to_fastpic


_n_jobs_for_upload = 20

_root_folders_set = (
    '/path/to/folder',
)
_spoiler_for_each_file = True


def process_one_pic(result_key, pic_path, tmp_dir):
    pic_url, pic_link = upload_file_to_fastpic(pic_path, tmp_dir)
    print(pic_url)
    return result_key, (pic_url, pic_link)


def upload_from_folder(folder_path):
    pics_to_upload = {}

    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.split('.')[-1] not in ('jpg', 'jpeg', 'bmp', 'png'):
                continue
            file_path = os.path.join(root, file)
            pics_to_upload[file] = file_path

    print(pics_to_upload)
    print('Need upload {} photo'.format(len(pics_to_upload)))

    result = {}
    tmp_dir = tempfile.mkdtemp()
    try:
        sub_results = Parallel(n_jobs=_n_jobs_for_upload, backend='threading')(
            delayed(process_one_pic)(key, pics_to_upload[key], tmp_dir) for key in sorted(pics_to_upload))

        for sub_result in sub_results:
            result[sub_result[0]] = sub_result[1]
    finally:
        shutil.rmtree(tmp_dir)
    return result


def print_result_to_file(result, result_file_path):
    with open(result_file_path, 'w', encoding='utf8', newline='') as codes_file:
        codes_file.write('[spoiler="Скриншоты"]')
        codes_file.write(os.linesep)
        codes_file.write(os.linesep)
        for result_key in sorted(result):
            if _spoiler_for_each_file:
                codes_file.write('[spoiler="{}"]'.format(result_key))
                codes_file.write(os.linesep)
            url, link = result[result_key]
            codes_file.write('[url={}][img]{}[/img][/url]'.format(link, url))
            if _spoiler_for_each_file:
                codes_file.write(os.linesep)
                codes_file.write('[/spoiler]')
                codes_file.write(os.linesep)
        codes_file.write(os.linesep)
        codes_file.write('[/spoiler]')


def main():
    for root_folder in _root_folders_set:
        result = upload_from_folder(root_folder)
        print_result_to_file(result, os.path.join(root_folder, 'result_codes.txt'))


if __name__ == '__main__':
    started = datetime.datetime.now()
    print(started, 'started')
    main()
    finished = datetime.datetime.now()
    print(finished, 'all done in', finished - started)



[((19, 24, 19, 65), 'fastpic_upload.upload_file_to_fastpic', 'upload_file_to_fastpic', ({(19, 47, 19, 55): 'pic_path', (19, 57, 19, 64): 'tmp_dir'}, {}), '(pic_path, tmp_dir)', False, 'from fastpic_upload import upload_file_to_fastpic\n'), ((27, 29, 27, 49), 'os.walk', 'os.walk', ({(27, 37, 27, 48): 'folder_path'}, {}), '(folder_path)', False, 'import os\n'), ((38, 14, 38, 32), 'tempfile.mkdtemp', 'tempfile.mkdtemp', ({}, {}), '()', False, 'import tempfile\n'), ((76, 14, 76, 37), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((79, 15, 79, 38), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((46, 8, 46, 30), 'shutil.rmtree', 'shutil.rmtree', ({(46, 22, 46, 29): 'tmp_dir'}, {}), '(tmp_dir)', False, 'import shutil\n'), ((31, 24, 31, 48), 'os.path.join', 'os.path.join', ({(31, 37, 31, 41): 'root', (31, 43, 31, 47): 'file'}, {}), '(root, file)', False, 'import os\n'), ((40, 22, 40, 78), 'joblib.Parallel', 'Parallel', (), '', False, 'from joblib import Parallel, delayed\n'), ((72, 37, 72, 82), 'os.path.join', 'os.path.join', ({(72, 50, 72, 61): 'root_folder', (72, 63, 72, 81): '"""result_codes.txt"""'}, {}), "(root_folder, 'result_codes.txt')", False, 'import os\n'), ((41, 12, 41, 36), 'joblib.delayed', 'delayed', ({(41, 20, 41, 35): 'process_one_pic'}, {}), '(process_one_pic)', False, 'from joblib import Parallel, delayed\n')]