Create imgflip_api.py
Browse files- utils/imgflip_api.py +90 -0
utils/imgflip_api.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
from typing import Dict, List, Any, Optional
|
| 5 |
+
import streamlit as st
|
| 6 |
+
|
| 7 |
+
def clean_text(text: str) -> str:
|
| 8 |
+
"""
|
| 9 |
+
Clean text for meme generation by limiting length and removing special characters.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
text: The text to clean
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
Cleaned text
|
| 16 |
+
"""
|
| 17 |
+
# Remove non-alphanumeric characters except spaces, periods, commas, and question marks
|
| 18 |
+
text = re.sub(r'[^\w\s.,?!]', '', text)
|
| 19 |
+
|
| 20 |
+
# Truncate to 100 characters if longer
|
| 21 |
+
if len(text) > 100:
|
| 22 |
+
text = text[:97] + '...'
|
| 23 |
+
|
| 24 |
+
return text
|
| 25 |
+
|
| 26 |
+
def create_meme(template_id: str, text_fields: List[str], article_title: str, article_abstract: str) -> Optional[str]:
|
| 27 |
+
"""
|
| 28 |
+
Create a meme using the Imgflip API.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
template_id: Imgflip template ID
|
| 32 |
+
text_fields: Predefined text fields for the template
|
| 33 |
+
article_title: Article title to use in text fields
|
| 34 |
+
article_abstract: Article abstract to use in text fields
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
URL of the generated meme image, or None if creation failed
|
| 38 |
+
"""
|
| 39 |
+
# Try to get API credentials from environment or secrets
|
| 40 |
+
username = os.environ.get("IMGFLIP_USERNAME")
|
| 41 |
+
password = os.environ.get("IMGFLIP_PASSWORD")
|
| 42 |
+
|
| 43 |
+
# If not found in environment, check streamlit secrets
|
| 44 |
+
if not username or not password:
|
| 45 |
+
try:
|
| 46 |
+
username = st.secrets["IMGFLIP_USERNAME"]
|
| 47 |
+
password = st.secrets["IMGFLIP_PASSWORD"]
|
| 48 |
+
except Exception:
|
| 49 |
+
st.error("Imgflip credentials not found. Please set the IMGFLIP_USERNAME and IMGFLIP_PASSWORD environment variables or in Streamlit secrets.")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
# Process text fields, replacing placeholders with article content
|
| 53 |
+
processed_fields = []
|
| 54 |
+
|
| 55 |
+
for field in text_fields:
|
| 56 |
+
if field == "{TITLE}":
|
| 57 |
+
processed_fields.append(clean_text(article_title))
|
| 58 |
+
elif field == "{ABSTRACT}":
|
| 59 |
+
processed_fields.append(clean_text(article_abstract))
|
| 60 |
+
else:
|
| 61 |
+
processed_fields.append(field)
|
| 62 |
+
|
| 63 |
+
# Create payload
|
| 64 |
+
payload = {
|
| 65 |
+
"template_id": template_id,
|
| 66 |
+
"username": username,
|
| 67 |
+
"password": password,
|
| 68 |
+
"font": "impact",
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
# Add text boxes
|
| 72 |
+
for i, text in enumerate(processed_fields):
|
| 73 |
+
payload[f"boxes[{i}][text]"] = text
|
| 74 |
+
|
| 75 |
+
# Make the API request
|
| 76 |
+
try:
|
| 77 |
+
response = requests.post("https://api.imgflip.com/caption_image", data=payload)
|
| 78 |
+
response.raise_for_status()
|
| 79 |
+
|
| 80 |
+
data = response.json()
|
| 81 |
+
|
| 82 |
+
if data.get("success"):
|
| 83 |
+
return data.get("data", {}).get("url")
|
| 84 |
+
else:
|
| 85 |
+
st.error(f"Failed to create meme: {data.get('error_message')}")
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
+
except requests.exceptions.RequestException as e:
|
| 89 |
+
st.error(f"Error calling Imgflip API: {e}")
|
| 90 |
+
return None
|