6 Commits

Author SHA1 Message Date
0a9e528418 v1.3.0: Bump version to 1.3.0
Some checks are pending
Docker Build & Push / build-and-test (push) Waiting to run
Docker Build & Push / docker (push) Blocked by required conditions
2026-02-11 00:01:06 -05:00
f74728fc73 fix: use MBID lookup for MusicBrainz genre enrichment
Search API doesn't return genres even with inc=genres parameter.
Now doing search to get MBID, then lookup by MBID to get genres.
2026-02-10 23:52:14 -05:00
87467be61b feat: add LyricsPlus API with modular orchestrator architecture
Add multi-source lyrics support with clean, modular architecture for easier debugging and maintenance.

New Features:
- LyricsPlusService: Multi-source lyrics API (Apple Music, Spotify, Musixmatch)
- LyricsOrchestrator: Priority-based coordinator for all lyrics sources
- Modular service architecture with independent error handling
- Word-level and line-level timing support with LRC conversion

Architecture:
- Priority chain: Spotify → LyricsPlus → LRCLib
- Each service logs independently (→ Trying, ✓ Found,  Not found)
- Fallback continues even if one service fails
- Easy to add new sources or modify priority

Benefits:
- Easier debugging with clear service-level logs
- Better maintainability with separated concerns
- More reliable with graceful fallback handling
- Extensible for future lyrics sources
2026-02-10 23:02:17 -05:00
713ecd4ec8 v1.2.6: fix search result ordering to prioritize local tracks
Some checks failed
Docker Build & Push / build-and-test (push) Has been cancelled
Docker Build & Push / docker (push) Has been cancelled
2026-02-10 13:36:06 -05:00
0ff1e3a428 v1.2.5: fix genre enrichment blocking cover art loading 2026-02-10 12:56:43 -05:00
cef18b9482 v1.2.5: prioritize local tracks and optimize genre enrichment
Local tracks now appear first in search results with +10 score boost. Genre enrichment is non-blocking for faster cover art and playback.
2026-02-10 12:50:52 -05:00
12 changed files with 764 additions and 96 deletions

View File

@@ -39,7 +39,9 @@ public class JellyfinController : ControllerBase
private readonly PlaylistSyncService? _playlistSyncService; private readonly PlaylistSyncService? _playlistSyncService;
private readonly SpotifyPlaylistFetcher? _spotifyPlaylistFetcher; private readonly SpotifyPlaylistFetcher? _spotifyPlaylistFetcher;
private readonly SpotifyLyricsService? _spotifyLyricsService; private readonly SpotifyLyricsService? _spotifyLyricsService;
private readonly LyricsPlusService? _lyricsPlusService;
private readonly LrclibService? _lrclibService; private readonly LrclibService? _lrclibService;
private readonly LyricsOrchestrator? _lyricsOrchestrator;
private readonly OdesliService _odesliService; private readonly OdesliService _odesliService;
private readonly RedisCacheService _cache; private readonly RedisCacheService _cache;
private readonly IConfiguration _configuration; private readonly IConfiguration _configuration;
@@ -64,7 +66,9 @@ public class JellyfinController : ControllerBase
PlaylistSyncService? playlistSyncService = null, PlaylistSyncService? playlistSyncService = null,
SpotifyPlaylistFetcher? spotifyPlaylistFetcher = null, SpotifyPlaylistFetcher? spotifyPlaylistFetcher = null,
SpotifyLyricsService? spotifyLyricsService = null, SpotifyLyricsService? spotifyLyricsService = null,
LrclibService? lrclibService = null) LyricsPlusService? lyricsPlusService = null,
LrclibService? lrclibService = null,
LyricsOrchestrator? lyricsOrchestrator = null)
{ {
_settings = settings.Value; _settings = settings.Value;
_spotifySettings = spotifySettings.Value; _spotifySettings = spotifySettings.Value;
@@ -80,7 +84,9 @@ public class JellyfinController : ControllerBase
_playlistSyncService = playlistSyncService; _playlistSyncService = playlistSyncService;
_spotifyPlaylistFetcher = spotifyPlaylistFetcher; _spotifyPlaylistFetcher = spotifyPlaylistFetcher;
_spotifyLyricsService = spotifyLyricsService; _spotifyLyricsService = spotifyLyricsService;
_lyricsPlusService = lyricsPlusService;
_lrclibService = lrclibService; _lrclibService = lrclibService;
_lyricsOrchestrator = lyricsOrchestrator;
_odesliService = odesliService; _odesliService = odesliService;
_cache = cache; _cache = cache;
_configuration = configuration; _configuration = configuration;
@@ -279,53 +285,50 @@ public class JellyfinController : ControllerBase
// Parse Jellyfin results into domain models // Parse Jellyfin results into domain models
var (localSongs, localAlbums, localArtists) = _modelMapper.ParseItemsResponse(jellyfinResult); var (localSongs, localAlbums, localArtists) = _modelMapper.ParseItemsResponse(jellyfinResult);
// Respect source ordering (SquidWTF/Tidal has better search ranking than our fuzzy matching) // Sort all results by match score (local tracks get +10 boost)
// Just interleave local and external results based on which source has better overall match // This ensures best matches appear first regardless of source
var allSongs = localSongs.Concat(externalResult.Songs)
.Select(s => new { Song = s, Score = FuzzyMatcher.CalculateSimilarity(cleanQuery, s.Title) + (s.IsLocal ? 10.0 : 0.0) })
.OrderByDescending(x => x.Score)
.Select(x => x.Song)
.ToList();
// Calculate average match score for each source to determine which should come first var allAlbums = localAlbums.Concat(externalResult.Albums)
var localSongsAvgScore = localSongs.Any() .Select(a => new { Album = a, Score = FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Title) + (a.IsLocal ? 10.0 : 0.0) })
? localSongs.Average(s => FuzzyMatcher.CalculateSimilarity(cleanQuery, s.Title)) .OrderByDescending(x => x.Score)
: 0.0; .Select(x => x.Album)
var externalSongsAvgScore = externalResult.Songs.Any() .ToList();
? externalResult.Songs.Average(s => FuzzyMatcher.CalculateSimilarity(cleanQuery, s.Title))
: 0.0;
var localAlbumsAvgScore = localAlbums.Any() var allArtists = localArtists.Concat(externalResult.Artists)
? localAlbums.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Title)) .Select(a => new { Artist = a, Score = FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Name) + (a.IsLocal ? 10.0 : 0.0) })
: 0.0; .OrderByDescending(x => x.Score)
var externalAlbumsAvgScore = externalResult.Albums.Any() .Select(x => x.Artist)
? externalResult.Albums.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Title)) .ToList();
: 0.0;
var localArtistsAvgScore = localArtists.Any() // Log top results for debugging
? localArtists.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Name))
: 0.0;
var externalArtistsAvgScore = externalResult.Artists.Any()
? externalResult.Artists.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Name))
: 0.0;
// Interleave results: put better-matching source first, preserve original ordering within each source
var allSongs = localSongsAvgScore >= externalSongsAvgScore
? localSongs.Concat(externalResult.Songs).ToList()
: externalResult.Songs.Concat(localSongs).ToList();
var allAlbums = localAlbumsAvgScore >= externalAlbumsAvgScore
? localAlbums.Concat(externalResult.Albums).ToList()
: externalResult.Albums.Concat(localAlbums).ToList();
var allArtists = localArtistsAvgScore >= externalArtistsAvgScore
? localArtists.Concat(externalResult.Artists).ToList()
: externalResult.Artists.Concat(localArtists).ToList();
// Log results for debugging
if (_logger.IsEnabled(LogLevel.Debug)) if (_logger.IsEnabled(LogLevel.Debug))
{ {
_logger.LogDebug("🎵 Songs: Local avg score={LocalScore:F2}, External avg score={ExtScore:F2}, Local first={LocalFirst}", if (allSongs.Any())
localSongsAvgScore, externalSongsAvgScore, localSongsAvgScore >= externalSongsAvgScore); {
_logger.LogDebug("💿 Albums: Local avg score={LocalScore:F2}, External avg score={ExtScore:F2}, Local first={LocalFirst}", var topSong = allSongs.First();
localAlbumsAvgScore, externalAlbumsAvgScore, localAlbumsAvgScore >= externalAlbumsAvgScore); var topScore = FuzzyMatcher.CalculateSimilarity(cleanQuery, topSong.Title) + (topSong.IsLocal ? 10.0 : 0.0);
_logger.LogDebug("🎤 Artists: Local avg score={LocalScore:F2}, External avg score={ExtScore:F2}, Local first={LocalFirst}", _logger.LogDebug("🎵 Top song: '{Title}' (local={IsLocal}, score={Score:F2})",
localArtistsAvgScore, externalArtistsAvgScore, localArtistsAvgScore >= externalArtistsAvgScore); topSong.Title, topSong.IsLocal, topScore);
}
if (allAlbums.Any())
{
var topAlbum = allAlbums.First();
var topScore = FuzzyMatcher.CalculateSimilarity(cleanQuery, topAlbum.Title) + (topAlbum.IsLocal ? 10.0 : 0.0);
_logger.LogDebug("💿 Top album: '{Title}' (local={IsLocal}, score={Score:F2})",
topAlbum.Title, topAlbum.IsLocal, topScore);
}
if (allArtists.Any())
{
var topArtist = allArtists.First();
var topScore = FuzzyMatcher.CalculateSimilarity(cleanQuery, topArtist.Name) + (topArtist.IsLocal ? 10.0 : 0.0);
_logger.LogDebug("🎤 Top artist: '{Name}' (local={IsLocal}, score={Score:F2})",
topArtist.Name, topArtist.IsLocal, topScore);
}
} }
// Convert to Jellyfin format // Convert to Jellyfin format
@@ -343,7 +346,7 @@ public class JellyfinController : ControllerBase
mergedAlbums.AddRange(playlistItems); mergedAlbums.AddRange(playlistItems);
} }
_logger.LogInformation("Merged results (preserving source order): Songs={Songs}, Albums={Albums}, Artists={Artists}", _logger.LogInformation("Merged and sorted results by score: Songs={Songs}, Albums={Albums}, Artists={Artists}",
mergedSongs.Count, mergedAlbums.Count, mergedArtists.Count); mergedSongs.Count, mergedAlbums.Count, mergedArtists.Count);
// Pre-fetch lyrics for top 3 songs in background (don't await) // Pre-fetch lyrics for top 3 songs in background (don't await)
@@ -1274,50 +1277,53 @@ public class JellyfinController : ControllerBase
searchArtists.Add(searchArtist); searchArtists.Add(searchArtist);
} }
// Use orchestrator for clean, modular lyrics fetching
LyricsInfo? lyrics = null; LyricsInfo? lyrics = null;
// Try Spotify lyrics ONLY if we have a valid Spotify track ID if (_lyricsOrchestrator != null)
// Spotify lyrics only work for tracks from injected playlists that have been matched
if (_spotifyLyricsService != null && _spotifyApiSettings.Enabled && !string.IsNullOrEmpty(spotifyTrackId))
{ {
// Validate that this is a real Spotify ID (not spotify:local or other invalid formats) lyrics = await _lyricsOrchestrator.GetLyricsAsync(
var cleanSpotifyId = spotifyTrackId.Replace("spotify:track:", "").Trim(); trackName: searchTitle,
artistNames: searchArtists.ToArray(),
// Spotify track IDs are 22 characters, base62 encoded albumName: searchAlbum,
if (cleanSpotifyId.Length == 22 && !cleanSpotifyId.Contains(":") && !cleanSpotifyId.Contains("local")) durationSeconds: song.Duration ?? 0,
{ spotifyTrackId: spotifyTrackId);
_logger.LogInformation("Trying Spotify lyrics for track ID: {SpotifyId} ({Artist} - {Title})",
cleanSpotifyId, searchArtist, searchTitle);
var spotifyLyrics = await _spotifyLyricsService.GetLyricsByTrackIdAsync(cleanSpotifyId);
if (spotifyLyrics != null && spotifyLyrics.Lines.Count > 0)
{
_logger.LogInformation("Found Spotify lyrics for {Artist} - {Title} ({LineCount} lines, type: {SyncType})",
searchArtist, searchTitle, spotifyLyrics.Lines.Count, spotifyLyrics.SyncType);
lyrics = _spotifyLyricsService.ToLyricsInfo(spotifyLyrics);
}
else
{
_logger.LogDebug("No Spotify lyrics found for track ID {SpotifyId}", cleanSpotifyId);
}
}
else
{
_logger.LogDebug("Invalid Spotify ID format: {SpotifyId}, skipping Spotify lyrics", spotifyTrackId);
}
} }
else
// Fall back to LRCLIB if no Spotify lyrics
if (lyrics == null)
{ {
_logger.LogInformation("Searching LRCLIB for lyrics: {Artists} - {Title}", // Fallback to manual fetching if orchestrator not available
string.Join(", ", searchArtists), _logger.LogWarning("LyricsOrchestrator not available, using fallback method");
searchTitle);
var lrclibService = HttpContext.RequestServices.GetService<LrclibService>(); // Try Spotify lyrics ONLY if we have a valid Spotify track ID
if (lrclibService != null) if (_spotifyLyricsService != null && _spotifyApiSettings.Enabled && !string.IsNullOrEmpty(spotifyTrackId))
{ {
lyrics = await lrclibService.GetLyricsAsync( var cleanSpotifyId = spotifyTrackId.Replace("spotify:track:", "").Trim();
if (cleanSpotifyId.Length == 22 && !cleanSpotifyId.Contains(":") && !cleanSpotifyId.Contains("local"))
{
var spotifyLyrics = await _spotifyLyricsService.GetLyricsByTrackIdAsync(cleanSpotifyId);
if (spotifyLyrics != null && spotifyLyrics.Lines.Count > 0)
{
lyrics = _spotifyLyricsService.ToLyricsInfo(spotifyLyrics);
}
}
}
// Fall back to LyricsPlus
if (lyrics == null && _lyricsPlusService != null)
{
lyrics = await _lyricsPlusService.GetLyricsAsync(
searchTitle,
searchArtists.ToArray(),
searchAlbum,
song.Duration ?? 0);
}
// Fall back to LRCLIB
if (lyrics == null && _lrclibService != null)
{
lyrics = await _lrclibService.GetLyricsAsync(
searchTitle, searchTitle,
searchArtists.ToArray(), searchArtists.ToArray(),
searchAlbum, searchAlbum,
@@ -1498,6 +1504,21 @@ public class JellyfinController : ControllerBase
_logger.LogDebug("🎵 Prefetching lyrics for: {Artist} - {Title}", searchArtist, searchTitle); _logger.LogDebug("🎵 Prefetching lyrics for: {Artist} - {Title}", searchArtist, searchTitle);
// Use orchestrator for prefetching
if (_lyricsOrchestrator != null)
{
await _lyricsOrchestrator.PrefetchLyricsAsync(
trackName: searchTitle,
artistNames: searchArtists.ToArray(),
albumName: searchAlbum,
durationSeconds: song.Duration ?? 0,
spotifyTrackId: spotifyTrackId);
return;
}
// Fallback to manual prefetching if orchestrator not available
_logger.LogWarning("LyricsOrchestrator not available for prefetch, using fallback method");
// Try Spotify lyrics if we have a valid Spotify track ID // Try Spotify lyrics if we have a valid Spotify track ID
if (_spotifyLyricsService != null && _spotifyApiSettings.Enabled && !string.IsNullOrEmpty(spotifyTrackId)) if (_spotifyLyricsService != null && _spotifyApiSettings.Enabled && !string.IsNullOrEmpty(spotifyTrackId))
{ {
@@ -1516,6 +1537,22 @@ public class JellyfinController : ControllerBase
} }
} }
// Fall back to LyricsPlus
if (_lyricsPlusService != null)
{
var lyrics = await _lyricsPlusService.GetLyricsAsync(
searchTitle,
searchArtists.ToArray(),
searchAlbum,
song.Duration ?? 0);
if (lyrics != null)
{
_logger.LogDebug("✓ Prefetched LyricsPlus lyrics for {Artist} - {Title}", searchArtist, searchTitle);
return; // Success, lyrics are now cached
}
}
// Fall back to LRCLIB // Fall back to LRCLIB
if (_lrclibService != null) if (_lrclibService != null)
{ {

View File

@@ -139,7 +139,7 @@ public class WebSocketProxyMiddleware
} }
// Set user agent // Set user agent
serverWebSocket.Options.SetRequestHeader("User-Agent", "Allstarr/1.0"); serverWebSocket.Options.SetRequestHeader("User-Agent", "Allstarr/1.3.0");
await serverWebSocket.ConnectAsync(new Uri(jellyfinWsUrl), context.RequestAborted); await serverWebSocket.ConnectAsync(new Uri(jellyfinWsUrl), context.RequestAborted);
_logger.LogDebug("✓ WEBSOCKET: Connected to Jellyfin WebSocket"); _logger.LogDebug("✓ WEBSOCKET: Connected to Jellyfin WebSocket");

View File

@@ -575,6 +575,12 @@ builder.Services.AddSingleton<allstarr.Services.Spotify.SpotifyApiClient>();
// Register Spotify lyrics service (uses Spotify's color-lyrics API) // Register Spotify lyrics service (uses Spotify's color-lyrics API)
builder.Services.AddSingleton<allstarr.Services.Lyrics.SpotifyLyricsService>(); builder.Services.AddSingleton<allstarr.Services.Lyrics.SpotifyLyricsService>();
// Register LyricsPlus service (multi-source lyrics API)
builder.Services.AddSingleton<allstarr.Services.Lyrics.LyricsPlusService>();
// Register Lyrics Orchestrator (manages priority-based lyrics fetching)
builder.Services.AddSingleton<allstarr.Services.Lyrics.LyricsOrchestrator>();
// Register Spotify playlist fetcher (uses direct Spotify API when SpotifyApi is enabled) // Register Spotify playlist fetcher (uses direct Spotify API when SpotifyApi is enabled)
builder.Services.AddSingleton<allstarr.Services.Spotify.SpotifyPlaylistFetcher>(); builder.Services.AddSingleton<allstarr.Services.Spotify.SpotifyPlaylistFetcher>();
builder.Services.AddHostedService(sp => sp.GetRequiredService<allstarr.Services.Spotify.SpotifyPlaylistFetcher>()); builder.Services.AddHostedService(sp => sp.GetRequiredService<allstarr.Services.Spotify.SpotifyPlaylistFetcher>());

View File

@@ -212,7 +212,18 @@ public class DeezerMetadataService : IMusicMetadataService
// Enrich with MusicBrainz genres if missing // Enrich with MusicBrainz genres if missing
if (_genreEnrichment != null && string.IsNullOrEmpty(song.Genre)) if (_genreEnrichment != null && string.IsNullOrEmpty(song.Genre))
{ {
await _genreEnrichment.EnrichSongGenreAsync(song); // Fire-and-forget: don't block the response waiting for genre enrichment
_ = Task.Run(async () =>
{
try
{
await _genreEnrichment.EnrichSongGenreAsync(song);
}
catch
{
// Silently ignore genre enrichment failures
}
});
} }
return song; return song;

View File

@@ -18,7 +18,7 @@ public class LrclibService
ILogger<LrclibService> logger) ILogger<LrclibService> logger)
{ {
_httpClient = httpClientFactory.CreateClient(); _httpClient = httpClientFactory.CreateClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Allstarr/1.0.0 (https://github.com/SoPat712/allstarr)"); _httpClient.DefaultRequestHeaders.Add("User-Agent", "Allstarr/1.3.0 (https://github.com/SoPat712/allstarr)");
_cache = cache; _cache = cache;
_logger = logger; _logger = logger;
} }

View File

@@ -0,0 +1,228 @@
using allstarr.Models.Lyrics;
using allstarr.Models.Settings;
using Microsoft.Extensions.Options;
namespace allstarr.Services.Lyrics;
/// <summary>
/// Orchestrates lyrics fetching from multiple sources with priority-based fallback.
/// Priority order: Spotify → LyricsPlus → LRCLib
/// Note: Jellyfin local lyrics are handled by the controller before calling this orchestrator.
/// </summary>
public class LyricsOrchestrator
{
private readonly SpotifyLyricsService _spotifyLyrics;
private readonly LyricsPlusService _lyricsPlus;
private readonly LrclibService _lrclib;
private readonly SpotifyApiSettings _spotifySettings;
private readonly ILogger<LyricsOrchestrator> _logger;
public LyricsOrchestrator(
SpotifyLyricsService spotifyLyrics,
LyricsPlusService lyricsPlus,
LrclibService lrclib,
IOptions<SpotifyApiSettings> spotifySettings,
ILogger<LyricsOrchestrator> logger)
{
_spotifyLyrics = spotifyLyrics;
_lyricsPlus = lyricsPlus;
_lrclib = lrclib;
_spotifySettings = spotifySettings.Value;
_logger = logger;
}
/// <summary>
/// Fetches lyrics with automatic fallback through all available sources.
/// Note: Jellyfin local lyrics are handled by the controller before calling this.
/// </summary>
/// <param name="trackName">Track title</param>
/// <param name="artistNames">Artist names (can be multiple)</param>
/// <param name="albumName">Album name</param>
/// <param name="durationSeconds">Track duration in seconds</param>
/// <param name="spotifyTrackId">Spotify track ID (if available)</param>
/// <returns>Lyrics info or null if not found</returns>
public async Task<LyricsInfo?> GetLyricsAsync(
string trackName,
string[] artistNames,
string? albumName,
int durationSeconds,
string? spotifyTrackId = null)
{
var artistName = string.Join(", ", artistNames);
_logger.LogInformation("🎵 Fetching lyrics for: {Artist} - {Track}", artistName, trackName);
// 1. Try Spotify lyrics (if Spotify ID provided)
if (!string.IsNullOrEmpty(spotifyTrackId))
{
var spotifyLyrics = await TrySpotifyLyrics(spotifyTrackId, artistName, trackName);
if (spotifyLyrics != null)
{
return spotifyLyrics;
}
}
// 2. Try LyricsPlus
var lyricsPlusLyrics = await TryLyricsPlusLyrics(trackName, artistNames, albumName, durationSeconds, artistName);
if (lyricsPlusLyrics != null)
{
return lyricsPlusLyrics;
}
// 3. Try LRCLib
var lrclibLyrics = await TryLrclibLyrics(trackName, artistNames, albumName, durationSeconds, artistName);
if (lrclibLyrics != null)
{
return lrclibLyrics;
}
_logger.LogInformation("❌ No lyrics found for: {Artist} - {Track}", artistName, trackName);
return null;
}
/// <summary>
/// Prefetches lyrics in the background (for cache warming).
/// Skips Jellyfin local since we don't have an itemId.
/// </summary>
public async Task<bool> PrefetchLyricsAsync(
string trackName,
string[] artistNames,
string? albumName,
int durationSeconds,
string? spotifyTrackId = null)
{
var artistName = string.Join(", ", artistNames);
_logger.LogDebug("🎵 Prefetching lyrics for: {Artist} - {Track}", artistName, trackName);
// 1. Try Spotify lyrics (if Spotify ID provided)
if (!string.IsNullOrEmpty(spotifyTrackId))
{
var spotifyLyrics = await TrySpotifyLyrics(spotifyTrackId, artistName, trackName);
if (spotifyLyrics != null)
{
return true;
}
}
// 2. Try LyricsPlus
var lyricsPlusLyrics = await TryLyricsPlusLyrics(trackName, artistNames, albumName, durationSeconds, artistName);
if (lyricsPlusLyrics != null)
{
return true;
}
// 3. Try LRCLib
var lrclibLyrics = await TryLrclibLyrics(trackName, artistNames, albumName, durationSeconds, artistName);
if (lrclibLyrics != null)
{
return true;
}
_logger.LogDebug("No lyrics found for prefetch: {Artist} - {Track}", artistName, trackName);
return false;
}
#region Private Helper Methods
private async Task<LyricsInfo?> TrySpotifyLyrics(string spotifyTrackId, string artistName, string trackName)
{
if (!_spotifySettings.Enabled)
{
_logger.LogDebug("Spotify API not enabled, skipping Spotify lyrics");
return null;
}
try
{
// Validate Spotify ID format
var cleanSpotifyId = spotifyTrackId.Replace("spotify:track:", "").Trim();
if (cleanSpotifyId.Length != 22 || cleanSpotifyId.Contains(":") || cleanSpotifyId.Contains("local"))
{
_logger.LogDebug("Invalid Spotify ID format: {SpotifyId}, skipping", spotifyTrackId);
return null;
}
_logger.LogDebug("→ Trying Spotify lyrics for track ID: {SpotifyId}", cleanSpotifyId);
var spotifyLyrics = await _spotifyLyrics.GetLyricsByTrackIdAsync(cleanSpotifyId);
if (spotifyLyrics != null && spotifyLyrics.Lines.Count > 0)
{
_logger.LogInformation("✓ Found Spotify lyrics for {Artist} - {Track} ({LineCount} lines, type: {SyncType})",
artistName, trackName, spotifyLyrics.Lines.Count, spotifyLyrics.SyncType);
return _spotifyLyrics.ToLyricsInfo(spotifyLyrics);
}
_logger.LogDebug("No Spotify lyrics found for track ID {SpotifyId}", cleanSpotifyId);
return null;
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Error fetching Spotify lyrics for track ID {SpotifyId}", spotifyTrackId);
return null;
}
}
private async Task<LyricsInfo?> TryLyricsPlusLyrics(
string trackName,
string[] artistNames,
string? albumName,
int durationSeconds,
string artistName)
{
try
{
_logger.LogDebug("→ Trying LyricsPlus for: {Artist} - {Track}", artistName, trackName);
var lyrics = await _lyricsPlus.GetLyricsAsync(trackName, artistNames, albumName, durationSeconds);
if (lyrics != null)
{
_logger.LogInformation("✓ Found LyricsPlus lyrics for {Artist} - {Track}", artistName, trackName);
return lyrics;
}
_logger.LogDebug("No LyricsPlus lyrics found for {Artist} - {Track}", artistName, trackName);
return null;
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Error fetching LyricsPlus lyrics for {Artist} - {Track}", artistName, trackName);
return null;
}
}
private async Task<LyricsInfo?> TryLrclibLyrics(
string trackName,
string[] artistNames,
string? albumName,
int durationSeconds,
string artistName)
{
try
{
_logger.LogDebug("→ Trying LRCLib for: {Artist} - {Track}", artistName, trackName);
var lyrics = await _lrclib.GetLyricsAsync(trackName, artistNames, albumName ?? string.Empty, durationSeconds);
if (lyrics != null)
{
_logger.LogInformation("✓ Found LRCLib lyrics for {Artist} - {Track}", artistName, trackName);
return lyrics;
}
_logger.LogDebug("No LRCLib lyrics found for {Artist} - {Track}", artistName, trackName);
return null;
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Error fetching LRCLib lyrics for {Artist} - {Track}", artistName, trackName);
return null;
}
}
#endregion
}

View File

@@ -0,0 +1,254 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using allstarr.Models.Lyrics;
using allstarr.Services.Common;
namespace allstarr.Services.Lyrics;
/// <summary>
/// Service for fetching lyrics from LyricsPlus API (https://lyricsplus.prjktla.workers.dev)
/// Supports multiple sources: Apple Music, Spotify, Musixmatch, and more
/// </summary>
public class LyricsPlusService
{
private readonly HttpClient _httpClient;
private readonly RedisCacheService _cache;
private readonly ILogger<LyricsPlusService> _logger;
private const string BaseUrl = "https://lyricsplus.prjktla.workers.dev/v2/lyrics/get";
public LyricsPlusService(
IHttpClientFactory httpClientFactory,
RedisCacheService cache,
ILogger<LyricsPlusService> logger)
{
_httpClient = httpClientFactory.CreateClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Allstarr/1.3.0 (https://github.com/SoPat712/allstarr)");
_cache = cache;
_logger = logger;
}
public async Task<LyricsInfo?> GetLyricsAsync(string trackName, string artistName, string? albumName, int durationSeconds)
{
return await GetLyricsAsync(trackName, new[] { artistName }, albumName, durationSeconds);
}
public async Task<LyricsInfo?> GetLyricsAsync(string trackName, string[] artistNames, string? albumName, int durationSeconds)
{
// Validate input parameters
if (string.IsNullOrWhiteSpace(trackName) || artistNames == null || artistNames.Length == 0)
{
_logger.LogDebug("Invalid parameters for LyricsPlus search: trackName={TrackName}, artistCount={ArtistCount}",
trackName, artistNames?.Length ?? 0);
return null;
}
var artistName = string.Join(", ", artistNames);
var cacheKey = $"lyricsplus:{artistName}:{trackName}:{albumName}:{durationSeconds}";
// Check cache
var cached = await _cache.GetStringAsync(cacheKey);
if (!string.IsNullOrEmpty(cached))
{
try
{
return JsonSerializer.Deserialize<LyricsInfo>(cached, JsonOptions);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to deserialize cached LyricsPlus lyrics");
}
}
try
{
// Build URL with query parameters
var url = $"{BaseUrl}?title={Uri.EscapeDataString(trackName)}&artist={Uri.EscapeDataString(artistName)}";
if (!string.IsNullOrEmpty(albumName))
{
url += $"&album={Uri.EscapeDataString(albumName)}";
}
if (durationSeconds > 0)
{
url += $"&duration={durationSeconds}";
}
// Add sources: apple, lyricsplus, musixmatch, spotify, musixmatch-word
url += "&source=apple,lyricsplus,musixmatch,spotify,musixmatch-word";
_logger.LogDebug("Fetching lyrics from LyricsPlus: {Url}", url);
var response = await _httpClient.GetAsync(url);
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
{
_logger.LogDebug("Lyrics not found on LyricsPlus for {Artist} - {Track}", artistName, trackName);
return null;
}
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
var lyricsResponse = JsonSerializer.Deserialize<LyricsPlusResponse>(json, JsonOptions);
if (lyricsResponse == null || lyricsResponse.Lyrics == null || lyricsResponse.Lyrics.Count == 0)
{
_logger.LogDebug("Empty lyrics response from LyricsPlus for {Artist} - {Track}", artistName, trackName);
return null;
}
// Convert to LyricsInfo format
var result = ConvertToLyricsInfo(lyricsResponse, trackName, artistName, albumName, durationSeconds);
if (result != null)
{
await _cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(result, JsonOptions), TimeSpan.FromDays(30));
_logger.LogInformation("✓ Retrieved lyrics from LyricsPlus for {Artist} - {Track} (type: {Type}, source: {Source})",
artistName, trackName, lyricsResponse.Type, lyricsResponse.Metadata?.Source);
}
return result;
}
catch (HttpRequestException ex)
{
_logger.LogWarning(ex, "Failed to fetch lyrics from LyricsPlus for {Artist} - {Track}", artistName, trackName);
return null;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching lyrics from LyricsPlus for {Artist} - {Track}", artistName, trackName);
return null;
}
}
private LyricsInfo? ConvertToLyricsInfo(LyricsPlusResponse response, string trackName, string artistName, string? albumName, int durationSeconds)
{
if (response.Lyrics == null || response.Lyrics.Count == 0)
{
return null;
}
string? syncedLyrics = null;
string? plainLyrics = null;
// Convert based on type
if (response.Type == "Word")
{
// Word-level timing - convert to line-level LRC
syncedLyrics = ConvertWordTimingToLrc(response.Lyrics);
plainLyrics = string.Join("\n", response.Lyrics.Select(l => l.Text));
}
else if (response.Type == "Line")
{
// Line-level timing - convert to LRC
syncedLyrics = ConvertLineTimingToLrc(response.Lyrics);
plainLyrics = string.Join("\n", response.Lyrics.Select(l => l.Text));
}
else
{
// Static or unknown type - just plain text
plainLyrics = string.Join("\n", response.Lyrics.Select(l => l.Text));
}
return new LyricsInfo
{
TrackName = trackName,
ArtistName = artistName,
AlbumName = albumName ?? string.Empty,
Duration = durationSeconds,
Instrumental = false,
PlainLyrics = plainLyrics,
SyncedLyrics = syncedLyrics
};
}
private string ConvertLineTimingToLrc(List<LyricsPlusLine> lines)
{
var lrcLines = new List<string>();
foreach (var line in lines)
{
if (line.Time.HasValue)
{
var timestamp = TimeSpan.FromMilliseconds(line.Time.Value);
var mm = (int)timestamp.TotalMinutes;
var ss = timestamp.Seconds;
var cs = timestamp.Milliseconds / 10; // Convert to centiseconds
lrcLines.Add($"[{mm:D2}:{ss:D2}.{cs:D2}]{line.Text}");
}
else
{
// No timing, just add the text
lrcLines.Add(line.Text);
}
}
return string.Join("\n", lrcLines);
}
private string ConvertWordTimingToLrc(List<LyricsPlusLine> lines)
{
// For word-level timing, we use the line start time
// (word-level detail is in syllabus array but we simplify to line-level for LRC)
return ConvertLineTimingToLrc(lines);
}
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
private class LyricsPlusResponse
{
[JsonPropertyName("type")]
public string Type { get; set; } = string.Empty; // "Word", "Line", or "Static"
[JsonPropertyName("metadata")]
public LyricsPlusMetadata? Metadata { get; set; }
[JsonPropertyName("lyrics")]
public List<LyricsPlusLine> Lyrics { get; set; } = new();
}
private class LyricsPlusMetadata
{
[JsonPropertyName("source")]
public string? Source { get; set; }
[JsonPropertyName("title")]
public string? Title { get; set; }
[JsonPropertyName("language")]
public string? Language { get; set; }
}
private class LyricsPlusLine
{
[JsonPropertyName("time")]
public long? Time { get; set; } // Milliseconds
[JsonPropertyName("duration")]
public long? Duration { get; set; }
[JsonPropertyName("text")]
public string Text { get; set; } = string.Empty;
[JsonPropertyName("syllabus")]
public List<LyricsPlusSyllable>? Syllabus { get; set; }
}
private class LyricsPlusSyllable
{
[JsonPropertyName("time")]
public long Time { get; set; }
[JsonPropertyName("duration")]
public long Duration { get; set; }
[JsonPropertyName("text")]
public string Text { get; set; } = string.Empty;
}
}

View File

@@ -25,7 +25,7 @@ public class MusicBrainzService
ILogger<MusicBrainzService> logger) ILogger<MusicBrainzService> logger)
{ {
_httpClient = httpClientFactory.CreateClient(); _httpClient = httpClientFactory.CreateClient();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "Allstarr/1.0.0 (https://github.com/SoPat712/allstarr)"); _httpClient.DefaultRequestHeaders.Add("User-Agent", "Allstarr/1.3.0 (https://github.com/SoPat712/allstarr)");
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_settings = settings.Value; _settings = settings.Value;
@@ -92,6 +92,7 @@ public class MusicBrainzService
/// <summary> /// <summary>
/// Searches for recordings by title and artist. /// Searches for recordings by title and artist.
/// Note: Search API doesn't return genres, only MBIDs. Use LookupByMbidAsync to get genres.
/// </summary> /// </summary>
public async Task<List<MusicBrainzRecording>> SearchRecordingsAsync(string title, string artist, int limit = 5) public async Task<List<MusicBrainzRecording>> SearchRecordingsAsync(string title, string artist, int limit = 5)
{ {
@@ -107,7 +108,8 @@ public class MusicBrainzService
// Build Lucene query // Build Lucene query
var query = $"recording:\"{title}\" AND artist:\"{artist}\""; var query = $"recording:\"{title}\" AND artist:\"{artist}\"";
var encodedQuery = Uri.EscapeDataString(query); var encodedQuery = Uri.EscapeDataString(query);
var url = $"{_settings.BaseUrl}/recording?query={encodedQuery}&fmt=json&limit={limit}&inc=genres+tags"; // Note: Search API doesn't support inc=genres, only returns basic info + MBIDs
var url = $"{_settings.BaseUrl}/recording?query={encodedQuery}&fmt=json&limit={limit}";
_logger.LogDebug("MusicBrainz search: {Url}", url); _logger.LogDebug("MusicBrainz search: {Url}", url);
@@ -140,9 +142,56 @@ public class MusicBrainzService
} }
} }
/// <summary>
/// Looks up a recording by MBID to get full details including genres.
/// </summary>
public async Task<MusicBrainzRecording?> LookupByMbidAsync(string mbid)
{
if (!_settings.Enabled)
{
return null;
}
await RateLimitAsync();
try
{
var url = $"{_settings.BaseUrl}/recording/{mbid}?fmt=json&inc=artists+releases+release-groups+genres+tags";
_logger.LogDebug("MusicBrainz MBID lookup: {Url}", url);
var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
_logger.LogWarning("MusicBrainz MBID lookup failed: {StatusCode}", response.StatusCode);
return null;
}
var json = await response.Content.ReadAsStringAsync();
var recording = JsonSerializer.Deserialize<MusicBrainzRecording>(json, JsonOptions);
if (recording == null)
{
_logger.LogDebug("No MusicBrainz recording found for MBID: {Mbid}", mbid);
return null;
}
var genres = recording.Genres?.Select(g => g.Name).Where(n => !string.IsNullOrEmpty(n)).ToList() ?? new List<string?>();
_logger.LogInformation("✓ Found MusicBrainz recording for MBID {Mbid}: {Title} by {Artist} (Genres: {Genres})",
mbid, recording.Title, recording.ArtistCredit?[0]?.Name ?? "Unknown", string.Join(", ", genres));
return recording;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error looking up MBID {Mbid} in MusicBrainz", mbid);
return null;
}
}
/// <summary> /// <summary>
/// Enriches a song with genre information from MusicBrainz. /// Enriches a song with genre information from MusicBrainz.
/// First tries ISRC lookup, then falls back to title/artist search. /// First tries ISRC lookup, then falls back to title/artist search + MBID lookup.
/// </summary> /// </summary>
public async Task<List<string>> GetGenresForSongAsync(string title, string artist, string? isrc = null) public async Task<List<string>> GetGenresForSongAsync(string title, string artist, string? isrc = null)
{ {
@@ -153,17 +202,23 @@ public class MusicBrainzService
MusicBrainzRecording? recording = null; MusicBrainzRecording? recording = null;
// Try ISRC lookup first (most accurate) // Try ISRC lookup first (most accurate and includes genres)
if (!string.IsNullOrEmpty(isrc)) if (!string.IsNullOrEmpty(isrc))
{ {
recording = await LookupByIsrcAsync(isrc); recording = await LookupByIsrcAsync(isrc);
} }
// Fall back to search if ISRC lookup failed or no ISRC provided // Fall back to search + MBID lookup if ISRC lookup failed or no ISRC provided
if (recording == null) if (recording == null)
{ {
var recordings = await SearchRecordingsAsync(title, artist, limit: 1); var recordings = await SearchRecordingsAsync(title, artist, limit: 1);
recording = recordings.FirstOrDefault(); var searchResult = recordings.FirstOrDefault();
// If we found a recording from search, do a full lookup by MBID to get genres
if (searchResult != null && !string.IsNullOrEmpty(searchResult.Id))
{
recording = await LookupByMbidAsync(searchResult.Id);
}
} }
if (recording == null) if (recording == null)

View File

@@ -186,7 +186,18 @@ public class QobuzMetadataService : IMusicMetadataService
// Enrich with MusicBrainz genres if missing // Enrich with MusicBrainz genres if missing
if (_genreEnrichment != null && song != null && string.IsNullOrEmpty(song.Genre)) if (_genreEnrichment != null && song != null && string.IsNullOrEmpty(song.Genre))
{ {
await _genreEnrichment.EnrichSongGenreAsync(song); // Fire-and-forget: don't block the response waiting for genre enrichment
_ = Task.Run(async () =>
{
try
{
await _genreEnrichment.EnrichSongGenreAsync(song);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Failed to enrich genre for {Title}", song.Title);
}
});
} }
return song; return song;

View File

@@ -1302,6 +1302,61 @@ public class SpotifyTrackMatchingService : BackgroundService
if (finalItems.Count > 0) if (finalItems.Count > 0)
{ {
// Enrich external tracks with genres from MusicBrainz
if (externalUsedCount > 0)
{
try
{
var genreEnrichment = _serviceProvider.GetService<GenreEnrichmentService>();
if (genreEnrichment != null)
{
_logger.LogInformation("🎨 Enriching {Count} external tracks with genres from MusicBrainz...", externalUsedCount);
// Extract external songs from matched tracks
var externalSongs = matchedTracks
.Where(t => t.MatchedSong != null && !t.MatchedSong.IsLocal)
.Select(t => t.MatchedSong!)
.ToList();
// Enrich genres in parallel
await genreEnrichment.EnrichSongsGenresAsync(externalSongs);
// Update the genres in finalItems
foreach (var item in finalItems)
{
if (item.TryGetValue("Id", out var idObj) && idObj is string id && id.StartsWith("ext-"))
{
// Find the corresponding song
var song = externalSongs.FirstOrDefault(s => s.Id == id);
if (song != null && !string.IsNullOrEmpty(song.Genre))
{
// Update Genres array
item["Genres"] = new[] { song.Genre };
// Update GenreItems array
item["GenreItems"] = new[]
{
new Dictionary<string, object?>
{
["Name"] = song.Genre,
["Id"] = $"genre-{song.Genre.ToLowerInvariant()}"
}
};
_logger.LogDebug("✓ Enriched {Title} with genre: {Genre}", song.Title, song.Genre);
}
}
}
_logger.LogInformation("✅ Genre enrichment complete for {Playlist}", playlistName);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to enrich genres for {Playlist}, continuing without genres", playlistName);
}
}
// Save to Redis cache with same expiration as matched tracks (until next cron run) // Save to Redis cache with same expiration as matched tracks (until next cron run)
var cacheKey = $"spotify:playlist:items:{playlistName}"; var cacheKey = $"spotify:playlist:items:{playlistName}";
await _cache.SetAsync(cacheKey, finalItems, cacheExpiration); await _cache.SetAsync(cacheKey, finalItems, cacheExpiration);

View File

@@ -292,7 +292,18 @@ public class SquidWTFMetadataService : IMusicMetadataService
// Enrich with MusicBrainz genres if missing (SquidWTF/Tidal doesn't provide genres) // Enrich with MusicBrainz genres if missing (SquidWTF/Tidal doesn't provide genres)
if (_genreEnrichment != null && string.IsNullOrEmpty(song.Genre)) if (_genreEnrichment != null && string.IsNullOrEmpty(song.Genre))
{ {
await _genreEnrichment.EnrichSongGenreAsync(song); // Fire-and-forget: don't block the response waiting for genre enrichment
_ = Task.Run(async () =>
{
try
{
await _genreEnrichment.EnrichSongGenreAsync(song);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Failed to enrich genre for {Title}", song.Title);
}
});
} }
// NOTE: Spotify ID conversion happens during download (in SquidWTFDownloadService) // NOTE: Spotify ID conversion happens during download (in SquidWTFDownloadService)

View File

@@ -5,9 +5,9 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>allstarr</RootNamespace> <RootNamespace>allstarr</RootNamespace>
<Version>1.2.2</Version> <Version>1.3.0</Version>
<AssemblyVersion>1.2.2.0</AssemblyVersion> <AssemblyVersion>1.3.0.0</AssemblyVersion>
<FileVersion>1.2.2.0</FileVersion> <FileVersion>1.3.0.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>