searchForTrackFromResult method
- Result result, [
- int itemCount = 5,
- int durationDelta = 15,
- double commonArtistThreshold = 0.5,
- bool albumMatchRequired = false,
override
Searches for tracks from a Result. Usually more accurate than searchForTrack.
Implementation
@override
Future<List<SpotifyResult>> searchForTrackFromResult(
Result result, [
int itemCount = 5,
int durationDelta = 15,
double commonArtistThreshold = 0.5,
bool albumMatchRequired = false,
]) async {
var searchQuery = await constructSearchQuery(result);
var results = await searchForTrack(searchQuery, itemCount);
var filteredResults = <SpotifyResult>[];
for (var spotifyResult in results) {
// Filter out results with more than durationDelta seconds difference.
if ((spotifyResult.sDuration - result.sDuration).abs() >= durationDelta) {
continue;
}
// Filter out results with less than commonArtistThreshold common artists.
var resultArtists = result.artists.map((artist) => artist.toLowerCase());
var spotifyArtists = spotifyResult.artists.map((sArtist) => sArtist.toLowerCase());
var commonArtists = resultArtists.where((artist) => spotifyArtists.contains(artist));
if (commonArtists.length < (resultArtists.length * commonArtistThreshold).round()) {
continue;
}
// Album Match depending on input parameters.
if (spotifyResult.album.toLowerCase() != result.album!.toLowerCase() && albumMatchRequired) {
continue;
}
// If a result passes all filters, add to filtered results.
filteredResults.add(spotifyResult);
}
return filteredResults;
}