[spotify] Select best artwork size even when not ordered descending

Spotify web API docs say that images are ordered wideest first, but that isn't
happening right now, so don't rely on that for size selection.

Also makes sure we pick an image even when max_w is smaller than smallest
available image.
This commit is contained in:
ejurgensen 2022-10-30 20:31:58 +01:00
parent 49f442bf9e
commit d468fd358e

View File

@ -381,7 +381,7 @@ request_endpoint(const char *uri)
goto out; goto out;
} }
// DPRINTF(E_DBG, L_SPOTIFY, "Wep api response for '%s'\n%s\n", uri, response_body); // DPRINTF(E_DBG, L_SPOTIFY, "Web api response for '%s'\n%s\n", uri, response_body);
json_response = json_tokener_parse(response_body); json_response = json_tokener_parse(response_body);
if (!json_response) if (!json_response)
@ -672,9 +672,10 @@ get_album_image(json_object *jsonalbum, int max_w)
json_object *jsonimage; json_object *jsonimage;
int image_count; int image_count;
int index; int index;
const char *artwork_url; int width;
int candidate_width = 0;
artwork_url = NULL; const char *artwork_url = NULL;
bool use_image;
if (!json_object_object_get_ex(jsonalbum, "images", &jsonimages)) if (!json_object_object_get_ex(jsonalbum, "images", &jsonimages))
{ {
@ -682,24 +683,33 @@ get_album_image(json_object *jsonalbum, int max_w)
return NULL; return NULL;
} }
// Find first image that has a smaller width than the given max_w // Find first image that has a smaller width than the given max_w (this should
// (this should avoid the need for resizing and improve performance at the cost of some quality loss) // avoid the need for resizing and improve performance at the cost of some
// Note that Spotify returns the images ordered descending by width (widest image first) // quality loss). If no sufficiently small image available, return smallest
// Special case is if no max width (max_w = 0) is given, the widest images will be used // best alternative. Special case is if no max width (max_w = 0) is given, the
// widest images will be used.
//
// Note that Spotify should return the images ordered descending by width
// (widest image first), but at one point had a bug that meant they didn't, so
// we don't rely on that here.
image_count = json_object_array_length(jsonimages); image_count = json_object_array_length(jsonimages);
for (index = 0; index < image_count; index++) for (index = 0; index < image_count; index++)
{ {
jsonimage = json_object_array_get_idx(jsonimages, index); jsonimage = json_object_array_get_idx(jsonimages, index);
if (jsonimage) if (!jsonimage)
{ continue;
artwork_url = jparse_str_from_obj(jsonimage, "url");
if (max_w <= 0 || jparse_int_from_obj(jsonimage, "width") <= max_w) width = jparse_int_from_obj(jsonimage, "width");
{
// We have the first image that has a smaller width than the given max_w use_image =
break; ((width <= max_w || max_w == 0 || candidate_width == 0) && width > candidate_width) ||
} (width > max_w && candidate_width > width);
}
if (!use_image)
continue;
candidate_width = width;
artwork_url = jparse_str_from_obj(jsonimage, "url");
} }
return artwork_url; return artwork_url;