Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import cloudinary.uploader
- import cloudinary.api
- from dotenv import load_dotenv
- load_dotenv()
- IMAGE_DIR = "./downloaded_images/"
- def get_modification_date(file_path):
- # Get the last modification time of the file and return it
- return os.path.getmtime(file_path)
- def generate_embed_code(image_url, alt_text):
- return f'<img data-cloudinary-src="{image_url}" alt="{alt_text}"/>'
- # Filter out .DS_Store and other hidden files
- files = [f for f in os.listdir(IMAGE_DIR) if not f.startswith('.')]
- files = sorted(files, key=lambda x: get_modification_date(os.path.join(IMAGE_DIR, x)))
- # Cloudinary configurations
- config = cloudinary.config(cloud_name='YOUR_CLOUD_NAME',
- api_key='YOUR_API_KEY',
- api_secret='YOUR_API_SECRET')
- # Iterate through files, two at a time (alt text and image)
- for i in range(0, len(files), 2):
- image_file = files[i]
- alt_text_file = files[i + 1]
- # Upload image to Cloudinary
- response = cloudinary.uploader.upload(os.path.join(IMAGE_DIR, image_file), public_id=image_file[:-4], folder="YOUR_FOLDER_NAME")
- print(generate_embed_code(response['secure_url'], alt_text_file[:-4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement