46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
|
from mastodon import Mastodon
|
||
|
import os
|
||
|
import random
|
||
|
|
||
|
# Mastodon API credentials
|
||
|
mastodon = Mastodon(
|
||
|
access_token = os.getenv('ACCESS_TOKEN'),
|
||
|
api_base_url = 'https://' + os.getenv('SERVER')
|
||
|
)
|
||
|
|
||
|
# Directory containing images
|
||
|
IMAGE_DIR = os.getenv('IMAGE_DIR')
|
||
|
|
||
|
def post_random_image():
|
||
|
# Get list of image files
|
||
|
valid_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.webp')
|
||
|
image_files = [f for f in os.listdir(IMAGE_DIR)
|
||
|
if f.lower().endswith(valid_extensions)]
|
||
|
|
||
|
if not image_files:
|
||
|
print("No valid images found in directory")
|
||
|
return
|
||
|
|
||
|
# Select random image
|
||
|
random_image = random.choice(image_files)
|
||
|
image_path = os.path.join(IMAGE_DIR, random_image)
|
||
|
|
||
|
try:
|
||
|
# Upload media
|
||
|
media = mastodon.media_post(
|
||
|
media_file = image_path
|
||
|
)
|
||
|
|
||
|
# Post status with media
|
||
|
mastodon.status_post(
|
||
|
media_ids = [media['id']],
|
||
|
status=''
|
||
|
)
|
||
|
|
||
|
except Exception as e:
|
||
|
print(f"Error posting image: {e}")
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
post_random_image()
|
||
|
|