简体   繁体   中英

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

I want to get the URL or bytes of the Telegram audio documents (music) album cover using MTProto API and Telethon Python lib, but I could not find such a thing by checking audio message properties. There was a thumbs property for the message attached media property that was null.

Note: When I download a media using client.download_media the cover is attached to the file, but I do not want to download it.

电报音频/音乐专辑封面 电报音频/音乐专辑封面

It's not through MTproto. each Telegram client gets it manually from a different provider, I'm going with Android implementation.

small is chat UI thumb, other is for player size. This is only a test for the track in your screenshot.

Deal with commented parts yourself and pass a valid message object, and use aiohttp instead and make this async func.

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

prints: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM