Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve apple music fuzzy matching #3011

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions frontend/js/src/common/brainzplayer/AppleMusicPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ export default class AppleMusicPlayer
const { onTrackNotFound } = this.props;
const trackName = getTrackName(listen);
const artistName = getArtistName(listen);
// Album name can give worse results, re;oving it from search terms
// const releaseName = _get(listen, "track_metadata.release_name");
const searchTerm = `${trackName} ${artistName}`;
if (!searchTerm) {
onTrackNotFound();
Expand All @@ -209,38 +207,44 @@ export default class AppleMusicPlayer
`/v1/catalog/{{storefrontId}}/search`,
{ term: searchTerm, types: "songs" }
);
const releaseName = _get(listen, "track_metadata.release_name");
// Remove accents from both the search term and the API results
const trackNameWithoutAccents = deburr(trackName);
const releaseNameWithoutAccents = deburr(releaseName);
const candidateMatches = response?.data?.results?.songs?.data.map(
(candidate) => ({
...candidate,
attributes: {
...candidate.attributes,
name: deburr(candidate.attributes.name),
albumName: deburr(candidate.attributes.albumName),
},
})
);
// Check if the first API result is a match
if (
new RegExp(escapeRegExp(trackNameWithoutAccents), "igu").test(
candidateMatches?.[0]?.attributes.name
)
) {
// First result matches track title, assume it's the correct result
await this.playAppleMusicId(candidateMatches[0].id);
return;
}
// Fallback to best fuzzy match based on track title
const fruzzyMatches = fuzzysort.go(
const fuzzyMatches = fuzzysort.go(
trackNameWithoutAccents,
candidateMatches,
{
key: "attributes.name",
limit: 1,
}
);
if (fruzzyMatches[0]) {
await this.playAppleMusicId(fruzzyMatches[0].obj.id);

const matchWithAlbum = fuzzyMatches.find((match) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably only be run if releaseName is truthy (I believe it is optional in the listen metadata so could potentially be undefined/null).

I'm also reading the documentation for fuzzysort, and I wonder if instead of a secondary pass, this couldn't all be achieved in a single pass (when calling fuzzysort.go above )by:

  • using both keys attributes.name and attribute.albumName
  • passing a custom scoreFn that boosts results that match the albumName

(See 'advanced usage' section https://github.com/farzher/fuzzysort?tab=readme-ov-file#advanced-usage )

const albumMatch = fuzzysort.single(
releaseNameWithoutAccents,
match.obj.attributes.albumName
);
return albumMatch && albumMatch.score > 0.8;
});

if (matchWithAlbum) {
await this.playAppleMusicId(matchWithAlbum.obj.id);
return;
}

// If no match found with album, play the best track match
if (fuzzyMatches[0]) {
await this.playAppleMusicId(fuzzyMatches[0].obj.id);
return;
}
// No good match, onTrackNotFound will be called in the code block below
Expand Down