searchForTrackFromResult method

  1. @override
Stream<LazySpotifyResult> searchForTrackFromResult(
  1. LazyResult result, [
  2. int itemCount = 5,
  3. int durationDelta = 15,
  4. double commonArtistThreshold = 0.5,
  5. bool albumMatchRequired = false,
])
override

Searches for tracks from a LazyResult. Usually more accurate than searchForTrack.

Implementation

@override
Stream<LazySpotifyResult> searchForTrackFromResult(
  LazyResult 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);

  await for (var spotifyResult in results) {
    var sDuration = await spotifyResult.sDuration();
    var rDuration = await result.sDuration();

    // Filter out results with more than durationDelta seconds difference.
    if ((sDuration - rDuration).abs() >= durationDelta) {
      continue;
    }

    // Filter out result with less than commonArtistThreshold common artists.
    var sArtists = (await spotifyResult.artists().toList()).map((artist) => artist.toLowerCase());
    var rArtists = (await result.artists().toList()).map((artist) => artist.toLowerCase());
    var cArtists = sArtists.where((sArtist) => rArtists.contains(sArtist)).length;

    if (cArtists < (rArtists.length * commonArtistThreshold).round()) {
      continue;
    }

    // Album Match depending on input parameters.
    var sAlbum = (await spotifyResult.album()).toLowerCase();
    var rAlbum = (await result.album()).toLowerCase();

    if (sAlbum != rAlbum && albumMatchRequired) {
      continue;
    }

    // Yield the result if all checks pass.
    yield spotifyResult;
  }
}