簡體   English   中英

如何使用 Telethon API 客戶端獲取 Telegram 音頻(音樂)專輯封面

[英]How to get Telegram audio (music) album cover using Telethon API client

我想使用 MTProto API 和Telethon Python lib 獲取電報音頻文檔(音樂)專輯封面的 URL 或字節,但我無法通過檢查音頻消息屬性找到這樣的東西。 消息附加media屬性有一個縮略圖屬性,即thumbs

注意:當我使用client.download_media下載媒體時,封面附在文件上,但我不想下載它。

電報音頻/音樂專輯封面 電報音頻/音樂專輯封面

這不是通過 MTproto。 每個 Telegram 客戶端都從不同的提供商手動獲取它,我將使用Android實現。

small 是聊天 UI thumb,other 是玩家大小。 這只是對屏幕截圖中曲目的測試。

自己處理評論部分並傳遞有效消息 object,並改用 aiohttp 並使此異步功能。

import requests

def get_cover(msg, limit=1, small=False):
    # if not (f := msg.file) and not all((f.title, f.performer)): return
    # performer, title = f.performer, f.title 

    performer, title = 'Sixthells', 'Mayhem'
    url = f"https://itunes.apple.com/search?term={performer} - {title}&entity=song&limit={limit}"
    headers = {"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A5297c Safari/602.1"}

    req = requests.post(url, headers=headers)
    if 200 >= req.status_code < 300:
        if (x := req.json())['resultCount']:
            small_url = x['results'][0]['artworkUrl100']
            file_url = small_url if small\
                       else small_url.replace('100x100', '600x600') 
                       # Telegram default. can increase 600x600. 
            return small_url, file_url

print(*get_cover(msg=None), sep='\n')

打印: https://is2-ssl.mzstatic.com/image/thumb/Music125/v4/76/36/42/7636429b-fced-ce2f-162b-f4864a1eacee/artwork.jpg/100x100bb.jpg

https://is2-ssl.mzstatic.com/image/thumb/Music125/v4/76/36/42/7636429b-fced-ce2f-162b-f4864a1eacee/artwork.jpg/600x600bb.jpg

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM