mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
Compare commits
124 Commits
beta
...
8e6eb5cc4a
| Author | SHA1 | Date | |
|---|---|---|---|
|
8e6eb5cc4a
|
|||
|
1326f1b3ab
|
|||
|
0011538966
|
|||
|
5acdacf132
|
|||
|
cef836da43
|
|||
|
26c9a72def
|
|||
|
f5124bdda2
|
|||
|
f7f57e711c
|
|||
|
76f633afce
|
|||
|
24df910ffa
|
|||
|
eb46692b25
|
|||
|
c54a32ccfc
|
|||
|
c0c7668cc4
|
|||
|
e860bbe0ee
|
|||
|
df3cc51e17
|
|||
|
027aeab969
|
|||
|
449bcc2561
|
|||
|
8da0bef481
|
|||
|
ae8afa20f8
|
|||
|
da1d28d292
|
|||
|
7e0ea501fc
|
|||
|
bb976fed4f
|
|||
|
df77b16640
|
|||
|
74ae85338c
|
|||
|
72b7198f1d
|
|||
|
b24dfb5b6a
|
|||
|
85f8e1cc5f
|
|||
|
74bd64c949
|
|||
|
1afa68064e
|
|||
|
5251c7ef6d
|
|||
|
63ab25ca91
|
|||
|
628f845e77
|
|||
|
8ef5ee7d8f
|
|||
|
fb3ea1b876
|
|||
|
3f3e1b708d
|
|||
|
bc4faead74
|
|||
|
6ffa2a3277
|
|||
|
c3c01b5559
|
|||
|
47d59ec0f5
|
|||
|
e7f72cd87a
|
|||
|
6d15d02f16
|
|||
|
3137cc4657
|
|||
|
18e700d6a4
|
|||
|
2420cd9a23
|
|||
|
65d6eb041a
|
|||
|
103808f079
|
|||
|
cd29e0de6c
|
|||
|
bd480be382
|
|||
|
293f6f5cc4
|
|||
|
e9b893eb3e
|
|||
|
51694a395d
|
|||
|
32166061ef
|
|||
|
a8845a9ef3
|
|||
|
e873cfe3bf
|
|||
|
43718eaefc
|
|||
|
5f9451f5b4
|
|||
|
2c3ef5c360
|
|||
|
4ba2245876
|
|||
|
c117fa41f6
|
|||
|
2b078453b2
|
|||
|
0ee1883ccb
|
|||
|
8912758b5e
|
|||
|
35d5249843
|
|||
|
62bfb367bc
|
|||
|
6f91361966
|
|||
|
d4036095f1
|
|||
|
6620b39357
|
|||
|
dcaa89171a
|
|||
|
1889dc6e19
|
|||
|
615ad58bc6
|
|||
|
6176777d0f
|
|||
|
a339574f05
|
|||
|
67b4fac64c
|
|||
|
ada6653bd1
|
|||
|
df8dbfc5e1
|
|||
|
e8d3fc4d17
|
|||
|
649351f68b
|
|||
|
3487f79b5e
|
|||
|
3a3f572ead
|
|||
|
d7f15fc3ab
|
|||
|
e43f5cd427
|
|||
|
1b79138923
|
|||
|
dda9736f8d
|
|||
|
9493cb48a5
|
|||
|
6c2453896f
|
|||
|
40594dea7e
|
|||
|
a06bf42887
|
|||
|
6713007650
|
|||
|
e7724c2cc0
|
|||
|
3358fe019d
|
|||
|
9efc54857f
|
|||
|
fcdf47984c
|
|||
|
040a5451a1
|
|||
|
8d76e97449
|
|||
|
a86a8013e6
|
|||
|
4c557a0325
|
|||
|
8540a22846
|
|||
|
36a224bd45
|
|||
|
3af3ebb52b
|
|||
|
614adb9892
|
|||
|
f434f13a19
|
|||
|
625a75f8f9
|
|||
|
d600c5e456
|
|||
|
e23e22a736
|
|||
|
ba0fe35e72
|
|||
|
6e9fe0e69e
|
|||
|
cba955c427
|
|||
|
192173ea64
|
|||
|
c33180abd7
|
|||
|
680454e76e
|
|||
|
34bfc20d28
|
|||
|
489159b424
|
|||
|
2bb754b245
|
|||
|
8d8c0892a2
|
|||
|
e12851e9ca
|
|||
|
f8969bea8d
|
|||
|
ceaa17f018
|
|||
|
9aa7ceb138
|
|||
|
72b1ebc2eb
|
|||
|
48a0351862
|
|||
|
4b95f9910c
|
|||
|
80424a867d
|
|||
|
4afd769602
|
|||
|
b47a5f9063
|
37
.env.example
37
.env.example
@@ -30,6 +30,12 @@ MUSIC_SERVICE=SquidWTF
|
||||
# Path where downloaded songs will be stored on the host (only applies if STORAGE_MODE=Permanent)
|
||||
DOWNLOAD_PATH=./downloads
|
||||
|
||||
# Path where favorited external tracks are permanently kept
|
||||
KEPT_PATH=./kept
|
||||
|
||||
# Path for cache files (Spotify missing tracks, etc.)
|
||||
CACHE_PATH=./cache
|
||||
|
||||
# ===== SQUIDWTF CONFIGURATION =====
|
||||
# Different quality options for SquidWTF. Only FLAC supported right now
|
||||
SQUIDWTF_QUALITY=FLAC
|
||||
@@ -95,3 +101,34 @@ STORAGE_MODE=Permanent
|
||||
# Based on last access time (updated each time the file is streamed)
|
||||
# Cache location: /tmp/allstarr-cache (or $TMPDIR/allstarr-cache if TMPDIR is set)
|
||||
CACHE_DURATION_HOURS=1
|
||||
|
||||
# ===== SPOTIFY PLAYLIST INJECTION (JELLYFIN ONLY) =====
|
||||
# REQUIRES: Jellyfin Spotify Import Plugin (https://github.com/Viperinius/jellyfin-plugin-spotify-import)
|
||||
# This feature intercepts Spotify Import plugin playlists (Release Radar, Discover Weekly) and fills them
|
||||
# with tracks auto-matched from external providers (SquidWTF, Deezer, Qobuz)
|
||||
# Uses JELLYFIN_URL and JELLYFIN_API_KEY configured above (no separate credentials needed)
|
||||
|
||||
# Enable Spotify playlist injection (optional, default: false)
|
||||
SPOTIFY_IMPORT_ENABLED=false
|
||||
|
||||
# Sync schedule: When does the Spotify Import plugin run?
|
||||
# Set these to match your plugin's sync schedule in Jellyfin
|
||||
# Example: If plugin runs daily at 4:15 PM, set HOUR=16 and MINUTE=15
|
||||
SPOTIFY_IMPORT_SYNC_START_HOUR=16
|
||||
SPOTIFY_IMPORT_SYNC_START_MINUTE=15
|
||||
|
||||
# Sync window: How long to search for missing tracks files (in hours)
|
||||
# The fetcher will check every 5 minutes within this window
|
||||
# Example: If plugin runs at 4:15 PM and window is 2 hours, checks from 4:00 PM to 6:00 PM
|
||||
SPOTIFY_IMPORT_SYNC_WINDOW_HOURS=2
|
||||
|
||||
# Playlist IDs to inject (comma-separated)
|
||||
# Get IDs from Jellyfin playlist URLs: https://jellyfin.example.com/web/#/details?id=PLAYLIST_ID
|
||||
# Example: SPOTIFY_IMPORT_PLAYLIST_IDS=4383a46d8bcac3be2ef9385053ea18df,ba50e26c867ec9d57ab2f7bf24cfd6b0
|
||||
SPOTIFY_IMPORT_PLAYLIST_IDS=
|
||||
|
||||
# Playlist names (comma-separated, must match Spotify Import plugin format)
|
||||
# IMPORTANT: Use the exact playlist names as they appear in Jellyfin
|
||||
# Must be in same order as SPOTIFY_IMPORT_PLAYLIST_IDS
|
||||
# Example: SPOTIFY_IMPORT_PLAYLIST_NAMES=Discover Weekly,Release Radar
|
||||
SPOTIFY_IMPORT_PLAYLIST_NAMES=
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -84,3 +84,6 @@ apis/*.json
|
||||
|
||||
# Original source code for reference
|
||||
originals/
|
||||
|
||||
# Sample missing playlists for Spotify integration testing
|
||||
sampleMissingPlaylists/
|
||||
51
README.md
51
README.md
@@ -16,13 +16,18 @@ Please report all bugs as soon as possible, as the Jellyfin addition is entirely
|
||||
Using Docker (recommended):
|
||||
|
||||
```bash
|
||||
# 1. Pull the latest image
|
||||
docker-compose pull
|
||||
# 1. Download the docker-compose.yml file and the .env.example file to a folder on the machine you have Docker
|
||||
|
||||
curl -O https://raw.githubusercontent.com/SoPat712/allstarr/refs/heads/main/docker-compose.yml \
|
||||
-O https://raw.githubusercontent.com/SoPat712/allstarr/refs/heads/main/.env.example
|
||||
|
||||
# 2. Configure environment
|
||||
cp .env.example .env
|
||||
vi .env # Edit with your settings
|
||||
|
||||
# 3. Pull the latest image
|
||||
docker-compose pull
|
||||
|
||||
# 3. Start services
|
||||
docker-compose up -d
|
||||
|
||||
@@ -35,7 +40,7 @@ The proxy will be available at `http://localhost:5274`.
|
||||
|
||||
### Nginx Proxy Setup (Required)
|
||||
|
||||
This service only exposes ports internally. You **must** use nginx to proxy to it:
|
||||
This service only exposes ports internally. You can use nginx to proxy to it, however PLEASE take significant precautions before exposing this! Everyone decides their own level of risk, but this is currently untested, potentially dangerous software, with almost unfettered access to your Jellyfin server. My recommendation is use Tailscale or something similar!
|
||||
|
||||
```nginx
|
||||
server {
|
||||
@@ -78,6 +83,7 @@ This project brings together all the music streaming providers into one unified
|
||||
- **Transparent Proxy**: Sits between your music clients and media server
|
||||
- **Automatic Search**: Searches streaming providers when songs aren't local
|
||||
- **On-the-Fly Downloads**: Songs download and cache for future use
|
||||
- **Favorite to Keep**: When you favorite an external track, it's automatically copied to a permanent `/kept` folder separate from the cache
|
||||
- **External Playlist Support**: Search and download playlists from Deezer, Qobuz, and SquidWTF with M3U generation
|
||||
- **Hi-Res Audio**: SquidWTF supports up to 24-bit/192kHz FLAC
|
||||
- **Full Metadata**: Downloaded files include complete ID3 tags (title, artist, album, track number, year, genre, BPM, ISRC, etc.) and cover art
|
||||
@@ -85,6 +91,7 @@ This project brings together all the music streaming providers into one unified
|
||||
- **Artist Deduplication**: Merges local and streaming artists to avoid duplicates
|
||||
- **Album Enrichment**: Adds missing tracks to local albums from streaming providers
|
||||
- **Cover Art Proxy**: Serves cover art for external content
|
||||
- **Spotify Playlist Injection** (Jellyfin only): Intercepts Spotify Import plugin playlists (Release Radar, Discover Weekly) and fills them with tracks auto-matched from streaming providers
|
||||
|
||||
## Supported Backends
|
||||
|
||||
@@ -282,6 +289,44 @@ Subsonic__EnableExternalPlaylists=false
|
||||
|
||||
> **Note**: Due to client-side filtering, playlists from streaming providers may not appear in the "Playlists" tab of some clients, but will show up in global search results.
|
||||
|
||||
### Spotify Playlist Injection (Jellyfin Only)
|
||||
|
||||
Allstarr can intercept Spotify Import plugin playlists (Release Radar, Discover Weekly) and fill them with tracks automatically matched from your configured streaming provider (SquidWTF, Deezer, or Qobuz).
|
||||
|
||||
**Requirements:**
|
||||
- [Jellyfin Spotify Import Plugin](https://github.com/Viperinius/jellyfin-plugin-spotify-import) installed and configured
|
||||
- Plugin must run on a daily schedule (e.g., 4:15 PM daily)
|
||||
- Jellyfin URL and API key configured (uses existing JELLYFIN_URL and JELLYFIN_API_KEY settings)
|
||||
|
||||
**Configuration:**
|
||||
|
||||
| Setting | Description |
|
||||
|---------|-------------|
|
||||
| `SpotifyImport:Enabled` | Enable Spotify playlist injection (default: `false`) |
|
||||
| `SpotifyImport:SyncStartHour` | Hour when plugin runs (24-hour format, 0-23) |
|
||||
| `SpotifyImport:SyncStartMinute` | Minute when plugin runs (0-59) |
|
||||
| `SpotifyImport:SyncWindowHours` | Hours to search for missing tracks files after sync time |
|
||||
| `SpotifyImport:Playlists` | Array of playlists to inject (Name, SpotifyName, Enabled) |
|
||||
|
||||
**How it works:**
|
||||
1. Jellyfin Spotify Import plugin runs daily and creates playlists + missing tracks files
|
||||
2. Allstarr fetches these missing tracks files within the configured time window
|
||||
3. For each missing track, Allstarr searches your streaming provider (SquidWTF, Deezer, or Qobuz)
|
||||
4. When you open the playlist in Jellyfin, Allstarr intercepts the request and returns matched tracks
|
||||
5. Tracks are downloaded on-demand when played
|
||||
6. On startup, Allstarr will fetch missing tracks if it hasn't run in the last 24 hours
|
||||
|
||||
**Environment variables:**
|
||||
```bash
|
||||
SPOTIFY_IMPORT_ENABLED=true
|
||||
SPOTIFY_IMPORT_SYNC_START_HOUR=16
|
||||
SPOTIFY_IMPORT_SYNC_START_MINUTE=15
|
||||
SPOTIFY_IMPORT_SYNC_WINDOW_HOURS=2
|
||||
SPOTIFY_IMPORT_PLAYLISTS=Release Radar,Discover Weekly
|
||||
```
|
||||
|
||||
> **Note**: This feature uses your existing JELLYFIN_URL and JELLYFIN_API_KEY settings. The plugin must be configured to run on a schedule, and the sync window should cover the plugin's execution time.
|
||||
|
||||
### Getting Credentials
|
||||
|
||||
#### Deezer ARL Token
|
||||
|
||||
@@ -85,7 +85,7 @@ public class JellyfinProxyServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetJsonAsync_IncludesAuthHeader()
|
||||
public async Task GetJsonAsync_WithoutClientHeaders_SendsNoAuth()
|
||||
{
|
||||
// Arrange
|
||||
HttpRequestMessage? captured = null;
|
||||
@@ -102,13 +102,10 @@ public class JellyfinProxyServiceTests
|
||||
// Act
|
||||
await _service.GetJsonAsync("Items");
|
||||
|
||||
// Assert
|
||||
// Assert - Should NOT include auth when no client headers provided
|
||||
Assert.NotNull(captured);
|
||||
Assert.True(captured!.Headers.Contains("Authorization"));
|
||||
var authHeader = captured.Headers.GetValues("Authorization").First();
|
||||
Assert.Contains("MediaBrowser", authHeader);
|
||||
Assert.Contains(_settings.ApiKey!, authHeader);
|
||||
Assert.Contains(_settings.ClientName!, authHeader);
|
||||
Assert.False(captured!.Headers.Contains("Authorization"));
|
||||
Assert.False(captured.Headers.Contains("X-Emby-Authorization"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -10,6 +10,7 @@ using allstarr.Services.Local;
|
||||
using allstarr.Services.Jellyfin;
|
||||
using allstarr.Services.Subsonic;
|
||||
using allstarr.Services.Lyrics;
|
||||
using allstarr.Filters;
|
||||
|
||||
namespace allstarr.Controllers;
|
||||
|
||||
@@ -22,6 +23,7 @@ namespace allstarr.Controllers;
|
||||
public class JellyfinController : ControllerBase
|
||||
{
|
||||
private readonly JellyfinSettings _settings;
|
||||
private readonly SpotifyImportSettings _spotifySettings;
|
||||
private readonly IMusicMetadataService _metadataService;
|
||||
private readonly ILocalLibraryService _localLibraryService;
|
||||
private readonly IDownloadService _downloadService;
|
||||
@@ -29,20 +31,24 @@ public class JellyfinController : ControllerBase
|
||||
private readonly JellyfinModelMapper _modelMapper;
|
||||
private readonly JellyfinProxyService _proxyService;
|
||||
private readonly PlaylistSyncService? _playlistSyncService;
|
||||
private readonly RedisCacheService _cache;
|
||||
private readonly ILogger<JellyfinController> _logger;
|
||||
|
||||
public JellyfinController(
|
||||
IOptions<JellyfinSettings> settings,
|
||||
IOptions<SpotifyImportSettings> spotifySettings,
|
||||
IMusicMetadataService metadataService,
|
||||
ILocalLibraryService localLibraryService,
|
||||
IDownloadService downloadService,
|
||||
JellyfinResponseBuilder responseBuilder,
|
||||
JellyfinModelMapper modelMapper,
|
||||
JellyfinProxyService proxyService,
|
||||
RedisCacheService cache,
|
||||
ILogger<JellyfinController> logger,
|
||||
PlaylistSyncService? playlistSyncService = null)
|
||||
{
|
||||
_settings = settings.Value;
|
||||
_spotifySettings = spotifySettings.Value;
|
||||
_metadataService = metadataService;
|
||||
_localLibraryService = localLibraryService;
|
||||
_downloadService = downloadService;
|
||||
@@ -50,6 +56,7 @@ public class JellyfinController : ControllerBase
|
||||
_modelMapper = modelMapper;
|
||||
_proxyService = proxyService;
|
||||
_playlistSyncService = playlistSyncService;
|
||||
_cache = cache;
|
||||
_logger = logger;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_settings.Url))
|
||||
@@ -114,6 +121,13 @@ public class JellyfinController : ControllerBase
|
||||
return Unauthorized(new { error = "Authentication required" });
|
||||
}
|
||||
|
||||
// Update Spotify playlist counts if enabled and response contains playlists
|
||||
if (_spotifySettings.Enabled && browseResult.RootElement.TryGetProperty("Items", out var _))
|
||||
{
|
||||
_logger.LogInformation("Browse result has Items, checking for Spotify playlists to update counts");
|
||||
browseResult = await UpdateSpotifyPlaylistCounts(browseResult);
|
||||
}
|
||||
|
||||
var result = JsonSerializer.Deserialize<object>(browseResult.RootElement.GetRawText());
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
@@ -169,31 +183,28 @@ public class JellyfinController : ControllerBase
|
||||
var (localSongs, localAlbums, localArtists) = _modelMapper.ParseItemsResponse(jellyfinResult);
|
||||
|
||||
// Score and filter Jellyfin results by relevance
|
||||
var scoredLocalSongs = ScoreSearchResults(cleanQuery, localSongs, s => s.Title, s => s.Artist, isExternal: false);
|
||||
var scoredLocalAlbums = ScoreSearchResults(cleanQuery, localAlbums, a => a.Title, a => a.Artist, isExternal: false);
|
||||
var scoredLocalArtists = ScoreSearchResults(cleanQuery, localArtists, a => a.Name, _ => null, isExternal: false);
|
||||
var scoredLocalSongs = ScoreSearchResults(cleanQuery, localSongs, s => s.Title, s => s.Artist, s => s.Album, isExternal: false);
|
||||
var scoredLocalAlbums = ScoreSearchResults(cleanQuery, localAlbums, a => a.Title, a => a.Artist, _ => null, isExternal: false);
|
||||
var scoredLocalArtists = ScoreSearchResults(cleanQuery, localArtists, a => a.Name, _ => null, _ => null, isExternal: false);
|
||||
|
||||
// Score external results with a small boost
|
||||
var scoredExternalSongs = ScoreSearchResults(cleanQuery, externalResult.Songs, s => s.Title, s => s.Artist, isExternal: true);
|
||||
var scoredExternalAlbums = ScoreSearchResults(cleanQuery, externalResult.Albums, a => a.Title, a => a.Artist, isExternal: true);
|
||||
var scoredExternalArtists = ScoreSearchResults(cleanQuery, externalResult.Artists, a => a.Name, _ => null, isExternal: true);
|
||||
var scoredExternalSongs = ScoreSearchResults(cleanQuery, externalResult.Songs, s => s.Title, s => s.Artist, s => s.Album, isExternal: true);
|
||||
var scoredExternalAlbums = ScoreSearchResults(cleanQuery, externalResult.Albums, a => a.Title, a => a.Artist, _ => null, isExternal: true);
|
||||
var scoredExternalArtists = ScoreSearchResults(cleanQuery, externalResult.Artists, a => a.Name, _ => null, _ => null, isExternal: true);
|
||||
|
||||
// Merge and sort by score (only include items with score >= 40)
|
||||
// Merge and sort by score (no filtering - just reorder by relevance)
|
||||
var allSongs = scoredLocalSongs.Concat(scoredExternalSongs)
|
||||
.Where(x => x.Score >= 40)
|
||||
.OrderByDescending(x => x.Score)
|
||||
.Select(x => x.Item)
|
||||
.ToList();
|
||||
|
||||
var allAlbums = scoredLocalAlbums.Concat(scoredExternalAlbums)
|
||||
.Where(x => x.Score >= 40)
|
||||
.OrderByDescending(x => x.Score)
|
||||
.Select(x => x.Item)
|
||||
.ToList();
|
||||
|
||||
// Dedupe artists by name, keeping highest scored version
|
||||
var artistScores = scoredLocalArtists.Concat(scoredExternalArtists)
|
||||
.Where(x => x.Score >= 40)
|
||||
.GroupBy(x => x.Item.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.Select(g => g.OrderByDescending(x => x.Score).First())
|
||||
.OrderByDescending(x => x.Score)
|
||||
@@ -210,7 +221,6 @@ public class JellyfinController : ControllerBase
|
||||
{
|
||||
var scoredPlaylists = playlistResult
|
||||
.Select(p => new { Playlist = p, Score = FuzzyMatcher.CalculateSimilarity(cleanQuery, p.Name) })
|
||||
.Where(x => x.Score >= 40)
|
||||
.OrderByDescending(x => x.Score)
|
||||
.Select(x => _responseBuilder.ConvertPlaylistToJellyfinItem(x.Playlist))
|
||||
.ToList();
|
||||
@@ -778,6 +788,23 @@ public class JellyfinController : ControllerBase
|
||||
|
||||
var contentType = response.Content.Headers.ContentType?.ToString() ?? "audio/mpeg";
|
||||
|
||||
// Forward caching headers for client-side caching
|
||||
if (response.Headers.ETag != null)
|
||||
{
|
||||
Response.Headers["ETag"] = response.Headers.ETag.ToString();
|
||||
}
|
||||
|
||||
if (response.Content.Headers.LastModified.HasValue)
|
||||
{
|
||||
Response.Headers["Last-Modified"] = response.Content.Headers.LastModified.Value.ToString("R");
|
||||
}
|
||||
|
||||
if (response.Headers.CacheControl != null)
|
||||
{
|
||||
Response.Headers["Cache-Control"] = response.Headers.CacheControl.ToString();
|
||||
}
|
||||
|
||||
// Forward range headers for seeking
|
||||
if (response.Content.Headers.ContentRange != null)
|
||||
{
|
||||
Response.Headers["Content-Range"] = response.Content.Headers.ContentRange.ToString();
|
||||
@@ -1006,15 +1033,21 @@ public class JellyfinController : ControllerBase
|
||||
var lyricsText = lyrics.SyncedLyrics ?? lyrics.PlainLyrics ?? "";
|
||||
var isSynced = !string.IsNullOrEmpty(lyrics.SyncedLyrics);
|
||||
|
||||
_logger.LogInformation("Lyrics for {Artist} - {Track}: synced={HasSynced}, plainLength={PlainLen}, syncedLength={SyncLen}",
|
||||
song.Artist, song.Title, isSynced, lyrics.PlainLyrics?.Length ?? 0, lyrics.SyncedLyrics?.Length ?? 0);
|
||||
|
||||
// Parse LRC format into individual lines for Jellyfin
|
||||
var lyricLines = new List<object>();
|
||||
var lyricLines = new List<Dictionary<string, object>>();
|
||||
|
||||
if (isSynced && !string.IsNullOrEmpty(lyrics.SyncedLyrics))
|
||||
{
|
||||
_logger.LogInformation("Parsing synced lyrics (LRC format)");
|
||||
// Parse LRC format: [mm:ss.xx] text
|
||||
// Skip ID tags like [ar:Artist], [ti:Title], etc.
|
||||
var lines = lyrics.SyncedLyrics.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
// Match timestamp format [mm:ss.xx] or [mm:ss.xxx]
|
||||
var match = System.Text.RegularExpressions.Regex.Match(line, @"^\[(\d+):(\d+)\.(\d+)\]\s*(.*)$");
|
||||
if (match.Success)
|
||||
{
|
||||
@@ -1027,21 +1060,40 @@ public class JellyfinController : ControllerBase
|
||||
var totalMilliseconds = (minutes * 60 + seconds) * 1000 + centiseconds * 10;
|
||||
var ticks = totalMilliseconds * 10000L;
|
||||
|
||||
lyricLines.Add(new
|
||||
// For synced lyrics, include Start timestamp
|
||||
lyricLines.Add(new Dictionary<string, object>
|
||||
{
|
||||
Start = ticks,
|
||||
Text = text
|
||||
["Text"] = text,
|
||||
["Start"] = ticks
|
||||
});
|
||||
}
|
||||
// Skip ID tags like [ar:Artist], [ti:Title], [length:2:23], etc.
|
||||
}
|
||||
_logger.LogInformation("Parsed {Count} synced lyric lines (skipped ID tags)", lyricLines.Count);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(lyricsText))
|
||||
{
|
||||
_logger.LogInformation("Splitting plain lyrics into lines (no timestamps)");
|
||||
// Plain lyrics - split by newlines and return each line separately
|
||||
// IMPORTANT: Do NOT include "Start" field at all for unsynced lyrics
|
||||
// Including it (even as null) causes clients to treat it as synced with timestamp 0:00
|
||||
var lines = lyricsText.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
lyricLines.Add(new Dictionary<string, object>
|
||||
{
|
||||
["Text"] = line.Trim()
|
||||
});
|
||||
}
|
||||
_logger.LogInformation("Split into {Count} plain lyric lines", lyricLines.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Plain lyrics - return as single block
|
||||
lyricLines.Add(new
|
||||
_logger.LogWarning("No lyrics text available");
|
||||
// No lyrics at all
|
||||
lyricLines.Add(new Dictionary<string, object>
|
||||
{
|
||||
Start = (long?)null,
|
||||
Text = lyricsText
|
||||
["Text"] = ""
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1058,6 +1110,17 @@ public class JellyfinController : ControllerBase
|
||||
Lyrics = lyricLines
|
||||
};
|
||||
|
||||
_logger.LogInformation("Returning lyrics response: {LineCount} lines, synced={IsSynced}", lyricLines.Count, isSynced);
|
||||
|
||||
// Log a sample of the response for debugging
|
||||
if (lyricLines.Count > 0)
|
||||
{
|
||||
var sampleLine = lyricLines[0];
|
||||
var hasStart = sampleLine.ContainsKey("Start");
|
||||
_logger.LogInformation("Sample line: Text='{Text}', HasStart={HasStart}",
|
||||
sampleLine.GetValueOrDefault("Text"), hasStart);
|
||||
}
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
@@ -1067,10 +1130,21 @@ public class JellyfinController : ControllerBase
|
||||
|
||||
/// <summary>
|
||||
/// Marks an item as favorite. For playlists, triggers a full download.
|
||||
/// Supports both /Users/{userId}/FavoriteItems/{itemId} and /UserFavoriteItems/{itemId}?userId=xxx
|
||||
/// </summary>
|
||||
[HttpPost("Users/{userId}/FavoriteItems/{itemId}")]
|
||||
public async Task<IActionResult> MarkFavorite(string userId, string itemId)
|
||||
[HttpPost("UserFavoriteItems/{itemId}")]
|
||||
public async Task<IActionResult> MarkFavorite(string itemId, string? userId = null)
|
||||
{
|
||||
// Get userId from query string if not in path
|
||||
if (string.IsNullOrEmpty(userId))
|
||||
{
|
||||
userId = Request.Query["userId"].ToString();
|
||||
}
|
||||
|
||||
_logger.LogInformation("MarkFavorite called: userId={UserId}, itemId={ItemId}, route={Route}",
|
||||
userId, itemId, Request.Path);
|
||||
|
||||
// Check if this is an external playlist - trigger download
|
||||
if (PlaylistIdHelper.IsExternalPlaylist(itemId))
|
||||
{
|
||||
@@ -1094,97 +1168,115 @@ public class JellyfinController : ControllerBase
|
||||
}
|
||||
});
|
||||
|
||||
return Ok(new { IsFavorite = true });
|
||||
// Return a minimal UserItemDataDto response
|
||||
return Ok(new
|
||||
{
|
||||
IsFavorite = true,
|
||||
ItemId = itemId
|
||||
});
|
||||
}
|
||||
|
||||
// Check if this is an external song/album
|
||||
var (isExternal, _, _) = _localLibraryService.ParseSongId(itemId);
|
||||
var (isExternal, provider, externalId) = _localLibraryService.ParseSongId(itemId);
|
||||
if (isExternal)
|
||||
{
|
||||
// External items don't exist in Jellyfin, so we can't favorite them there
|
||||
// Just return success - the client will show it as favorited
|
||||
_logger.LogDebug("Favoriting external item {ItemId} (not synced to Jellyfin)", itemId);
|
||||
return Ok(new { IsFavorite = true });
|
||||
_logger.LogInformation("Favoriting external item {ItemId}, copying to kept folder", itemId);
|
||||
|
||||
// Copy the track to kept folder in background
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await CopyExternalTrackToKeptAsync(itemId, provider!, externalId!);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to copy external track {ItemId} to kept folder", itemId);
|
||||
}
|
||||
});
|
||||
|
||||
// Return a minimal UserItemDataDto response
|
||||
return Ok(new
|
||||
{
|
||||
IsFavorite = true,
|
||||
ItemId = itemId
|
||||
});
|
||||
}
|
||||
|
||||
// For local Jellyfin items, proxy the request through
|
||||
var endpoint = $"Users/{userId}/FavoriteItems/{itemId}";
|
||||
|
||||
try
|
||||
// Use the official Jellyfin endpoint format
|
||||
var endpoint = $"UserFavoriteItems/{itemId}";
|
||||
if (!string.IsNullOrEmpty(userId))
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, $"{_settings.Url?.TrimEnd('/')}/{endpoint}");
|
||||
|
||||
// Forward client authentication
|
||||
if (Request.Headers.TryGetValue("X-Emby-Authorization", out var embyAuth))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", embyAuth.ToString());
|
||||
}
|
||||
else if (Request.Headers.TryGetValue("Authorization", out var auth))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("Authorization", auth.ToString());
|
||||
}
|
||||
|
||||
var response = await _proxyService.HttpClient.SendAsync(request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return Ok(new { IsFavorite = true });
|
||||
}
|
||||
|
||||
_logger.LogWarning("Failed to favorite item in Jellyfin: {StatusCode}", response.StatusCode);
|
||||
return _responseBuilder.CreateError((int)response.StatusCode, "Failed to mark favorite");
|
||||
endpoint = $"{endpoint}?userId={userId}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
_logger.LogInformation("Proxying favorite request to Jellyfin: {Endpoint}", endpoint);
|
||||
|
||||
var result = await _proxyService.PostJsonAsync(endpoint, "{}", Request.Headers);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_logger.LogError(ex, "Error favoriting item {ItemId}", itemId);
|
||||
return _responseBuilder.CreateError(500, "Failed to mark favorite");
|
||||
_logger.LogWarning("Failed to favorite item in Jellyfin - proxy returned null");
|
||||
return StatusCode(500, new { error = "Failed to mark favorite" });
|
||||
}
|
||||
|
||||
return new JsonResult(JsonSerializer.Deserialize<object>(result.RootElement.GetRawText()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an item from favorites.
|
||||
/// Supports both /Users/{userId}/FavoriteItems/{itemId} and /UserFavoriteItems/{itemId}?userId=xxx
|
||||
/// </summary>
|
||||
[HttpDelete("Users/{userId}/FavoriteItems/{itemId}")]
|
||||
public async Task<IActionResult> UnmarkFavorite(string userId, string itemId)
|
||||
[HttpDelete("UserFavoriteItems/{itemId}")]
|
||||
public async Task<IActionResult> UnmarkFavorite(string itemId, string? userId = null)
|
||||
{
|
||||
// External items can't be unfavorited
|
||||
// Get userId from query string if not in path
|
||||
if (string.IsNullOrEmpty(userId))
|
||||
{
|
||||
userId = Request.Query["userId"].ToString();
|
||||
}
|
||||
|
||||
_logger.LogInformation("UnmarkFavorite called: userId={UserId}, itemId={ItemId}, route={Route}",
|
||||
userId, itemId, Request.Path);
|
||||
|
||||
// External items can't be unfavorited (they're not really favorited in Jellyfin)
|
||||
var (isExternal, _, _) = _localLibraryService.ParseSongId(itemId);
|
||||
if (isExternal || PlaylistIdHelper.IsExternalPlaylist(itemId))
|
||||
{
|
||||
return Ok(new { IsFavorite = false });
|
||||
_logger.LogInformation("Unfavoriting external item {ItemId} - returning success", itemId);
|
||||
return Ok(new
|
||||
{
|
||||
IsFavorite = false,
|
||||
ItemId = itemId
|
||||
});
|
||||
}
|
||||
|
||||
// Proxy to Jellyfin to unfavorite
|
||||
var url = $"Users/{userId}/FavoriteItems/{itemId}";
|
||||
|
||||
try
|
||||
// Use the official Jellyfin endpoint format
|
||||
var endpoint = $"UserFavoriteItems/{itemId}";
|
||||
if (!string.IsNullOrEmpty(userId))
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Delete, $"{_settings.Url?.TrimEnd('/')}/{url}");
|
||||
|
||||
// Forward client authentication
|
||||
if (Request.Headers.TryGetValue("X-Emby-Authorization", out var embyAuth))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", embyAuth.ToString());
|
||||
}
|
||||
else if (Request.Headers.TryGetValue("Authorization", out var auth))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("Authorization", auth.ToString());
|
||||
}
|
||||
|
||||
var response = await _proxyService.HttpClient.SendAsync(request);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return Ok(new { IsFavorite = false });
|
||||
}
|
||||
|
||||
return _responseBuilder.CreateError(500, "Failed to unfavorite item");
|
||||
endpoint = $"{endpoint}?userId={userId}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
_logger.LogInformation("Proxying unfavorite request to Jellyfin: {Endpoint}", endpoint);
|
||||
|
||||
var result = await _proxyService.DeleteAsync(endpoint, Request.Headers);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_logger.LogError(ex, "Error unfavoriting item {ItemId}", itemId);
|
||||
return _responseBuilder.CreateError(500, "Failed to unfavorite item");
|
||||
// DELETE often returns 204 No Content, which is success
|
||||
_logger.LogInformation("Unfavorite succeeded (no content returned)");
|
||||
return Ok(new
|
||||
{
|
||||
IsFavorite = false,
|
||||
ItemId = itemId
|
||||
});
|
||||
}
|
||||
|
||||
return new JsonResult(JsonSerializer.Deserialize<object>(result.RootElement.GetRawText()));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -1238,10 +1330,55 @@ public class JellyfinController : ControllerBase
|
||||
{
|
||||
try
|
||||
{
|
||||
var (provider, externalId) = PlaylistIdHelper.ParsePlaylistId(playlistId);
|
||||
var tracks = await _metadataService.GetPlaylistTracksAsync(provider, externalId);
|
||||
_logger.LogInformation("=== GetPlaylistTracks called === PlaylistId: {PlaylistId}", playlistId);
|
||||
|
||||
return _responseBuilder.CreateItemsResponse(tracks);
|
||||
// Check if this is an external playlist (Deezer/Qobuz) first
|
||||
if (PlaylistIdHelper.IsExternalPlaylist(playlistId))
|
||||
{
|
||||
var (provider, externalId) = PlaylistIdHelper.ParsePlaylistId(playlistId);
|
||||
var tracks = await _metadataService.GetPlaylistTracksAsync(provider, externalId);
|
||||
return _responseBuilder.CreateItemsResponse(tracks);
|
||||
}
|
||||
|
||||
// Check if this is a Spotify playlist (by ID)
|
||||
_logger.LogInformation("Spotify Import Enabled: {Enabled}, Configured IDs: {Count}",
|
||||
_spotifySettings.Enabled, _spotifySettings.PlaylistIds.Count);
|
||||
|
||||
if (_spotifySettings.Enabled &&
|
||||
_spotifySettings.PlaylistIds.Any(id => id.Equals(playlistId, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
// Get playlist info from Jellyfin to get the name for matching missing tracks
|
||||
_logger.LogInformation("Fetching playlist info from Jellyfin for ID: {PlaylistId}", playlistId);
|
||||
var playlistInfo = await _proxyService.GetJsonAsync($"Items/{playlistId}", null, Request.Headers);
|
||||
|
||||
if (playlistInfo != null && playlistInfo.RootElement.TryGetProperty("Name", out var nameElement))
|
||||
{
|
||||
var playlistName = nameElement.GetString() ?? "";
|
||||
_logger.LogInformation("✓ MATCHED! Intercepting Spotify playlist: {PlaylistName} (ID: {PlaylistId})",
|
||||
playlistName, playlistId);
|
||||
return await GetSpotifyPlaylistTracksAsync(playlistName, playlistId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Could not get playlist name from Jellyfin for ID: {PlaylistId}", playlistId);
|
||||
}
|
||||
}
|
||||
|
||||
// Regular Jellyfin playlist - proxy through
|
||||
var endpoint = $"Playlists/{playlistId}/Items";
|
||||
if (Request.QueryString.HasValue)
|
||||
{
|
||||
endpoint = $"{endpoint}{Request.QueryString.Value}";
|
||||
}
|
||||
|
||||
_logger.LogInformation("Proxying to Jellyfin: {Endpoint}", endpoint);
|
||||
var result = await _proxyService.GetJsonAsync(endpoint, null, Request.Headers);
|
||||
if (result == null)
|
||||
{
|
||||
return _responseBuilder.CreateError(404, "Playlist not found");
|
||||
}
|
||||
|
||||
return new JsonResult(JsonSerializer.Deserialize<object>(result.RootElement.GetRawText()));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1585,11 +1722,80 @@ public class JellyfinController : ControllerBase
|
||||
/// <summary>
|
||||
/// Catch-all endpoint that proxies unhandled requests to Jellyfin transparently.
|
||||
/// This route has the lowest priority and should only match requests that don't have SearchTerm.
|
||||
/// Blocks dangerous admin endpoints for security.
|
||||
/// </summary>
|
||||
[HttpGet("{**path}", Order = 100)]
|
||||
[HttpPost("{**path}", Order = 100)]
|
||||
public async Task<IActionResult> ProxyRequest(string path)
|
||||
{
|
||||
// DEBUG: Log EVERY request to see what's happening
|
||||
_logger.LogWarning("ProxyRequest called with path: {Path}", path);
|
||||
|
||||
// Log endpoint usage to file for analysis
|
||||
await LogEndpointUsageAsync(path, Request.Method);
|
||||
|
||||
// Block dangerous admin endpoints
|
||||
var blockedPrefixes = new[]
|
||||
{
|
||||
"system/restart", // Server restart
|
||||
"system/shutdown", // Server shutdown
|
||||
"system/configuration", // System configuration changes
|
||||
"system/logs", // Server logs access
|
||||
"system/activitylog", // Activity log access
|
||||
"plugins/", // Plugin management (install/uninstall/configure)
|
||||
"scheduledtasks/", // Scheduled task management
|
||||
"startup/", // Initial server setup
|
||||
"users/new", // User creation
|
||||
"library/refresh", // Library scan (expensive operation)
|
||||
"library/virtualfolders", // Library folder management
|
||||
"branding/", // Branding configuration
|
||||
"displaypreferences/", // Display preferences (if not user-specific)
|
||||
"notifications/admin" // Admin notifications
|
||||
};
|
||||
|
||||
// Check if path matches any blocked prefix
|
||||
if (blockedPrefixes.Any(prefix =>
|
||||
path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
_logger.LogWarning("BLOCKED: Access denied to admin endpoint: {Path} from {IP}",
|
||||
path,
|
||||
HttpContext.Connection.RemoteIpAddress);
|
||||
return StatusCode(403, new
|
||||
{
|
||||
error = "Access to administrative endpoints is not allowed through this proxy",
|
||||
path = path
|
||||
});
|
||||
}
|
||||
|
||||
// Intercept Spotify playlist requests by ID
|
||||
if (_spotifySettings.Enabled &&
|
||||
path.StartsWith("playlists/", StringComparison.OrdinalIgnoreCase) &&
|
||||
path.Contains("/items", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Extract playlist ID from path: playlists/{id}/items
|
||||
var parts = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length >= 2 && parts[0].Equals("playlists", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var playlistId = parts[1];
|
||||
|
||||
_logger.LogInformation("=== PLAYLIST REQUEST ===");
|
||||
_logger.LogInformation("Playlist ID: {PlaylistId}", playlistId);
|
||||
_logger.LogInformation("Spotify Enabled: {Enabled}", _spotifySettings.Enabled);
|
||||
_logger.LogInformation("Configured IDs: {Ids}", string.Join(", ", _spotifySettings.PlaylistIds));
|
||||
_logger.LogInformation("Is configured: {IsConfigured}", _spotifySettings.PlaylistIds.Contains(playlistId, StringComparer.OrdinalIgnoreCase));
|
||||
|
||||
// Check if this playlist ID is configured for Spotify injection
|
||||
if (_spotifySettings.PlaylistIds.Any(id => id.Equals(playlistId, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
_logger.LogInformation("========================================");
|
||||
_logger.LogInformation("=== INTERCEPTING SPOTIFY PLAYLIST ===");
|
||||
_logger.LogInformation("Playlist ID: {PlaylistId}", playlistId);
|
||||
_logger.LogInformation("========================================");
|
||||
return await GetPlaylistTracks(playlistId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle non-JSON responses (robots.txt, etc.)
|
||||
if (path.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) ||
|
||||
path.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
|
||||
@@ -1715,6 +1921,13 @@ public class JellyfinController : ControllerBase
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// Modify response if it contains Spotify playlists to update ChildCount
|
||||
if (_spotifySettings.Enabled && result.RootElement.TryGetProperty("Items", out var items))
|
||||
{
|
||||
_logger.LogInformation("Response has Items property, checking for Spotify playlists to update counts");
|
||||
result = await UpdateSpotifyPlaylistCounts(result);
|
||||
}
|
||||
|
||||
return new JsonResult(JsonSerializer.Deserialize<object>(result.RootElement.GetRawText()));
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -1728,6 +1941,141 @@ public class JellyfinController : ControllerBase
|
||||
|
||||
#region Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Updates ChildCount for Spotify playlists in the response to show total tracks (local + matched).
|
||||
/// </summary>
|
||||
private async Task<JsonDocument> UpdateSpotifyPlaylistCounts(JsonDocument response)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!response.RootElement.TryGetProperty("Items", out var items))
|
||||
{
|
||||
return response;
|
||||
}
|
||||
|
||||
var itemsArray = items.EnumerateArray().ToList();
|
||||
var modified = false;
|
||||
var updatedItems = new List<Dictionary<string, object>>();
|
||||
|
||||
_logger.LogInformation("Checking {Count} items for Spotify playlists", itemsArray.Count);
|
||||
|
||||
foreach (var item in itemsArray)
|
||||
{
|
||||
var itemDict = JsonSerializer.Deserialize<Dictionary<string, object>>(item.GetRawText());
|
||||
if (itemDict == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if this is a Spotify playlist
|
||||
if (item.TryGetProperty("Id", out var idProp))
|
||||
{
|
||||
var playlistId = idProp.GetString();
|
||||
_logger.LogDebug("Checking item with ID: {Id}", playlistId);
|
||||
|
||||
if (!string.IsNullOrEmpty(playlistId) &&
|
||||
_spotifySettings.PlaylistIds.Any(id => id.Equals(playlistId, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
_logger.LogInformation("Found Spotify playlist: {Id}", playlistId);
|
||||
|
||||
// This is a Spotify playlist - get the actual track count
|
||||
var playlistIndex = _spotifySettings.PlaylistIds.FindIndex(id =>
|
||||
id.Equals(playlistId, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (playlistIndex >= 0 && playlistIndex < _spotifySettings.PlaylistNames.Count)
|
||||
{
|
||||
var playlistName = _spotifySettings.PlaylistNames[playlistIndex];
|
||||
var missingTracksKey = $"spotify:missing:{playlistName}";
|
||||
var missingTracks = await _cache.GetAsync<List<allstarr.Models.Spotify.MissingTrack>>(missingTracksKey);
|
||||
|
||||
_logger.LogInformation("Cache lookup for {Key}: {Count} tracks",
|
||||
missingTracksKey, missingTracks?.Count ?? 0);
|
||||
|
||||
// Fallback to file cache
|
||||
if (missingTracks == null || missingTracks.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("Trying file cache for {Name}", playlistName);
|
||||
missingTracks = await LoadMissingTracksFromFile(playlistName);
|
||||
_logger.LogInformation("File cache result: {Count} tracks", missingTracks?.Count ?? 0);
|
||||
}
|
||||
|
||||
if (missingTracks != null && missingTracks.Count > 0)
|
||||
{
|
||||
// Update ChildCount to show the number of tracks we'll provide
|
||||
itemDict["ChildCount"] = missingTracks.Count;
|
||||
modified = true;
|
||||
_logger.LogInformation("✓ Updated ChildCount for Spotify playlist {Name} to {Count}",
|
||||
playlistName, missingTracks.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("No missing tracks found for {Name}", playlistName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updatedItems.Add(itemDict);
|
||||
}
|
||||
|
||||
if (!modified)
|
||||
{
|
||||
_logger.LogInformation("No Spotify playlists found to update");
|
||||
return response;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Modified {Count} Spotify playlists, rebuilding response",
|
||||
updatedItems.Count(i => i.ContainsKey("ChildCount")));
|
||||
|
||||
// Rebuild the response with updated items
|
||||
var responseDict = JsonSerializer.Deserialize<Dictionary<string, object>>(response.RootElement.GetRawText());
|
||||
if (responseDict != null)
|
||||
{
|
||||
responseDict["Items"] = updatedItems;
|
||||
var updatedJson = JsonSerializer.Serialize(responseDict);
|
||||
return JsonDocument.Parse(updatedJson);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to update Spotify playlist counts");
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs endpoint usage to a file for analysis.
|
||||
/// Creates a CSV file with timestamp, method, path, and query string.
|
||||
/// </summary>
|
||||
private async Task LogEndpointUsageAsync(string path, string method)
|
||||
{
|
||||
try
|
||||
{
|
||||
var logDir = "/app/cache/endpoint-usage";
|
||||
Directory.CreateDirectory(logDir);
|
||||
|
||||
var logFile = Path.Combine(logDir, "endpoints.csv");
|
||||
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
var queryString = Request.QueryString.HasValue ? Request.QueryString.Value : "";
|
||||
|
||||
// Sanitize path and query for CSV (remove commas, quotes, newlines)
|
||||
var sanitizedPath = path.Replace(",", ";").Replace("\"", "'").Replace("\n", " ").Replace("\r", " ");
|
||||
var sanitizedQuery = queryString.Replace(",", ";").Replace("\"", "'").Replace("\n", " ").Replace("\r", " ");
|
||||
|
||||
var logLine = $"{timestamp},{method},{sanitizedPath},{sanitizedQuery}\n";
|
||||
|
||||
// Append to file (thread-safe)
|
||||
await System.IO.File.AppendAllTextAsync(logFile, logLine);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Don't let logging failures break the request
|
||||
_logger.LogDebug(ex, "Failed to log endpoint usage");
|
||||
}
|
||||
}
|
||||
|
||||
private static string[]? ParseItemTypes(string? includeItemTypes)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(includeItemTypes))
|
||||
@@ -1761,28 +2109,52 @@ public class JellyfinController : ControllerBase
|
||||
private static List<(T Item, int Score)> ScoreSearchResults<T>(
|
||||
string query,
|
||||
List<T> items,
|
||||
Func<T, string> primaryField,
|
||||
Func<T, string?> secondaryField,
|
||||
Func<T, string> titleField,
|
||||
Func<T, string?> artistField,
|
||||
Func<T, string?> albumField,
|
||||
bool isExternal = false)
|
||||
{
|
||||
return items.Select(item =>
|
||||
{
|
||||
var primary = primaryField(item) ?? "";
|
||||
var secondary = secondaryField(item) ?? "";
|
||||
var title = titleField(item) ?? "";
|
||||
var artist = artistField(item) ?? "";
|
||||
var album = albumField(item) ?? "";
|
||||
|
||||
// Score against primary field (title/name)
|
||||
var primaryScore = FuzzyMatcher.CalculateSimilarity(query, primary);
|
||||
// Token-based fuzzy matching: split query and fields into words
|
||||
var queryTokens = query.ToLower()
|
||||
.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.ToList();
|
||||
|
||||
// Score against secondary field (artist) if provided
|
||||
var secondaryScore = string.IsNullOrEmpty(secondary)
|
||||
? 0
|
||||
: FuzzyMatcher.CalculateSimilarity(query, secondary);
|
||||
var fieldText = $"{title} {artist} {album}".ToLower();
|
||||
var fieldTokens = fieldText
|
||||
.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.ToList();
|
||||
|
||||
// Use the better of the two scores
|
||||
var baseScore = Math.Max(primaryScore, secondaryScore);
|
||||
if (queryTokens.Count == 0) return (item, 0);
|
||||
|
||||
// Count how many query tokens match field tokens (with fuzzy tolerance)
|
||||
var matchedTokens = 0;
|
||||
foreach (var queryToken in queryTokens)
|
||||
{
|
||||
// Check if any field token matches this query token
|
||||
var hasMatch = fieldTokens.Any(fieldToken =>
|
||||
{
|
||||
// Exact match or substring match
|
||||
if (fieldToken.Contains(queryToken) || queryToken.Contains(fieldToken))
|
||||
return true;
|
||||
|
||||
// Fuzzy match with Levenshtein distance
|
||||
var similarity = FuzzyMatcher.CalculateSimilarity(queryToken, fieldToken);
|
||||
return similarity >= 70; // 70% similarity threshold for individual words
|
||||
});
|
||||
|
||||
if (hasMatch) matchedTokens++;
|
||||
}
|
||||
|
||||
// Score = percentage of query tokens that matched
|
||||
var baseScore = (matchedTokens * 100) / queryTokens.Count;
|
||||
|
||||
// Give external results a small boost (+5 points) to prioritize the larger catalog
|
||||
// This means external results will rank slightly higher when scores are close
|
||||
var finalScore = isExternal ? Math.Min(100, baseScore + 5) : baseScore;
|
||||
|
||||
return (item, finalScore);
|
||||
@@ -1790,5 +2162,550 @@ public class JellyfinController : ControllerBase
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Spotify Playlist Injection
|
||||
|
||||
/// <summary>
|
||||
/// Gets tracks for a Spotify playlist by matching missing tracks against external providers
|
||||
/// and merging with existing local tracks from Jellyfin.
|
||||
/// </summary>
|
||||
private async Task<IActionResult> GetSpotifyPlaylistTracksAsync(string spotifyPlaylistName, string playlistId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cacheKey = $"spotify:matched:{spotifyPlaylistName}";
|
||||
var cachedTracks = await _cache.GetAsync<List<Song>>(cacheKey);
|
||||
|
||||
if (cachedTracks != null)
|
||||
{
|
||||
_logger.LogDebug("Returning {Count} cached matched tracks for {Playlist}",
|
||||
cachedTracks.Count, spotifyPlaylistName);
|
||||
return _responseBuilder.CreateItemsResponse(cachedTracks);
|
||||
}
|
||||
|
||||
// Get existing Jellyfin playlist items (tracks the plugin already found)
|
||||
var existingTracksResponse = await _proxyService.GetJsonAsync(
|
||||
$"Playlists/{playlistId}/Items",
|
||||
null,
|
||||
Request.Headers);
|
||||
|
||||
var existingTracks = new List<Song>();
|
||||
var existingSpotifyIds = new HashSet<string>();
|
||||
|
||||
if (existingTracksResponse != null &&
|
||||
existingTracksResponse.RootElement.TryGetProperty("Items", out var items))
|
||||
{
|
||||
foreach (var item in items.EnumerateArray())
|
||||
{
|
||||
var song = _modelMapper.ParseSong(item);
|
||||
existingTracks.Add(song);
|
||||
|
||||
// Track Spotify IDs to avoid duplicates
|
||||
if (item.TryGetProperty("ProviderIds", out var providerIds) &&
|
||||
providerIds.TryGetProperty("Spotify", out var spotifyId))
|
||||
{
|
||||
existingSpotifyIds.Add(spotifyId.GetString() ?? "");
|
||||
}
|
||||
}
|
||||
_logger.LogInformation("Found {Count} existing tracks in Jellyfin playlist", existingTracks.Count);
|
||||
}
|
||||
|
||||
var missingTracksKey = $"spotify:missing:{spotifyPlaylistName}";
|
||||
var missingTracks = await _cache.GetAsync<List<allstarr.Models.Spotify.MissingTrack>>(missingTracksKey);
|
||||
|
||||
// Fallback to file cache if Redis is empty
|
||||
if (missingTracks == null || missingTracks.Count == 0)
|
||||
{
|
||||
missingTracks = await LoadMissingTracksFromFile(spotifyPlaylistName);
|
||||
|
||||
// If we loaded from file, restore to Redis
|
||||
if (missingTracks != null && missingTracks.Count > 0)
|
||||
{
|
||||
await _cache.SetAsync(missingTracksKey, missingTracks, TimeSpan.FromHours(24));
|
||||
_logger.LogInformation("Restored {Count} missing tracks from file cache for {Playlist}",
|
||||
missingTracks.Count, spotifyPlaylistName);
|
||||
}
|
||||
}
|
||||
|
||||
if (missingTracks == null || missingTracks.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("No missing tracks found for {Playlist}, returning {Count} existing tracks",
|
||||
spotifyPlaylistName, existingTracks.Count);
|
||||
return _responseBuilder.CreateItemsResponse(existingTracks);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Matching {Count} missing tracks for {Playlist}",
|
||||
missingTracks.Count, spotifyPlaylistName);
|
||||
|
||||
// Match missing tracks (excluding ones we already have locally)
|
||||
var matchTasks = missingTracks
|
||||
.Where(track => !existingSpotifyIds.Contains(track.SpotifyId))
|
||||
.Select(async track =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// Search with just title and artist for better matching
|
||||
var query = $"{track.Title} {track.PrimaryArtist}";
|
||||
var results = await _metadataService.SearchSongsAsync(query, limit: 5);
|
||||
|
||||
if (results.Count == 0)
|
||||
return (track.SpotifyId, (Song?)null);
|
||||
|
||||
// Fuzzy match to find best result
|
||||
var bestMatch = results
|
||||
.Select(song => new
|
||||
{
|
||||
Song = song,
|
||||
TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, song.Title),
|
||||
ArtistScore = FuzzyMatcher.CalculateSimilarity(track.PrimaryArtist, song.Artist),
|
||||
TotalScore = 0.0
|
||||
})
|
||||
.Select(x => new
|
||||
{
|
||||
x.Song,
|
||||
x.TitleScore,
|
||||
x.ArtistScore,
|
||||
TotalScore = (x.TitleScore * 0.6) + (x.ArtistScore * 0.4) // Weight title more
|
||||
})
|
||||
.OrderByDescending(x => x.TotalScore)
|
||||
.FirstOrDefault();
|
||||
|
||||
// Only return if match is good enough (>60% combined score)
|
||||
if (bestMatch != null && bestMatch.TotalScore >= 60)
|
||||
{
|
||||
_logger.LogDebug("Matched '{Title}' by {Artist} -> '{MatchTitle}' by {MatchArtist} (score: {Score:F1})",
|
||||
track.Title, track.PrimaryArtist,
|
||||
bestMatch.Song.Title, bestMatch.Song.Artist,
|
||||
bestMatch.TotalScore);
|
||||
return (track.SpotifyId, (Song?)bestMatch.Song);
|
||||
}
|
||||
|
||||
_logger.LogDebug("No good match for '{Title}' by {Artist} (best score: {Score:F1})",
|
||||
track.Title, track.PrimaryArtist, bestMatch?.TotalScore ?? 0);
|
||||
return (track.SpotifyId, (Song?)null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Failed to match track: {Title} - {Artist}",
|
||||
track.Title, track.PrimaryArtist);
|
||||
return (track.SpotifyId, (Song?)null);
|
||||
}
|
||||
});
|
||||
|
||||
var matchResults = await Task.WhenAll(matchTasks);
|
||||
var matchedBySpotifyId = matchResults
|
||||
.Where(x => x.Item2 != null)
|
||||
.ToDictionary(x => x.SpotifyId, x => x.Item2!);
|
||||
|
||||
// Build final track list in Spotify playlist order
|
||||
var finalTracks = new List<Song>();
|
||||
foreach (var missingTrack in missingTracks)
|
||||
{
|
||||
// Check if we have it locally first
|
||||
var existingTrack = existingTracks.FirstOrDefault(t =>
|
||||
t.Title.Equals(missingTrack.Title, StringComparison.OrdinalIgnoreCase) &&
|
||||
t.Artist.Equals(missingTrack.PrimaryArtist, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (existingTrack != null)
|
||||
{
|
||||
finalTracks.Add(existingTrack);
|
||||
}
|
||||
else if (matchedBySpotifyId.TryGetValue(missingTrack.SpotifyId, out var matchedTrack))
|
||||
{
|
||||
finalTracks.Add(matchedTrack);
|
||||
}
|
||||
// Skip tracks we couldn't match
|
||||
}
|
||||
|
||||
await _cache.SetAsync(cacheKey, finalTracks, TimeSpan.FromHours(1));
|
||||
|
||||
_logger.LogInformation("Final playlist: {Total} tracks ({Existing} local, {Matched} matched, {Missing} missing)",
|
||||
finalTracks.Count,
|
||||
existingTracks.Count,
|
||||
matchedBySpotifyId.Count,
|
||||
missingTracks.Count - existingTracks.Count - matchedBySpotifyId.Count);
|
||||
|
||||
return _responseBuilder.CreateItemsResponse(finalTracks);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting Spotify playlist tracks {PlaylistName}", spotifyPlaylistName);
|
||||
return _responseBuilder.CreateError(500, "Failed to get Spotify playlist tracks");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies an external track to the kept folder when favorited.
|
||||
/// </summary>
|
||||
private async Task CopyExternalTrackToKeptAsync(string itemId, string provider, string externalId)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get the song metadata
|
||||
var song = await _metadataService.GetSongAsync(provider, externalId);
|
||||
if (song == null)
|
||||
{
|
||||
_logger.LogWarning("Could not find song metadata for {ItemId}", itemId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Trigger download first
|
||||
_logger.LogInformation("Downloading track for kept folder: {ItemId}", itemId);
|
||||
string downloadPath;
|
||||
|
||||
try
|
||||
{
|
||||
downloadPath = await _downloadService.DownloadSongAsync(provider, externalId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to download track {ItemId}", itemId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create kept folder structure: /app/kept/Artist/Album/
|
||||
var keptBasePath = "/app/kept";
|
||||
var keptArtistPath = Path.Combine(keptBasePath, PathHelper.SanitizeFileName(song.Artist));
|
||||
var keptAlbumPath = Path.Combine(keptArtistPath, PathHelper.SanitizeFileName(song.Album));
|
||||
|
||||
Directory.CreateDirectory(keptAlbumPath);
|
||||
|
||||
// Copy file to kept folder
|
||||
var fileName = Path.GetFileName(downloadPath);
|
||||
var keptFilePath = Path.Combine(keptAlbumPath, fileName);
|
||||
|
||||
if (System.IO.File.Exists(keptFilePath))
|
||||
{
|
||||
_logger.LogInformation("Track already exists in kept folder: {Path}", keptFilePath);
|
||||
return;
|
||||
}
|
||||
|
||||
System.IO.File.Copy(downloadPath, keptFilePath, overwrite: false);
|
||||
_logger.LogInformation("✓ Copied favorited track to kept folder: {Path}", keptFilePath);
|
||||
|
||||
// Also copy cover art if it exists
|
||||
var coverPath = Path.Combine(Path.GetDirectoryName(downloadPath)!, "cover.jpg");
|
||||
if (System.IO.File.Exists(coverPath))
|
||||
{
|
||||
var keptCoverPath = Path.Combine(keptAlbumPath, "cover.jpg");
|
||||
if (!System.IO.File.Exists(keptCoverPath))
|
||||
{
|
||||
System.IO.File.Copy(coverPath, keptCoverPath, overwrite: false);
|
||||
_logger.LogDebug("Copied cover art to kept folder");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error copying external track {ItemId} to kept folder", itemId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads missing tracks from file cache as fallback when Redis is empty.
|
||||
/// </summary>
|
||||
private async Task<List<allstarr.Models.Spotify.MissingTrack>?> LoadMissingTracksFromFile(string playlistName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var safeName = string.Join("_", playlistName.Split(Path.GetInvalidFileNameChars()));
|
||||
var filePath = Path.Combine("/app/cache/spotify", $"{safeName}_missing.json");
|
||||
|
||||
if (!System.IO.File.Exists(filePath))
|
||||
{
|
||||
_logger.LogDebug("No file cache found for {Playlist} at {Path}", playlistName, filePath);
|
||||
return null;
|
||||
}
|
||||
|
||||
var fileAge = DateTime.UtcNow - System.IO.File.GetLastWriteTimeUtc(filePath);
|
||||
if (fileAge > TimeSpan.FromHours(24))
|
||||
{
|
||||
_logger.LogDebug("File cache for {Playlist} is too old ({Age:F1}h)", playlistName, fileAge.TotalHours);
|
||||
return null;
|
||||
}
|
||||
|
||||
var json = await System.IO.File.ReadAllTextAsync(filePath);
|
||||
var tracks = JsonSerializer.Deserialize<List<allstarr.Models.Spotify.MissingTrack>>(json);
|
||||
|
||||
_logger.LogInformation("Loaded {Count} missing tracks from file cache for {Playlist} (age: {Age:F1}h)",
|
||||
tracks?.Count ?? 0, playlistName, fileAge.TotalHours);
|
||||
|
||||
return tracks;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to load missing tracks from file for {Playlist}", playlistName);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manual trigger endpoint to force fetch Spotify missing tracks.
|
||||
/// GET /spotify/sync?api_key=YOUR_KEY
|
||||
/// </summary>
|
||||
[HttpGet("spotify/sync", Order = 1)]
|
||||
[ServiceFilter(typeof(ApiKeyAuthFilter))]
|
||||
public async Task<IActionResult> TriggerSpotifySync()
|
||||
{
|
||||
if (!_spotifySettings.Enabled)
|
||||
{
|
||||
return BadRequest(new { error = "Spotify Import is not enabled" });
|
||||
}
|
||||
|
||||
_logger.LogInformation("Manual Spotify sync triggered");
|
||||
|
||||
var results = new Dictionary<string, object>();
|
||||
|
||||
for (int i = 0; i < _spotifySettings.PlaylistIds.Count; i++)
|
||||
{
|
||||
var playlistId = _spotifySettings.PlaylistIds[i];
|
||||
|
||||
try
|
||||
{
|
||||
// Use configured name if available, otherwise use ID
|
||||
var playlistName = i < _spotifySettings.PlaylistNames.Count
|
||||
? _spotifySettings.PlaylistNames[i]
|
||||
: playlistId;
|
||||
|
||||
_logger.LogInformation("Fetching missing tracks for {Playlist} (ID: {Id})", playlistName, playlistId);
|
||||
|
||||
// Try to fetch the missing tracks file - search last 24 hours
|
||||
var now = DateTime.UtcNow;
|
||||
var searchStart = now.AddHours(-24);
|
||||
|
||||
var httpClient = new HttpClient();
|
||||
var found = false;
|
||||
|
||||
// Search every minute for the last 24 hours (1440 attempts max)
|
||||
for (var time = searchStart; time <= now; time = time.AddMinutes(1))
|
||||
{
|
||||
var filename = $"{playlistName}_missing_{time:yyyy-MM-dd_HH-mm}.json";
|
||||
var url = $"{_settings.Url}/Viperinius.Plugin.SpotifyImport/MissingTracksFile" +
|
||||
$"?name={Uri.EscapeDataString(filename)}&api_key={_settings.ApiKey}";
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Trying {Filename}", filename);
|
||||
var response = await httpClient.GetAsync(url);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var tracks = ParseMissingTracksJson(json);
|
||||
|
||||
if (tracks.Count > 0)
|
||||
{
|
||||
var cacheKey = $"spotify:missing:{playlistName}";
|
||||
await _cache.SetAsync(cacheKey, tracks, TimeSpan.FromHours(24));
|
||||
|
||||
results[playlistName] = new {
|
||||
status = "success",
|
||||
tracks = tracks.Count,
|
||||
filename = filename
|
||||
};
|
||||
|
||||
_logger.LogInformation("✓ Cached {Count} missing tracks for {Playlist} from {Filename}",
|
||||
tracks.Count, playlistName, filename);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Failed to fetch {Filename}", filename);
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
results[playlistName] = new { status = "not_found", message = "No missing tracks file found" };
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error syncing playlist {PlaylistId}", playlistId);
|
||||
results[playlistId] = new { status = "error", message = ex.Message };
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(results);
|
||||
}
|
||||
|
||||
private List<allstarr.Models.Spotify.MissingTrack> ParseMissingTracksJson(string json)
|
||||
{
|
||||
var tracks = new List<allstarr.Models.Spotify.MissingTrack>();
|
||||
|
||||
try
|
||||
{
|
||||
var doc = JsonDocument.Parse(json);
|
||||
|
||||
foreach (var item in doc.RootElement.EnumerateArray())
|
||||
{
|
||||
var track = new allstarr.Models.Spotify.MissingTrack
|
||||
{
|
||||
SpotifyId = item.GetProperty("Id").GetString() ?? "",
|
||||
Title = item.GetProperty("Name").GetString() ?? "",
|
||||
Album = item.GetProperty("AlbumName").GetString() ?? "",
|
||||
Artists = item.GetProperty("ArtistNames")
|
||||
.EnumerateArray()
|
||||
.Select(a => a.GetString() ?? "")
|
||||
.Where(a => !string.IsNullOrEmpty(a))
|
||||
.ToList()
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(track.Title))
|
||||
{
|
||||
tracks.Add(track);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to parse missing tracks JSON");
|
||||
}
|
||||
|
||||
return tracks;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Spotify Debug
|
||||
|
||||
/// <summary>
|
||||
/// Clear Spotify playlist cache to force re-matching.
|
||||
/// GET /spotify/clear-cache?api_key=YOUR_KEY
|
||||
/// </summary>
|
||||
[HttpGet("spotify/clear-cache")]
|
||||
[ServiceFilter(typeof(ApiKeyAuthFilter))]
|
||||
public async Task<IActionResult> ClearSpotifyCache()
|
||||
{
|
||||
if (!_spotifySettings.Enabled)
|
||||
{
|
||||
return BadRequest(new { error = "Spotify Import is not enabled" });
|
||||
}
|
||||
|
||||
var cleared = new List<string>();
|
||||
|
||||
foreach (var playlistName in _spotifySettings.PlaylistNames)
|
||||
{
|
||||
var matchedKey = $"spotify:matched:{playlistName}";
|
||||
await _cache.DeleteAsync(matchedKey);
|
||||
cleared.Add(playlistName);
|
||||
_logger.LogInformation("Cleared cache for {Playlist}", playlistName);
|
||||
}
|
||||
|
||||
return Ok(new { status = "success", cleared = cleared });
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Debug & Monitoring
|
||||
|
||||
/// <summary>
|
||||
/// Gets endpoint usage statistics from the log file.
|
||||
/// GET /debug/endpoint-usage?api_key=YOUR_KEY
|
||||
/// Optional query params: top=50 (default 100), since=2024-01-01
|
||||
/// </summary>
|
||||
[HttpGet("debug/endpoint-usage")]
|
||||
[ServiceFilter(typeof(ApiKeyAuthFilter))]
|
||||
public async Task<IActionResult> GetEndpointUsage(
|
||||
[FromQuery] int top = 100,
|
||||
[FromQuery] string? since = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var logFile = "/app/cache/endpoint-usage/endpoints.csv";
|
||||
|
||||
if (!System.IO.File.Exists(logFile))
|
||||
{
|
||||
return Ok(new
|
||||
{
|
||||
message = "No endpoint usage data collected yet",
|
||||
endpoints = Array.Empty<object>()
|
||||
});
|
||||
}
|
||||
|
||||
var lines = await System.IO.File.ReadAllLinesAsync(logFile);
|
||||
|
||||
// Parse CSV and filter by date if provided
|
||||
DateTime? sinceDate = null;
|
||||
if (!string.IsNullOrEmpty(since) && DateTime.TryParse(since, out var parsedDate))
|
||||
{
|
||||
sinceDate = parsedDate;
|
||||
}
|
||||
|
||||
var entries = lines
|
||||
.Select(line => line.Split(','))
|
||||
.Where(parts => parts.Length >= 3)
|
||||
.Where(parts => !sinceDate.HasValue ||
|
||||
(DateTime.TryParse(parts[0], out var entryDate) && entryDate >= sinceDate.Value))
|
||||
.Select(parts => new
|
||||
{
|
||||
Timestamp = parts[0],
|
||||
Method = parts.Length > 1 ? parts[1] : "",
|
||||
Path = parts.Length > 2 ? parts[2] : "",
|
||||
Query = parts.Length > 3 ? parts[3] : ""
|
||||
})
|
||||
.ToList();
|
||||
|
||||
// Group by path and count
|
||||
var pathCounts = entries
|
||||
.GroupBy(e => new { e.Method, e.Path })
|
||||
.Select(g => new
|
||||
{
|
||||
Method = g.Key.Method,
|
||||
Path = g.Key.Path,
|
||||
Count = g.Count(),
|
||||
FirstSeen = g.Min(e => e.Timestamp),
|
||||
LastSeen = g.Max(e => e.Timestamp)
|
||||
})
|
||||
.OrderByDescending(x => x.Count)
|
||||
.Take(top)
|
||||
.ToList();
|
||||
|
||||
return Ok(new
|
||||
{
|
||||
totalRequests = entries.Count,
|
||||
uniqueEndpoints = pathCounts.Count,
|
||||
topEndpoints = pathCounts,
|
||||
logFile = logFile,
|
||||
logSize = new FileInfo(logFile).Length
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to get endpoint usage");
|
||||
return StatusCode(500, new { error = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the endpoint usage log file.
|
||||
/// DELETE /debug/endpoint-usage?api_key=YOUR_KEY
|
||||
/// </summary>
|
||||
[HttpDelete("debug/endpoint-usage")]
|
||||
[ServiceFilter(typeof(ApiKeyAuthFilter))]
|
||||
public IActionResult ClearEndpointUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var logFile = "/app/cache/endpoint-usage/endpoints.csv";
|
||||
|
||||
if (System.IO.File.Exists(logFile))
|
||||
{
|
||||
System.IO.File.Delete(logFile);
|
||||
return Ok(new { status = "success", message = "Endpoint usage log cleared" });
|
||||
}
|
||||
|
||||
return Ok(new { status = "success", message = "No log file to clear" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to clear endpoint usage log");
|
||||
return StatusCode(500, new { error = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
// force rebuild Sun Jan 25 13:22:47 EST 2026
|
||||
|
||||
52
allstarr/Filters/ApiKeyAuthFilter.cs
Normal file
52
allstarr/Filters/ApiKeyAuthFilter.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Options;
|
||||
using allstarr.Models.Settings;
|
||||
|
||||
namespace allstarr.Filters;
|
||||
|
||||
/// <summary>
|
||||
/// Simple API key authentication filter for admin endpoints.
|
||||
/// Validates against Jellyfin API key via query parameter or header.
|
||||
/// </summary>
|
||||
public class ApiKeyAuthFilter : IAsyncActionFilter
|
||||
{
|
||||
private readonly JellyfinSettings _settings;
|
||||
private readonly ILogger<ApiKeyAuthFilter> _logger;
|
||||
|
||||
public ApiKeyAuthFilter(
|
||||
IOptions<JellyfinSettings> settings,
|
||||
ILogger<ApiKeyAuthFilter> logger)
|
||||
{
|
||||
_settings = settings.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
var request = context.HttpContext.Request;
|
||||
|
||||
// Extract API key from query parameter or header
|
||||
var apiKey = request.Query["api_key"].FirstOrDefault()
|
||||
?? request.Headers["X-Api-Key"].FirstOrDefault()
|
||||
?? request.Headers["X-Emby-Token"].FirstOrDefault();
|
||||
|
||||
// Validate API key
|
||||
if (string.IsNullOrEmpty(apiKey) || !string.Equals(apiKey, _settings.ApiKey, StringComparison.Ordinal))
|
||||
{
|
||||
_logger.LogWarning("Unauthorized access attempt to {Path} from {IP}",
|
||||
request.Path,
|
||||
context.HttpContext.Connection.RemoteIpAddress);
|
||||
|
||||
context.Result = new UnauthorizedObjectResult(new
|
||||
{
|
||||
error = "Unauthorized",
|
||||
message = "Valid API key required. Provide via ?api_key=YOUR_KEY or X-Api-Key header."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogDebug("API key authentication successful for {Path}", request.Path);
|
||||
await next();
|
||||
}
|
||||
}
|
||||
47
allstarr/Models/Settings/SpotifyImportSettings.cs
Normal file
47
allstarr/Models/Settings/SpotifyImportSettings.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
namespace allstarr.Models.Settings;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for Spotify playlist injection feature.
|
||||
/// Requires Jellyfin Spotify Import Plugin: https://github.com/Viperinius/jellyfin-plugin-spotify-import
|
||||
/// Uses JellyfinSettings.Url and JellyfinSettings.ApiKey for API access.
|
||||
/// </summary>
|
||||
public class SpotifyImportSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Enable Spotify playlist injection feature
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Hour when Spotify Import plugin runs (24-hour format, 0-23)
|
||||
/// Example: 16 for 4:00 PM
|
||||
/// </summary>
|
||||
public int SyncStartHour { get; set; } = 16;
|
||||
|
||||
/// <summary>
|
||||
/// Minute when Spotify Import plugin runs (0-59)
|
||||
/// Example: 15 for 4:15 PM
|
||||
/// </summary>
|
||||
public int SyncStartMinute { get; set; } = 15;
|
||||
|
||||
/// <summary>
|
||||
/// How many hours to search for missing tracks files after sync start time
|
||||
/// Example: 2 means search from 4:00 PM to 6:00 PM
|
||||
/// </summary>
|
||||
public int SyncWindowHours { get; set; } = 2;
|
||||
|
||||
/// <summary>
|
||||
/// Comma-separated list of Jellyfin playlist IDs to inject
|
||||
/// Example: "4383a46d8bcac3be2ef9385053ea18df,ba50e26c867ec9d57ab2f7bf24cfd6b0"
|
||||
/// Get IDs from Jellyfin playlist URLs
|
||||
/// </summary>
|
||||
public List<string> PlaylistIds { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Comma-separated list of playlist names (must match Spotify Import plugin format)
|
||||
/// Example: "Discover_Weekly,Release_Radar"
|
||||
/// Must be in same order as PlaylistIds
|
||||
/// Plugin replaces spaces with underscores in filenames
|
||||
/// </summary>
|
||||
public List<string> PlaylistNames { get; set; } = new();
|
||||
}
|
||||
12
allstarr/Models/Spotify/MissingTrack.cs
Normal file
12
allstarr/Models/Spotify/MissingTrack.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace allstarr.Models.Spotify;
|
||||
|
||||
public class MissingTrack
|
||||
{
|
||||
public string SpotifyId { get; set; } = string.Empty;
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string Album { get; set; } = string.Empty;
|
||||
public List<string> Artists { get; set; } = new();
|
||||
|
||||
public string PrimaryArtist => Artists.FirstOrDefault() ?? "";
|
||||
public string AllArtists => string.Join(", ", Artists);
|
||||
}
|
||||
@@ -108,6 +108,42 @@ builder.Services.Configure<SquidWTFSettings>(
|
||||
builder.Configuration.GetSection("SquidWTF"));
|
||||
builder.Services.Configure<RedisSettings>(
|
||||
builder.Configuration.GetSection("Redis"));
|
||||
// Configure Spotify Import settings with custom playlist parsing from env var
|
||||
builder.Services.Configure<SpotifyImportSettings>(options =>
|
||||
{
|
||||
builder.Configuration.GetSection("SpotifyImport").Bind(options);
|
||||
|
||||
// Parse SPOTIFY_IMPORT_PLAYLIST_IDS env var (comma-separated) into PlaylistIds array
|
||||
var playlistIdsEnv = builder.Configuration.GetValue<string>("SpotifyImport:PlaylistIds");
|
||||
if (!string.IsNullOrWhiteSpace(playlistIdsEnv) && options.PlaylistIds.Count == 0)
|
||||
{
|
||||
options.PlaylistIds = playlistIdsEnv
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(id => id.Trim())
|
||||
.Where(id => !string.IsNullOrEmpty(id))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// Parse SPOTIFY_IMPORT_PLAYLIST_NAMES env var (comma-separated) into PlaylistNames array
|
||||
var playlistNamesEnv = builder.Configuration.GetValue<string>("SpotifyImport:PlaylistNames");
|
||||
if (!string.IsNullOrWhiteSpace(playlistNamesEnv) && options.PlaylistNames.Count == 0)
|
||||
{
|
||||
options.PlaylistNames = playlistNamesEnv
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(name => name.Trim())
|
||||
.Where(name => !string.IsNullOrEmpty(name))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// Log configuration at startup
|
||||
Console.WriteLine($"Spotify Import: Enabled={options.Enabled}, SyncHour={options.SyncStartHour}:{options.SyncStartMinute:D2}, WindowHours={options.SyncWindowHours}");
|
||||
Console.WriteLine($"Spotify Import Playlist IDs: {options.PlaylistIds.Count} configured");
|
||||
for (int i = 0; i < options.PlaylistIds.Count; i++)
|
||||
{
|
||||
var name = i < options.PlaylistNames.Count ? options.PlaylistNames[i] : options.PlaylistIds[i];
|
||||
Console.WriteLine($" - {name} (ID: {options.PlaylistIds[i]})");
|
||||
}
|
||||
});
|
||||
|
||||
// Get shared settings from the active backend config
|
||||
MusicService musicService;
|
||||
@@ -138,6 +174,7 @@ if (backendType == BackendType.Jellyfin)
|
||||
builder.Services.AddSingleton<JellyfinModelMapper>();
|
||||
builder.Services.AddScoped<JellyfinProxyService>();
|
||||
builder.Services.AddScoped<JellyfinAuthFilter>();
|
||||
builder.Services.AddScoped<allstarr.Filters.ApiKeyAuthFilter>();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -229,6 +266,9 @@ builder.Services.AddHostedService<StartupValidationOrchestrator>();
|
||||
// Register cache cleanup service (only runs when StorageMode is Cache)
|
||||
builder.Services.AddHostedService<CacheCleanupService>();
|
||||
|
||||
// Register Spotify missing tracks fetcher (only runs when SpotifyImport is enabled)
|
||||
builder.Services.AddHostedService<allstarr.Services.Spotify.SpotifyMissingTracksFetcher>();
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(policy =>
|
||||
|
||||
@@ -205,18 +205,11 @@ public class JellyfinProxyService
|
||||
_logger.LogWarning("✗ No client headers provided for {Url}", url);
|
||||
}
|
||||
|
||||
// Use API key if no valid client auth was found
|
||||
// DO NOT use server API key as fallback - let Jellyfin handle unauthenticated requests
|
||||
// If client doesn't provide auth, they get what they deserve (401 from Jellyfin)
|
||||
if (!authHeaderAdded)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_settings.ApiKey))
|
||||
{
|
||||
request.Headers.Add("Authorization", GetAuthorizationHeader());
|
||||
_logger.LogInformation("→ Using API key for {Url}", url);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("✗ No authentication available for {Url} - request will fail", url);
|
||||
}
|
||||
_logger.LogInformation("No client auth provided for {Url} - forwarding without auth", url);
|
||||
}
|
||||
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
@@ -297,8 +290,10 @@ public class JellyfinProxyService
|
||||
{
|
||||
if (header.Key.Equals("X-Emby-Authorization", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", header.Value.ToString());
|
||||
var headerValue = header.Value.ToString();
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
|
||||
authHeaderAdded = true;
|
||||
_logger.LogDebug("Forwarded X-Emby-Authorization from client");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -309,21 +304,34 @@ public class JellyfinProxyService
|
||||
{
|
||||
if (header.Key.Equals("Authorization", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("Authorization", header.Value.ToString());
|
||||
var headerValue = header.Value.ToString();
|
||||
|
||||
// Check if it's MediaBrowser/Jellyfin format
|
||||
if (headerValue.Contains("MediaBrowser", StringComparison.OrdinalIgnoreCase) ||
|
||||
headerValue.Contains("Client=", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Forward as X-Emby-Authorization
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
|
||||
_logger.LogDebug("Converted Authorization to X-Emby-Authorization");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Standard Bearer token
|
||||
request.Headers.TryAddWithoutValidation("Authorization", headerValue);
|
||||
_logger.LogDebug("Forwarded Authorization header");
|
||||
}
|
||||
authHeaderAdded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For login requests without auth headers, provide a minimal client auth header
|
||||
// DO NOT use server credentials as fallback
|
||||
// Exception: For auth endpoints, client provides their own credentials in the body
|
||||
// For all other endpoints, if client doesn't provide auth, let Jellyfin reject it
|
||||
if (!authHeaderAdded)
|
||||
{
|
||||
var clientAuthHeader = $"MediaBrowser Client=\"{_settings.ClientName}\", " +
|
||||
$"Device=\"{_settings.DeviceName}\", " +
|
||||
$"DeviceId=\"{_settings.DeviceId}\", " +
|
||||
$"Version=\"{_settings.ClientVersion}\"";
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", clientAuthHeader);
|
||||
_logger.LogInformation("No client auth provided for POST {Url} - forwarding without auth", url);
|
||||
}
|
||||
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
@@ -390,6 +398,95 @@ public class JellyfinProxyService
|
||||
return (body, contentType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a DELETE request to the Jellyfin server.
|
||||
/// Forwards client headers for authentication passthrough.
|
||||
/// </summary>
|
||||
public async Task<JsonDocument?> DeleteAsync(string endpoint, IHeaderDictionary clientHeaders)
|
||||
{
|
||||
var url = BuildUrl(endpoint, null);
|
||||
|
||||
using var request = new HttpRequestMessage(HttpMethod.Delete, url);
|
||||
|
||||
bool authHeaderAdded = false;
|
||||
|
||||
// Forward authentication headers from client (case-insensitive)
|
||||
foreach (var header in clientHeaders)
|
||||
{
|
||||
if (header.Key.Equals("X-Emby-Authorization", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var headerValue = header.Value.ToString();
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
|
||||
authHeaderAdded = true;
|
||||
_logger.LogDebug("Forwarded X-Emby-Authorization from client");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!authHeaderAdded)
|
||||
{
|
||||
foreach (var header in clientHeaders)
|
||||
{
|
||||
if (header.Key.Equals("Authorization", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var headerValue = header.Value.ToString();
|
||||
|
||||
// Check if it's MediaBrowser/Jellyfin format
|
||||
if (headerValue.Contains("MediaBrowser", StringComparison.OrdinalIgnoreCase) ||
|
||||
headerValue.Contains("Client=", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Forward as X-Emby-Authorization
|
||||
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
|
||||
_logger.LogDebug("Converted Authorization to X-Emby-Authorization");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Standard Bearer token
|
||||
request.Headers.TryAddWithoutValidation("Authorization", headerValue);
|
||||
_logger.LogDebug("Forwarded Authorization header");
|
||||
}
|
||||
authHeaderAdded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!authHeaderAdded)
|
||||
{
|
||||
_logger.LogInformation("No client auth provided for DELETE {Url} - forwarding without auth", url);
|
||||
}
|
||||
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
_logger.LogInformation("DELETE to Jellyfin: {Url}", url);
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogWarning("Jellyfin DELETE request failed: {StatusCode} for {Url}. Response: {Response}",
|
||||
response.StatusCode, url, errorContent);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle 204 No Content responses
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Handle empty responses
|
||||
if (string.IsNullOrWhiteSpace(responseContent))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return JsonDocument.Parse(responseContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Safely sends a GET request to the Jellyfin server, returning null on failure.
|
||||
/// </summary>
|
||||
|
||||
@@ -289,6 +289,24 @@ public class JellyfinResponseBuilder
|
||||
var providerIds = (Dictionary<string, string>)item["ProviderIds"]!;
|
||||
providerIds["ISRC"] = song.Isrc;
|
||||
}
|
||||
|
||||
// Add MediaSources with bitrate for external tracks
|
||||
item["MediaSources"] = new[]
|
||||
{
|
||||
new Dictionary<string, object?>
|
||||
{
|
||||
["Id"] = song.Id,
|
||||
["Type"] = "Default",
|
||||
["Container"] = "flac",
|
||||
["Size"] = (song.Duration ?? 180) * 1337 * 128, // Approximate file size
|
||||
["Bitrate"] = 1337000, // 1337 kbps in bps
|
||||
["Path"] = $"/music/{song.Artist}/{song.Album}/{song.Title}.flac",
|
||||
["Protocol"] = "File",
|
||||
["SupportsDirectStream"] = true,
|
||||
["SupportsTranscoding"] = true,
|
||||
["SupportsDirectPlay"] = true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(song.Genre))
|
||||
@@ -304,11 +322,11 @@ public class JellyfinResponseBuilder
|
||||
/// </summary>
|
||||
public Dictionary<string, object?> ConvertAlbumToJellyfinItem(Album album)
|
||||
{
|
||||
// Add " - SW" suffix to external album names
|
||||
// Add " - S" suffix to external album names (S = SquidWTF)
|
||||
var albumName = album.Title;
|
||||
if (!album.IsLocal)
|
||||
{
|
||||
albumName = $"{album.Title} - SW";
|
||||
albumName = $"{album.Title} - S";
|
||||
}
|
||||
|
||||
var item = new Dictionary<string, object?>
|
||||
@@ -371,11 +389,11 @@ public class JellyfinResponseBuilder
|
||||
/// </summary>
|
||||
public Dictionary<string, object?> ConvertArtistToJellyfinItem(Artist artist)
|
||||
{
|
||||
// Add " - SW" suffix to external artist names
|
||||
// Add " - S" suffix to external artist names (S = SquidWTF)
|
||||
var artistName = artist.Name;
|
||||
if (!artist.IsLocal)
|
||||
{
|
||||
artistName = $"{artist.Name} - SW";
|
||||
artistName = $"{artist.Name} - S";
|
||||
}
|
||||
|
||||
var item = new Dictionary<string, object?>
|
||||
|
||||
@@ -42,25 +42,100 @@ public class LrclibService
|
||||
|
||||
try
|
||||
{
|
||||
var url = $"{BaseUrl}/get?" +
|
||||
$"track_name={Uri.EscapeDataString(trackName)}&" +
|
||||
$"artist_name={Uri.EscapeDataString(artistName)}&" +
|
||||
$"album_name={Uri.EscapeDataString(albumName)}&" +
|
||||
$"duration={durationSeconds}";
|
||||
// First try search API for fuzzy matching (more forgiving)
|
||||
var searchUrl = $"{BaseUrl}/search?" +
|
||||
$"track_name={Uri.EscapeDataString(trackName)}&" +
|
||||
$"artist_name={Uri.EscapeDataString(artistName)}";
|
||||
|
||||
_logger.LogDebug("Fetching lyrics from LRCLIB: {Url}", url);
|
||||
_logger.LogInformation("Searching LRCLIB: {Url}", searchUrl);
|
||||
|
||||
var response = await _httpClient.GetAsync(url);
|
||||
var searchResponse = await _httpClient.GetAsync(searchUrl);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
if (searchResponse.IsSuccessStatusCode)
|
||||
{
|
||||
var searchJson = await searchResponse.Content.ReadAsStringAsync();
|
||||
var searchResults = JsonSerializer.Deserialize<List<LrclibResponse>>(searchJson, JsonOptions);
|
||||
|
||||
if (searchResults != null && searchResults.Count > 0)
|
||||
{
|
||||
// Find best match by comparing track name, artist, and duration
|
||||
LrclibResponse? bestMatch = null;
|
||||
double bestScore = 0;
|
||||
|
||||
foreach (var result in searchResults)
|
||||
{
|
||||
// Calculate similarity scores
|
||||
var trackScore = CalculateSimilarity(trackName, result.TrackName ?? "");
|
||||
var artistScore = CalculateSimilarity(artistName, result.ArtistName ?? "");
|
||||
|
||||
// Duration match (within 5 seconds is good)
|
||||
var durationDiff = Math.Abs(result.Duration - durationSeconds);
|
||||
var durationScore = durationDiff <= 5 ? 100.0 : Math.Max(0, 100 - (durationDiff * 2));
|
||||
|
||||
// Bonus for having synced lyrics (prefer synced over plain)
|
||||
var syncedBonus = !string.IsNullOrEmpty(result.SyncedLyrics) ? 20.0 : 0.0;
|
||||
|
||||
// Weighted score: track name most important, then artist, then duration, plus synced bonus
|
||||
var totalScore = (trackScore * 0.5) + (artistScore * 0.3) + (durationScore * 0.2) + syncedBonus;
|
||||
|
||||
_logger.LogDebug("Candidate: {Track} by {Artist} - Score: {Score:F1} (track:{TrackScore:F1}, artist:{ArtistScore:F1}, duration:{DurationScore:F1}, synced:{Synced})",
|
||||
result.TrackName, result.ArtistName, totalScore, trackScore, artistScore, durationScore, !string.IsNullOrEmpty(result.SyncedLyrics));
|
||||
|
||||
if (totalScore > bestScore)
|
||||
{
|
||||
bestScore = totalScore;
|
||||
bestMatch = result;
|
||||
}
|
||||
}
|
||||
|
||||
// Only use result if score is good enough (>60%)
|
||||
if (bestMatch != null && bestScore >= 60)
|
||||
{
|
||||
_logger.LogInformation("✓ Found lyrics via search for {Artist} - {Track} (ID: {Id}, score: {Score:F1}, synced: {HasSynced})",
|
||||
artistName, trackName, bestMatch.Id, bestScore, !string.IsNullOrEmpty(bestMatch.SyncedLyrics));
|
||||
|
||||
var result = new LyricsInfo
|
||||
{
|
||||
Id = bestMatch.Id,
|
||||
TrackName = bestMatch.TrackName ?? trackName,
|
||||
ArtistName = bestMatch.ArtistName ?? artistName,
|
||||
AlbumName = bestMatch.AlbumName ?? albumName,
|
||||
Duration = (int)Math.Round(bestMatch.Duration),
|
||||
Instrumental = bestMatch.Instrumental,
|
||||
PlainLyrics = bestMatch.PlainLyrics,
|
||||
SyncedLyrics = bestMatch.SyncedLyrics
|
||||
};
|
||||
|
||||
await _cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(result, JsonOptions), TimeSpan.FromDays(30));
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Best match score too low ({Score:F1}), trying exact match", bestScore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to exact match API if search didn't find good results
|
||||
var exactUrl = $"{BaseUrl}/get?" +
|
||||
$"track_name={Uri.EscapeDataString(trackName)}&" +
|
||||
$"artist_name={Uri.EscapeDataString(artistName)}&" +
|
||||
$"album_name={Uri.EscapeDataString(albumName)}&" +
|
||||
$"duration={durationSeconds}";
|
||||
|
||||
_logger.LogDebug("Trying exact match from LRCLIB: {Url}", exactUrl);
|
||||
|
||||
var exactResponse = await _httpClient.GetAsync(exactUrl);
|
||||
|
||||
if (exactResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
_logger.LogDebug("Lyrics not found for {Artist} - {Track}", artistName, trackName);
|
||||
return null;
|
||||
}
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
exactResponse.EnsureSuccessStatusCode();
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
var json = await exactResponse.Content.ReadAsStringAsync();
|
||||
var lyrics = JsonSerializer.Deserialize<LrclibResponse>(json, JsonOptions);
|
||||
|
||||
if (lyrics == null)
|
||||
@@ -68,7 +143,7 @@ public class LrclibService
|
||||
return null;
|
||||
}
|
||||
|
||||
var result = new LyricsInfo
|
||||
var exactResult = new LyricsInfo
|
||||
{
|
||||
Id = lyrics.Id,
|
||||
TrackName = lyrics.TrackName ?? trackName,
|
||||
@@ -80,11 +155,11 @@ public class LrclibService
|
||||
SyncedLyrics = lyrics.SyncedLyrics
|
||||
};
|
||||
|
||||
await _cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(result, JsonOptions), TimeSpan.FromDays(30));
|
||||
await _cache.SetStringAsync(cacheKey, JsonSerializer.Serialize(exactResult, JsonOptions), TimeSpan.FromDays(30));
|
||||
|
||||
_logger.LogInformation("Retrieved lyrics for {Artist} - {Track} (ID: {Id})", artistName, trackName, lyrics.Id);
|
||||
_logger.LogInformation("Retrieved lyrics via exact match for {Artist} - {Track} (ID: {Id})", artistName, trackName, lyrics.Id);
|
||||
|
||||
return result;
|
||||
return exactResult;
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
@@ -98,6 +173,28 @@ public class LrclibService
|
||||
}
|
||||
}
|
||||
|
||||
private static double CalculateSimilarity(string str1, string str2)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
|
||||
return 0;
|
||||
|
||||
str1 = str1.ToLowerInvariant();
|
||||
str2 = str2.ToLowerInvariant();
|
||||
|
||||
if (str1 == str2)
|
||||
return 100;
|
||||
|
||||
// Simple token-based matching
|
||||
var tokens1 = str1.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var tokens2 = str2.Split(new[] { ' ', '-', '_' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (tokens1.Length == 0 || tokens2.Length == 0)
|
||||
return 0;
|
||||
|
||||
var matchedTokens = tokens1.Count(t1 => tokens2.Any(t2 => t2.Contains(t1) || t1.Contains(t2)));
|
||||
return (matchedTokens * 100.0) / Math.Max(tokens1.Length, tokens2.Length);
|
||||
}
|
||||
|
||||
public async Task<LyricsInfo?> GetLyricsCachedAsync(string trackName, string artistName, string albumName, int durationSeconds)
|
||||
{
|
||||
try
|
||||
|
||||
472
allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs
Normal file
472
allstarr/Services/Spotify/SpotifyMissingTracksFetcher.cs
Normal file
@@ -0,0 +1,472 @@
|
||||
using allstarr.Models.Settings;
|
||||
using allstarr.Models.Spotify;
|
||||
using allstarr.Services.Common;
|
||||
using allstarr.Services.Jellyfin;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace allstarr.Services.Spotify;
|
||||
|
||||
public class SpotifyMissingTracksFetcher : BackgroundService
|
||||
{
|
||||
private readonly IOptions<SpotifyImportSettings> _spotifySettings;
|
||||
private readonly IOptions<JellyfinSettings> _jellyfinSettings;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly RedisCacheService _cache;
|
||||
private readonly ILogger<SpotifyMissingTracksFetcher> _logger;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private bool _hasRunOnce = false;
|
||||
private Dictionary<string, string> _playlistIdToName = new();
|
||||
private const string CacheDirectory = "/app/cache/spotify";
|
||||
|
||||
public SpotifyMissingTracksFetcher(
|
||||
IOptions<SpotifyImportSettings> spotifySettings,
|
||||
IOptions<JellyfinSettings> jellyfinSettings,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
RedisCacheService cache,
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<SpotifyMissingTracksFetcher> logger)
|
||||
{
|
||||
_spotifySettings = spotifySettings;
|
||||
_jellyfinSettings = jellyfinSettings;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_cache = cache;
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("========================================");
|
||||
_logger.LogInformation("SpotifyMissingTracksFetcher: Starting up...");
|
||||
|
||||
// Ensure cache directory exists
|
||||
Directory.CreateDirectory(CacheDirectory);
|
||||
|
||||
if (!_spotifySettings.Value.Enabled)
|
||||
{
|
||||
_logger.LogInformation("Spotify playlist injection is DISABLED");
|
||||
_logger.LogInformation("========================================");
|
||||
return;
|
||||
}
|
||||
|
||||
var jellyfinUrl = _jellyfinSettings.Value.Url;
|
||||
var apiKey = _jellyfinSettings.Value.ApiKey;
|
||||
|
||||
if (string.IsNullOrEmpty(jellyfinUrl) || string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
_logger.LogWarning("Jellyfin URL or API key not configured, Spotify playlist injection disabled");
|
||||
_logger.LogInformation("========================================");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Spotify Import ENABLED");
|
||||
_logger.LogInformation("Configured Playlist IDs: {Count}", _spotifySettings.Value.PlaylistIds.Count);
|
||||
|
||||
// Fetch playlist names from Jellyfin
|
||||
await LoadPlaylistNamesAsync();
|
||||
|
||||
foreach (var kvp in _playlistIdToName)
|
||||
{
|
||||
_logger.LogInformation(" - {Name} (ID: {Id})", kvp.Value, kvp.Key);
|
||||
}
|
||||
_logger.LogInformation("========================================");
|
||||
|
||||
// Run once on startup if we haven't run in the last 24 hours
|
||||
if (!_hasRunOnce)
|
||||
{
|
||||
var shouldRunOnStartup = await ShouldRunOnStartupAsync();
|
||||
if (shouldRunOnStartup)
|
||||
{
|
||||
_logger.LogInformation("Running initial fetch on startup (bypassing sync window check)");
|
||||
try
|
||||
{
|
||||
await FetchMissingTracksAsync(stoppingToken, bypassSyncWindowCheck: true);
|
||||
_hasRunOnce = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during startup fetch");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Skipping startup fetch - already have recent cache");
|
||||
_hasRunOnce = true;
|
||||
}
|
||||
}
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await FetchMissingTracksAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error fetching Spotify missing tracks");
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadPlaylistNamesAsync()
|
||||
{
|
||||
_playlistIdToName.Clear();
|
||||
|
||||
// Use configured playlist names instead of fetching from API
|
||||
for (int i = 0; i < _spotifySettings.Value.PlaylistIds.Count; i++)
|
||||
{
|
||||
var playlistId = _spotifySettings.Value.PlaylistIds[i];
|
||||
var playlistName = i < _spotifySettings.Value.PlaylistNames.Count
|
||||
? _spotifySettings.Value.PlaylistNames[i]
|
||||
: playlistId; // Fallback to ID if name not configured
|
||||
|
||||
_playlistIdToName[playlistId] = playlistName;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> ShouldRunOnStartupAsync()
|
||||
{
|
||||
_logger.LogInformation("=== STARTUP CACHE CHECK ===");
|
||||
_logger.LogInformation("Cache directory: {Dir}", CacheDirectory);
|
||||
_logger.LogInformation("Checking {Count} playlists", _playlistIdToName.Count);
|
||||
|
||||
// List all files in cache directory for debugging
|
||||
try
|
||||
{
|
||||
if (Directory.Exists(CacheDirectory))
|
||||
{
|
||||
var files = Directory.GetFiles(CacheDirectory, "*.json");
|
||||
_logger.LogInformation("Found {Count} JSON files in cache directory:", files.Length);
|
||||
foreach (var file in files)
|
||||
{
|
||||
var fileInfo = new FileInfo(file);
|
||||
var age = DateTime.UtcNow - fileInfo.LastWriteTimeUtc;
|
||||
_logger.LogInformation(" - {Name} (age: {Age:F1}h, size: {Size} bytes)",
|
||||
Path.GetFileName(file), age.TotalHours, fileInfo.Length);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Cache directory does not exist: {Dir}", CacheDirectory);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error listing cache directory");
|
||||
}
|
||||
|
||||
// Check file cache first, then Redis
|
||||
foreach (var playlistName in _playlistIdToName.Values)
|
||||
{
|
||||
var filePath = GetCacheFilePath(playlistName);
|
||||
_logger.LogInformation("Checking playlist: {Playlist}", playlistName);
|
||||
_logger.LogInformation(" Expected file path: {Path}", filePath);
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var fileAge = DateTime.UtcNow - File.GetLastWriteTimeUtc(filePath);
|
||||
_logger.LogInformation(" File exists! Age: {Age:F1}h", fileAge.TotalHours);
|
||||
|
||||
if (fileAge < TimeSpan.FromHours(24))
|
||||
{
|
||||
_logger.LogInformation(" ✓ Found recent file cache (age: {Age:F1}h)", fileAge.TotalHours);
|
||||
|
||||
// Load from file into Redis if not already there
|
||||
var key = $"spotify:missing:{playlistName}";
|
||||
if (!await _cache.ExistsAsync(key))
|
||||
{
|
||||
_logger.LogInformation(" Loading into Redis...");
|
||||
await LoadFromFileCache(playlistName);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation(" Already in Redis");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation(" File too old ({Age:F1}h > 24h), will fetch new", fileAge.TotalHours);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation(" File does not exist at expected path");
|
||||
}
|
||||
|
||||
var cacheKey = $"spotify:missing:{playlistName}";
|
||||
if (await _cache.ExistsAsync(cacheKey))
|
||||
{
|
||||
_logger.LogInformation(" ✓ Found in Redis cache");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation(" Not in Redis cache");
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("=== NO RECENT CACHE FOUND - WILL FETCH ===");
|
||||
return true;
|
||||
}
|
||||
|
||||
private string GetCacheFilePath(string playlistName)
|
||||
{
|
||||
var safeName = string.Join("_", playlistName.Split(Path.GetInvalidFileNameChars()));
|
||||
return Path.Combine(CacheDirectory, $"{safeName}_missing.json");
|
||||
}
|
||||
|
||||
private async Task LoadFromFileCache(string playlistName)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filePath = GetCacheFilePath(playlistName);
|
||||
if (!File.Exists(filePath))
|
||||
return;
|
||||
|
||||
var json = await File.ReadAllTextAsync(filePath);
|
||||
var tracks = JsonSerializer.Deserialize<List<MissingTrack>>(json);
|
||||
|
||||
if (tracks != null && tracks.Count > 0)
|
||||
{
|
||||
var cacheKey = $"spotify:missing:{playlistName}";
|
||||
var fileAge = DateTime.UtcNow - File.GetLastWriteTimeUtc(filePath);
|
||||
var ttl = TimeSpan.FromHours(24) - fileAge;
|
||||
|
||||
if (ttl > TimeSpan.Zero)
|
||||
{
|
||||
await _cache.SetAsync(cacheKey, tracks, ttl);
|
||||
_logger.LogInformation("Loaded {Count} tracks from file cache for {Playlist}",
|
||||
tracks.Count, playlistName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to load file cache for {Playlist}", playlistName);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveToFileCache(string playlistName, List<MissingTrack> tracks)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filePath = GetCacheFilePath(playlistName);
|
||||
var json = JsonSerializer.Serialize(tracks, new JsonSerializerOptions { WriteIndented = true });
|
||||
await File.WriteAllTextAsync(filePath, json);
|
||||
_logger.LogInformation("Saved {Count} tracks to file cache for {Playlist}",
|
||||
tracks.Count, playlistName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to save file cache for {Playlist}", playlistName);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task FetchMissingTracksAsync(CancellationToken cancellationToken, bool bypassSyncWindowCheck = false)
|
||||
{
|
||||
var settings = _spotifySettings.Value;
|
||||
var now = DateTime.UtcNow;
|
||||
var syncStart = now.Date
|
||||
.AddHours(settings.SyncStartHour)
|
||||
.AddMinutes(settings.SyncStartMinute);
|
||||
var syncEnd = syncStart.AddHours(settings.SyncWindowHours);
|
||||
|
||||
// Only run after the sync window has passed (unless bypassing for startup)
|
||||
if (!bypassSyncWindowCheck && now < syncEnd)
|
||||
{
|
||||
_logger.LogInformation("Skipping fetch - sync window not passed yet (now: {Now}, window ends: {End})",
|
||||
now, syncEnd);
|
||||
return;
|
||||
}
|
||||
|
||||
if (bypassSyncWindowCheck)
|
||||
{
|
||||
_logger.LogInformation("=== FETCHING MISSING TRACKS (STARTUP MODE) ===");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("=== FETCHING MISSING TRACKS (SYNC WINDOW PASSED) ===");
|
||||
}
|
||||
|
||||
_logger.LogInformation("Processing {Count} playlists", _playlistIdToName.Count);
|
||||
foreach (var kvp in _playlistIdToName)
|
||||
{
|
||||
_logger.LogInformation("Fetching playlist: {Name}", kvp.Value);
|
||||
await FetchPlaylistMissingTracksAsync(kvp.Value, cancellationToken);
|
||||
}
|
||||
|
||||
_logger.LogInformation("=== FINISHED FETCHING MISSING TRACKS ===");
|
||||
}
|
||||
|
||||
private async Task FetchPlaylistMissingTracksAsync(
|
||||
string playlistName,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var cacheKey = $"spotify:missing:{playlistName}";
|
||||
|
||||
if (await _cache.ExistsAsync(cacheKey))
|
||||
{
|
||||
_logger.LogInformation(" ✓ Cache already exists for {Playlist}, skipping fetch", playlistName);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation(" No cache found, will search for missing tracks file...");
|
||||
|
||||
var settings = _spotifySettings.Value;
|
||||
var jellyfinUrl = _jellyfinSettings.Value.Url;
|
||||
var apiKey = _jellyfinSettings.Value.ApiKey;
|
||||
|
||||
if (string.IsNullOrEmpty(jellyfinUrl) || string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
_logger.LogWarning(" Jellyfin URL or API key not configured, skipping fetch");
|
||||
return;
|
||||
}
|
||||
|
||||
var httpClient = _httpClientFactory.CreateClient();
|
||||
|
||||
// Start from the configured sync time (most likely time)
|
||||
var now = DateTime.UtcNow;
|
||||
var todaySync = now.Date
|
||||
.AddHours(settings.SyncStartHour)
|
||||
.AddMinutes(settings.SyncStartMinute);
|
||||
|
||||
// If we haven't reached today's sync time yet, start from yesterday's sync time
|
||||
var syncTime = now >= todaySync ? todaySync : todaySync.AddDays(-1);
|
||||
|
||||
_logger.LogInformation(" Searching +12h forward, -24h backward from {SyncTime}", syncTime);
|
||||
|
||||
var found = false;
|
||||
|
||||
// Search forward 12 hours from sync time
|
||||
_logger.LogInformation(" Phase 1: Searching forward 12 hours from sync time...");
|
||||
for (var minutesAhead = 0; minutesAhead <= 720; minutesAhead++) // 720 minutes = 12 hours
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) break;
|
||||
|
||||
var time = syncTime.AddMinutes(minutesAhead);
|
||||
if (await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Small delay every 60 requests
|
||||
if (minutesAhead > 0 && minutesAhead % 60 == 0)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
// Then search backwards 24 hours from sync time to catch yesterday's file
|
||||
if (!found)
|
||||
{
|
||||
_logger.LogInformation(" Phase 2: Searching backward 24 hours from sync time...");
|
||||
for (var minutesBehind = 1; minutesBehind <= 1440; minutesBehind++) // 1440 minutes = 24 hours
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) break;
|
||||
|
||||
var time = syncTime.AddMinutes(-minutesBehind);
|
||||
if (await TryFetchMissingTracksFile(playlistName, time, jellyfinUrl, apiKey, httpClient, cancellationToken))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Small delay every 60 requests
|
||||
if (minutesBehind % 60 == 0)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
_logger.LogWarning(" ✗ Could not find missing tracks file (searched +12h/-24h window)");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> TryFetchMissingTracksFile(
|
||||
string playlistName,
|
||||
DateTime time,
|
||||
string jellyfinUrl,
|
||||
string apiKey,
|
||||
HttpClient httpClient,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var filename = $"{playlistName}_missing_{time:yyyy-MM-dd_HH-mm}.json";
|
||||
var url = $"{jellyfinUrl}/Viperinius.Plugin.SpotifyImport/MissingTracksFile" +
|
||||
$"?name={Uri.EscapeDataString(filename)}&api_key={apiKey}";
|
||||
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Trying {Filename}", filename);
|
||||
var response = await httpClient.GetAsync(url, cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
var tracks = ParseMissingTracks(json);
|
||||
|
||||
if (tracks.Count > 0)
|
||||
{
|
||||
var cacheKey = $"spotify:missing:{playlistName}";
|
||||
|
||||
// Save to both Redis and file
|
||||
await _cache.SetAsync(cacheKey, tracks, TimeSpan.FromHours(24));
|
||||
await SaveToFileCache(playlistName, tracks);
|
||||
|
||||
_logger.LogInformation(
|
||||
"✓ Cached {Count} missing tracks for {Playlist} from {Filename}",
|
||||
tracks.Count, playlistName, filename);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Failed to fetch {Filename}", filename);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<MissingTrack> ParseMissingTracks(string json)
|
||||
{
|
||||
var tracks = new List<MissingTrack>();
|
||||
|
||||
try
|
||||
{
|
||||
var doc = JsonDocument.Parse(json);
|
||||
|
||||
foreach (var item in doc.RootElement.EnumerateArray())
|
||||
{
|
||||
var track = new MissingTrack
|
||||
{
|
||||
SpotifyId = item.GetProperty("Id").GetString() ?? "",
|
||||
Title = item.GetProperty("Name").GetString() ?? "",
|
||||
Album = item.GetProperty("AlbumName").GetString() ?? "",
|
||||
Artists = item.GetProperty("ArtistNames")
|
||||
.EnumerateArray()
|
||||
.Select(a => a.GetString() ?? "")
|
||||
.Where(a => !string.IsNullOrEmpty(a))
|
||||
.ToList()
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(track.Title))
|
||||
{
|
||||
tracks.Add(track);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to parse missing tracks JSON");
|
||||
}
|
||||
|
||||
return tracks;
|
||||
}
|
||||
}
|
||||
@@ -78,11 +78,18 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new List<Song>();
|
||||
throw new HttpRequestException($"HTTP {response.StatusCode}");
|
||||
}
|
||||
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// Check for error in response body
|
||||
var result = JsonDocument.Parse(json);
|
||||
if (result.RootElement.TryGetProperty("detail", out _) ||
|
||||
result.RootElement.TryGetProperty("error", out _))
|
||||
{
|
||||
throw new HttpRequestException("API returned error response");
|
||||
}
|
||||
|
||||
var songs = new List<Song>();
|
||||
if (result.RootElement.TryGetProperty("data", out var data) &&
|
||||
@@ -188,7 +195,15 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
||||
{
|
||||
foreach(var playlist in items.EnumerateArray())
|
||||
{
|
||||
playlists.Add(ParseTidalPlaylist(playlist));
|
||||
try
|
||||
{
|
||||
playlists.Add(ParseTidalPlaylist(playlist));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Failed to parse playlist, skipping");
|
||||
// Skip this playlist and continue with others
|
||||
}
|
||||
}
|
||||
}
|
||||
return playlists;
|
||||
|
||||
@@ -4,5 +4,23 @@
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"SpotifyImport": {
|
||||
"Enabled": false,
|
||||
"SyncStartHour": 16,
|
||||
"SyncStartMinute": 15,
|
||||
"SyncWindowHours": 2,
|
||||
"Playlists": [
|
||||
{
|
||||
"Name": "Release Radar",
|
||||
"SpotifyName": "Release Radar",
|
||||
"Enabled": true
|
||||
},
|
||||
{
|
||||
"Name": "Discover Weekly",
|
||||
"SpotifyName": "Discover Weekly",
|
||||
"Enabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"Type": "Subsonic"
|
||||
},
|
||||
"Subsonic": {
|
||||
"Url": "https://navidrome.local.bransonb.com",
|
||||
"Url": "http://localhost:4533",
|
||||
"MusicService": "SquidWTF",
|
||||
"ExplicitFilter": "All",
|
||||
"DownloadMode": "Track",
|
||||
@@ -42,5 +42,23 @@
|
||||
"Redis": {
|
||||
"Enabled": true,
|
||||
"ConnectionString": "localhost:6379"
|
||||
},
|
||||
"SpotifyImport": {
|
||||
"Enabled": false,
|
||||
"SyncStartHour": 16,
|
||||
"SyncStartMinute": 15,
|
||||
"SyncWindowHours": 2,
|
||||
"Playlists": [
|
||||
{
|
||||
"Name": "Release Radar",
|
||||
"SpotifyName": "Release Radar",
|
||||
"Enabled": true
|
||||
},
|
||||
{
|
||||
"Name": "Discover Weekly",
|
||||
"SpotifyName": "Discover Weekly",
|
||||
"Enabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,14 @@ services:
|
||||
- Jellyfin__CacheDurationHours=${CACHE_DURATION_HOURS:-1}
|
||||
- Jellyfin__EnableExternalPlaylists=${ENABLE_EXTERNAL_PLAYLISTS:-true}
|
||||
|
||||
# ===== SPOTIFY PLAYLIST INJECTION (JELLYFIN ONLY) =====
|
||||
- SpotifyImport__Enabled=${SPOTIFY_IMPORT_ENABLED:-false}
|
||||
- SpotifyImport__SyncStartHour=${SPOTIFY_IMPORT_SYNC_START_HOUR:-16}
|
||||
- SpotifyImport__SyncStartMinute=${SPOTIFY_IMPORT_SYNC_START_MINUTE:-15}
|
||||
- SpotifyImport__SyncWindowHours=${SPOTIFY_IMPORT_SYNC_WINDOW_HOURS:-2}
|
||||
- SpotifyImport__PlaylistIds=${SPOTIFY_IMPORT_PLAYLIST_IDS:-}
|
||||
- SpotifyImport__PlaylistNames=${SPOTIFY_IMPORT_PLAYLIST_NAMES:-}
|
||||
|
||||
# ===== SHARED =====
|
||||
- Library__DownloadPath=/app/downloads
|
||||
- SquidWTF__Quality=${SQUIDWTF_QUALITY:-FLAC}
|
||||
@@ -85,6 +93,8 @@ services:
|
||||
- Qobuz__Quality=${QOBUZ_QUALITY:-FLAC}
|
||||
volumes:
|
||||
- ${DOWNLOAD_PATH:-./downloads}:/app/downloads
|
||||
- ${KEPT_PATH:-./kept}:/app/kept
|
||||
- ${CACHE_PATH:-./cache}:/app/cache
|
||||
|
||||
networks:
|
||||
allstarr-network:
|
||||
|
||||
Reference in New Issue
Block a user