dw26
dw26

Reputation: 1

Musixmatch API not returning lyrics for a known song even though it exists on Musixmatch

I've been working with the Musixmatch API to retrieve lyrics for various songs. While the API works perfectly for some songs, it fails to return lyrics for certain tracks even though these tracks clearly exist on the Musixmatch website.

For example, the song "Ride" by "Lana Del Rey" is available on the Musixmatch website (URL: https://www.musixmatch.com/lyrics/Lana-Del-Rey/Ride), but when I try to fetch the lyrics using the API, it returns either "No lyrics found" or a 404 status code. Here's the code I'm using:

import requests
import time

def get_lyrics(track_title, artist_name):
    base_url = "https://api.musixmatch.com/ws/1.1/"
    lyrics_url = f"{base_url}matcher.lyrics.get"
    
    params = {
        'q_track': track_title,
        'q_artist': artist_name,
        'apikey': 'mykey'  # Replaced with my actual API key
    }
    
    for attempt in range(3):  # Retry up to 3 times
        response = requests.get(lyrics_url, params=params)
        
        if response.status_code == 200:
            try:
                json_response = response.json()
                body = json_response['message']['body']
                
                if isinstance(body, dict) and 'lyrics' in body:
                    lyrics = body['lyrics'].get('lyrics_body', 'No lyrics found.')
                    return lyrics
                else:
                    return "No lyrics found."
            
            except ValueError:
                return "No lyrics found or invalid response format."
        
        elif response.status_code == 404:
            return "Lyrics not found for this track."
        
        elif response.status_code == 500:
            print("Server error (500): Retrying...")
            time.sleep(2)
        
        else:
            return f"Error: API request failed with status code {response.status_code}"
    
    return "Error: Could not retrieve lyrics after multiple attempts."

# Testing the function
lyrics = get_lyrics("ride", "lana del rey")
print(lyrics)

Despite the fact that the song "Ride" by "Lana Del Rey" is available on Musixmatch, the API does not return the lyrics and instead results in "No lyrics found." This issue occurs for other known tracks as well.

** I understand the API can't return full lyrics of song.

  1. Is there a known issue with the Musixmatch API where certain songs are available on the website but not retrievable via the API?

  2. Could this be related to regional restrictions or some kind of data synchronization issue between the Musixmatch website and their API?

  3. Are there any workarounds or additional steps I should consider to ensure that these lyrics can be retrieved?

Upvotes: 0

Views: 54

Answers (1)

Obaskly
Obaskly

Reputation: 724

As I mentioned before, your script relies on the matcher.lyrics.get endpoint which attempts to match the song title and artist directly. It can fail due to slight discrepancies in the title, artist name or maybe API's limitations in finding an exact match. So even though the song exists on Musixmatch the API returned "No lyrics found" or a 404 error.

I tried the search for the track ID using the track.search endpoint and then fetch the lyrics using that specific ID with the track.lyrics.get endpoint. And the script worked as expected

import requests

def search_and_get_lyrics(track_title, artist_name, api_key):
    base_url = "https://api.musixmatch.com/ws/1.1/"
    
    # Search for the track ID
    search_params = {
        'q_track': track_title,
        'q_artist': artist_name,
        'f_has_lyrics': 1,
        'apikey': api_key,
        'page_size': 1,  # Only get the top result
    }
    search_response = requests.get(f"{base_url}track.search", params=search_params).json()
    tracks = search_response['message']['body']['track_list']
    
    if not tracks:
        return "Track ID not found. Unable to retrieve lyrics."

    track_id = tracks[0]['track']['track_id']
    
    # Get the lyrics using the track ID
    lyrics_params = {'track_id': track_id, 'apikey': api_key}
    lyrics_response = requests.get(f"{base_url}track.lyrics.get", params=lyrics_params).json()
    lyrics_body = lyrics_response['message']['body']['lyrics']['lyrics_body']
    
    return lyrics_body or "No lyrics found."

api_key = 'api'

# Test
lyrics = search_and_get_lyrics("Ride", "Lana Del Rey", api_key)
print(lyrics)

Output:

Mm-mm-mm-mm, mm-mm-mm,
Mm-mm-mm-mm, mm-mm-mm.

I've been out on that open road,
You can be my full time daddy,
White and gold.

Singing blues has been gettin' old,
You can be my full time baby,
Hot or cold.

Don't break me down,
(Don't break me down),
I been travelling too long,
(I been travelling too long),
I've been tryin' too hard,
(I've been tryin' too hard),
With one pretty song,
(With one pretty song, oh).

I hear the birds on the summer breeze,
I drive fast, I am alone at midnight.
...

******* This Lyrics is NOT for Commercial use *******
(1409624878984)

Upvotes: 0

Related Questions