merge pull request #144 from Xtao-Labs/thumbs

[feat] Show tg audio's thumbnail
This commit is contained in:
levina 2022-02-13 22:19:21 +07:00 committed by GitHub
commit e41749a149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 19 deletions

View File

@ -9,6 +9,14 @@ from PIL import (
def changeImageSize(maxWidth, maxHeight, image): def changeImageSize(maxWidth, maxHeight, image):
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] widthRatio = maxWidth / image.size[0]
heightRatio = maxHeight / image.size[1] heightRatio = maxHeight / image.size[1]
newWidth = int(widthRatio * image.size[0]) newWidth = int(widthRatio * image.size[0])
@ -18,13 +26,17 @@ def changeImageSize(maxWidth, maxHeight, image):
async def thumb(thumbnail, title, userid, ctitle): async def thumb(thumbnail, title, userid, ctitle):
img_path = f"search/thumb{userid}.png"
if 'http' in thumbnail:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(thumbnail) as resp: async with session.get(thumbnail) as resp:
if resp.status == 200: if resp.status == 200:
f = await aiofiles.open(f"search/thumb{userid}.png", mode="wb") f = await aiofiles.open(img_path, mode="wb")
await f.write(await resp.read()) await f.write(await resp.read())
await f.close() await f.close()
image1 = Image.open(f"search/thumb{userid}.png") else:
img_path = thumbnail
image1 = Image.open(img_path)
image2 = Image.open("driver/source/LightGreen.png") image2 = Image.open("driver/source/LightGreen.png")
image3 = changeImageSize(1280, 720, image1) image3 = changeImageSize(1280, 720, image1)
image4 = changeImageSize(1280, 720, image2) image4 = changeImageSize(1280, 720, image2)
@ -49,6 +61,6 @@ async def thumb(thumbnail, title, userid, ctitle):
) )
img.save(f"search/final{userid}.png") img.save(f"search/final{userid}.png")
os.remove(f"search/temp{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" final = f"search/final{userid}.png"
return final return final

View File

@ -147,16 +147,22 @@ async def play(c: Client, m: Message):
suhu = await replied.reply("📥 downloading audio...") suhu = await replied.reply("📥 downloading audio...")
dl = await replied.download() dl = await replied.download()
link = replied.link link = replied.link
songname = "Audio"
thumbnail = f"{IMG_5}"
try: try:
if replied.audio: if replied.audio:
if replied.audio.title:
songname = replied.audio.title[:70] songname = replied.audio.title[:70]
else:
songname = replied.audio.file_name[:70] 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) duration = convert_seconds(replied.audio.duration)
elif replied.voice: elif replied.voice:
songname = "Voice Note" songname = "Voice Note"
duration = convert_seconds(replied.voice.duration) duration = convert_seconds(replied.voice.duration)
except BaseException: except BaseException:
songname = "Audio" pass
if chat_id in QUEUE: if chat_id in QUEUE:
await suhu.edit("🔄 Queueing Track...") await suhu.edit("🔄 Queueing Track...")
@ -164,7 +170,6 @@ async def play(c: Client, m: Message):
ctitle = await CHAT_TITLE(gcname) ctitle = await CHAT_TITLE(gcname)
title = songname title = songname
userid = m.from_user.id userid = m.from_user.id
thumbnail = f"{IMG_5}"
image = await thumb(thumbnail, title, userid, ctitle) image = await thumb(thumbnail, title, userid, ctitle)
pos = add_to_queue(chat_id, songname, dl, link, "Audio", 0) 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})" 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) ctitle = await CHAT_TITLE(gcname)
title = songname title = songname
userid = m.from_user.id userid = m.from_user.id
thumbnail = f"{IMG_5}"
image = await thumb(thumbnail, title, userid, ctitle) image = await thumb(thumbnail, title, userid, ctitle)
await suhu.edit("🔄 Joining Group Call...") await suhu.edit("🔄 Joining Group Call...")
await music_on(chat_id) await music_on(chat_id)