Artist¶
The Artist object which holds the details of the artist and the Song objects of that artist.
Properties¶
Artist’s name. |
|
URL to the artist’s image |
|
Song objects saved for the artist. |
Methods¶
Gets the artist’s song. |
|
Adds a song to the Artist. |
|
Converts the Song object to a json string. |
|
Converts all song lyrics to a single string. |
|
Saves all lyrics within an Artist object to a single file. |
-
class
lyricsgenius.artist.
Artist
(client, json_dict)¶ An artist with songs from the Genius.com database.
- Returns
Artist object contatining artist data and song lyrics.
- Return type
-
property
name
¶ Artist’s name.
- Returns
str
-
property
image_url
¶ URL to the artist’s image
- Returns
str
|None
-
property
songs
¶ Song objects saved for the artist.
- Returns
A list contatining
Song
objects.- Return type
list
-
property
num_songs
¶ Number of the songs in the Artist object. Equivolant to len(artist.songs)
- Returns
int
-
add_song
(new_song, verbose=True, include_features=False)¶ Adds a song to the Artist.
This method adds a new song to the artist object. It checks if the song is already in artist’s songs and whether the song’s artist is the same as the Artist object.
- Parameters
new_song (
Song
) – Song to be added.verbose (
bool
, optional) – prints operation result.include_features (
bool
, optional) – If True, includes tracks featuring the artist.
- Returns
0 for success and 1 for failure.
- Return type
int
Examples
genius = Genius(token) artist = genius.search_artist('Andy Shauf', max_songs=3) # Way 1 song = genius.search_song('To You', artist.name) artist.add_song(song) # Way 2 artist.add_song('To You')
-
song
(song_name)¶ Gets the artist’s song.
If the song is in the artist’s songs, returns the song. Otherwise searches Genius for the song and then returns the song.
- Parameters
song_name (
str
) – name of the song. the result is returned as a string.sanitize (
bool
) – Sanitizes the filename if True.
- Returns
- If it can’t find the song,
returns None.
- Return type
Song
|None
-
to_json
(filename=None, 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.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)¶ Converts all song lyrics to a single string.
- 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
| :obj: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=False, binary_encoding=False, ensure_ascii=True, sanitize=True, verbose=True)¶ Saves all lyrics within an Artist object to a single file. If the extension is ‘json’, the method will save artist information and artist songs. If you only want the songs lyrics, set
extension
to txt. If you choose to go with JSON (which is the default extension), you can access the lyrics by accessing theSong
objects inside the songs key of the JSON file. 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.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('file.json', 'r') as f: data = json.load(f) for song in data['songs']: print(song.lyrics)
Warning
If you set
sanitize
to False, the file name may contain invalid characters, and thefore cause the saving process to fail.