lyrics - Search for song lyrics

The Lyrics module is part of the codefurther Python package, and is designed to be used in UK schools to provide students with access to data that describes the words to popular songs.

Lyrics is part of a wider initiative that I’m referring to as CodeFurther. The hope is that by providing simple interfaces to information that is relevant to students, they will be able to relate to the data and imagine more ways in which they could consume and use it in their code - and hopefully CodeFurther.

The data that Lyrics accesses is provided by Wikia and should be accessed as as part of its Lyrics Wikia site.


Features

Lyrics provides:

  • Search for an artist on Lyrics Wikia
  • Search for the lyrics of a song by a specific artist
  • Search for all of the songs by a specific artist that are present on Lyrics Wikia.

Usage

Directions exposes a very simple API to developers. It is accessed by importing the Lyrics class into your module and creating an instance of this class, like so:

from codefurther.lyrics import Lyrics
lyrics_machine = Lyrics()

To print out the lyrics to a song, the song_lyrics() method is used to to find the song, given the name of the artist and the name of the song.:

lyrics_list = lyrics_machine.song_lyrics("billy bragg", "days like these")

The lyrics are returned as a list of str items, and be printed simply, like so.:

for line in lyrics_list:
    print( line )

The Lyrics instance exposes a number of properties to the programmer. These include:

The example code below shows how you can use these properties. This code, simply returns the lyrics to a song, given the name of the artist and the name of the song.:

from codefurther.lyrics import Lyrics

lyrics_machine = Lyrics()

lyrics_list = lyrics_machine.song_lyrics("billy bragg", "days like these")

for count, line in enumerate(lyrics_list):
    print(
        "{}. {}".format(
            count+1,
            line
        )
    )

This results in the following output.:

1. The party that became so powerful
2. By sinking foreign boats
3. Is dreaming up new promises
4. Because promises win votes
.
.
.
31. Peace, bread, work, and freedom
32. Is the best we can achieve
33. And wearing badges is not enough
34. In days like these

The following code find all of the songs for a given artist.:

from codefurther.lyrics import Lyrics

lyrics_machine = Lyrics()

song_list = lyrics_machine.artist_songs("billy bragg")

for count, song in enumerate(song_list):
    print(
        "{}. {}".format(
            count+1,
            song
        )
    )

Resulting in the following output.:

1. The Milkman of Human Kindness
2. To Have and to Have Not
3. Richard
.
.
.
396. To Have And Have Not
397. Walk Away Renee
398. Youngest Son

This code allows the programmer to search for the exact name of an artist.:

from codefurther.lyrics import Lyrics

lyrics_machine = Lyrics()

artist_details = lyrics_machine.artist_search("Billy Bragg")

print(artist_details)

If an exact match of the artist is not found, then the nearest match is returned.


Lyrics API

Return the lyrics given an artist and song.

class lyrics.Lyrics(base_url='http://cflyricsserver.herokuapp.com/lyricsapi/')

Provides the programmer with properties that return lyrics from the Wikia site.

The programmer creates an instance of this object, and then uses the exposed properties to access the data about the lyrics.

error_format

str

The format string to be used when creating error messages.

bad_response

str

The text to be used in the raised error if the server response is unexpected.

artist_exists(artist)

Determines whether an artist exists in Lyrics Wikia USING THE SPELLING and puntuation provided.

Proceed with a little caution as I’m not completely sure that these results are accurate.

Returns True if the artist specified is found exactly. It may be that the artist is known by another, similar name.

Parameters:artist – (str) The name of the artist being searched for.
Returns:(bool): If the artist was found exactly as named in the search results, then True is returned, otherwise False is returned.
Return type:result

Returns the first result of a search for the given artist on Lyrics Wikia.

Proceed with a little caution as I’m not completely sure that these results are accurate.

Returns a string containing the search result. The actual string returned depends on what the search functionality at Lyrics Wikia returns.

Parameters:artist – (str) The name of the artist being searched for.
Returns:(str): The result of the search. If the string contains a colon :, then it typically means that an artist and song has been returned, separated by the colon. If a string is returned without a colon, then it likely means that only an artist match was found, but the artise returned should be checked to see if it is the same as the artist that was searched for.
Return type:result
artist_songs(artist)

Returns a generator that yields song titles for the given artist.

If the all_details flag is set to True, then a dict is returned that contains. Returns an empty generator if no songs were found for the specified artist.

Parameters:

artist – (str) The name of the artist for the song being looked up.

Returns:

(list): A list of str representing each song of the artist.

Return type:

song_list

Raises:
  • ValueError – If artist is None or "" (empty).
  • ValueError – If the response from the server is not in the correct format.
song_lyrics(artist, title)

Return a list of string lyrics for the given artist and song title.

Parameters:
  • artist – (str) The name of the artist for the song being looked up.
  • title – (str) The name of the song being looked up.
Returns:

(list) of (str) one for each lyric line in the song. Blank lines can be returned to space verses from the chorus, etc.