sferaggio / deezer-flac-download

A program to freely download Deezer FLAC files.
Do What The F*ck You Want To Public License
24 stars 7 forks source link

Get albums (or tracks directly) from playlist #7

Open dnk8n opened 4 weeks ago

dnk8n commented 4 weeks ago

I explored with some python code, the version that gets albums from playlist:


import httpx

def get_album_info(playlist_id):
    # Step1: Try get all info from API first
    album_info = get_album_info_from_playlist(playlist_id)
    # Step2: Check through each id to make sure there is data
    for album_id, (artist_name, album_title) in album_info.copy().items():
        test = get_album_info_raw(album_id)
        if test.get("error"):
            print(f"BAD ALBUM: {album_id}")
            print(f"    artist_name: {artist_name}")
            print(f"    album_title: {album_title}")
            del album_info[album_id]
            searched_album_info = search_album_info(artist_name, album_title)
            if searched_album_info:
                album_info[searched_album_info["album"]["id"]] = (searched_album_info["artist"]["name"], searched_album_info["album"]["title"])
    return album_info

def search_album_info(artist_name, album_title):
    url = f'https://api.deezer.com/search?q=artist:"{artist_name}" album:"{album_title}"'
    print(f'    SEARCHING: {url}')
    with httpx.Client() as client:
        response = client.get(url)
        # We assume that the first entry is acceptable, as the closest match (web often gives a different result though)
        # If no matches try again with revised album title if possibel, assume artist name won't need iteration
        try:
            return response.json()["data"][0]
        except IndexError:
            revised_album_title = ' '.join(album_title.split()[:-1])
            if revised_album_title:
                return search_album_info(artist_name, revised_album_title)
            else:
                return None

def get_album_info_raw(album_id):
    url = f"https://api.deezer.com/album/{album_id}"
    with httpx.Client() as client:
        response = client.get(url)
        return response.json()

def get_album_info_from_playlist(playlist_id):
    url = f"https://api.deezer.com/playlist/{playlist_id}"
    with httpx.Client() as client:
        response = client.get(url)
        # NOTE: This has the added benefit of collapsing duplicate albums if the playlist contains more than 1 song from that album
        return {t["album"]["id"]: (t["artist"]["name"], t["album"]["title"]) for t in response.json()['tracks']['data']}

# Example usage:
playlist_id = 1282495565  # Replace with playlist id
album_info = get_album_info(playlist_id)
print(album_info)
print(' '.join([str(a) for a in album_info.keys()]))

This allows me to get a verified list of album ids, which I can then feed into the main Go program.

dnk8n commented 4 weeks ago

Careful, after reading the docs, the search should be:

url = f'https://api.deezer.com/search?q=artist:"{artist_name}" album:"{album_title}"'

not: url = f'https://api.deezer.com/search?q=artist:"{artist_name}"&q=album:"{album_title}"'

Updated.