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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
67 changes: 42 additions & 25 deletions frontend/js/src/common/brainzplayer/AppleMusicPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { get as _get, deburr, escapeRegExp, isString } from "lodash";
import { get as _get, escapeRegExp, isString } from "lodash";
import { faApple } from "@fortawesome/free-brands-svg-icons";
import { Link } from "react-router-dom";
import fuzzysort from "fuzzysort";
Expand Down 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,42 +207,61 @@ export default class AppleMusicPlayer
`/v1/catalog/{{storefrontId}}/search`,
{ term: searchTerm, types: "songs" }
);
// Remove accents from both the search term and the API results
const trackNameWithoutAccents = deburr(trackName);
const releaseName = _get(listen, "track_metadata.release_name", "");
const candidateMatches = response?.data?.results?.songs?.data.map(
(candidate) => ({
...candidate,
attributes: {
...candidate.attributes,
name: deburr(candidate.attributes.name),
name: candidate.attributes.name,
albumName: 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;

let fuzzyMatches;
if (releaseName) {
// If we have a release name, search for both track and album
fuzzyMatches = fuzzysort.go(
`${trackName} ${releaseName}`,
candidateMatches,
{
keys: ["attributes.name", "attributes.albumName"],
scoreFn: (a) => {
const NO_MATCH = -Infinity;
const trackScore = a[0]?.score ?? NO_MATCH;
const albumScore = a[1]?.score ?? NO_MATCH;

return trackScore + (albumScore > 0 ? albumScore * 0.8 : 0);
},
}
);
}
// Fallback to best fuzzy match based on track title
const fruzzyMatches = fuzzysort.go(
trackNameWithoutAccents,
candidateMatches,
{
key: "attributes.name",
limit: 1,
if (!fuzzyMatches || !fuzzyMatches.length) {
// Check if the first API result is a match
if (
new RegExp(escapeRegExp(trackName), "igu").test(
candidateMatches?.[0]?.attributes.name
)
) {
// First result matches track title, assume it's the correct result
await this.playAppleMusicId(candidateMatches[0].id);
return;
}
);
if (fruzzyMatches[0]) {
await this.playAppleMusicId(fruzzyMatches[0].obj.id);
// Otherwise just search for track name
fuzzyMatches = fuzzysort.go(trackName, candidateMatches, {
key: "attributes.name",
});
}

// 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
} catch (error) {
// eslint-disable-next-line no-console
console.debug("Apple Music API request failed:", error);
}
onTrackNotFound();
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"fetch-retry": "^5.0.3",
"file-saver": "^2.0.5",
"fuse.js": "^7.0.0",
"fuzzysort": "^3.0.1",
"fuzzysort": "^3.1.0",
"he": "^1.2.0",
"highlight.js": "^11.6.0",
"html2canvas": "^1.4.1",
Expand Down