searchSong method
Searches for a specific song and gets its lyrics returning Song
in case it's successful and null
otherwise .
You must pass either a title
or a songId
.
title: Song title to search for.
Args:
artist
(optional): Name of the artist.
getFullInfo
(optional): Get full info for each song (slower), if songId is provided full info of the song will be obtained by deafult.
songId
(optional): Song ID.
Example: {@tool snippet}
Genius genius = Genius(accessToken: TOKEN);
Song? song = (await genius.searchSong(artist: 'Eminem', title: 'Beautiful'));
if (song != null) {
print(song.lyrics);
}
{@end-tool}
Implementation
Future<Song?> searchSong(
{String? artist,
String? title,
int? songId,
bool getFullInfo = true}) async {
try {
Map<String, dynamic>? songInfo;
if (songId == null && title == null) {
return _verbosePrint(
'Specified song does not contain lyrics. Rejecting.');
}
if (songId != null) {
getFullInfo = false;
songInfo = (await song(songId: songId));
} else {
Map<String, dynamic>? serachResponse =
(await _searchAll(searchTerm: '$title $artist'));
if (serachResponse != null) {
songInfo = _getItemFromSearchResponse(
response: serachResponse,
searchTerm: title!,
type: 'song',
resultType: 'title',
artist: artist,
);
}
}
if (songInfo == null) {
return _verbosePrint('No result found');
}
if (songInfo['lyrics_state'] != 'complete' && skipNonSongs) {
return _verbosePrint(
'Specified song does not contain lyrics. Rejecting.');
}
songId = songInfo['id'];
if (songId != null && getFullInfo) {
Map<String, dynamic>? fullSongInfo = await song(songId: songId);
if (fullSongInfo != null) {
songInfo = fullSongInfo;
} else {
_verbosePrint('error getting full song info');
}
}
String? url = (songInfo['url']);
if (url == null) {
return _verbosePrint('Song url not found. Rejecting.');
}
return Song(songInfo: songInfo, lyrics: (await lyrics(url: url)) ?? "");
} catch (e) {
return _verbosePrint('Error: ${e.toString()}');
}
}