/SpotifyLyrics

Get Musixmatch Lyrics Free via Spotify

Primary LanguagePython

Spotify icon

Spotify Lyrics - Python Approach

Musixmatch icon

Description

A python approach to fetch Musixmatchs' (synchronized) lyrics locally via Spotify

Usage

  • spotify_lyrics.py: Main module (SpotifyLyrics)
  • example_usage.py: Demonstrating its usage:
from spotify_lyrics import SpotifyLyrics

# Spotify Song > Right Click > Share > Copy Song Link (track_id is within link between ..track/ and ?si=..)
track_id = "48UPSzbZjgc449aqz8bxox" 

# create a .env and set your SP_DC_COOKIE there, check env.txt and further down in README.md for further explanation
sp_dc_cookie = os.getenv("SP_DC_COOKIE")

spotify = SpotifyLyrics(sp_dc_cookie)
lyrics_json = spotify.getLyrics(track_id)

ic| lyrics_json: {'colors': {'background': -16745031, 'highlightText': -1, 'text': -16777216},
                  'hasVocalRemoval': False,
                  'lyrics': {'alternatives': [],
                             'capStatus': 'NONE',
                             'fullscreenAction': 'FULLSCREEN_LYRICS',
                             'isDenseTypeface': False,
                             'isRtlLanguage': False,
                             'language': 'en',
                             'lines': [{'endTimeMs': '0',
                                        'startTimeMs': '20530',
                                        'syllables': [],
                                        'words': 'Psychic spies from China try to steal your '
                                                 "mind's elation"},
                                       {'endTimeMs': '0',
                                        'startTimeMs': '25400',
                                        'syllables': [],
                                        'words': 'And little girls from Sweden dream of '     
                                                 'silver-screen quotation'},
                                       {'endTimeMs': '0',
                                      .
                                      .
                                      .
lyrics_tupel_array = spotify.convertJsonLyricsToTupelArray(lyrics_json)

ic| lyrics_tupel_array: [('20530', "Psychic spies from China try to steal your mind's elation"),
                         ('25400', 'And little girls from Sweden dream of silver-screen quotation'),
                         ('30420', 'And if you want these kind of dreams'),
                         ('33030', "It's Californication"),
                         ('36460', '♪'),
                         ('45180', "It's the edge of the world, end all of western civilization"),
                         ('50570', "The Sun may rise in the East, at least it's settled in the final location"),
                         ('55650', "It's understood that Hollywood sells Californication"),
                         ('61380', '♪'),
                         ('70890', 'Pay your surgeon very well to break the spell of aging'),
                         ('75430', "Celebrity skin, is this your chin, or is that war you're waging?"),
                         ('81770', 'Firstborn unicorn'),
                         ('85860', 'Hardcore soft-porn'),
                         ('90450', 'Dream of Californication'),
                         .
                         .
                         .

Clone the repository or simple copy spotify_lyrics.py into your project and install the required dependencies. Make sure to obtain your SP_DC cookie and set it in your environment variables as SP_DC_COOKIE. Example usage can be found above and in example_usage.py. I recommend using the icecream module for such lyrics projects as it can also structure your print/log statements and makes reading and debugging lyrics easier.

Further Explanation

  • SP_DC Cookie: This cookie is used to obtain the access token required to request lyrics via Spotifys web player. A detailed guide on how to find the SP_DC cookie was provided by akashrchandran -> here. You need to manually retrieve this cookie from a browser session where you are logged into Spotify. (Cookie is valid for about a year). There are no straightforward methods to retrieve the SP_DC cookie programmatically due to the way web cookies are managed.

  • Access Token: Once the SP_DC cookie is obtained, it is used to request an access token from Spotify. This token is then used to authenticate and authorize requests for fetching lyrics.

Disclaimer

This approach is not novel and takes inspiration from various GitHub projects that focus on fetching synchronized lyrics via Spotify. Notably, it draws from the work by akashrchandran, as seen in his spotify-lyrics-api project using PHP where he also provides a Rest API endpoint. My particular implementation offers a local, lightweight python approach.

Please note that this method involves accessing Musixmatch API synced lyrics via Spotify and might fall into a legal grey area (IANAL). It is recommended to use this for personal, educational, or research purposes only, and not in a production environment.