[feat] Show tg audio's thumbnail

This commit is contained in:
xtaodada 2022-02-13 16:22:10 +08:00
parent 72d1cc6afd
commit cc8f002851
No known key found for this signature in database
GPG Key ID: EE4DC37B55E24736
2 changed files with 35 additions and 19 deletions

View File

@ -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

View File

@ -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)