searchAlbum method
Searches for a specific album and gets its songs.
You must pass either a name
or an albumId
.
You use name
to search for the album name and optionally along with artist
wich is the artist name
If you use the albumId
then there's no need for either a name
or a artist
If getFullInfo
is true it gets the full info for the album (slower), if albumId is provided full info of the song will be obtained by deafult.
Example:
{@tool snippet}
Genius genius = Genius(accessToken: TOKEN);
Album? album = (await genius.searchAlbum(name: 'Relapse', artist: 'Eminem'));
if (album != null) {
print(album.name);
}
{@end-tool}
Implementation
Future<Album?> searchAlbum(
{String? name,
int? albumId,
String artist = '',
bool getFullInfo = true}) async {
if (name == null && albumId == null) {
return _verbosePrint("You must pass either a `name` or an `albumId`.");
}
Map<String, dynamic>? albumInfo;
if (albumId != null) {
getFullInfo = false;
albumInfo = await album(albumId: albumId);
} else {
Map<String, dynamic>? response =
(await _searchAll(searchTerm: '$name $artist'));
if (response != null) {
albumInfo = _getItemFromSearchResponse(
response: response,
searchTerm: name!,
type: 'album',
resultType: 'name');
}
}
if (albumInfo == null) {
return _verbosePrint('No results for $name $artist. Rejecting.');
}
albumId = albumInfo['id'];
if (albumId == null) {
return _verbosePrint('Something wrong with the album id. Rejecting.');
}
List<Song> tracks = [];
int? nextPage = 1;
while (nextPage != null) {
Map<String, dynamic>? albumTracksResponse =
await _albumTracksPage(albumId: albumId, perPage: 50, page: nextPage);
if (albumTracksResponse == null) {
return _verbosePrint('Error getting album tracks. Rejecting.');
}
List<dynamic>? trakList = await albumTracksResponse['tracks'];
if (trakList != null) {
for (var track in trakList) {
Map<String, dynamic>? songInfo =
(track['song'] as Map<String, dynamic>?);
if (songInfo != null) {
String? songLyrics;
if (songInfo['lyrics_state'] == 'complete' &&
songInfo['url'] != null) {
songLyrics = await lyrics(url: songInfo['url']);
} else {
songLyrics = "";
}
track = Song(songInfo: songInfo, lyrics: songLyrics ?? '');
tracks.add(track);
}
}
}
nextPage = albumTracksResponse['next_page'];
}
if (getFullInfo) {
Map<String, dynamic>? fullAlbumInfo = await album(albumId: albumId);
if (fullAlbumInfo != null) {
albumInfo = fullAlbumInfo;
} else {
_verbosePrint('error getting full album info');
}
}
return Album(albumInfo: albumInfo, tracks: tracks);
}