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