Skip to content

Commit

Permalink
Make bandcamp search result matching more robust
Browse files Browse the repository at this point in the history
Some metadata providers like Discogs provide track names which include
traling spaces which caused the search to fail. Removing leading and
trailing spaces from track and artist names allows us to find any
previously missed matches.
  • Loading branch information
sferra committed Feb 16, 2024
1 parent d01e199 commit a93054f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 11 additions & 4 deletions packages/core/src/plugins/stream/BandcampPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BandcampPlugin } from '.';
import fn = jest.fn;
import { Bandcamp } from '../../rest';
import spyOn = jest.spyOn;
import { BandcampSearchResult } from '../../rest/Bandcamp';
import fn = jest.fn;
import spyOn = jest.spyOn;

describe('Bandcamp plugin tests', () => {
let plugin: BandcampPlugin;
Expand Down Expand Up @@ -88,8 +88,8 @@ describe('Bandcamp plugin tests', () => {
tags: []
};
const streamQuery = {
artist: 'Artist Name',
track: 'Track Name',
artist: ' Artist Name',
track: 'Track Name ',
...irrelevantSearchResultAttributes
};
const matcher = plugin.createTrackMatcher(streamQuery);
Expand Down Expand Up @@ -130,4 +130,11 @@ describe('Bandcamp plugin tests', () => {
const tracks = searchResults.filter(matcher);
expect(tracks).toEqual([matchingResult, matchingResult]);
});

test('normalizes value for matching search results', () => {
// mixed case search term padded with spaces
const termToNormalize = ' Search Term ';
const normalizedTerm = plugin.normalizeForMatching(termToNormalize);
expect(normalizedTerm).toEqual('search term');
});
});
12 changes: 8 additions & 4 deletions packages/core/src/plugins/stream/BandcampPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ class BandcampPlugin extends StreamProviderPlugin {
}

createTrackMatcher(query: StreamQuery): (item: BandcampSearchResult) => boolean {
const lowerCaseTrack: string = query.track.toLowerCase();
const lowerCaseArtist: string = query.artist.toLowerCase();
const normalizedTrack: string = this.normalizeForMatching(query.track);
const normalizedArtist: string = this.normalizeForMatching(query.artist);
return (searchResult) =>
searchResult.type === 'track' &&
searchResult.artist.toLowerCase() === lowerCaseArtist &&
searchResult.name.toLowerCase() === lowerCaseTrack;
this.normalizeForMatching(searchResult.artist) === normalizedArtist &&
this.normalizeForMatching(searchResult.name) === normalizedTrack;
}

normalizeForMatching(term: string): string {
return term.trim().toLowerCase();
}

resultToStream(result: BandcampSearchResult, stream: string, duration: number): StreamData {
Expand Down

0 comments on commit a93054f

Please sign in to comment.