Spaces:
Paused
Paused
File size: 4,913 Bytes
2f67506 | 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 | #
# Copyright (C) 2021-2022 by TeamYukki@Github, < https://github.com/TeamYukki >.
#
# This file is part of < https://github.com/TeamYukki/YukkiMusicBot > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/TeamYukki/YukkiMusicBot/blob/master/LICENSE >
#
# All rights reserved.
import os
import re
import textwrap
import traceback
import aiofiles
import aiohttp
from PIL import (Image, ImageDraw, ImageEnhance, ImageFilter,
ImageFont, ImageOps)
from youtubesearchpython.__future__ import VideosSearch
from config import MUSIC_BOT_NAME, YOUTUBE_IMG_URL
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
async def gen_thumb(videoid):
if os.path.isfile(f"cache/{videoid}.png"):
return f"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"cache/thumb{videoid}.png", mode="wb"
)
await f.write(await resp.read())
await f.close()
youtube = Image.open(f"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.6)
Xcenter = youtube.width / 2
Ycenter = youtube.height / 2
x1 = Xcenter - 250
y1 = Ycenter - 250
x2 = Xcenter + 250
y2 = Ycenter + 250
logo = youtube.crop((x1, y1, x2, y2))
logo.thumbnail((520, 520), Image.Resampling.LANCZOS)
logo = ImageOps.expand(logo, border=15, fill="white")
background.paste(logo, (50, 100))
draw = ImageDraw.Draw(background)
font = ImageFont.truetype("assets/font2.ttf", 40)
font2 = ImageFont.truetype("assets/font2.ttf", 70)
arial = ImageFont.truetype("assets/font2.ttf", 30)
name_font = ImageFont.truetype("assets/font.ttf", 30)
para = textwrap.wrap(title, width=32)
j = 0
draw.text(
(5, 5), f"{MUSIC_BOT_NAME}", fill="white", font=name_font
)
draw.text(
(600, 150),
"NOW PLAYING",
fill="white",
stroke_width=2,
stroke_fill="white",
font=font2,
)
for line in para:
if j == 1:
j += 1
draw.text(
(600, 340),
f"{line}",
fill="white",
stroke_width=1,
stroke_fill="white",
font=font,
)
if j == 0:
j += 1
draw.text(
(600, 280),
f"{line}",
fill="white",
stroke_width=1,
stroke_fill="white",
font=font,
)
draw.text(
(600, 450),
f"Views : {views[:23]}",
(255, 255, 255),
font=arial,
)
draw.text(
(600, 500),
f"Duration : {duration[:23]} Mins",
(255, 255, 255),
font=arial,
)
draw.text(
(600, 550),
f"Channel : {channel}",
(255, 255, 255),
font=arial,
)
try:
os.remove(f"cache/thumb{videoid}.png")
except:
pass
background.save(f"cache/{videoid}.png")
return f"cache/{videoid}.png"
except Exception:
print(traceback.format_exc())
return YOUTUBE_IMG_URL
|