File size: 11,659 Bytes
a8e9b84
 
677cbbf
a8e9b84
 
 
 
 
 
 
 
c059037
a8e9b84
677cbbf
a8e9b84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677cbbf
 
a8e9b84
 
 
 
 
 
 
677cbbf
a8e9b84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677cbbf
a8e9b84
 
 
677cbbf
a8e9b84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677cbbf
a8e9b84
 
677cbbf
 
a8e9b84
 
 
677cbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import os
import re
import textwrap

import aiofiles
import aiohttp
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
from unidecode import unidecode
from youtubesearchpython.__future__ import VideosSearch

from DragMusic import app
from DragMusic.core.mongo import mongodb
from config import YOUTUBE_IMG_URL
from DragMusic.logging import LOGGER


def changeImageSize(maxWidth, maxHeight, image):
    widthRatio = maxWidth / image.size[0]
    heightRatio = maxHeight / image.size[1]
    newWidth = int(widthRatio * image.size[0])
    newHeight = int(heightRatio * image.size[1])
    newImage = image.resize((newWidth, newHeight))
    return newImage


def clear(text):
    list = text.split(" ")
    title = ""
    for i in list:
        if len(title) + len(i) < 60:
            title += " " + i
    return title.strip()


async def get_thumb(videoid):
    if os.path.isfile(f"/tmp/cache/{videoid}.png"):
        return f"/tmp/cache/{videoid}.png"

    url = f"https://www.youtube.com/watch?v={videoid}"
    try:
        results = VideosSearch(url, limit=1)
        for result in (await results.next())["result"]:
            try:
                title = result["title"]
                title = re.sub("\\W+", " ", title)
                title = title.title()
            except:
                title = "Unsupported Title"
            try:
                duration = result["duration"]
            except:
                duration = "Unknown Mins"
            thumbnail = result["thumbnails"][0]["url"].split("?")[0]
            try:
                views = result["viewCount"]["short"]
            except:
                views = "Unknown Views"
            try:
                channel = result["channel"]["name"]
            except:
                channel = "Unknown Channel"

        async with aiohttp.ClientSession() as session:
            async with session.get(thumbnail) as resp:
                if resp.status == 200:
                    f = await aiofiles.open(f"/tmp/cache/thumb{videoid}.png", mode="wb")
                    await f.write(await resp.read())
                    await f.close()

        youtube = Image.open(f"/tmp/cache/thumb{videoid}.png")
        image1 = changeImageSize(1280, 720, youtube)
        image2 = image1.convert("RGBA")
        background = image2.filter(filter=ImageFilter.BoxBlur(10))
        enhancer = ImageEnhance.Brightness(background)
        background = enhancer.enhance(0.5)
        draw = ImageDraw.Draw(background)
        arial = ImageFont.truetype("DragMusic/assets/font2.ttf", 30)
        font = ImageFont.truetype("DragMusic/assets/font.ttf", 30)
        draw.text((1110, 8), unidecode(app.name), fill="white", font=arial)
        draw.text(
            (55, 560),
            f"{channel} | {views[:23]}",
            (255, 255, 255),
            font=arial,
        )
        draw.text(
            (57, 600),
            clear(title),
            (255, 255, 255),
            font=font,
        )
        draw.line(
            [(55, 660), (1220, 660)],
            fill="white",
            width=5,
            joint="curve",
        )
        draw.ellipse(
            [(918, 648), (942, 672)],
            outline="white",
            fill="white",
            width=15,
        )
        draw.text(
            (36, 685),
            "00:00",
            (255, 255, 255),
            font=arial,
        )
        draw.text(
            (1185, 685),
            f"{duration[:23]}",
            (255, 255, 255),
            font=arial,
        )
        try:
            os.remove(f"/tmp/cache/thumb{videoid}.png")
        except:
            pass
        background.save(f"/tmp/cache/{videoid}.png")
        return f"/tmp/cache/{videoid}.png"
    except Exception as e:
        print(e)
        return YOUTUBE_IMG_URL


async def gen_thumb(videoid: str, user_id: int) -> str:
    if os.path.isfile(f"/tmp/cache/{videoid}.png"):
        return f"/tmp/cache/{videoid}.png"
    url = f"https://www.youtube.com/watch?v={videoid}"
    try:
        results = VideosSearch(url, limit=1)
        for result in (await results.next())["result"]:
            try:
                title = result["title"]
                title = re.sub("\\W+", " ", title)
                title = title.title()
            except:
                title = "Unsupported Title"
            try:
                duration = result["duration"]
            except:
                duration = "Unknown Mins"
            thumbnail = result["thumbnails"][0]["url"].split("?")[0]
            try:
                views = result["viewCount"]["short"]
            except:
                views = "Unknown Views"
            try:
                channel = result["channel"]["name"]
            except:
                channel = "Unknown Channel"

        async with aiohttp.ClientSession() as session:
            async with session.get(thumbnail) as resp:
                if resp.status == 200:
                    ctitle = (await app.get_chat(user_id)).title
                    ctitle = await crop_title(ctitle)
                    try:
                        f = await aiofiles.open(f"/tmp/cache/thumb{videoid}.png", mode="wb")
                        await f.write(await resp.read())
                        await f.close()
                        youtube = Image.open(f"/tmp/cache/thumb{videoid}.png")
                        image1 = changeImageSize(1280, 720, youtube)
                        image2 = image1.convert("RGBA")
                        background = image2.filter(filter=ImageFilter.BoxBlur(30))
                        enhancer = ImageEnhance.Brightness(background)
                        background = enhancer.enhance(0.5)
                        draw = ImageDraw.Draw(background)
                        arial = ImageFont.truetype("DragMusic/assets/font2.ttf", 40)
                        font = ImageFont.truetype("DragMusic/assets/font.ttf", 30)
                        draw.text((1110, 8), unidecode(app.name), fill="white", font=arial)
                        draw.text(
                            (55, 560),
                            f"{channel} | {views[:23]}",
                            (255, 255, 255),
                            font=arial,
                        )
                        draw.text(
                            (57, 600),
                            clear(title),
                            (255, 255, 255),
                            font=font,
                        )
                        draw.line(
                            [(55, 660), (1220, 660)],
                            fill="white",
                            width=5,
                            joint="curve",
                        )
                        draw.ellipse(
                            [(918, 648), (942, 672)],
                            outline="white",
                            fill="white",
                            width=15,
                        )
                        draw.text(
                            (36, 685),
                            "00:00",
                            (255, 255, 255),
                            font=arial,
                        )
                        draw.text(
                            (1185, 685),
                            f"{duration[:23]}",
                            (255, 255, 255),
                            font=arial,
                        )
                    except Exception:
                        pass
                    circle = Image.open("DragMusic/assets/circle.png")
                    try:
                        pfp = Image.open(
                            await app.download_media(
                                (await app.get_chat(user_id)).photo.big_file_id,
                                file_name=f"pfp{user_id}.png",
                            )
                        )
                    except Exception:
                        pfp = Image.open("DragMusic/assets/profile.png")
                    pfp = pfp.resize((450, 450))
                    pfp = crop_center(pfp, 450, 450)
                    pfp = mask_circle_solid(pfp, (0, 0, 0, 0), 225)

                    #
                    image3 = Image.new("RGBA", (1280, 720))
                    image3.paste(background, (0, 0))
                    image3.paste(youtube, (200, 70), mask=youtube)
                    image3.paste(pfp, (410, 420), mask=pfp)
                    image3.paste(circle, (410, 420), mask=circle)

                    # Drawing with Impact Font
                    draw = ImageDraw.Draw(image3)
                    font = ImageFont.truetype("DragMusic/assets/font.ttf", 60)
                    font2 = ImageFont.truetype("DragMusic/assets/font2.ttf", 70)
                    arial = ImageFont.truetype("DragMusic/assets/font2.ttf", 40)
                    gfont = ImageFont.truetype("DragMusic/assets/font2.ttf", 40)
                    para = textwrap.wrap(title, width=32)
                    j = 0
                    try:
                        if para[0]:
                            draw.text(
                                (5, 5),
                                f"{para[0]}",
                                fill="white",
                                stroke_width=3,
                                stroke_fill="black",
                                font=font,
                            )
                        if para[1]:
                            draw.text(
                                (5, 70),
                                f"{para[1]}",
                                fill="white",
                                stroke_width=3,
                                stroke_fill="black",
                                font=font,
                            )
                    except:
                        pass
                    text_w, text_h = draw.textsize(f"Playing on: {ctitle}", font=gfont)
                    draw.text(
                        ((1280 - text_w) / 2, 615),
                        f"Playing on: {ctitle}",
                        fill="white",
                        font=gfont,
                    )
                    draw.text((740, 480), "Duration:", fill="white", font=arial)
                    draw.text(
                        (915, 480),
                        f"{duration} Mins",
                        fill="white",
                        font=arial,
                    )
                    draw.text(
                        (740, 530),
                        f"Views : {views}",
                        (255, 255, 255),
                        font=arial,
                    )

                    try:
                        os.remove(f"/tmp/cache/thumb{videoid}.png")
                    except:
                        pass
                    background.save(f"/tmp/cache/{videoid}.png")
                    return f"/tmp/cache/{videoid}.png"
    except Exception as e:
        LOGGER(__name__).error(e)
        return ""


def crop_center(pil_img, crop_width, crop_height):
    img_width, img_height = pil_img.size
    return pil_img.crop(
        (
            (img_width - crop_width) // 2,
            (img_height - crop_height) // 2,
            (img_width + crop_width) // 2,
            (img_height + crop_height) // 2,
        )
    )


def mask_circle_solid(im, a, size):
    output = Image.new("RGBA", (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(output)
    draw.ellipse((0, 0, size, size), fill=a)
    output.putalpha(220)
    return output


async def crop_title(title):
    if len(title) > 20:
        return f"{title[:20]}..."
    else:
        return title