Advertisement
nathan_markerio

Upload to Cloudinary + Print code block

Nov 14th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import os
  2.  
  3. import cloudinary.uploader
  4. import cloudinary.api
  5.  
  6. from dotenv import load_dotenv
  7. load_dotenv()
  8.  
  9. IMAGE_DIR = "./downloaded_images/"
  10.  
  11. def get_modification_date(file_path):
  12.     # Get the last modification time of the file and return it
  13.     return os.path.getmtime(file_path)
  14.  
  15. def generate_embed_code(image_url, alt_text):
  16.     return f'<img data-cloudinary-src="{image_url}" alt="{alt_text}"/>'
  17.  
  18. # Filter out .DS_Store and other hidden files
  19. files = [f for f in os.listdir(IMAGE_DIR) if not f.startswith('.')]
  20. files = sorted(files, key=lambda x: get_modification_date(os.path.join(IMAGE_DIR, x)))
  21.  
  22. # Cloudinary configurations
  23. config = cloudinary.config(cloud_name='YOUR_CLOUD_NAME',
  24.                            api_key='YOUR_API_KEY',
  25.                            api_secret='YOUR_API_SECRET')
  26.  
  27. # Iterate through files, two at a time (alt text and image)
  28. for i in range(0, len(files), 2):
  29.     image_file = files[i]
  30.     alt_text_file = files[i + 1]
  31.     # Upload image to Cloudinary
  32.     response = cloudinary.uploader.upload(os.path.join(IMAGE_DIR, image_file), public_id=image_file[:-4], folder="YOUR_FOLDER_NAME")
  33.     print(generate_embed_code(response['secure_url'], alt_text_file[:-4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement