#!/bin/python3 """ For a particular category Will create markdown files and copy the image """ import os import shutil import magic import mimetypes import json DEFAULT_TAGS = ['interieur-nef'] SOURCE_DIR = './to_import_interieur_nef/' CONTENT_DIR = '../content/images/' # get the current index for the outputdir content_dir_files = os.listdir(CONTENT_DIR) current_index = 1 if len(content_dir_files) > 0: id_lists = [] for dir in content_dir_files: try: dir_as_integer = int(dir) except ValueError: continue id_lists.append(dir_as_integer) current_index = max(sorted(id_lists))+1 print(f"current index is {current_index}") processed_images = [] to_import_files = os.listdir(SOURCE_DIR) created_dirs = [] final_image_list = [] for original_file_name in to_import_files: print(f'> {current_index} loading {original_file_name}') # new_dir_name = str(current_index).zfill(5) + '/' new_dir_name = str(current_index) + '/' if os.path.exists(CONTENT_DIR + new_dir_name): continue os.mkdir(CONTENT_DIR + new_dir_name) created_dirs.append(new_dir_name) image_old_path = SOURCE_DIR + original_file_name mime_type = magic.from_file(image_old_path, mime=True) print(f' found mime {mime_type}') new_ext = list(filter(lambda x: x[1] == mime_type, mimetypes.types_map.items()))[0][0] image_new_path = CONTENT_DIR + new_dir_name + 'original' + new_ext # processed_images.append({ # 'id': current_index, # 'original_name': original_file_name, # 'mime_type': mime_type, # 'tags': INITIAL_TAGS # # name, description # }) # copy the image to the final place shutil.copy(image_old_path, image_new_path) img = { 'id': current_index, 'original_name': original_file_name, 'mime_type': mime_type, 'tags': DEFAULT_TAGS # name, description } # create the markdown file along image_md = open(CONTENT_DIR + new_dir_name + "index.md", "a") image_md.write("---\n") image_md.write("title: Image\n") image_md.write(f"original_name: {img['original_name']}\n") image_md.write(f"mime_type: {img['mime_type']}\n") image_md.write(f"imtags:\n") for t in img['tags']: image_md.write(f" - {t}\n") image_md.write("---\n") image_md.write("\n") # description about the image goes after (like the legend) image_md.close() final_image_list.append(img) current_index += 1 print(json.dumps(created_dirs)) print(json.dumps(final_image_list)) # import json # import pathlib # import os # import shutil # inp_file = open('./images_list.json') # images = json.loads(inp_file.read()) # CONTENT_BASE = '../content/images/' # FILES_BASE = '../static/files/images/' # dirs = os.listdir(FILES_BASE) # print(sorted(dirs)) # for dir in dirs: # if os.path.exists(CONTENT_BASE + dir): # continue # os.mkdir(CONTENT_BASE + dir) # id = int(dir) # print(id - 1) # img = images[int(dir)-1] # print(img)