Skip to content

improved spotify api queries for better results#26

Open
Smekker11 wants to merge 2 commits into
lacymorrow:masterfrom
Smekker11:master
Open

improved spotify api queries for better results#26
Smekker11 wants to merge 2 commits into
lacymorrow:masterfrom
Smekker11:master

Conversation

@Smekker11
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the search logic to improve artist matching by focusing on the primary artist and implementing a fallback query mechanism. It also introduces an extractImage helper to handle image selection based on size and artist name. Several issues were identified: a potential runtime error when accessing images from items that lack them, redundant variable assignments and string replacements, and a regression where the search method is hardcoded to 'album', which prevents retrieving artist profile images and breaks compatibility with the existing logic.

Comment thread index.js Outdated
Comment on lines +126 to +128
const images = match.images

let smallest = images[0]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Accessing images[0] without checking if the array exists or is empty can lead to a runtime error when trying to access .url later (e.g., at line 149 or 159). Adding a safety check ensures the function handles items without images gracefully.

			const images = match.images || []

			if ( images.length === 0 ) {

				return null

			}

			let smallest = images[0]

Comment thread index.js Outdated

// Default options
let query = artist.replace( '&', 'and' )
let query = artist.replace( '&', ';' )
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The initial assignment to query is redundant because the variable is completely overwritten in all logic branches below (lines 81, 85, and 92).

Suggested change
let query = artist.replace( '&', ';' )
let query

Comment thread index.js Outdated
let method = 'artist'
// Use only primary artist for better matching
const primaryArtist = artist.split( /[;,&]/ )[0].trim()
let primaryQuery = primaryArtist.replace( '&', ';' )
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The .replace( '&', ';' ) call is redundant here. primaryArtist is derived from splitting the artist string using & as a delimiter (line 67), so it cannot contain an ampersand.

Suggested change
let primaryQuery = primaryArtist.replace( '&', ';' )
let primaryQuery = primaryArtist

Comment thread index.js Outdated
const primaryArtist = artist.split( /[;,&]/ )[0].trim()
let primaryQuery = primaryArtist.replace( '&', ';' )

let method = 'album'
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The search method is now hardcoded to 'album', whereas it previously defaulted to 'artist' when no album was specified. This change prevents the library from retrieving artist profile images. Furthermore, the extractImage helper (line 123) relies on the artists property, which exists on album objects but not on artist objects, making this logic tightly coupled to the 'album' search type. If this regression is intentional, it should be documented as a breaking change.

Copy link
Copy Markdown
Owner

@lacymorrow lacymorrow left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! The intent (better matching via field-qualified queries + fallback) is good, but there are a few issues to address before this can land:

Blocking

  1. Regression: artist-only lookup is broken. When opts.album is null, method is now hardcoded to 'album' (it used to default to 'artist'). That removes the ability to fetch artist profile images, and the new extractImage reads item.artists — a field that doesn't exist on artist search results — so the artist code path can't work at all. Please restore the method = 'artist' default and branch extractImage accordingly (artist results don't have an artists array; match against item.name).

  2. Crash risk in extractImage. match.images can be missing or empty for some Spotify items. images[0].url will throw. Guard with something like:

    const images = (match && match.images) || []
    if (images.length === 0) return null

Should fix

  1. '&' → ';' substitution is suspicious. The original code used 'and', which is meaningful to Spotify's tokenizer; ; isn't, and String.prototype.replace with a string only replaces the first occurrence. If the goal is to normalize multi-artist strings, use replaceAll('&', 'and') (or a regex) and document why.

  2. Redundant let query = artist.replace('&', ';') at the top of the function — overwritten in every branch below. Replace with let query.

  3. Redundant .replace('&', ';') on primaryArtistprimaryArtist is derived by splitting on &, so it can't contain one. Just use primaryArtist.

  4. Trailing newline removed at EOF — please restore.

Nit

  • limit=10 is reasonable, but consider documenting the matching strategy in a comment so the next reader understands why extractImage looks for an exact (case-insensitive) artist-name match before falling back to items[0].

Happy to re-review once these are addressed.

- Restore method='artist' default when opts.album is null
- Branch extractImage for artist results (match item.name, not item.artists)
- Guard match.images: fallback to [] and return null if empty
- Remove redundant initial query assignment (overwritten in every branch)
- Remove redundant primaryQuery variable (primaryArtist already & -free via split)
- Restore trailing newline at EOF
- Add comment documenting extractImage matching strategy
@lacymorrow
Copy link
Copy Markdown
Owner

All the review feedback is addressed in bc2f766.

Blocking fixes:

  • method defaults to 'artist' again — only switches to 'album' when opts.album is provided
  • extractImage branches on method: artist results match against item.name, album results use item.artists[].name
  • match.images is now guarded — falls back to [] and returns null early if empty

Should-fix items:

  • Initial let query = artist.replace(...) replaced with a bare let query
  • primaryQuery removed — primaryArtist is already split on &, so the .replace call was a no-op; queries now use primaryArtist directly
  • Trailing newline restored

Nit:

  • Three-line comment added above extractImage explaining the matching strategy: exact name match first, items[0] fallback, and why the artist/album branch is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants