File size: 5,387 Bytes
69e4168
774f130
69e4168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
774f130
 
 
 
 
 
 
 
 
69e4168
 
 
 
 
 
 
 
 
774f130
69e4168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
774f130
69e4168
 
 
 
774f130
69e4168
774f130
69e4168
 
774f130
ef98b6f
774f130
69e4168
 
 
 
 
 
 
 
 
 
774f130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69e4168
 
 
774f130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbd626b
 
 
 
 
774f130
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import requests
from PIL import Image, ImageDraw, ImageFont
import streamlit as st
import random

st.title("Social Poster Generator")
st.markdown("Create and share beautiful, real-quote posters for social media.")

platform = st.selectbox(
    "Choose Platform", ["LinkedIn", "Instagram", "TikTok", "Facebook", "Other"]
)

quote_type = st.selectbox(
    "Type of Content",
    ["Author", "Topic", "Islamic Verse", "Hadith", "AI Generated"]
)

user_input = st.text_input("Enter author, topic, or keyword (optional)")

# ---------- Local fallback quotes ----------
local_quotes = [
    {"q": "The best way to predict the future is to invent it.", "a": "Alan Kay"},
    {"q": "Life is what happens when you're busy making other plans.", "a": "John Lennon"},
    {"q": "Be yourself; everyone else is already taken.", "a": "Oscar Wilde"},
    {"q": "Success is not final, failure is not fatal: It is the courage to continue that counts.", "a": "Winston Churchill"}
]

# ---------- Generate Poster Button ----------
if st.button("Generate Poster", key="main_gen_poster"):
    st.write("Generating...")
    quote, ref = None, None

    # -----------------
    # HADITH
    # -----------------
    if quote_type == "Hadith":
        try:
            res = requests.get("https://random-hadith-api.herokuapp.com/randomHadith", timeout=5)
            if res.status_code != 200:
                st.error("Hadith API returned an error.")
            else:
                data = res.json()
                quote = data.get("data", {}).get("hadith")
                ref = data.get("data", {}).get("book", "Unknown Hadith Collection")
                if quote:
                    st.success(f'"{quote}"')
                    st.info(f"Reference: {ref}")
                else:
                    st.info("No hadith found from the API.")
        except Exception as e:
            st.error(f"API error: {e}")

    # -----------------
    # ISLAMIC VERSE
    # -----------------
    elif quote_type == "Islamic Verse":
        try:
            surah = random.randint(1, 114)
            ayah = random.randint(1, 7)  # simple range to avoid errors
            url = f"https://api.alquran.cloud/v1/ayah/{surah}:{ayah}/en.asad"
            res = requests.get(url, timeout=5)
            data = res.json()
            if data.get("status") == "OK":
                quote = data["data"]["text"]
                ref = f'{data["data"]["surah"]["englishName"]} {data["data"]["numberInSurah"]}'
                st.success(f'"{quote}"')
                st.info(f"Reference: {ref}")
            else:
                st.info("No verse found. Try again.")
        except Exception as e:
            st.error(f"API error: {e}")

    # -----------------
    # AI GENERATED
    # -----------------
    elif quote_type == "AI Generated":
        quote = "Your AI-generated inspirational message goes here!"
        ref = "AI Model"
        st.success(quote)
        st.info(ref)

    # -----------------
    # AUTHOR / TOPIC QUOTES
    # -----------------
    elif quote_type in ["Author", "Topic"]:
        try:
            # Attempt to get random quote from ZenQuotes
            res = requests.get("https://zenquotes.io/api/random", timeout=5)
            data = res.json()
            if isinstance(data, list) and len(data) > 0:
                q = data[0]
                quote = q.get("q")
                ref = q.get("a")
            else:
                # fallback to local list
                q = random.choice(local_quotes)
                quote = q["q"]
                ref = q["a"]
            st.success(f'"{quote}"')
            st.info(f"Reference: {ref}")
        except Exception:
            # Always fallback to local quotes if ZenQuotes fails
            q = random.choice(local_quotes)
            quote = q["q"]
            ref = q["a"]
            st.success(f'"{quote}"')
            st.info(f"Reference: {ref}")

    else:
        st.info("Please enter a valid author/topic or select a different type above.")

    # -----------------
    # OPTIONAL: Generate poster image using PIL
    # -----------------
    if quote:
        try:
            img = Image.new("RGB", (800, 600), color=(255, 255, 255))
            draw = ImageDraw.Draw(img)
            font_size = 30
            try:
                font = ImageFont.truetype("arial.ttf", font_size)
            except:
                font = ImageFont.load_default()
            # Wrap text
            lines = []
            words = quote.split()
            line = ""
            for word in words:
                if len(line + " " + word) <= 40:
                    line += " " + word
                else:
                    lines.append(line.strip())
                    line = word
            lines.append(line.strip())
            # Draw text
            y_text = 100
            for l in lines:
                # Pillow >=10 replacement for textsize
                bbox = draw.textbbox((0, 0), l, font=font)
                w = bbox[2] - bbox[0]
                h = bbox[3] - bbox[1]
                draw.text(((800 - w) / 2, y_text), l, font=font, fill=(0, 0, 0))
                y_text += h + 10
            # Draw reference
            draw.text((800-400, 500), f"- {ref}", font=font, fill=(0, 0, 0))
            st.image(img)
        except Exception as e:
            st.warning(f"Could not generate poster image: {e}")