File size: 7,942 Bytes
d9d1a01
 
 
c241033
 
19c6bae
 
c241033
 
7f6eb14
d9d1a01
 
 
eed277e
d9d1a01
7f6eb14
c241033
eed277e
 
 
c241033
e387fe3
d9d1a01
 
 
 
 
 
 
 
 
 
 
eed277e
 
c241033
 
 
eed277e
 
c241033
eed277e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f6eb14
c241033
19c6bae
 
 
 
c241033
19c6bae
 
e387fe3
c241033
 
19c6bae
c241033
 
 
 
 
 
 
 
 
 
19c6bae
 
eed277e
d9d1a01
 
c241033
 
eed277e
c241033
7f6eb14
c241033
d9d1a01
7f6eb14
d9d1a01
7f6eb14
c241033
 
 
 
 
 
 
 
eed277e
c241033
eed277e
c241033
eed277e
c241033
eed277e
 
 
 
 
c241033
 
 
eed277e
 
c241033
eed277e
 
c241033
eed277e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c241033
 
 
eed277e
c241033
eed277e
 
c241033
 
eed277e
 
c241033
 
 
d9d1a01
 
eed277e
 
 
 
 
 
c241033
eed277e
c241033
eed277e
 
c241033
eed277e
c241033
 
 
 
 
 
 
eed277e
 
d9d1a01
 
eed277e
 
 
 
 
c241033
d9d1a01
 
c241033
d9d1a01
 
eed277e
 
 
 
 
 
d9d1a01
e387fe3
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import requests
import textwrap
from moviepy.editor import ImageClip, ColorClip, CompositeVideoClip
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import uuid
import os
import time
import glob

# ุฅู†ุดุงุก ุงู„ู…ุฌู„ุฏุงุช ุงู„ู„ุงุฒู…ุฉ
os.makedirs("static/videos", exist_ok=True)

app = FastAPI(
    title="Auto Quotes Video & SEO API",
    description="API ู„ุฅู†ุดุงุก ููŠุฏูŠูˆู‡ุงุช ุงู„ุญูƒู… ู…ุน ุชูˆู„ูŠุฏ ุจูŠุงู†ุงุช ุงู„ูŠูˆุชูŠูˆุจ (ุงู„ุนู†ูˆุงู†ุŒ ุงู„ูˆุตูุŒ ูˆุงู„ูˆุณูˆู…).",
    version="1.2.0"
)

app.mount("/static", StaticFiles(directory="static"), name="static")

def cleanup_old_videos():
    now = time.time()
    for f in glob.glob("static/videos/*.mp4"):
        if os.stat(f).st_mtime < now - 600:
            try:
                os.remove(f)
            except:
                pass

def fetch_random_quote_data():
    """ุฌู„ุจ ุงู„ุญูƒู…ุฉ ูˆุชูˆู„ูŠุฏ ุจูŠุงู†ุงุช ุงู„ูŠูˆุชูŠูˆุจ ู„ู‡ุง"""
    try:
        response = requests.get("https://dummyjson.com/quotes/random", timeout=5)
        data = response.json()
        quote = data["quote"]
        author = data["author"]
    except:
        quote = "The best way to predict the future is to invent it."
        author = "Alan Kay"

    full_text = f'"{quote}"\n\nโ€” {author}'
    
    # ุชูˆู„ูŠุฏ ุจูŠุงู†ุงุช ุงู„ูŠูˆุชูŠูˆุจ (SEO)
    title = f"Life Changing Wisdom: {quote[:40]}... #Shorts"
    description = f"Inspirational quote by {author}.\n\nText: {quote}\n\n#wisdom #quotes #motivation #inspiration #shorts"
    tags = ["wisdom", "motivation", "quotes", author.replace(" ", ""), "inspirational", "shorts", "life-lessons"]
    
    return {
        "text": full_text,
        "title": title,
        "description": description,
        "tags": tags
    }

def create_text_image(text, size=(720, 1280), font_size=45):
    img = Image.new('RGBA', size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    try:
        font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
    except:
        font = ImageFont.load_default()

    lines = textwrap.wrap(text, width=25)
    y_text = size[1] / 3
    
    for line in lines:
        bbox = draw.textbbox((0, 0), line, font=font)
        w = bbox[2] - bbox[0]
        h = bbox[3] - bbox[1]
        x = (size[0] - w) / 2
        
        draw.text((x+3, y_text+3), line, font=font, fill=(0, 0, 0, 150))
        draw.text((x, y_text), line, font=font, fill=(255, 255, 255, 255))
        y_text += h + 20
        
    return np.array(img)

def generate_video_file(quote_text):
    filename = f"quote_{uuid.uuid4().hex}.mp4"
    output_path = os.path.join("static", "videos", filename)
    
    bg = ColorClip(size=(720, 1280), color=(15, 23, 42)).set_duration(6)
    text_arr = create_text_image(quote_text)
    txt_clip = ImageClip(text_arr).set_duration(6)
    
    video = CompositeVideoClip([bg, txt_clip])
    video.write_videofile(output_path, fps=24, codec="libx264", audio=False)
    
    return filename

@app.get("/", response_class=HTMLResponse, include_in_schema=False)
def index_page():
    return """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Auto YouTube Content API</title>
        <style>
            body { font-family: 'Segoe UI', sans-serif; background-color: #0f172a; color: white; text-align: center; padding: 30px; }
            h1 { color: #38bdf8; }
            .btn { background-color: #38bdf8; color: #0f172a; border: none; padding: 12px 24px; font-size: 16px; font-weight: bold; border-radius: 8px; cursor: pointer; margin: 10px; transition: 0.3s; }
            .btn:hover { background-color: #0284c7; color: white; }
            .content-box { background: #1e293b; padding: 20px; border-radius: 12px; margin-top: 20px; text-align: left; display: none; max-width: 600px; margin-left: auto; margin-right: auto; }
            .meta-item { margin-bottom: 15px; border-bottom: 1px solid #334155; padding-bottom: 10px; }
            .meta-title { color: #94a3b8; font-size: 14px; font-weight: bold; text-transform: uppercase; }
            video { border-radius: 15px; width: 300px; margin-top: 20px; }
            code { color: #38bdf8; background: #0f172a; padding: 2px 5px; border-radius: 4px; }
        </style>
    </head>
    <body>
        <h1>๐Ÿš€ YouTube Auto-Content Creator</h1>
        <p>Get the video, title, description, and tags in one click.</p>
        
        <button class="btn" onclick="generate()" id="gen-btn">๐ŸŽฌ Generate Full Package</button>
        <a href="/docs" target="_blank"><button class="btn" style="background:#475569; color:white;">๐Ÿ“– API Docs</button></a>
        
        <div id="loader" style="display:none;">โณ Processing...</div>

        <div id="result" class="content-box">
            <div class="meta-item">
                <div class="meta-title">Video Link</div>
                <div id="res-url" style="word-break: break-all; color: #10b981;"></div>
            </div>
            <div class="meta-item">
                <div class="meta-title">YouTube Title</div>
                <div id="res-title"></div>
            </div>
            <div class="meta-item">
                <div class="meta-title">Description</div>
                <div id="res-desc" style="white-space: pre-wrap;"></div>
            </div>
            <div class="meta-item">
                <div class="meta-title">Tags</div>
                <div id="res-tags" style="color: #fbbf24;"></div>
            </div>
            <center><video id="res-video" controls></video></center>
        </div>

        <script>
            async function generate() {
                const btn = document.getElementById('gen-btn');
                const loader = document.getElementById('loader');
                const result = document.getElementById('result');

                btn.disabled = true;
                loader.style.display = "block";
                result.style.display = "none";

                try {
                    const response = await fetch('/api/generate');
                    const data = await response.json();
                    
                    if (data.status === "success") {
                        document.getElementById('res-url').innerText = data.video_url;
                        document.getElementById('res-title').innerText = data.youtube_data.title;
                        document.getElementById('res-desc').innerText = data.youtube_data.description;
                        document.getElementById('res-tags').innerText = data.youtube_data.tags.join(", ");
                        document.getElementById('res-video').src = data.video_url;
                        
                        result.style.display = "block";
                    }
                } catch (e) { alert("Error!"); }
                finally {
                    btn.disabled = false;
                    loader.style.display = "none";
                }
            }
        </script>
    </body>
    </html>
    """

@app.get("/api/generate", tags=["YouTube Content"])
def api_generate_content(request: Request):
    cleanup_old_videos()
    
    # ุฌู„ุจ ุงู„ุจูŠุงู†ุงุช ูˆุงู„ู†ุต
    quote_data = fetch_random_quote_data()
    
    # ุฅู†ุดุงุก ุงู„ููŠุฏูŠูˆ
    video_filename = generate_video_file(quote_data["text"])
    
    base_url = str(request.base_url).replace("http://", "https://")
    video_url = f"{base_url}static/videos/{video_filename}"
    
    return {
        "status": "success",
        "video_url": video_url,
        "youtube_data": {
            "title": quote_data["title"],
            "description": quote_data["description"],
            "tags": quote_data["tags"]
        }
    }