refactor: extract Spotify ID from URL instead of entityUniqueId

- Use linksByPlatform.spotify.url from Odesli response
- Extract track ID from Spotify URL using regex
- More reliable than parsing entityUniqueId format
- Matches the approach used in ConvertToSpotifyIdViaOdesliAsync
This commit is contained in:
2026-02-07 12:06:48 -05:00
parent 47b9427c20
commit 7e6bed51e1

View File

@@ -298,18 +298,21 @@ public class SquidWTFMetadataService : IMusicMetadataService
var odesliJson = await odesliResponse.Content.ReadAsStringAsync(); var odesliJson = await odesliResponse.Content.ReadAsStringAsync();
var odesliDoc = JsonDocument.Parse(odesliJson); var odesliDoc = JsonDocument.Parse(odesliJson);
// Extract Spotify track ID from the Spotify URL
if (odesliDoc.RootElement.TryGetProperty("linksByPlatform", out var platforms) && if (odesliDoc.RootElement.TryGetProperty("linksByPlatform", out var platforms) &&
platforms.TryGetProperty("spotify", out var spotifyPlatform) && platforms.TryGetProperty("spotify", out var spotifyPlatform) &&
spotifyPlatform.TryGetProperty("entityUniqueId", out var spotifyIdEl)) spotifyPlatform.TryGetProperty("url", out var spotifyUrlEl))
{ {
var rawSpotifyId = spotifyIdEl.GetString(); var spotifyUrl = spotifyUrlEl.GetString();
// Odesli returns format "SPOTIFY_SONG::trackid", extract just the track ID if (!string.IsNullOrEmpty(spotifyUrl))
if (!string.IsNullOrEmpty(rawSpotifyId))
{ {
song.SpotifyId = rawSpotifyId.Contains("::") // Extract ID from URL: https://open.spotify.com/track/{id}
? rawSpotifyId.Split("::")[1] var match = System.Text.RegularExpressions.Regex.Match(spotifyUrl, @"spotify\.com/track/([a-zA-Z0-9]+)");
: rawSpotifyId; if (match.Success)
_logger.LogInformation("✓ Converted squidwtf/{ExternalId} → Spotify ID {SpotifyId}", externalId, song.SpotifyId); {
song.SpotifyId = match.Groups[1].Value;
_logger.LogInformation("✓ Converted squidwtf/{ExternalId} → Spotify ID {SpotifyId}", externalId, song.SpotifyId);
}
} }
} }
} }