-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeme_generator.py
More file actions
43 lines (34 loc) · 1.5 KB
/
Copy pathmeme_generator.py
File metadata and controls
43 lines (34 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import random
import os
import uuid
import requests
from dotenv import load_dotenv
from meme_templates import get_random_template
# Load credentials from .env
load_dotenv()
IMGFLIP_USERNAME = os.getenv("IMGFLIP_USERNAME")
IMGFLIP_PASSWORD = os.getenv("IMGFLIP_PASSWORD")
def create_meme_imgflip(caption, template_id):
if not IMGFLIP_USERNAME or not IMGFLIP_PASSWORD:
raise Exception("Imgflip credentials not set in .env")
if not caption or not caption.strip():
raise Exception("Caption is empty")
response = requests.post("https://api.imgflip.com/caption_image", data={
'template_id': template_id,
'username': IMGFLIP_USERNAME,
'password': IMGFLIP_PASSWORD,
'text0': caption,
'text1': '' # optional second line
}).json()
if not response["success"]:
raise Exception("Failed to generate meme: " + response["error_message"])
return response["data"]["url"]
def generate_meme(captions, selected_index=None):
if not captions:
raise Exception("No captions provided.")
selected_caption = captions[selected_index] if selected_index is not None else random.choice(captions)
# ✅ Add random string to ensure unique URL
caption_with_uuid = f"{selected_caption} ({str(uuid.uuid4())[:5]})"
template = get_random_template()
meme_url = create_meme_imgflip(caption_with_uuid, template["id"])
return meme_url, selected_caption # 👈 Return original caption for display