-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix imprecise stream search in the Bandcamp plugin #1549
Conversation
When the name of a track was very simple, the actual search match could be on a page higher than the configured maximum. This caused the search to fail and the track wouldn't be played. The amount of search results can be greatly reduced by simply adding the artist name to the query.
Lines 18-34 in BandcampPlugin.tsOh, sweet innocent coder, Nuki spies a loop that's begging for a functional paradigm makeover! Right now, you're imperatively looping through pages as if we're back in 2005. Let's upgrade that for-loop to a modern, sleek, and asynchronous approach! Here's what Nuki suggests:
Such functional beauty, much wow! 😍 Now, bring out your refactoring spell! Replace: const limit = 5;
let tracks: BandcampSearchResult[];
const trackQuery: string = this.createTrackQuery(query);
const isMatchingTrack = this.createTrackMatcher(query);
for (let page = 0; page < limit; page++) {
const searchResults = await Bandcamp.search(trackQuery, page);
tracks = searchResults.filter(isMatchingTrack);
if (tracks.length > 0) {
break;
}
}
return tracks; With something more modern and functional, like: const trackQuery: string = this.createTrackQuery(query);
const isMatchingTrack = this.createTrackMatcher(query);
const limit = 5;
const trackPromises = Array.from({ length: limit }, (_, page) =>
Bandcamp.search(trackQuery, page)
);
const tracks = (await Promise.all(trackPromises))
.flat()
.filter(isMatchingTrack);
return tracks.length > 0 ? [tracks[0]] : []; This way you're fetching all pages in parallel and then stopping after finding the first match. Efficient and elegant, like Nuki's coding style! ✨ General CommentNuki is utterly bamboozled by the amount of code duplication between Do consider abstracting common logic or, even better, use some mocks (nicely done with the Keep the tests lean and mean, and the code clean! 🧼 (And always leave room for dessert 🍰!) |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #1549 +/- ##
==========================================
+ Coverage 71.58% 71.60% +0.02%
==========================================
Files 364 364
Lines 6707 6713 +6
Branches 479 479
==========================================
+ Hits 4801 4807 +6
Misses 1512 1512
Partials 394 394 ☔ View full report in Codecov by Sentry. |
When the name of a track was very simple, the actual search match could be on a page higher than the configured maximum. This caused the search to fail and the track wouldn't be played. The amount of search results can be greatly reduced by simply adding the artist name to the query.
To reproduce the issue:
The search could be additionally improved by searching only for tracks (and not artiststs and albums) when attempting to find streams, but that would require extending the bandcamp scraper module with this kind of functionality. Yet it shouldn't be too hard.
Notes: