From cc8f00285138825c01d6b338fe808d9b37e65f16 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 13 Feb 2022 16:22:10 +0800 Subject: [PATCH] [feat] Show tg audio's thumbnail --- driver/design/thumbnail.py | 38 +++++++++++++++++++++++++------------- program/music.py | 16 ++++++++++------ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/driver/design/thumbnail.py b/driver/design/thumbnail.py index b9b6cd4..ae21e87 100644 --- a/driver/design/thumbnail.py +++ b/driver/design/thumbnail.py @@ -9,22 +9,34 @@ from PIL import ( 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)) + if image.size[0] == image.size[1]: + # Does not change the scale of the orientation image and displays it centered. + # It may look even better + newImage = image.resize((maxHeight, maxHeight)) + img = Image.new("RGBA", (maxWidth, maxHeight)) + img.paste(newImage, (int((maxWidth - maxHeight) / 2), 0)) + return img + else: + 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 thumb(thumbnail, title, userid, ctitle): - async with aiohttp.ClientSession() as session: - async with session.get(thumbnail) as resp: - if resp.status == 200: - f = await aiofiles.open(f"search/thumb{userid}.png", mode="wb") - await f.write(await resp.read()) - await f.close() - image1 = Image.open(f"search/thumb{userid}.png") + img_path = f"search/thumb{userid}.png" + if 'http' in thumbnail: + async with aiohttp.ClientSession() as session: + async with session.get(thumbnail) as resp: + if resp.status == 200: + f = await aiofiles.open(img_path, mode="wb") + await f.write(await resp.read()) + await f.close() + else: + img_path = thumbnail + image1 = Image.open(img_path) image2 = Image.open("driver/source/LightGreen.png") image3 = changeImageSize(1280, 720, image1) image4 = changeImageSize(1280, 720, image2) @@ -49,6 +61,6 @@ async def thumb(thumbnail, title, userid, ctitle): ) img.save(f"search/final{userid}.png") os.remove(f"search/temp{userid}.png") - os.remove(f"search/thumb{userid}.png") + os.remove(img_path) final = f"search/final{userid}.png" return final diff --git a/program/music.py b/program/music.py index 4f14d52..b390df7 100644 --- a/program/music.py +++ b/program/music.py @@ -147,24 +147,29 @@ async def play(c: Client, m: Message): suhu = await replied.reply("📥 downloading audio...") dl = await replied.download() link = replied.link + songname = "Audio" + thumbnail = f"{IMG_5}" try: if replied.audio: - songname = replied.audio.title[:70] - songname = replied.audio.file_name[:70] + if replied.audio.title: + songname = replied.audio.title[:70] + else: + songname = replied.audio.file_name[:70] + if replied.audio.thumbs: + thumbnail = await c.download_media(replied.audio.thumbs[0].file_id) duration = convert_seconds(replied.audio.duration) elif replied.voice: songname = "Voice Note" duration = convert_seconds(replied.voice.duration) except BaseException: - songname = "Audio" - + pass + if chat_id in QUEUE: await suhu.edit("🔄 Queueing Track...") gcname = m.chat.title ctitle = await CHAT_TITLE(gcname) title = songname userid = m.from_user.id - thumbnail = f"{IMG_5}" image = await thumb(thumbnail, title, userid, ctitle) pos = add_to_queue(chat_id, songname, dl, link, "Audio", 0) requester = f"[{m.from_user.first_name}](tg://user?id={m.from_user.id})" @@ -182,7 +187,6 @@ async def play(c: Client, m: Message): ctitle = await CHAT_TITLE(gcname) title = songname userid = m.from_user.id - thumbnail = f"{IMG_5}" image = await thumb(thumbnail, title, userid, ctitle) await suhu.edit("🔄 Joining Group Call...") await music_on(chat_id)