searchArtist method
Searches for a specific artist and gets their songs.
This method looks for the artist by the name or by the ID if it's provided in artistId
.
It returrns an Artist
object if the search is successful and null
otherwise.
maxSongs
specifies the max number of songs it should get
sort
to sort the songs obtained by popularity, title or release date
artistId
allows user to pass an artist ID.
includeFeatures
f True, includes tracks featuring the artist
///Example:
{@tool snippet}
Genius genius = Genius(accessToken: TOKEN);
Artist? artist = await genius.searchArtist(artistName: 'Eminem', maxSongs: 10);
if (artist != null) {
for (var song in artist.songs) {
print(song.lyrics);
}
}
{@end-tool}
Implementation
/// if (artist != null) {
/// for (var song in artist.songs) {
/// print(song.lyrics);
/// }
/// }
/// ```
/// {@end-tool}
Future<Artist?> searchArtist(
{required String artistName,
int? maxSongs,
SongsSorting sort = SongsSorting.popularity,
int perPage = 20,
bool getFullInfo = true,
int? artistId,
bool includeFeatures = false}) async {
if (artistId == null) {
_verbosePrint('Searching for songs by $artistName');
Map<String, dynamic>? response =
(await _searchAll(searchTerm: artistName));
if (response != null) {
artistId = _getItemFromSearchResponse(
response: response,
searchTerm: artistName,
type: 'artist',
resultType: 'name')?['id'];
}
}
if (artistId == null) {
return _verbosePrint("No results found for $artistName");
}
Map<String, dynamic>? artistInfo = (await artist(artistId: artistId));
if (artistInfo == null) {
return _verbosePrint("No results found for the artist");
}
Artist artistFound = Artist(artistInfo: artistInfo);
int? page = 1;
if (maxSongs != null) {
if (maxSongs > 50) {
maxSongs = 50;
}
bool reachedMaxSongs = (maxSongs == 0) ? true : false;
while (!reachedMaxSongs) {
Map<String, dynamic>? artistSongsResponse = await _artistSongsPage(
artistId: artistId, perPage: perPage, page: page!, sort: sort);
if (artistSongsResponse == null) {
return _verbosePrint('Error getting artist songs. Rejecting.');
}
List<dynamic>? songsOnPage = await artistSongsResponse['songs'];
if (songsOnPage != null) {
for (var songInfo in songsOnPage) {
if (songInfo != null) {
String? songLyrics;
if (songInfo['lyrics_state'] == 'complete' &&
songInfo['url'] != null) {
songLyrics = await lyrics(url: songInfo['url']);
} else {
if (skipNonSongs) {
_verbosePrint(
"${(songInfo['title'] ?? 'a song')} is not valid. Skipping.");
continue;
}
songLyrics = '';
}
if (getFullInfo) {
if (songInfo['id'] != null) {
Map<String, dynamic>? fullSongInfo =
await song(songId: songInfo['id']);
songInfo = fullSongInfo;
} else {
_verbosePrint(
'error getting full song info for ${songInfo['title'] ?? 'a song'}');
}
}
Song newSong = Song(songInfo: songInfo, lyrics: songLyrics ?? '');
artistFound.addSong(
newSong: newSong,
verbose: verbose,
includeFeatures: includeFeatures);
reachedMaxSongs = (artistFound.numSongs >= maxSongs);
if (reachedMaxSongs) {
_verbosePrint(
'\nReached user-specified song limit ($maxSongs).');
break;
}
}
}
}
page = artistSongsResponse['next_page'];
if (page == null) break;
}
}
return artistFound;
}