Song¶
This is the Song object which holds the details of the song.
Properties¶
Title of the song. |
|
Primary artist on the song. |
|
Full set of song lyrics. |
|
Name of the album the song is on. |
|
Year the song was released. |
|
URL to the song on Genius. |
|
URL to the song album. |
|
Artists featured on the song. |
|
Producers of the song. |
|
External IDs of the song. |
|
List of artists credited as writers. |
|
URL to the song’s cover art. |
Methods¶
Creates a dictionary from the song object. |
|
Converts the Song object to a json string. |
|
Saves the song lyrics as a text file. |
|
Save Song lyrics and metadata to a JSON or TXT file. |
-
class
lyricsgenius.song.
Song
(json_dict, lyrics='')¶ A song from the Genius.com database.
- Returns
-
property
title
¶ Title of the song.
- Returns
str
-
property
artist
¶ Primary artist on the song.
- Returns
str
-
property
lyrics
¶ Full set of song lyrics.
- Returns
str
-
property
album
¶ Name of the album the song is on.
- Returns
str
|None
-
property
year
¶ Year the song was released.
- Returns
str
-
property
url
¶ URL to the song on Genius.
- Returns
str
-
property
album_url
¶ URL to the song album.
- Returns
str
|None
-
property
featured_artists
¶ Artists featured on the song.
- Returns
list
-
property
producer_artists
¶ Producers of the song.
- Returns
list
-
property
media
¶ External IDs of the song. For example song’s YouTube link, Spotify ID and link.
- Returns
list
-
property
writer_artists
¶ List of artists credited as writers.
- Returns
list
-
property
song_art_image_url
¶ URL to the song’s cover art.
- Returns
str
-
to_dict
()¶ Creates a dictionary from the song object. Used in
save_lyrics()
to create json object.- Returns
dict
-
to_json
(filename=None, full_data=True, sanitize=True, ensure_ascii=True)¶ Converts the Song object to a json string.
- Parameters
filename (
str
, optional) – Output filename, a string. If not specified, the result is returned as a string.full_data (
str
) – Provides full song metadata when set to True.sanitize (
bool
, optional) – Sanitizes the filename if True.ensure_ascii (
bool
, optional) – If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped.
- Returns
If filename is None, returns the lyrics as a plain string, otherwise None.
- Return type
str
|None
Warning
If you set
sanitize
to False, the file name may contain invalid characters, and thefore cause the saving to fail.
-
to_text
(filename=None, binary_encoding=False, sanitize=True)¶ Saves the song lyrics as a text file.
- Parameters
filename (
str
, optional) – Output filename, a string. If not specified, the result is returned as a string.binary_encoding (
bool
, optional) – Enables binary encoding of text data.sanitize (
bool
, optional) – Sanitizes the filename if True.
- Returns
If
filename
is None, returns the lyrics as a plain string, otherwise None.- Return type
str
|None
Warning
If you set
sanitize
to False, the file name may contain invalid characters, and thefore cause the saving to fail.
-
save_lyrics
(filename=None, extension='json', overwrite=None, binary_encoding=False, ensure_ascii=True, full_data=True, sanitize=True, verbose=True)¶ Save Song lyrics and metadata to a JSON or TXT file.
If the extension is ‘json’ (which the default), the lyrics will be saved alongside the song’s information. Take a look at the example below.
- Parameters
filename (
str
, optional) – Output filename, a string. If not specified, the result is returned as a string.extension (
str
, optional) – Format of the file (json or txt).overwrite (
bool
, optional) – Overwrites preexisting file if True. Otherwise prompts user for input.binary_encoding (
bool
, optional) – Enables binary encoding of text data.ensure_ascii (
bool
, optional) – If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped.full_data (
str
) – Provides full song metadata when set to True.sanitize (
bool
, optional) – Sanitizes the filename if True.verbose (
bool
, optional) – prints operation result.
Examples
# getting songs lyrics from saved JSON file import json with open('song.json', 'r') as f: data = json.load(f) print(data['lyrics'])
Note
If
full_data
is set to False, only the following attributes of the song will be available:title
,album
,year
,lyrics
, andsong_art_image_url
Warning
If you set
sanitize
to False, the file name may contain invalid characters, and thefore cause the saving to fail.