103 lines
2.7 KiB
Python
103 lines
2.7 KiB
Python
#!/bin/python3
|
|
"""
|
|
For a particular category
|
|
Will create markdown files and copy the image
|
|
"""
|
|
import os
|
|
import shutil
|
|
import magic
|
|
import mimetypes
|
|
|
|
DEFAULT_TAGS = ['crypte']
|
|
SOURCE_DIR = './to_import_crypte/'
|
|
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:
|
|
current_index = max(sorted([int(r) for r in content_dir_files]))+1
|
|
|
|
print(f"current index is {current_index}")
|
|
|
|
processed_images = []
|
|
|
|
to_import_files = os.listdir(SOURCE_DIR)
|
|
|
|
for original_file_name in to_import_files:
|
|
print(f'> {current_index} loading {original_file_name}')
|
|
|
|
new_dir_name = str(current_index).zfill(5) + '/'
|
|
|
|
if os.path.exists(CONTENT_DIR + new_dir_name):
|
|
continue
|
|
|
|
os.mkdir(CONTENT_DIR + 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"tags: {img['tags']}\n")
|
|
image_md.write("---\n")
|
|
image_md.write("\n")
|
|
# description about the image goes after (like the legend)
|
|
image_md.close()
|
|
|
|
current_index += 1
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|