Compare commits

..

355 Commits

Author SHA1 Message Date
7cee0911b6 fix: progress bar external detection and download row removal
Some checks failed
CI / build-and-test (push) Has been cancelled
- Handle JsonElement when deserializing ProviderIds from cache
- Check for external provider keys (SquidWTF, Deezer, Qobuz, Tidal)
- Fix row removal selector to properly escape path
- Progress bar now correctly shows local vs external split
2026-02-06 22:33:08 -05:00
a2b1eace5f feat: add kept downloads section to admin UI
- List all downloaded files with artist/album/file info
- Download button to save files locally
- Delete button with live row removal
- Shows total file count and size
- Auto-refreshes every 30 seconds
- Security: path validation to prevent directory traversal
2026-02-06 22:29:28 -05:00
ac1fbd4b34 fix: progress bar and add missing tracks section
- Fix external track detection in progress bar (check for external provider names in ProviderIds)
- Add missing tracks section at bottom of Active Playlists tab
- Shows all unmatched tracks across all playlists
- Includes Map to Local and Map to External buttons for each missing track
- Auto-refreshes with other playlist data
2026-02-06 22:12:15 -05:00
a6ac0dfbd2 feat: aggressive track matching with optimal order
- Strip decorators FIRST (feat, remaster, explicit, etc)
- Substring matching SECOND (cheap, high-precision)
- Levenshtein distance THIRD (expensive, fuzzy)
- Greedy assignment LAST (optimal global matching)
- Lower threshold to 40 (was 50-60) for max coverage
- Accept artist priority matches (artist 70+, title 30+)
- Handles cases like 'luther' → 'luther (feat. sza)'
- Handles cases like 'a' → 'a-blah' with same artist
- Prevents duplicate assignments across tracks
2026-02-06 21:22:42 -05:00
bb3140a247 feat: add Spotify ID to local tracks for lyrics support
- Inject Spotify ID into ProviderIds for manually mapped local tracks
- Also add Spotify ID to fuzzy-matched local tracks
- Enables Spotify Lyrics API to work for local Jellyfin tracks
- Fallback to Spotify lyrics when local track has no embedded lyrics
2026-02-06 20:32:32 -05:00
791e6a69d9 feat: re-add manual local Jellyfin track mapping support
- Allow mapping Spotify tracks to local Jellyfin tracks via JellyfinId
- Supports both local (Jellyfin) and external (provider) manual mappings
- Local mappings take priority over fuzzy matching
- Helps when automatic matching fails for tracks already in Jellyfin library
2026-02-06 20:29:29 -05:00
3ffa09dcfa fix: improve fuzzy matching for tracks with special formatting
- Lower matching threshold from 60 to 50 for more lenient matching
- Add fallback to trust provider's top result when artist matches well (>=70)
- Helps match tracks with parentheses, brackets, and stylized titles like 'PiLlOwT4lK'
- Provider search already does fuzzy matching, so trust it when artist is correct
2026-02-06 20:16:36 -05:00
b366a4b771 fix: add rate limiting for Odesli/song.link API
- Implemented semaphore-based rate limiter (10 requests per minute)
- Odesli API allows 10 requests per 60 seconds
- Rate limiter ensures 1 request per 6 seconds maximum
- Prevents API rate limit violations
- Cache still used first (30 day TTL) to minimize API calls
2026-02-06 19:55:16 -05:00
960d15175e fix: remove artist deduplication and add placeholder image support
- Removed ALL artist deduplication from search results
- Show both local and external artists with same name (e.g., Taylor Swift + Taylor Swift [S])
- Users can now browse external artist albums not in local library
- Added placeholder image support for missing cover art
- Placeholder served for both Jellyfin and external providers when image is null
- Added logging to SquidWTF artist search to show actual URLs
- Removed temporary documentation files from repo
- TIDAL image URLs already correctly implemented with UUID splitting

Changes:
- SearchItems: No deduplication, sort by relevance score
- SearchHints: No deduplication, show all matches
- GetArtists: No deduplication, show all matches
- GetImage: Returns placeholder.png when image unavailable
- Added GetPlaceholderImageAsync() helper method
2026-02-06 19:49:26 -05:00
1d774111e7 fix: show both local and external artists with same name
- Artists with same name now appear separately (local + external [S])
- Fixed deduplication to keep one local AND one external per name
- Added logging to SquidWTF artist search to show actual URLs
- External artists get [S] suffix to distinguish from local
- Allows users to browse external artist albums not in local library
- TIDAL image URLs already correctly implemented with UUID splitting
2026-02-06 19:41:06 -05:00
99d701a355 docs: add session management fix documentation and update TODO 2026-02-06 16:37:26 -05:00
73509eb80b feat: create sessions and WebSocket connections for external track playback
- External tracks now create Jellyfin sessions on playback start
- Sessions maintained via WebSocket connections to Jellyfin
- Session activity updated during progress reports
- Sessions auto-cleanup after 50s grace period when playback stops
- Clients playing external tracks now appear in Jellyfin dashboard
- Added comprehensive testing documentation
2026-02-06 16:36:23 -05:00
eb8e3196da feat: Odesli/song.link conversion for Spotify lyrics on external tracks 2026-02-06 16:30:13 -05:00
401d0b4008 feat: add Clear Cache & Rebuild button for playlists in Admin UI
- New endpoint: POST /api/admin/playlists/{name}/clear-cache
- Clears Redis cache keys (items, matched tracks, missing tracks)
- Deletes file caches
- Triggers automatic rebuild with latest code (includes Spotify IDs)
- Added prominent button in Admin UI playlist table
- Shows confirmation dialog with details of what will be cleared
2026-02-06 14:57:07 -05:00
6ccc6a4a0d debug: add logging to verify Spotify IDs in cached playlist items
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-06 14:15:49 -05:00
c54503f486 fix: Spotify lyrics validation and proactive prefetching
- Only attempt Spotify lyrics for tracks with valid Spotify IDs (22 chars, no 'local' or ':')
- Add Spotify IDs to external matched tracks in playlists for lyrics support
- Proactively fetch and cache lyrics when playback starts (background task)
- Fix pre-existing SubSonicController bug (missing _cache field)
- Lyrics now ready instantly when requested by client
2026-02-06 13:04:40 -05:00
fbac81df64 feat: add 1-hour cache for playlist cover images 2026-02-06 12:31:56 -05:00
3a433e276c refactor: reorganize apis folder into steering and api-calls 2026-02-06 12:20:54 -05:00
0c14f4a760 chore: explicitly ignore documentation files in apis folder 2026-02-06 12:16:39 -05:00
28c4f8f5df Remove local Jellyfin manual mapping, keep only external mappings 2026-02-06 12:05:26 -05:00
a3830c54c4 Use Jellyfin item IDs for lyrics check instead of searching
- Lyrics prefetch now uses playlist items cache which has Jellyfin item IDs
- Directly queries /Audio/{itemId}/Lyrics endpoint (no search needed)
- Eliminates all 401 errors and 'no client headers' warnings
- Priority order: 1) Local Jellyfin lyrics, 2) Spotify lyrics API, 3) LRCLib
- Much more efficient - no fuzzy searching required
- Only searches by artist/title as fallback if item ID not available
- All 225 tests passing
2026-02-06 11:53:35 -05:00
4226ead53a Add file-based caching for admin UI and fix Jellyfin API usage
- Added 5-minute file cache for playlist summary to speed up admin UI loads
- Added refresh parameter to force cache bypass
- Invalidate cache when playlists are refreshed or tracks are matched
- Fixed incorrect use of anyProviderIdEquals (Emby API) in Jellyfin
- Now searches Jellyfin by artist and title instead of provider ID
- Fixes 401 errors and 'no client headers' warnings in lyrics prefetch
- All 225 tests passing
2026-02-06 11:48:01 -05:00
2155c4a9d5 Fix delete button for manual track mappings
- Use data attributes instead of inline onclick to avoid quote escaping issues
- Add event listeners after rendering the table
- Fixes issue where Remove button didn't work due to escaped quotes in onclick attribute
2026-02-06 11:42:01 -05:00
a56b2c3ea3 Add delete button for manual track mappings
- Added DELETE /api/admin/mappings/tracks endpoint
- Removes mapping from JSON file and Redis cache
- Deletes file if it becomes empty after removal
- Added 'Remove' button to each mapping in web UI
- Enhanced confirm dialog explaining consequences for both local and external mappings
- Supports removing both Jellyfin (local) and external provider mappings
- Allows phasing out local mappings in favor of Spotify Import plugin
2026-02-06 11:36:51 -05:00
810247ba8c Add manual track mappings display to web UI
- Shows all manual mappings in Active Playlists tab
- Displays summary counts (total, jellyfin, external)
- Table shows playlist, Spotify ID, type, target, and creation date
- Color-coded badges for jellyfin vs external mappings
- Auto-refreshes every 30 seconds
- Helps review mappings before phasing out local ones
2026-02-06 11:18:48 -05:00
96814aa91b Add endpoint to view all manual track mappings
- GET /api/admin/mappings/tracks returns all manual mappings
- Shows both Jellyfin (local) and external provider mappings
- Groups by playlist and includes creation timestamps
- Returns counts for jellyfin vs external mappings
2026-02-06 11:16:09 -05:00
d52c0fc938 Add Spotify ID lookup for external tracks to enable Spotify lyrics
- External tracks from playlists now look up their Spotify ID from matched tracks cache
- Enables Spotify lyrics API to work for SquidWTF/Deezer/Qobuz tracks
- Searches through all playlist matched tracks to find the Spotify ID
- Falls back to LRCLIB if no Spotify ID found or lyrics unavailable
2026-02-06 11:14:55 -05:00
64eff088fa Remove incorrect healthcheck from spotify-lyrics service 2026-02-06 11:02:00 -05:00
ff6dfede87 Change Spotify lyrics API external port to 8365 2026-02-06 10:54:43 -05:00
d8696e254f Expose Spotify lyrics API on port 8080 for testing 2026-02-06 10:52:44 -05:00
261f20f378 Add Spotify lyrics test endpoint
- Add GET /api/admin/lyrics/spotify/test endpoint
- Accepts trackId query parameter (Spotify track ID)
- Returns lyrics in both JSON and LRC format
- Useful for testing Spotify lyrics API integration
2026-02-06 10:51:16 -05:00
ad5fea7d8e Fix LRCLib and SquidWTF error handling
Some checks failed
CI / build-and-test (push) Has been cancelled
- Handle nullable duration in LRCLib API responses
- Validate input parameters before making LRCLib requests
- Change SquidWTF artist warning to debug level (expected behavior)
- Prevent JSON deserialization errors when duration is null
- Prevent 400 Bad Request errors from empty track names
2026-02-06 02:07:45 -05:00
8a3abdcbf7 Fix LyricsStartupValidator build errors
- Remove duplicate _httpClient field (use inherited one)
- Replace ValidationResult.Warning with ValidationResult.Failure
- Use PARTIAL status for partial failures
2026-02-06 01:54:32 -05:00
f103dac6c8 Add comprehensive lyrics startup validation with '22' test
- Revert album endpoint back to ?id= (correct parameter)
- Update SquidWTF validator to test '22' by Taylor Swift
- Create LyricsStartupValidator testing all lyrics services:
  * LRCLib API
  * Spotify Lyrics Sidecar (docker container)
  * Spotify API configuration
- Test song: '22' by Taylor Swift (Spotify ID: 3yII7UwgLF6K5zW3xad3MP)
- Register lyrics validator in startup orchestrator
2026-02-06 01:48:12 -05:00
7abc26c069 Fix album detail endpoint to use correct parameter
- Change album endpoint from ?id= to ?f= to match API spec
- Album search parsing is correct (data.albums.items)
- Album detail parsing is correct (data with items array)
2026-02-06 01:45:10 -05:00
a2e9021100 Fix artist detail parsing to handle missing artist property
- Add TryGetProperty check for artist field in albums response
- Log response keys when artist data not found for debugging
- Improves error handling when API returns albums without artist field
2026-02-06 01:39:43 -05:00
5f22fb0a3b Integrate Spotify lyrics sidecar service
- Add spotify-lyrics-api sidecar container to docker-compose
- Replace direct Spotify API lyrics code with sidecar API calls
- Update SpotifyLyricsService to use sidecar exclusively
- Add LyricsApiUrl setting to SpotifyApiSettings
- Update prefetch to try Spotify lyrics first, then LRCLib
- Remove unused direct API authentication and parsing code
2026-02-06 01:24:49 -05:00
a3d1d81810 Add Spotify lyrics sidecar service and integrate with prefetch
- Add spotify-lyrics-api container to docker-compose
- Update SpotifyLyricsService to use sidecar API
- Prefetch now tries Spotify lyrics first (using track ID), then LRCLib
- Add SPOTIFY_LYRICS_API_URL setting
- Sidecar handles sp_dc cookie authentication automatically
2026-02-06 01:21:30 -05:00
2dd7020a61 Add logging to Spotify lyrics search for better debugging
Show when Spotify search is skipped, fails, or finds no matches
2026-02-06 00:59:23 -05:00
e36e685bee Fix lyrics cache key mismatch between prefetch and lookup
Use same artist format (comma-separated) in prefetch as LrclibService
to ensure cached lyrics are found during playback
2026-02-06 00:58:13 -05:00
7ff6dbbe7a Prioritize local Jellyfin lyrics over LRCLib in prefetch
Some checks failed
CI / build-and-test (push) Has been cancelled
- Check for embedded lyrics in local Jellyfin tracks before fetching from LRCLib
- Remove previously cached LRCLib lyrics when local lyrics are found
- Prevents unnecessary API calls and respects user's embedded lyrics
- Tracks with local lyrics are counted as 'cached' in prefetch stats
2026-02-05 15:09:59 -05:00
e0dbd1d4fd feat: Add lyrics ID mapping system, fix playlist display, enhance track view
- Add complete lyrics ID mapping system with Redis cache, file persistence, and cache warming
- Manual lyrics mappings checked FIRST before automatic search in LrclibService
- Add lyrics status badge to track view (blue badge shows when lyrics are cached)
- Enhance search links to show 'Search: Track Title - Artist Name'
- Fix Active Playlists tab to read from .env file directly (shows all 18 playlists now)
- Add Map Lyrics ID button to every track with modal for entering lrclib.net IDs
- Add POST /api/admin/lyrics/map and GET /api/admin/lyrics/mappings endpoints
- Lyrics mappings stored in /app/cache/lyrics_mappings.json with no expiration
- Cache warming loads lyrics mappings on startup
- All mappings follow same pattern as track mappings (Redis + file + warming)
2026-02-05 14:58:57 -05:00
328a6a0eea Add lyrics completion bar per playlist showing percentage of tracks with cached lyrics
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-05 12:44:11 -05:00
9abb53de1a Fix search to use SquidWTF HiFi API with round-robin base URLs, capitalize provider names in UI, and widen tracks modal to 90% 2026-02-05 12:35:33 -05:00
349fb740a2 Fix scrobbling: track playing item in session and send proper PlaybackStopped data on cleanup 2026-02-05 11:56:26 -05:00
b604d61039 Adjust modal size to 75% width and 65% height, call PlaybackStopped when cleaning up sessions 2026-02-05 11:53:35 -05:00
3b8d83b43e Add lyrics prefetch endpoint and UI button: prefetch lyrics for individual playlists with progress feedback 2026-02-05 11:45:36 -05:00
8555b67a38 Fix external track streaming: normalize provider names to lowercase (squidwtf, deezer, qobuz) 2026-02-05 11:40:45 -05:00
629e95ac30 Improve logging: clarify search vs manual mappings, show manual mapping counts in final log 2026-02-05 11:38:26 -05:00
2153a24c86 Make modal wider (800px) and taller (90vh) to fit buttons side by side 2026-02-05 11:35:09 -05:00
1ddb3954f3 Add missing using statement for IMusicMetadataService 2026-02-05 11:27:22 -05:00
3319c9b21b Fix external mapping: add Map to External button for external tracks, fetch metadata from provider, set searchQuery for missing tracks 2026-02-05 11:23:01 -05:00
8966fb1fa2 Add lyrics prefetching for injected playlists with file cache
New LyricsPrefetchService automatically fetches lyrics for all tracks in
Spotify injected playlists. Lyrics are cached in Redis and persisted to
disk for fast loading on startup.

Features:
- Prefetches lyrics for all playlist tracks on startup (after 3min delay)
- Daily refresh to catch new tracks
- File cache at /app/cache/lyrics for persistence
- Cache warming on startup loads lyrics from disk into Redis
- Rate limited to 2 requests/second to be respectful to LRCLIB API
- Logs fetched/cached/missing counts per playlist

Benefits:
- Instant lyrics availability for playlist tracks
- Survives container restarts
- Reduces API calls during playback
- Better user experience with pre-loaded lyrics
2026-02-05 11:15:42 -05:00
3b24ef3e78 Fix: Fetch full metadata for manual external mappings
Manual external mappings now fetch complete track metadata from the
provider (SquidWTF) instead of using minimal Spotify metadata. This
ensures proper IDs, artist IDs, album IDs, cover art, and all metadata
needed for playback.

Also fixed admin UI to properly detect manual external mappings so
tracks show as 'External' instead of 'Missing'.

Changes:
- Fetch full Song metadata using GetSongAsync when manual mapping exists
- Fallback to minimal metadata if fetch fails
- Admin controller now checks isManualMapping flag to set correct status
- Tracks with manual external mappings now show proper provider badge
2026-02-05 11:13:26 -05:00
dbeb060d52 Fix: Manual external mappings now work correctly for playback
Two critical fixes:
1. External songs from manual mappings now get proper IDs (ext-{provider}-song-{id})
   - Previously had no ID, causing 'dummy' errors in Jellyfin
   - Now follows same format as auto-matched external tracks

2. Admin UI now correctly shows manual external mappings as available
   - Previously showed as 'Missing' even after mapping
   - Now properly detects manual external mappings and shows provider badge

This fixes the 400 Bad Request errors when trying to play manually mapped tracks.
2026-02-05 11:12:15 -05:00
2155a287a5 Add manual mapping indicators and search button for missing tracks
- Manual mappings now show a blue 'Manual' badge next to the track status
- Added search button (🔍) for missing tracks to help find them
- Backend now returns isManualMapping, manualMappingType, and manualMappingId
- Frontend displays manual mapping indicators for both local and external tracks
- Missing tracks now show a search link to help locate them on SquidWTF
2026-02-05 10:20:31 -05:00
cb57b406c1 Fix: Manual external mappings now properly included in playlist cache
The bug was in PreBuildPlaylistItemsCacheAsync - when a manual external
mapping was found, it was added to matchedTracks but the code used
'continue' to skip to the next track WITHOUT adding it to finalItems.

This meant external manual mappings were never included in the playlist
cache that gets served to clients.

The fix converts the external song to Jellyfin item format and adds it
to finalItems before continuing, ensuring manual external mappings are
properly included in the pre-built playlist cache.
2026-02-05 10:07:57 -05:00
e91833ebbb Fix variable name conflict and change cache logs to DEBUG level
- Fixed CS0136 error: renamed 'doc' to 'extDoc' in AdminController to avoid variable name conflict
- Changed all Redis cache logs (HIT/MISS/SET) to DEBUG level instead of suppressing
- This allows cache logs to be visible in docker logs but not as noisy at INFO level
2026-02-05 09:59:28 -05:00
2e1577eb5a Fix external mapping deserialization and suppress cache MISS logs
- Fixed RuntimeBinderException when processing external mappings by replacing dynamic with JsonDocument parsing
- Suppressed cache MISS logs for manual/external mappings (they're expected to be missing most of the time)
- Only log manual/external mapping HITs at INFO level, other cache operations at DEBUG level
- Applied fix to SpotifyTrackMatchingService (2 locations) and AdminController (2 locations)
2026-02-05 09:57:07 -05:00
7cb722c396 Fix HasValue method to handle JsonElement properly
- Changed parameter type from dynamic? to object? to avoid runtime binding issues
- Added check for JsonValueKind.Undefined in addition to Null
- Fixes crash when checking external mappings that return JsonElement
- Applied fix to both AdminController and SpotifyTrackMatchingService
2026-02-05 09:40:39 -05:00
9dcaddb2db Make manual mappings permanent and persist to file
- Manual mappings now have NO expiration (permanent in Redis)
- Save manual mappings to /app/cache/mappings/*.json files
- Load manual mappings on startup via CacheWarmingService
- Manual mappings are first-order and survive restarts/cache clears
- User decisions are now truly permanent
2026-02-05 09:33:37 -05:00
5766cf9f62 Delete file caches when manual mappings are created
- When mapping a track to local or external, delete both Redis and file caches
- This forces the matched tracks cache to rebuild with the new mapping
- Ensures manual mappings are immediately reflected in playlists
2026-02-05 09:31:07 -05:00
a12d5ea3c9 Fix excessive track matching and reduce HTTP logging noise
- Added 5-minute cooldown between matching runs to prevent spam
- Improved cache checking to skip unnecessary matching
- Persist matched tracks cache to file for faster restarts
- Cache warming service now loads matched tracks on startup
- Suppress verbose HTTP client logs (LogicalHandler/ClientHandler)
- Only run matching when cache is missing or manual mappings added
2026-02-05 09:30:00 -05:00
25bbf45cbb Fix memory leak in ActiveDownloads dictionary
- Changed ActiveDownloads from Dictionary to ConcurrentDictionary for thread safety
- Added automatic cleanup of completed downloads after 5 minutes
- Added automatic cleanup of failed downloads after 2 minutes
- This fixes the 929MB -> 10MB memory issue where downloads were never removed from tracking
2026-02-05 09:19:32 -05:00
3fd13b855d Fix RuntimeBinderException, add session cleanup, memory stats endpoint, and fix all warnings
- Fixed RuntimeBinderException when comparing JsonElement with null
- Added HasValue() helper method for safe dynamic type checking
- Implemented intelligent session cleanup:
  * 50 seconds after playback stops (allows song changes)
  * 3 minutes of total inactivity (catches crashed clients)
- Added memory stats endpoint: GET /api/admin/memory-stats
- Added sessions monitoring endpoint: GET /api/admin/sessions
- Added GetSessionsInfo() to JellyfinSessionManager for debugging
- Fixed all nullable reference warnings
- Reduced warnings from 10 to 0
2026-02-05 09:17:40 -05:00
d9c0b8bb54 Add separate 'Map to External' button for missing tracks
Some checks failed
CI / build-and-test (push) Has been cancelled
- Missing tracks now show both 'Map to Local' and 'Map to External' buttons
- External tracks continue to show only 'Map to Local' button
- Added openExternalMap() function that opens modal in external mapping mode
- Added event listeners for .map-external-btn buttons
- External mapping button styled with warning color to distinguish from local mapping
- Users can now easily choose between mapping to Jellyfin tracks or external provider IDs
2026-02-05 00:26:02 -05:00
400ea31477 Fix missing track labeling and add external manual mapping support
- Fixed syntax errors in AdminController.cs (missing braces, duplicate code)
- Implemented proper track status logic to distinguish between:
  * Local tracks: isLocal=true, externalProvider=null
  * External matched tracks: isLocal=false, externalProvider='SquidWTF'
  * Missing tracks: isLocal=null, externalProvider=null
- Added external manual mapping support for SquidWTF/Deezer/Qobuz IDs
- Updated frontend UI with dual mapping modes (Jellyfin vs External)
- Extended ManualMappingRequest class with ExternalProvider + ExternalId fields
- Updated SpotifyTrackMatchingService to handle external manual mappings
- Fixed variable name conflicts and dynamic argument casting issues
- All tests passing (225/225)

Resolves issue where missing tracks incorrectly showed provider name instead of 'Missing' status.
2026-02-05 00:15:23 -05:00
b1cab0ddfc Fix missing track labeling and add external manual mapping support
FIXES:
- Fixed track display logic to properly distinguish between external matched and missing tracks
- Missing tracks now show 'Missing' instead of incorrectly showing provider name
- Added support for manual external provider mappings (e.g., SquidWTF IDs)

CHANGES:
- Extended ManualMappingRequest to support ExternalProvider + ExternalId
- Updated SaveManualMapping to handle both Jellyfin and external mappings
- Updated SpotifyTrackMatchingService to check for external manual mappings
- Updated AdminController track details to use proper missing/matched logic

NOTE: Build currently has syntax errors that need to be fixed, but core logic is implemented.
2026-02-04 23:56:21 -05:00
7cba915c5e Fix authentication issues in SpotifyTrackMatchingService
- Fixed SpotifyTrackMatchingService to use GetJsonAsyncInternal for authenticated requests
- This resolves 401 Unauthorized errors when fetching existing playlist tracks
- Should prevent unnecessary rematching when cache is warm
- Fixes 'No Items found in Jellyfin playlist response' warnings

The service was using GetJsonAsync with null headers, causing 401 errors.
Now uses server API key authentication for internal operations.
2026-02-04 23:44:45 -05:00
dfd7d678e7 Add internal API method and fix playlist count authentication
- Added GetJsonAsyncInternal method to JellyfinProxyService for server-side requests
- Uses server API key instead of client tokens for internal operations
- Updated UpdateSpotifyPlaylistCounts to use internal method with proper authentication
- This should resolve 401 Unauthorized errors when updating playlist counts

Now Spotify playlists should show correct track counts without authentication issues.
2026-02-04 23:42:16 -05:00
4071f6d650 Fix authentication issue in UpdateSpotifyPlaylistCounts
- Added UserId parameter to playlist items request to avoid 401 Unauthorized
- Fixed JsonElement casting issue that was causing InvalidCastException
- This should resolve both the authentication error and the track count update

Now Spotify playlists should show correct track counts without authentication errors.
2026-02-04 23:40:13 -05:00
d045b33afd Fix Spotify playlist track counts to include external tracks
- Changed totalAvailableCount calculation to include both local and external matched tracks
- Updated logging to show breakdown of local vs external tracks
- This fixes Discover Weekly and other external-only playlists showing 0 tracks in clients

Now playlists with all external tracks will show correct track counts in Feishin and other clients.
2026-02-04 23:35:10 -05:00
4f74b34b9a Fix Spotify playlist track counts in client responses
- Fixed UpdateSpotifyPlaylistCounts to use GetPlaylistByJellyfinId instead of GetPlaylistById
- Fixed GetSpotifyPlaylistTracksOrderedAsync to use correct playlist config lookup
- Added diagnostic logging for playlist config lookups
- Removed test-websocket.html file

This fixes the issue where Spotify playlists showed 0 tracks in playlist lists
but worked correctly when accessed directly.
2026-02-04 23:31:30 -05:00
b7417614b3 Remove memory optimization markdown file 2026-02-04 23:18:38 -05:00
72b1584d51 Fix admin dashboard to show total playable tracks (local + external matched) 2026-02-04 23:16:56 -05:00
4b289e4ddd Move admin endpoints to internal port 5275 for security 2026-02-04 22:55:21 -05:00
07844cc9c5 Add GC hints to prevent memory leaks from large byte arrays 2026-02-04 22:50:35 -05:00
1601b96800 Add memory monitoring endpoint 2026-02-04 22:45:11 -05:00
7db66067f4 Complete mark-for-deletion system and memory optimization 2026-02-04 22:41:08 -05:00
f44d8652b4 Improve favorite/unfavorite logic - copy from cache, avoid re-downloads 2026-02-04 22:34:11 -05:00
8fad6d8c4e Fix manual mapping detection in Active Playlists tab
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-04 19:35:34 -05:00
d11b656b23 Add loading state to save mapping button and timeout handling 2026-02-04 19:24:02 -05:00
cf1428d678 Fix manual mapping race condition and add log gitignore 2026-02-04 19:17:48 -05:00
030937b196 Add error handling and better logging for playlist cache deserialization 2026-02-04 19:10:04 -05:00
f77281fd3d Fix GetJellyfinTrack: add UserId and verify Audio type for URL-based mapping 2026-02-04 19:04:12 -05:00
791a8b3fdb Fix Jellyfin search: add UserId and verify Audio type 2026-02-04 19:03:21 -05:00
7311bbc04a Add debug logging to GetPlaylists cache reading 2026-02-04 18:59:00 -05:00
696a2d56f2 Fix manual mappings: preserve on rematch + fix local/external count detection 2026-02-04 18:53:09 -05:00
5680b9c7c9 Fix GetPlaylists to use pre-built cache with manual mappings for accurate counts 2026-02-04 18:49:12 -05:00
1d31784ff8 Fix manual mapping: add immediate playlist rebuild and manual mapping priority in cache builder 2026-02-04 18:38:25 -05:00
10e58eced9 fix: add authentication to playlist cache pre-building
- PreBuildPlaylistItemsCacheAsync was failing with HTTP 401
- Background services don't have client headers for authentication
- Now manually creates X-Emby-Authorization header with API key
- Fixes 'Failed to fetch Jellyfin playlist items: HTTP 401' warning
- Playlist items cache now builds successfully after track matching

All 225 tests pass.
2026-02-04 18:23:11 -05:00
0937fcf163 fix: accurate playlist counting and three-color progress bars
- Fix playlist counting logic to use fuzzy matching (same as track view)
- Count local tracks by matching Jellyfin tracks to Spotify tracks
- Count external matched tracks from cache
- Count missing tracks (not found locally or externally)
- Progress bars now show three colors:
  * Green: Local tracks in Jellyfin
  * Orange: External matched tracks (SquidWTF/Deezer/Qobuz)
  * Grey: Missing tracks (not found anywhere)
- Add 'Missing' badge to tracks that couldn't be found
- Missing tracks can still be manually mapped
- Fixes incorrect counts like '28 matched • 1 missing' showing 29 external tracks

All 225 tests pass.
2026-02-04 17:49:10 -05:00
506f39d606 feat: instant UI update after manual track mapping
- Backend now returns mapped track details after saving
- Frontend updates track in-place without requiring page refresh
- Track status changes from External to Local immediately
- Map button is removed after successful mapping
- Playlist counts refresh in background
- Improved UX: no more 'refresh the playlist' message

All 225 tests pass.
2026-02-04 17:44:57 -05:00
7bb7c6a40e fix: manual mapping UI and [S] tag consistency
- Fix manual mapping track selection visual feedback (use accent color + background)
- Clear all playlist caches after manual mapping (matched, ordered, items)
- Strip [S] suffix from titles/artists/albums when searching for lyrics
- Add [S] suffix to artist and album names when song has [S] for consistency
- Ensures external tracks are clearly marked across all metadata fields

All 225 tests pass.
2026-02-04 17:31:56 -05:00
3403f7a8c9 fix: remove orphaned code causing JavaScript syntax error
Removed duplicate/orphaned lines after searchProvider() function that were
causing 'expected expression, got }' syntax error in admin UI.
2026-02-04 17:06:24 -05:00
3e5c57766b feat: pre-build playlist cache and make matching interval configurable
- Pre-build playlist items cache during track matching for instant serving
- Add PreBuildPlaylistItemsCacheAsync() to SpotifyTrackMatchingService
- Combines local Jellyfin tracks + external matched tracks in correct Spotify order
- Saves to both Redis and file cache for persistence across restarts
- Change matching interval from hardcoded 30 minutes to configurable (default: 24 hours)
- Add SPOTIFY_IMPORT_MATCHING_INTERVAL_HOURS environment variable
- Set to 0 to only run once on startup (manual trigger still works)
- Add endpoint usage files to .gitignore
- Update documentation in README and .env.example

Rationale: Spotify playlists like Discover Weekly update once per week,
so running every 24 hours is more than sufficient. Pre-building the cache
eliminates slow 'on the fly' playlist building.

All 225 tests pass.
2026-02-04 17:03:50 -05:00
24c6219189 Fix external track counting by checking matched tracks cache
- External tracks are injected on-the-fly, not stored in Jellyfin DB
- Check spotify:matched:ordered cache to get accurate external count
- Calculate external tracks as: total matched - local tracks
- This will properly show the two-color progress bar (green local + orange external)
- All 225 tests passing
2026-02-04 16:54:56 -05:00
ea21d5aa77 Add clickable search links and enhanced debug logging
- Made search query clickable - opens provider-specific search
- Added searchProvider() function with URLs for Deezer, Qobuz, SquidWTF
- Enhanced console logging to debug progress bar data
- Logs raw playlist data and calculated percentages
- Search link opens in new tab
2026-02-04 16:51:46 -05:00
ee84770397 Improve progress bar visibility and add debug logging
- Increased progress bar height from 8px to 12px for better visibility
- Changed colors to more vibrant shades (green #10b981, orange #f59e0b)
- Added console debug logging for playlist stats
- Shows local (green) and external (orange) track percentages side-by-side
2026-02-04 16:50:20 -05:00
7ccb660299 Add startup cache warming service
- Proactively loads all file caches into Redis on container startup
- Warms genre cache (30-day expiration)
- Warms playlist items cache (24-hour expiration)
- Logs warming progress and duration
- Ensures fast access immediately after restart
- Cleans up expired genre cache files automatically
- All 225 tests passing
2026-02-04 16:46:27 -05:00
0793c4614b Add file-based caching for MusicBrainz genres
- Dual-layer caching: Redis (fast) + file system (persistent)
- File cache survives container restarts
- 30-day cache expiration for both layers
- Negative result caching to avoid repeated failed lookups
- Safe file names using base64 encoding
- Automatic cache restoration to Redis on startup
- Cache directory: /app/cache/genres
2026-02-04 16:44:35 -05:00
bf02dc5a57 Add MusicBrainz genre enrichment and improve track counting
- Fixed external track detection (check for provider prefix in ID)
- Added genre support to MusicBrainz service (inc=genres+tags)
- Created GenreEnrichmentService for async genre lookup with caching
- Show provider name and search query for external tracks in admin UI
- Display search query that will be used for external track streaming
- Aggregate playlist genres from track genres
- All 225 tests passing
2026-02-04 16:43:17 -05:00
7938871556 Release 1.0.0 - Production ready
- Fixed AdminController export/import .env endpoints (moved from ConfigUpdateRequest class)
- Added ArtistId and AlbumId to integration test fixtures
- All 225 tests passing
- Version set to 1.0.0 (semantic versioning)
- MusicBrainz service ready for future ISRC-based matching (1.1.0)
- Import/export handles full .env configuration with timestamped backups
2026-02-04 16:33:58 -05:00
39f6893741 Add MusicBrainz API integration for metadata enrichment
- Added MusicBrainzSettings model with username/password authentication
- Created MusicBrainzService with ISRC lookup and recording search
- Implements proper rate limiting (1 req/sec) per MusicBrainz rules
- Added meaningful User-Agent header as required
- Registered service in Program.cs with configuration
- Added MusicBrainz section to appsettings.json
- Credentials stored in .env (MUSICBRAINZ_USERNAME/PASSWORD)

Next: Add to admin UI and implement import/export for .env
2026-02-04 16:23:16 -05:00
cd4fd702fc Match Jellyfin response structure exactly based on real API responses
Verified against real Jellyfin responses for tracks, albums, artists, and playlists:
- Reordered fields to match Jellyfin's exact field order
- Added missing fields: PremiereDate, HasLyrics, Container, ETag, etc.
- Fixed MediaType to 'Unknown' for albums/artists (not null)
- Fixed UserData.Key format to match Jellyfin patterns
- Added ParentLogoItemId, ParentBackdropItemId for proper hierarchy
- Fixed Genres/GenreItems to always be arrays (never null)
- Added complete MediaStream structure with all Jellyfin fields
- Playlists now have MediaType='Audio' to match real playlists
- All responses now perfectly mimic real Jellyfin structure
2026-02-04 16:17:45 -05:00
038c3a9614 Fix playlist count caching and make external tracks perfectly mimic Jellyfin responses
- Fixed UpdateSpotifyPlaylistCounts to properly handle file cache without skipping items
- Added Genres and GenreItems fields to all tracks (empty array if no genre)
- Added complete MediaStreams with audio codec info for external tracks
- Added missing MediaSource fields: IgnoreDts, IgnoreIndex, GenPtsInput, HasSegments
- Ensured Artists array never contains null values
- All external tracks now have proper genre arrays to match Jellyfin structure
2026-02-04 16:12:41 -05:00
6e966f9e0d Fix nullability warnings in SpotifyTrackMatchingService 2026-02-04 16:10:16 -05:00
b778b3d31e Fix MediaSources null array fields and add logging for artist albums
- Added MediaStreams, MediaAttachments, Formats as empty arrays instead of null
- Added RunTimeTicks field to MediaSources
- Added detailed logging to GetExternalChildItems to debug artist album issues
- This should fix 'type Null is not a subtype of type List<dynamic>' error
2026-02-04 16:04:04 -05:00
526a079368 Fix compilation errors and nullability warnings
- Fixed LrclibService.GetLyricsAsync call to use empty string and 0 for duration
- Fixed nullability warnings in SpotifyTrackMatchingService by explicitly casting to nullable tuple
2026-02-04 15:40:52 -05:00
7a7b884af2 Update playlist progress bar to show stacked blue/yellow segments
- Blue segment shows local tracks percentage
- Yellow segment shows external matched tracks percentage
- Bar fills to 100% when all tracks are matched
- Added tooltips showing track counts on hover
2026-02-04 15:37:07 -05:00
6ab5e44112 Fix apostrophe normalization syntax error - use Unicode escape sequences 2026-02-04 15:33:59 -05:00
7c92515723 Fix null boolean error and playlist count showing 0 after restart
- Added all required boolean fields to MediaSources (IsRemote, IsInfiniteStream, RequiresOpening, etc)
- UpdateSpotifyPlaylistCounts now loads from file cache if Redis is empty
- This fixes 'type Null is not a subtype of type bool' error in Finamp
- Playlist counts now show correctly even after container restart
2026-02-04 15:32:18 -05:00
8091d30602 Add parallel provider racing for searches and lyrics pre-fetching
- Created ParallelMetadataService to race all providers and return fastest result
- Search now uses parallel service when available for lower latency
- Pre-fetch LRCLib lyrics for top 3 search results in background
- FuzzyMatcher already handles apostrophe normalization (applied everywhere)
2026-02-04 15:29:56 -05:00
e7ff330625 Add logging for server-to-client WebSocket messages to debug remote control
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-04 11:30:12 -05:00
aadda9b873 Fix apostrophe matching, add URL input for track mapping, improve search caching
Some checks failed
CI / build-and-test (push) Has been cancelled
- Enhanced FuzzyMatcher to normalize apostrophes (', ', ', etc) for better matching
- Added Redis-only caching for search results (15 min TTL)
- Added pattern-based cache deletion for search and image keys
- Added URL input field in Map to Local modal to paste Jellyfin track URLs
- Added /api/admin/jellyfin/track/{id} endpoint to fetch track details by ID
- Fixed duplicate cache key declaration in GetSpotifyPlaylistTracksOrderedAsync
- Updated cache clearing to include new spotify:playlist:items:* keys
2026-02-04 01:44:56 -05:00
8a84237f13 Fix MediaSources field appending to query string
- Properly parse and modify Fields parameter instead of appending to end
- Fixes 400 BadRequest errors from malformed URLs
- Now correctly adds MediaSources to Fields parameter value
2026-02-03 23:56:40 -05:00
e3a118e578 Fix bitrate for injected playlists by preserving raw Jellyfin items
CRITICAL FIX: Don't convert Jellyfin items to Song objects and back!

- Get raw Jellyfin playlist items with MediaSources field
- Reorder them according to Spotify positions
- Inject external tracks where needed
- Return raw items preserving ALL Jellyfin metadata (bitrate, etc)

This ensures local tracks in Spotify playlists show correct bitrate
just like regular Jellyfin playlists.
2026-02-03 23:41:29 -05:00
e17eee9bf3 Fix Map to Local button using data attributes instead of inline onclick
- Avoid JavaScript escaping issues with inline onclick handlers
- Use data attributes to store track info
- Add event listeners after DOM is created
- Prevents syntax errors with special characters in track names
2026-02-03 23:24:19 -05:00
4229924f61 Fix bitrate showing 0K for favorited songs and browse views
Some checks failed
CI / build-and-test (push) Has been cancelled
- Ensure MediaSources field is included when proxying browse requests
- Applies to /Users/{userId}/Items (favorites, recently played, etc)
- Jellyfin doesn't include MediaSources by default, must be requested
- Now bitrate info shows correctly in all browse contexts
2026-02-03 20:04:10 -05:00
a2a48f6ed9 Fix Map to Local button when artists array is empty
- Safely handle empty or undefined artists array
- Prevents 'expected expression, got }' JavaScript error
- Also fixed artists display to handle undefined arrays
2026-02-03 19:55:30 -05:00
c7785b6488 Fix playlist track count to show actual available tracks
- Changed ChildCount to reflect tracks actually in Jellyfin (local + external matched)
- Previously was incorrectly adding missing tracks to the count
- Now clients see the correct number of playable tracks
- Uses spotify:matched:ordered cache to count external matched tracks
2026-02-03 19:53:22 -05:00
af03a53af5 Enhanced playlist statistics in admin dashboard
Backend changes:
- Distinguish between local tracks (in Jellyfin library) and external tracks (downloaded)
- Track external matched vs external missing counts
- Calculate completion percentage for each playlist

Frontend changes:
- Show detailed breakdown: X local • Y matched • Z missing
- Display completion percentage with progress bar
- Color-coded stats (green=local, blue=matched, yellow=missing)
- Updated table headers for clarity
2026-02-03 19:49:32 -05:00
c1c2212b53 Fix undefined artists array causing syntax error 2026-02-03 19:46:48 -05:00
17560f0d34 Fix Map to Local button by properly escaping JavaScript parameters
- Use JSON.stringify instead of escapeHtml for onclick parameters
- Prevents JavaScript syntax errors when track names contain quotes
- Fixes button doing nothing when clicked
2026-02-03 19:46:31 -05:00
6ab314f603 Enhanced cache clearing to include all Spotify playlist Redis keys
- Added clearing of spotify:matched:* and spotify:matched:ordered:* keys
- This ensures bitrate metadata fix takes effect after cache clear
- Returns count of cleared Redis keys in response
2026-02-03 19:41:16 -05:00
64ac09becf Preserve MediaSources metadata for local tracks in playlists to show bitrate 2026-02-03 19:35:39 -05:00
a0bbb7cd4c Integrate WebSocket proxy with session manager to cleanup sessions on client disconnect 2026-02-03 19:10:02 -05:00
4bd478e85c Add HLS playlist (.m3u8, .ts) support to binary proxy handler 2026-02-03 19:04:24 -05:00
f7a88791e8 Fix GetImage endpoint to proxy images instead of redirecting 2026-02-03 19:02:42 -05:00
9f8b3d65fb Fix HttpClient.SendAsync call for image proxy 2026-02-03 18:59:58 -05:00
1a1f9e136f Fix image proxy to handle binary data instead of JSON parsing 2026-02-03 18:59:03 -05:00
48f69b766d Add manual track mapping feature
- Add 'Map to Local' button for external tracks in playlist viewer
- Search Jellyfin library to find local tracks
- Save manual mappings (Spotify ID → Jellyfin ID) in cache
- Manual mappings take priority over fuzzy matching
- Clear playlist cache when mapping is saved to force refresh
- UI shows which tracks are manually mapped in logs
2026-02-03 18:57:19 -05:00
d619881b8e Fix: Correct matching logic - Jellyfin tracks first, then fill gaps
CRITICAL FIX: Changed matching strategy completely
- Step 1: Match ALL Jellyfin tracks to Spotify positions (fuzzy 70%)
- Step 2: Build playlist in Spotify order using matched Jellyfin tracks
- Step 3: Fill remaining gaps with external tracks (cached or on-demand)
- Step 4: Add any unmatched Jellyfin tracks at the end

This ensures Jellyfin tracks are ALWAYS used when they match, preventing
external tracks from being used when local versions exist.
2026-02-03 18:45:07 -05:00
dccdb7b744 Fix: Add on-demand external track search when cache is empty
- Search external providers in real-time if no cached match exists
- Use fuzzy matching (60% threshold) for external tracks
- Ensures external tracks are always available even without pre-matching
- Local tracks still prioritized first (70% threshold)
2026-02-03 18:39:33 -05:00
f240423822 Fix: Prioritize LOCAL tracks in Spotify playlist injection - match by name only
- Remove Spotify ID/ISRC matching (Jellyfin plugin doesn't add these)
- Use ONLY fuzzy name matching (title + artist, 70% threshold)
- LOCAL tracks ALWAYS used first before external providers
- Include ALL tracks from Jellyfin playlist (even if not in Spotify)
- Prevent duplicate track usage with HashSet tracking
- AdminController also updated to match by name for Local/External badges
- Better logging with emojis for debugging
2026-02-03 18:36:33 -05:00
1492778b14 UI fixes: Match per playlist, Match All button, local/external labels, preserve tab on reload 2026-02-03 18:27:29 -05:00
08af650d6c Add fuzzy name matching as fallback for local tracks + better error logging
- Add fuzzy matching by title+artist as fallback (like Jellyfin Spotify Import plugin)
- Add clear error messages when JELLYFIN_USER_ID is not configured
- Add emoji logging for easier debugging (🔍 📌  )
- Check HTTP status code when fetching playlist items
- This should fix the issue where all tracks show [S] even when they exist locally
2026-02-03 18:23:39 -05:00
c44be48eb9 Fix: Add UserId parameter for Jellyfin playlist operations + UI improvements
- CRITICAL FIX: Add UserId parameter to all Jellyfin playlist item fetches (fixes 400 BadRequest errors)
- Fix GetPlaylists to correctly count local/missing tracks
- Fix GetSpotifyPlaylistTracksOrderedAsync to find local tracks (was serving external tracks for everything)
- Fix SpotifyTrackMatchingService to skip tracks already in Jellyfin
- Add detailed debug logging for track matching (LOCAL by ISRC/Spotify ID, EXTERNAL match, NO MATCH)
- Add 'Match Tracks' button for individual playlists (not all playlists)
- Add 'Match All Tracks' button for matching all playlists at once
- Add JELLYFIN_USER_ID to web UI configuration tab for easy setup
- Add /api/admin/playlists/match-all endpoint

This fixes the issue where local tracks weren't being used - the system was downloading
from SquidWTF even when files existed locally in Jellyfin.
2026-02-03 18:14:13 -05:00
b16d16c9c9 Fix: Register SpotifyTrackMatchingService as singleton for DI
- Register as singleton first, then as hosted service
- Allows AdminController to inject and trigger matching manually
- Added better logging for Jellyfin playlist fetch failures
- Logs when JellyfinId is missing or API calls fail
- Helps debug '0 local / 0 missing' issue
2026-02-03 17:51:55 -05:00
e51d569d79 Show actual local/missing track counts in Active Playlists
- Fetches current Jellyfin playlist track count
- Compares with Spotify playlist total to calculate missing tracks
- Shows: '25 local / 5 missing' instead of just track count
- Local = tracks currently in Jellyfin playlist
- Missing = Spotify tracks not yet in Jellyfin (need to be matched)
- Helps users see which playlists need track matching
2026-02-03 17:48:49 -05:00
363c9e6f1b Add Match Tracks button and Local/External column to Active Playlists
- Added 'Match Tracks' button to trigger matching for specific playlist
- Added Local/External column (shows '-' for now, will populate after matching)
- New endpoint: POST /api/admin/playlists/{name}/match
- Injects SpotifyTrackMatchingService into AdminController
- UI shows: Name | Spotify ID | Total | Local/External | Cache Age | Actions
- Allows users to manually trigger matching without waiting 30 minutes
2026-02-03 17:47:44 -05:00
f813fe9eeb Fix: Match local Jellyfin tracks by ISRC instead of Spotify ID
- Local Jellyfin tracks don't have Spotify IDs (only plugin-added tracks do)
- Now matches by ISRC first (most reliable), then falls back to Spotify ID
- Builds dictionaries for fast lookup: existingBySpotifyId and existingByIsrc
- Prioritizes local tracks over external matches
- Logs: 'X tracks (Y with Spotify IDs, Z with ISRCs)'
- This fixes all tracks showing [S] - now uses local files when available
2026-02-03 17:45:53 -05:00
ef0ee65160 Optimize UI: Update playlist status without refetching all playlists
- After linking/unlinking, update UI state directly instead of refetching
- Avoids 7+ second delay from fetching track stats for all playlists
- Added data-playlist-id attribute to table rows for easy lookup
- Only refreshes Active Playlists tab (fast operation)
- Much better UX when linking multiple playlists
2026-02-03 17:40:47 -05:00
b3bfa16b93 Fix RemovePlaylist to use new 4-field config format
- Include JellyfinId when serializing playlists after removal
- Ensures config format stays consistent: [Name,SpotifyId,JellyfinId,position]
- Fixes issue where removed playlists would stay in the list
2026-02-03 17:35:54 -05:00
aa9b5c874d Skip matching tracks already in Jellyfin playlist
- Fetch existing tracks from Jellyfin playlist before matching
- Extract Spotify IDs from ProviderIds to identify already-matched tracks
- Only search for tracks not already in Jellyfin
- Logs: 'Matching X/Y tracks (skipping Z already in Jellyfin)'
- Significantly reduces matching time for playlists with local content
- Example: If 20/50 tracks exist locally, only searches for 30 tracks
2026-02-03 17:29:28 -05:00
e3546425eb Optimize track matching with parallel batch processing
- Process tracks in batches of 11 (matches SquidWTF provider count)
- Each batch runs 11 parallel searches (one per provider)
- Wait 150ms between batches (not between individual tracks)
- This is ~11x faster: 50 tracks now takes ~1 second instead of ~7.5 seconds
- Round-robin in SquidWTF ensures each parallel request hits a different provider
- Maintains rate limiting while maximizing throughput
2026-02-03 17:20:05 -05:00
5646aa07ea Fix playlist injection: Store Jellyfin playlist ID in config
- Added JellyfinId field to SpotifyPlaylistConfig model
- Updated config format: [[Name,SpotifyId,JellyfinId,position],...]
- LinkPlaylist now stores both Jellyfin and Spotify playlist IDs
- IsSpotifyPlaylist() now checks by Jellyfin playlist ID (not Spotify ID)
- GetJellyfinPlaylists shows linked status by checking JellyfinId
- Updated Program.cs to parse new 4-field format
- Backward compatible: JellyfinId defaults to empty string for old configs

This fixes the issue where playlists weren't being recognized as configured
because the code was checking Jellyfin playlist IDs against Spotify IDs.
2026-02-03 17:18:08 -05:00
7cdf7e3806 Switch to Spotify GraphQL API to match Jellyfin plugin approach
- Replace REST API calls with GraphQL API (api-partner.spotify.com/pathfinder/v1/query)
- Use same persisted query approach as Jellyfin Spotify Import plugin
- GraphQL API is more reliable and has better rate limits
- Fetch playlists with operationName: fetchPlaylist
- Parse GraphQL response structure (playlistV2, itemV2, etc)
- This should eliminate TooManyRequests errors
2026-02-03 17:10:38 -05:00
fe9c1e17be Add rate limiting delays to prevent Spotify 429 errors
- Increase delay between playlist fetches from 1s to 3s
- Only delay between playlists, not after the last one
- Add debug logging for rate limit delays
- Spotify is very aggressive with rate limiting on their API
2026-02-03 17:05:34 -05:00
63324def62 Fix Jellyfin BadRequest errors when fetching playlist items
- Add UserId parameter to playlist items API call (required by Jellyfin)
- Auto-fetch first user if no user ID configured
- Simplify Fields parameter to only request Path
- This fixes the 400 BadRequest errors when loading Jellyfin playlists tab
2026-02-03 17:03:04 -05:00
ff72ae2395 Fix playlist linking to save all playlists and add restart banner
- Read playlists from .env file instead of stale in-memory config
- This fixes issue where linking multiple playlists only saved the last one
- Add prominent restart banner at top when config changes
- Update UI immediately after linking/unlinking playlists
- Banner shows 'Restart Now' button and dismissible option
- All config changes now show the restart banner instead of toast messages
- GetJellyfinPlaylists now reads from .env for accurate linked status
2026-02-03 17:00:23 -05:00
1a3134083b Fix remaining pragma warnings in debug console output 2026-02-03 16:55:32 -05:00
bd64f437cd Fix web UI infinite loop and improve cookie age tracking
- Add cookieDateInitialized flag to prevent infinite init-cookie-date calls
- Auto-initialize cookie date when cookie exists but date not tracked
- Improve local vs external track detection in Jellyfin playlists
- Support multiple Spotify playlist ID formats (ID, URI, URL)
- Fix pragma warnings in legacy playlist parsing code
- Simplify GetPlaylistTrackStats to check Path property for local tracks
2026-02-03 16:54:40 -05:00
5606706dc8 Fix web UI config persistence and cookie age tracking
- Add SPOTIFY_API_SESSION_COOKIE_SET_DATE to docker-compose.yml env mapping
- Mount .env file in container for web UI to update
- Add SessionCookieSetDate loading in Program.cs
- Improve .env update logic with better error handling and logging
- Auto-initialize cookie date when cookie exists but date not set
- Simplify local vs external track detection in Jellyfin playlists
- Enhanced Spotify playlist ID parsing (supports ID, URI, and URL formats)
- Better UI clarity: renamed tabs to 'Link Playlists' and 'Active Playlists'
2026-02-03 16:50:19 -05:00
79a9e4063d Remove unnecessary /me API call - not needed for sp_dc auth 2026-02-03 16:27:19 -05:00
c33c85455f Fix JS syntax error: escape quotes in onclick handlers for playlist names 2026-02-03 16:19:13 -05:00
5af2bb1113 Remove hardcoded playlists from appsettings - use web UI instead 2026-02-03 16:14:28 -05:00
2c1297ebec Fix playlist config: dedupe entries, use Spotify playlist ID for lookup 2026-02-03 16:12:25 -05:00
df7f11e769 Add local/external track columns to Jellyfin playlists, remove libraries filter 2026-02-03 16:08:49 -05:00
75c7acb745 Fix Jellyfin playlists tab: linked Spotify ID, track count, and user/library filters 2026-02-03 15:13:29 -05:00
c7f6783fa2 CRITICAL: Stop hammering Spotify API on status checks
- GetStatus() no longer calls Spotify API at all
- Status is now determined purely from configuration
- Fixes rate limiting issue (429 errors every 30 seconds)
- Spotify API should only be called when actually fetching playlists
2026-02-03 15:08:17 -05:00
4c6406ef8f Add full-screen restart overlay for better UX
- Shows overlay with spinner during container restart
- Displays status messages: stopping, waiting, reloading
- Shows elapsed time while waiting for server
- Extended timeout to 60 seconds
- Hides overlay and shows toast if timeout exceeded
2026-02-03 15:06:38 -05:00
3ddf51924b Add container restart capability from admin UI
- Added POST /api/admin/restart endpoint using Docker socket
- Mounts Docker socket read-only in docker-compose.yml
- Admin UI now has working 'Restart Container' button
- Auto-reloads page after container comes back up
- Falls back to manual restart instructions if socket unavailable
2026-02-03 15:05:43 -05:00
3826f29019 Add Jellyfin playlist discovery and linking feature
- Added GET /api/admin/jellyfin/playlists to fetch all playlists from Jellyfin
- Added POST /api/admin/jellyfin/playlists/{id}/link to link playlist to Spotify
- Added DELETE /api/admin/jellyfin/playlists/{name}/unlink to remove link
- Added new 'Jellyfin Playlists' tab in admin UI showing all playlists
- Shows link status for each playlist (Linked/Not Linked)
- Link modal accepts Spotify playlist ID or full URL
- Renamed 'Playlists' tab to 'Configured Playlists' for clarity
2026-02-03 15:03:31 -05:00
4036c739a3 Remove LocalTracksPosition UI, auto-init cookie date tracking
- Removed LocalTracksPosition from playlist table (uses Spotify order now)
- Removed LocalTracksPosition from add playlist modal and JS
- Added /api/admin/config/init-cookie-date endpoint to auto-set date
- Cookie date auto-initializes when cookie exists but date is unknown
- Improved user display message when profile scope unavailable
- TOTP tokens work for playlists but don't have user-read-private scope
2026-02-03 15:00:12 -05:00
b7379e2fd4 Change external track labeling from ' - S' to ' [S]' format
- Updated album suffix from ' - S' to ' [S]'
- Updated artist suffix from ' - S' to ' [S]'
- Added song title suffix ' [S]' for external/streaming tracks
- Cleaner bracket format is more visible and consistent
2026-02-03 14:56:01 -05:00
c9895f6d1a Expand admin UI with full config editing and sp_dc cookie age tracking
- Fix auth status detection to use token validity instead of /me endpoint
- Add SessionCookieSetDate to SpotifyApiSettings for tracking cookie age
- Auto-set cookie date when updating sp_dc via admin UI
- Add edit buttons for all config settings (Spotify, Deezer, Qobuz, SquidWTF, Jellyfin)
- Show cookie age with color-coded expiration warnings (green/yellow/red)
- Display cookie age on both Dashboard and Config tabs
- Add generic edit setting modal supporting text/password/number/toggle/select inputs
- Remove SquidWTF base URL (not configurable)
- Add restart container button with manual restart instructions
2026-02-03 14:52:40 -05:00
71c4241a8a Fix AdminStaticFilesMiddleware to use simple file serving instead of internal ASP.NET classes 2026-02-03 14:43:24 -05:00
ffed9a67f3 Expose admin UI port 5275 in docker-compose (for direct/Tailscale access) 2026-02-03 14:39:18 -05:00
a8d04b225b Move admin UI to separate internal port (5275) for security
- Admin API and static files only accessible on port 5275
- Main proxy port (8080) no longer serves admin endpoints
- AdminPortFilter rejects admin requests on wrong port
- AdminStaticFilesMiddleware only serves static files on admin port
- Port 5275 NOT exposed in Dockerfile or docker-compose by default
- Access admin UI via SSH tunnel or by uncommenting port mapping
2026-02-03 14:39:07 -05:00
6abf0e0717 Add web dashboard UI for configuration and playlist management
- Add AdminController with API endpoints for status, playlists, config
- Add web UI dashboard at /admin with dark theme
- Disable SpotifyMissingTracksFetcher when SpotifyApi is enabled with cookie
- Support viewing playlists, tracks, and managing configuration
- Add .env file modification from UI (requires restart to apply)
- Dashboard auto-refreshes status every 30 seconds
2026-02-03 14:37:26 -05:00
8e7fc8b4ef Add info-level logging for Spotify auth debugging 2026-02-03 14:31:28 -05:00
b2c28d10f1 Fix TOTP secrets URL - use correct xyloflake/spot-secrets-go repo 2026-02-03 14:29:39 -05:00
a335997196 Implement TOTP-based Spotify authentication (like Jellyfin plugin)
- Use pre-scraped TOTP secrets from Viperinius/spotify-totp-secrets
- Generate TOTP code using server time and cipher transformation
- Add OTP.NET package for TOTP generation
- Match the authentication flow used by jellyfin-plugin-spotify-import
2026-02-03 14:27:18 -05:00
590f8f76cb Fix Spotify token endpoint - add required query params and fix cookie handling 2026-02-03 14:22:51 -05:00
1532d74a20 Add SpotifyApi environment variables to docker-compose.yml 2026-02-03 14:18:25 -05:00
f5ce355747 Fix logging levels - use appropriate debug/info/warn/error levels 2026-02-03 14:12:39 -05:00
494b4bbbc2 Reduce WebSocket/session logging noise, add playback progress logging
- Changed routine WebSocket messages (Sessions, KeepAlive) to debug level
- Reduced keep-alive spam - 401 on expired tokens is expected, not an error
- Added periodic progress logging for local tracks (every 10 seconds)
- Log warnings only when progress reports fail
2026-02-03 14:09:40 -05:00
375e1894f3 Add Spotify direct API integration for lyrics, ISRC matching, and playlist ordering
Features:
- SpotifyApiClient: Direct Spotify API client using sp_dc session cookie
- SpotifyLyricsService: Fetch synced lyrics from Spotify's color-lyrics API
- SpotifyPlaylistFetcher: Get playlists with correct track ordering and ISRC codes
- SpotifyTrackMatchingService: ISRC-based exact track matching for external providers

Improvements:
- Lyrics endpoint now prioritizes: 1) Jellyfin embedded, 2) Spotify synced, 3) LRCLIB
- Fixed playback progress reporting - removed incorrect body wrapping for Jellyfin API
- Added SpotifyApiSettings configuration model

Security:
- Session cookie and client ID properly masked in startup logs
- All credentials read from environment variables only
2026-02-03 14:06:40 -05:00
bbb0d9bb73 track count fix
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-03 01:49:58 -05:00
0356f3c54d logic fix 2 2026-02-03 01:46:38 -05:00
0c25d16e42 logic fix 2026-02-03 01:44:02 -05:00
4c3709113f Add legacy Spotify playlist format support with position control 2026-02-03 01:39:33 -05:00
12db8370a3 format 2026-02-03 01:29:27 -05:00
64be6eddf4 .env format 2026-02-03 01:24:08 -05:00
0980547848 Combine Spotify playlist config into single JSON array with local track position option 2026-02-03 00:23:57 -05:00
2bb1ffa581 Fix Spotify playlist to return local tracks + external tracks
Local tracks in Jellyfin playlist are now returned first, with
matched external tracks (from squid.wtf) appended at the end.

Previously the code tried to match local tracks by exact title/artist
which often failed due to naming differences.
2026-02-03 00:06:08 -05:00
51702a544b fix spotify search: start 24h ahead, search backwards for 72h 2026-02-02 23:49:39 -05:00
d9375405a5 add debug logging for websocket auth headers
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-02 20:45:00 -05:00
83063f594a fix websocket session auth and header parsing for jellyfin dashboard 2026-02-02 20:41:25 -05:00
b40349206d websocket on behalf of client 2026-02-02 20:31:07 -05:00
8dbac23944 literally just logging 2026-02-02 20:24:43 -05:00
9fb86d3839 fixed universal 2026-02-02 20:16:02 -05:00
bb2bda1379 x-forwarded-for headers 2026-02-02 20:14:11 -05:00
e9f72efb01 websocket logging 2026-02-02 19:53:39 -05:00
ab36a43892 another 2026-02-02 19:06:33 -05:00
2ffb769a6f another 2026-02-02 18:48:19 -05:00
045c810abc lets try that again 2026-02-02 18:42:12 -05:00
4c55520ce0 entirely new session handling 2026-02-02 18:37:38 -05:00
04079223c2 sync window for spotify playlists 2026-02-02 18:24:49 -05:00
1bb902d96a better searching for the file 2026-02-02 18:22:16 -05:00
b5f3f54c8b I really hate time 2026-02-02 18:19:12 -05:00
3bcb60a09a logging 2026-02-02 18:16:18 -05:00
ba78ed0883 Artists, not artist but now for lyrics 2026-02-02 18:12:12 -05:00
d0f26c0182 Artists, not artist 2026-02-02 15:57:34 -05:00
91275a2835 timestamp logging 2026-02-02 14:40:40 -05:00
ccbc9cf859 multiple artists big fix! 2026-02-02 14:35:53 -05:00
97975f1e08 oh how i hate time 2026-02-02 13:31:10 -05:00
ff48891a5a oh how i hate time 2026-02-02 13:24:21 -05:00
273fac7a0a log time message 2026-02-02 13:19:05 -05:00
12436c2f9c Whoops! cache wasn't clearing for main provider! please remove the files in your downloads folder and pull this commit 2026-02-02 13:13:35 -05:00
2315d6ab9f reduce overlapping runs 2026-02-02 13:05:36 -05:00
6eaeee9a67 remove nonfunctional endpoint
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-02 13:00:04 -05:00
9dd49a2f43 add round robin load balancing so providers dont hate me 2026-02-02 12:57:04 -05:00
2bc2816191 stupid timezones 2026-02-02 12:43:23 -05:00
1a2e160279 Update README pertaining to spotify playlist import 2026-02-02 12:32:11 -05:00
4111b5228d prematch on startup 2026-02-02 12:31:09 -05:00
82b480c47e remove useless md 2026-02-02 12:26:31 -05:00
0d246a8e74 gitignore update 2026-02-02 12:25:21 -05:00
fc3a8134ca spotify track prematching 2026-02-02 12:24:30 -05:00
2f91457e52 fix playlist, fix session sending 2026-02-02 12:18:29 -05:00
77774120bf playback start websocket 2026-02-02 12:09:29 -05:00
936fa27aa7 websocket 5
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-02-02 03:01:58 -05:00
b90ce423d7 websocket 4.0 2026-02-02 02:53:14 -05:00
5f038965a2 websocket 3 2026-02-02 02:51:00 -05:00
229fa0bf65 websocket 2.0 2026-02-02 02:47:12 -05:00
1aec76c3dd websocket fix 2026-02-02 02:43:55 -05:00
96b06d5d4f region fix 2026-02-02 02:40:18 -05:00
747d310375 websocket stuff 2026-02-02 02:39:06 -05:00
fc78a095a9 Websocket Proxying 2026-02-02 02:19:43 -05:00
65ca80f9a0 Fix: Properly handle HTTP status codes in Jellyfin proxy
- Refactor proxy service methods to return (Body, StatusCode) tuples
- Add HandleProxyResponse helper for consistent status code handling
- Fix 401 authentication errors being returned as 204
- Add explicit session reporting endpoints for playback tracking
- Ensure local track playback sessions are forwarded to Jellyfin
- All 219 tests passing
2026-02-02 01:05:25 -05:00
8e6eb5cc4a Support both favorite endpoint formats
Some checks failed
CI / build-and-test (push) Has been cancelled
- Add /UserFavoriteItems/{itemId} route (official Jellyfin API)
- Keep /Users/{userId}/FavoriteItems/{itemId} for compatibility
- Support userId in query string or path
- Add route logging to debug which endpoint is being called
2026-02-01 18:29:31 -05:00
1326f1b3ab Fix unsynced lyrics and favorite endpoints
- Fix unsynced lyrics showing all at 0:00 by omitting Start field
- Add DeleteAsync to proxy service for proper auth forwarding
- Fix favorite/unfavorite endpoints to use proxy service
- Add comprehensive logging for debugging
2026-02-01 18:24:09 -05:00
0011538966 Use LRClib search API with fuzzy matching and prefer synced lyrics
Some checks failed
CI / build-and-test (push) Has been cancelled
- Search API is more forgiving than exact get endpoint
- Scores results by track/artist similarity and duration match
- +20 point bonus for results with synced lyrics
- Falls back to exact match if search fails
- Improves lyrics hit rate for metadata variations
2026-02-01 12:30:24 -05:00
5acdacf132 Fix unsynced lyrics displaying as one big line
When LRClib returns plain/unsynced lyrics, they contain newlines but
were being sent as a single text block. Jellyfin clients would display
them all on one line.

Now splits plain lyrics by newlines and sends each line separately,
so they display properly line-by-line in the client.

LRClib search URL format:
https://lrclib.net/api/get\?track_name\=\{track\}\&artist_name\=\{artist\}\&album_name\=\{album\}\&duration\=\{seconds\}
2026-02-01 12:23:08 -05:00
cef836da43 Fix Spotify playlist ChildCount in SearchItems endpoint
The playlist list was going through SearchItems (not ProxyRequest), so
UpdateSpotifyPlaylistCounts was never called. Now updates counts in both:
- SearchItems when browsing playlists (no search term)
- ProxyRequest for other playlist list requests

This fixes playlists showing 0 tracks when they should show the count
of missing tracks available.
2026-02-01 12:05:45 -05:00
26c9a72def Remove server API key fallback for client requests
SECURITY FIX: Stop using server API key when clients don't provide auth

Before: If client sent no auth → proxy used server API key → gave them access
After: If client sends no auth → proxy sends no auth → Jellyfin rejects (401)

This ensures:
- Unauthenticated users can't piggyback on server credentials
- All actions are properly attributed to the actual user
- Jellyfin's auth system works as intended
- Server API key only used for internal operations (images, library detection)

Updated test to reflect new behavior: GetJsonAsync without client headers
should NOT add any authentication.
2026-02-01 12:00:38 -05:00
f5124bdda2 Add debug logging for Spotify playlist ChildCount updates
- Log when checking items for Spotify playlists
- Log cache lookups and file cache fallbacks
- Log successful ChildCount updates
- Log when no Spotify playlists found
- Helps diagnose why playlist counts might not be updating correctly
2026-02-01 11:56:34 -05:00
f7f57e711c Add endpoint usage logging for analysis
- Log all proxied endpoints to /app/cache/endpoint-usage/endpoints.csv
- CSV format: timestamp, method, path, query string
- Add GET /debug/endpoint-usage?api_key=KEY to view statistics
  - Shows top N endpoints by usage count
  - Filter by date with since parameter
  - Returns total requests, unique endpoints, first/last seen
- Add DELETE /debug/endpoint-usage?api_key=KEY to clear logs
- Thread-safe file appending
- Helps identify which endpoints clients actually use
- Can inform future blocklist/allowlist decisions
2026-02-01 11:52:44 -05:00
76f633afce Add security blocklist for dangerous admin endpoints
- Block system restart/shutdown endpoints
- Block system configuration changes
- Block plugin management (install/uninstall/configure)
- Block scheduled task management
- Block server startup/setup endpoints
- Block user creation endpoint
- Block library management (refresh, virtual folders)
- Block server logs and activity log access
- Log blocked attempts with IP address for security monitoring
- Returns 403 Forbidden with descriptive error message

This maintains client compatibility via catch-all proxy while preventing
unauthorized access to administrative functions.
2026-02-01 11:48:45 -05:00
24df910ffa Update Spotify playlist ChildCount to show actual track count
- Intercept playlist list responses and update ChildCount for Spotify playlists
- Shows the number of missing tracks found (local + matched external)
- Fixes playlist showing 0 songs when Jellyfin has no local files
- Reads from cache (Redis or file) to get accurate count
2026-02-01 11:38:25 -05:00
eb46692b25 Extend backward search window to 24 hours for Spotify missing tracks
- Search forward 12 hours from sync time
- Search backward 24 hours from sync time (was 12 hours)
- Ensures yesterday's file is always found when running at 11 AM after 4 PM sync
- Sync runs daily at 4:15 PM, so 24h backward always catches previous day's file
2026-02-01 11:33:42 -05:00
c54a32ccfc Add extensive logging to debug startup cache loading
- List all files in cache directory on startup
- Show expected file paths and actual file existence
- Log each step of cache checking process
- Add phase indicators for forward/backward search
- Show when cache exists and fetch is skipped
- Help diagnose why yesterday's cache files aren't being loaded
2026-02-01 11:28:00 -05:00
c0c7668cc4 Bypass sync window check on startup to fetch missing tracks immediately
- On startup, if no cache exists, fetch immediately regardless of sync window
- Regular background checks still respect sync window timing
- Ensures playlists are populated even if app restarts before sync time
2026-02-01 11:22:12 -05:00
e860bbe0ee Fix nullable warnings in SpotifyMissingTracksFetcher 2026-02-01 11:18:58 -05:00
df3cc51e17 Fix DownloadSongAsync return type handling 2026-02-01 11:18:17 -05:00
027aeab969 Fix compilation errors in favorite-to-keep and file cache features 2026-02-01 11:14:18 -05:00
449bcc2561 Fix spotify/sync endpoint route priority 2026-02-01 11:11:11 -05:00
8da0bef481 Add favorite-to-keep feature for external tracks
- When favoriting an external track, automatically copy to /kept folder
- Organized as Artist/Album/Track structure
- Includes cover art if available
- Downloads track first if not already cached
- Add KEPT_PATH and CACHE_PATH volumes to docker-compose
- Update .env.example and README with new feature
2026-02-01 11:09:18 -05:00
ae8afa20f8 Add file-based persistence for Spotify missing tracks cache
- Save missing tracks to /app/cache/spotify/*.json files
- 24-hour TTL for file cache
- Fallback to file cache when Redis is empty/restarted
- Controller checks file cache before returning empty playlists
- Ensures playlist data persists across Redis restarts
2026-02-01 11:07:19 -05:00
da1d28d292 Change playlist debug logs from warning to info level
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-01-31 21:32:20 -05:00
7e0ea501fc Fix Spotify missing tracks search order: forward 12h then backward 12h 2026-01-31 21:19:39 -05:00
bb976fed4f add 1337 kbps bitrate to external track MediaSources 2026-01-31 21:08:51 -05:00
df77b16640 fix method name: MapToSong -> ParseSong 2026-01-31 20:57:19 -05:00
74ae85338c merge local Jellyfin tracks with matched external tracks in Spotify playlists 2026-01-31 20:56:03 -05:00
72b7198f1d add fuzzy matching for Spotify track matching with scoring 2026-01-31 20:52:25 -05:00
b24dfb5b6a simplify Spotify track matching to title + artist only 2026-01-31 20:51:55 -05:00
85f8e1cc5f fix SquidWTF fallback to detect API error responses 2026-01-31 20:43:40 -05:00
74bd64c949 add missing using statement for ApiKeyAuthFilter
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-01-31 20:03:26 -05:00
1afa68064e add API key authentication to Spotify admin endpoints 2026-01-31 20:01:59 -05:00
5251c7ef6d add spotify cache clear endpoint 2026-01-31 20:01:12 -05:00
63ab25ca91 parse SPOTIFY_IMPORT_PLAYLIST_NAMES env var 2026-01-31 19:53:03 -05:00
628f845e77 add missing SPOTIFY_IMPORT_PLAYLIST_NAMES env var 2026-01-31 19:49:57 -05:00
8ef5ee7d8f fix background service to use configured playlist names 2026-01-31 19:44:47 -05:00
fb3ea1b876 fix playlist names format in env example 2026-01-31 19:42:11 -05:00
3f3e1b708d add configurable playlist names env var 2026-01-31 19:37:10 -05:00
bc4faead74 fix playlist names to use underscores for filenames 2026-01-31 19:35:55 -05:00
6ffa2a3277 use hardcoded playlist names for Spotify sync 2026-01-31 19:34:45 -05:00
c3c01b5559 check every minute for missing tracks files 2026-01-31 19:33:02 -05:00
47d59ec0f5 search last 24 hours for missing tracks files in manual sync 2026-01-31 19:30:01 -05:00
e7f72cd87a add manual Spotify sync trigger endpoint 2026-01-31 19:12:34 -05:00
6d15d02f16 add Spotify Import environment variables to docker-compose.yml 2026-01-31 18:11:25 -05:00
3137cc4657 fix: remove duplicate Spotify playlist interception code with compilation error 2026-01-31 18:07:22 -05:00
18e700d6a4 Fix compilation errors and remove unused code 2026-01-31 18:03:55 -05:00
2420cd9a23 Fix SpotifyMissingTracksFetcher to work with ID-based configuration and add detailed logging 2026-01-31 18:00:24 -05:00
65d6eb041a Refactor Spotify playlist injection to use playlist IDs instead of names 2026-01-31 17:59:01 -05:00
103808f079 Add logging to every ProxyRequest call 2026-01-31 17:54:51 -05:00
cd29e0de6c Add debug logging to diagnose Spotify configuration issue 2026-01-31 17:21:14 -05:00
bd480be382 Fix Spotify playlist interception in ProxyRequest with detailed logging 2026-01-31 17:17:59 -05:00
293f6f5cc4 Add detailed logging to GetPlaylistItems interception 2026-01-31 17:16:10 -05:00
e9b893eb3e Add detailed Spotify Import configuration logging 2026-01-31 17:14:24 -05:00
51694a395d Add comprehensive debug logging for Spotify playlist interception 2026-01-31 17:09:28 -05:00
32166061ef Add Spotify playlist interception in ProxyRequest method 2026-01-31 17:07:31 -05:00
a8845a9ef3 Change GetPlaylistItems route order to 1 and add debug logging 2026-01-31 17:05:56 -05:00
e873cfe3bf Fix JsonDocument.RootElement access in GetPlaylistTracks 2026-01-31 17:03:36 -05:00
43718eaefc Remove duplicate GetPlaylistItems route and add constructor logging 2026-01-31 17:02:35 -05:00
5f9451f5b4 Add playlist parsing from env var and debug logging for Spotify feature 2026-01-31 17:00:16 -05:00
2c3ef5c360 Fix Spotify playlist injection: add dedicated route, startup fetch, and clarify config 2026-01-31 16:57:52 -05:00
4ba2245876 refactor: optimize playlist interception order
- Check external playlists first (fast check)
- Only check Spotify if enabled (avoid extra API call)
- Fall back to regular Jellyfin playlists
- Maintains correct behavior for all playlist types
2026-01-31 16:54:39 -05:00
c117fa41f6 fix: add route to intercept playlist items requests
- Add GetPlaylistItems route with Order=5 (before catch-all)
- Intercepts /playlists/{id}/items requests
- Routes to GetPlaylistTracks for Spotify playlist handling
2026-01-31 16:54:01 -05:00
2b078453b2 refactor: reuse Jellyfin settings for Spotify feature
- Remove duplicate JellyfinUrl and ApiKey from SpotifyImportSettings
- Use existing JELLYFIN_URL and JELLYFIN_API_KEY settings
- Simplify configuration - no duplicate settings needed
- Update documentation and .env.example
2026-01-31 16:52:26 -05:00
0ee1883ccb fix: intercept existing Jellyfin Spotify playlists
- Check playlist name instead of creating virtual playlists
- Intercept playlist items request for Spotify playlists
- Fill empty playlists with matched tracks from providers
- Works with existing Jellyfin Spotify Import plugin playlists
2026-01-31 16:50:16 -05:00
8912758b5e feat: spotify playlist injection
- Add SpotifyImportSettings configuration model
- Create SpotifyMissingTracksFetcher background service
- Inject virtual Spotify playlists into search results
- Auto-match tracks from external providers
- Update README with feature documentation
- Configure sync window and playlist settings
2026-01-31 16:43:49 -05:00
35d5249843 chore: add sampleMissingPlaylists to gitignore 2026-01-31 12:09:16 -05:00
62bfb367bc docs: update README
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-01-31 11:15:44 -05:00
6f91361966 refactor: use token-based fuzzy matching for flexible search
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-01-31 01:03:14 -05:00
d4036095f1 fix: check all permutations of title/artist/album in search scoring 2026-01-31 01:00:56 -05:00
6620b39357 fix: remove score filtering, add combined title+artist matching 2026-01-31 01:00:00 -05:00
dcaa89171a refactor: change external suffix from H to S (SquidWTF) 2026-01-31 00:50:55 -05:00
1889dc6e19 fix: gracefully skip malformed playlists instead of failing all endpoints 2026-01-31 00:47:59 -05:00
615ad58bc6 refactor: change external provider suffix from SW to H 2026-01-31 00:08:11 -05:00
6176777d0f fix: forward client auth headers for login 2026-01-30 22:09:09 -05:00
a339574f05 fix: forward caching headers for client-side caching
Jellyfin sends ETag, Last-Modified, and Cache-Control headers that allow clients like Feishin to cache songs locally. Proxy now forwards these headers so clients don't re-download songs unnecessarily.
2026-01-30 22:02:35 -05:00
67b4fac64c Merge branch 'main' into dev
Some checks failed
CI / build-and-test (push) Has been cancelled
2026-01-30 14:12:05 -05:00
ada6653bd1 fix: enable deduplication for cache mode
- Cache mode now registers downloaded songs in mappings
- Prevents duplicate downloads in cache mode
- Only library scan/album download skip cache mode
- Fixes wasted API calls and duplicate cache files
2026-01-30 14:10:34 -05:00
df8dbfc5e1 fix: prevent duplicate downloads by registering before releasing lock
- Move RegisterDownloadedSongAsync call before lock release
- Prevents race condition where multiple threads download same song
- Second thread now finds registered mapping before starting download
- Fixes duplicate files like 'Song (1).flac', 'Song (2).flac'
- Library scan and album download still happen after lock release
2026-01-30 13:37:42 -05:00
e8d3fc4d17 refactor: simplify endpoint comments to just names 2026-01-30 13:21:44 -05:00
649351f68b feat: add automatic fallback support for SquidWTF endpoints
- Decode 6 base64 URLs at startup (1 primary + 5 backups)
- Automatic fallback when endpoint fails
- All services try next endpoint on failure
- Metadata, Download, and Validator all support fallback
- Endpoints: triton.squid.wtf, wolf/hund/maus/vogel/katze.qqdl.site
- Logs which endpoint is being used
- Cycles through all endpoints before giving up
2026-01-30 13:21:23 -05:00
3487f79b5e refactor: pass decoded SquidWTF URL from Program.cs to services
- Decode base64 URL once at startup in Program.cs
- Pass decoded URL as constructor parameter to all SquidWTF services
- Services receive plain URL, no decoding logic in service classes
- Cleaner separation: encoding/decoding only in Program.cs
- All service code remains unchanged, just receives decoded URL
2026-01-30 13:16:26 -05:00
3a3f572ead fix: decode base64 URLs once at startup in static fields
- Changed from decoding in constructor to static readonly fields
- Decode happens once per class initialization, stored in memory
- Cleaner implementation, no repeated decoding per instance
- All three SquidWTF services updated (Metadata, Download, Validator)
2026-01-30 13:13:16 -05:00
d7f15fc3ab Add base64 encoding for SquidWTF endpoint (without trailing slash) 2026-01-30 13:08:10 -05:00
e43f5cd427 REVERT: Remove base64 encoding and endpoint fallback, restore simple hardcoded URL 2026-01-30 12:57:42 -05:00
1b79138923 Fix HTTP 307 redirect handling: configure HttpClient to follow redirects 2026-01-30 12:49:58 -05:00
dda9736f8d Fix endpoint fallback: remove shared state, always try primary first 2026-01-30 12:46:40 -05:00
9493cb48a5 Merge branch 'main' into dev 2026-01-30 12:40:41 -05:00
6c2453896f Add automatic endpoint fallback to all SquidWTF metadata search methods 2026-01-30 12:40:34 -05:00
40594dea7e Merge branch 'main' into dev 2026-01-30 12:38:57 -05:00
a06bf42887 Standardize all SquidWTF API URL formats to prevent double slashes 2026-01-30 12:38:19 -05:00
6713007650 Merge branch 'main' into dev 2026-01-30 12:37:13 -05:00
e7724c2cc0 Fix double slash bug in SquidWTF API URLs 2026-01-30 12:36:16 -05:00
3358fe019d Merge branch 'main' into dev 2026-01-30 12:26:43 -05:00
9efc54857f Update startup validator to use base64 encoded endpoint 2026-01-30 12:26:14 -05:00
fcdf47984c Merge branch 'main' into dev 2026-01-30 12:25:28 -05:00
040a5451a1 Fix remaining BaseUrl references 2026-01-30 12:24:39 -05:00
8d76e97449 Merge branch 'main' into dev 2026-01-30 12:13:17 -05:00
a86a8013e6 Add backup API endpoints with automatic fallback 2026-01-30 12:11:41 -05:00
4c557a0325 Merge branch 'main' into dev 2026-01-30 12:01:12 -05:00
8540a22846 Add Contributing section with development setup 2026-01-30 11:59:58 -05:00
36a224bd45 Consolidate Quick Start sections and update to use docker pull 2026-01-30 11:57:05 -05:00
3af3ebb52b Merge branch 'main' into dev 2026-01-30 11:54:57 -05:00
614adb9892 Add build status badges for main and beta branches 2026-01-30 11:51:49 -05:00
f434f13a19 Merge branch 'main' into dev 2026-01-30 11:48:53 -05:00
625a75f8f9 Update docker-compose to use GHCR image by default 2026-01-30 11:46:56 -05:00
d600c5e456 Merge branch 'main' into dev 2026-01-30 11:39:14 -05:00
e23e22a736 Fix nullable reference warnings 2026-01-30 11:36:59 -05:00
ba0fe35e72 Merge branch 'main' into dev 2026-01-30 11:32:42 -05:00
6e9fe0e69e Upgrade to .NET 10.0 2026-01-30 11:31:45 -05:00
cba955c427 Fix JellyfinProxyServiceTests for RedisCacheService parameter 2026-01-30 11:27:14 -05:00
192173ea64 Merge branch 'main' into dev 2026-01-30 11:23:41 -05:00
c33180abd7 Fix: Parse LRC format into individual lines for Feishin compatibility
Feishin expects lyrics as an array of {Start, Text} objects with timestamps
in ticks, not as a single LRC-formatted text block
2026-01-30 03:00:55 -05:00
680454e76e Clarify lyrics preference: synced lyrics preferred over plain
Already working correctly, just made the code clearer with explicit variable
2026-01-30 02:28:05 -05:00
34bfc20d28 Change Redis cache logging from Debug to Info level
Makes cache hits/misses visible in production logs without needing Debug level
2026-01-30 02:20:11 -05:00
489159b424 Add debug logging for Redis cache hits/misses
Now logs when cache entries are retrieved (HIT/MISS) and when they're stored
2026-01-30 02:17:45 -05:00
2bb754b245 Fix: Pass through full query string for browse requests
Recently added, recently played, and frequently played sections use
different query parameters (SortBy, SortOrder, Filters, etc) that we
weren't forwarding. Now we pass the complete query string to Jellyfin.
2026-01-30 02:15:48 -05:00
8d8c0892a2 Fix: Handle decimal duration values from LRCLIB API
LRCLIB returns duration as a decimal/float, not int. Convert to int using Math.Round.
2026-01-30 02:13:10 -05:00
e12851e9ca Fix: Handle nullable Duration in lyrics endpoint 2026-01-30 02:10:25 -05:00
f8969bea8d Add LRCLIB lyrics integration for Jellyfin
- Create LrclibService to fetch lyrics from lrclib.net API
- Add LyricsInfo model for lyrics data
- Add /Audio/{itemId}/Lyrics and /Items/{itemId}/Lyrics endpoints
- Support both local and external songs
- Cache lyrics for 30 days in Redis
- Return lyrics in Jellyfin format with synced/plain lyrics
2026-01-30 02:09:27 -05:00
ceaa17f018 Fix: Register SquidWTFSettings configuration in Program.cs
The SquidWTF quality setting from .env was not being loaded because
SquidWTFSettings wasn't registered with the DI container.
2026-01-30 00:50:34 -05:00
9aa7ceb138 Remove unused _preferredQuality field from SquidWTFDownloadService
The quality setting is already being used correctly via _squidwtfSettings.Quality
2026-01-30 00:49:41 -05:00
72b1ebc2eb Fix: Wrap playback request bodies in required field names for Jellyfin
Jellyfin expects playback endpoints to have bodies wrapped:
- /Sessions/Playing -> {"playbackStartInfo": {...}}
- /Sessions/Playing/Progress -> {"playbackProgressInfo": {...}}
- /Sessions/Playing/Stopped -> {"playbackStopInfo": {...}}

This fixes the 'field is required' validation errors.
2026-01-30 00:48:38 -05:00
48a0351862 Improve POST body debugging for playback endpoints
- Better logging to show what client sends vs what we forward
- Log all headers when body is empty to help diagnose issues
2026-01-30 00:45:16 -05:00
4b95f9910c Fix: Ensure POST requests always send body content to Jellyfin
- Always send body content for POST requests, even if empty (send '{}')
- Update TODO.md to mark tasks 2 and 3 as done
- Improve logging for POST body debugging
2026-01-30 00:44:39 -05:00
80424a867d Fix: Update last access time for cached files to enable proper cleanup 2026-01-30 00:41:17 -05:00
4afd769602 Fix: Proxy Artists/{id}/Similar to correct Jellyfin endpoint 2026-01-30 00:29:56 -05:00
b47a5f9063 Add ' - SW' suffix to external albums and artists
Makes it easy to distinguish external content from local Jellyfin library
2026-01-30 00:21:37 -05:00
29 changed files with 1564 additions and 2557 deletions

View File

@@ -18,30 +18,28 @@ SUBSONIC_URL=http://localhost:4533
# Server URL (required if using Jellyfin backend) # Server URL (required if using Jellyfin backend)
JELLYFIN_URL=http://localhost:8096 JELLYFIN_URL=http://localhost:8096
# API key for SERVER-SIDE operations only (get from Jellyfin Dashboard > API Keys) # API key for authentication (get from Jellyfin Dashboard > API Keys)
# This is used by Allstarr to query Jellyfin's library on behalf of the server
# CLIENT authentication is handled transparently - clients authenticate directly with Jellyfin
JELLYFIN_API_KEY= JELLYFIN_API_KEY=
# User ID for SERVER-SIDE library queries (get from Jellyfin Dashboard > Users > click user > check URL) # User ID (get from Jellyfin Dashboard > Users > click user > check URL)
# This determines which user's library Allstarr queries when searching/browsing
JELLYFIN_USER_ID= JELLYFIN_USER_ID=
# Music library ID (optional, auto-detected if not set) # Music library ID (optional, auto-detected if not set)
# If you have multiple libraries, set this to filter to music only
JELLYFIN_LIBRARY_ID= JELLYFIN_LIBRARY_ID=
# ===== MUSIC SOURCE SELECTION ===== # ===== MUSIC SOURCE SELECTION =====
# Music service to use: SquidWTF, Deezer, or Qobuz (default: SquidWTF) # Music service to use: SquidWTF, Deezer, or Qobuz (default: SquidWTF)
MUSIC_SERVICE=SquidWTF MUSIC_SERVICE=SquidWTF
# Base directory for all downloads (default: ./downloads) # Path where downloaded songs will be stored on the host (only applies if STORAGE_MODE=Permanent)
# This creates three subdirectories:
# - downloads/permanent/ - Permanently downloaded tracks (STORAGE_MODE=Permanent)
# - downloads/cache/ - Temporarily cached tracks (STORAGE_MODE=Cache)
# - downloads/kept/ - Favorited external tracks (always permanent)
DOWNLOAD_PATH=./downloads 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 ===== # ===== SQUIDWTF CONFIGURATION =====
# Different quality options for SquidWTF. Only FLAC supported right now # Different quality options for SquidWTF. Only FLAC supported right now
SQUIDWTF_QUALITY=FLAC SQUIDWTF_QUALITY=FLAC
@@ -110,14 +108,27 @@ CACHE_DURATION_HOURS=1
# ===== SPOTIFY PLAYLIST INJECTION (JELLYFIN ONLY) ===== # ===== SPOTIFY PLAYLIST INJECTION (JELLYFIN ONLY) =====
# REQUIRES: Jellyfin Spotify Import Plugin (https://github.com/Viperinius/jellyfin-plugin-spotify-import) # REQUIRES: Jellyfin Spotify Import Plugin (https://github.com/Viperinius/jellyfin-plugin-spotify-import)
# This feature intercepts Spotify Import plugin playlists and fills them with tracks from external providers # 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) # Uses JELLYFIN_URL and JELLYFIN_API_KEY configured above (no separate credentials needed)
# Enable Spotify playlist injection (optional, default: false) # Enable Spotify playlist injection (optional, default: false)
SPOTIFY_IMPORT_ENABLED=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
# Matching interval: How often to run track matching (in hours) # Matching interval: How often to run track matching (in hours)
# Spotify playlists like Discover Weekly update once per week, Release Radar updates weekly # Spotify playlists like Discover Weekly update once per week, Release Radar updates weekly
# Most playlists don't change frequently, so running once per day is reasonable
# Set to 0 to only run once on startup (manual trigger via admin UI still works) # Set to 0 to only run once on startup (manual trigger via admin UI still works)
# Default: 24 hours # Default: 24 hours
SPOTIFY_IMPORT_MATCHING_INTERVAL_HOURS=24 SPOTIFY_IMPORT_MATCHING_INTERVAL_HOURS=24

6
.gitignore vendored
View File

@@ -89,9 +89,6 @@ apis/api-calls/*.json
!apis/api-calls/jellyfin-openapi-stable.json !apis/api-calls/jellyfin-openapi-stable.json
apis/temp.json apis/temp.json
# Temporary documentation files
apis/*.md
# Log files for debugging # Log files for debugging
apis/api-calls/*.log apis/api-calls/*.log
@@ -104,6 +101,3 @@ originals/
# Sample missing playlists for Spotify integration testing # Sample missing playlists for Spotify integration testing
sampleMissingPlaylists/ sampleMissingPlaylists/
# Migration guide (local only)
MIGRATION.md

333
README.md
View File

@@ -5,7 +5,11 @@
[![Docker Image](https://img.shields.io/badge/docker-ghcr.io%2Fsopat712%2Fallstarr-blue)](https://github.com/SoPat712/allstarr/pkgs/container/allstarr) [![Docker Image](https://img.shields.io/badge/docker-ghcr.io%2Fsopat712%2Fallstarr-blue)](https://github.com/SoPat712/allstarr/pkgs/container/allstarr)
[![License](https://img.shields.io/badge/license-GPL--3.0-green)](LICENSE) [![License](https://img.shields.io/badge/license-GPL--3.0-green)](LICENSE)
A media server proxy that integrates music streaming providers with your local library. Works with **Jellyfin** and **Subsonic-compatible** servers. When a song isn't in your local library, it gets fetched from your configured provider, downloaded, and served to your client. The downloaded song then lives in your library for next time. A media server proxy that integrates music streaming providers with your local library. Works with **Jellyfin** and **Subsonic-compatible** servers (Navidrome). When a song isn't in your local library, it gets fetched from your configured provider, downloaded, and served to your client. The downloaded song then lives in your library for next time.
**THIS IS UNDER ACTIVE DEVELOPMENT**
Please report all bugs as soon as possible, as the Jellyfin addition is entirely a test at this point
## Quick Start ## Quick Start
@@ -36,15 +40,15 @@ The proxy will be available at `http://localhost:5274`.
## Web Dashboard ## Web Dashboard
Allstarr includes a web UI for easy configuration and playlist management, accessible at `http://localhost:5275` Allstarr includes a web-based dashboard for easy configuration and playlist management, accessible at `http://localhost:5275` (internal port, not exposed through reverse proxy).
### Features ### Features
- **Playlist Management**: Link Jellyfin playlists to Spotify playlists with just a few clicks - **Real-time Status**: Monitor Spotify authentication, cookie age, and playlist sync status
- **Provider Matching**: It should fill in the gaps of your Jellyfin library with tracks from your selected provider - **Playlist Management**: Link Jellyfin playlists to Spotify playlists with a few clicks
- **WebUI**: Update settings without manually editing .env files - **Configuration Editor**: Update settings without manually editing .env files
- **Music**: Using multiple sources for music (optimized for SquidWTF right now, though) - **Track Viewer**: Browse tracks in your configured playlists
- **Lyrics**: Using multiple sources for lyrics, first Jellyfin Lyrics, then Spotify Lyrics, then LrcLib as a last resort - **Cache Management**: Clear cached data and restart the container
### Quick Setup with Web UI ### Quick Setup with Web UI
@@ -61,20 +65,18 @@ Allstarr includes a web UI for easy configuration and playlist management, acces
- `37i9dQZF1DXcBWIGoYBM5M` (just the ID) - `37i9dQZF1DXcBWIGoYBM5M` (just the ID)
- `spotify:playlist:37i9dQZF1DXcBWIGoYBM5M` (Spotify URI) - `spotify:playlist:37i9dQZF1DXcBWIGoYBM5M` (Spotify URI)
- `https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M` (full URL) - `https://open.spotify.com/playlist/37i9dQZF1DXcBWIGoYBM5M` (full URL)
4. **Restart** to apply changes (should be a banner) 4. **Restart** to apply changes (button in Configuration tab)
Then, proceeed to **Active Playlists**, which shows you which Spotify playlists are currently being monitored and filled with tracks, and lets you do a bunch of useful operations on them. ### Why Two Playlist Tabs?
- **Link Playlists**: Shows all Jellyfin playlists and lets you connect them to Spotify
- **Active Playlists**: Shows which Spotify playlists are currently being monitored and filled with tracks
### Configuration Persistence ### Configuration Persistence
The web UI updates your `.env` file directly. Changes persist across container restarts, but require a restart to take effect. In development mode, the `.env` file is in your project root. In Docker, it's at `/app/.env`. The web UI updates your `.env` file directly. Changes persist across container restarts, but require a restart to take effect. In development mode, the `.env` file is in your project root. In Docker, it's at `/app/.env`.
There's an environment variable to modify this. **Recommended workflow**: Use the `sp_dc` cookie method (simpler and more reliable than the Jellyfin Spotify Import plugin).
**Recommended workflow**: Use the `sp_dc` cookie method alongside the [Spotify Import Plugin](https://github.com/Viperinius/jellyfin-plugin-spotify-import?tab=readme-ov-file).
### Nginx Proxy Setup (Required) ### Nginx Proxy Setup (Required)
@@ -140,7 +142,6 @@ This project brings together all the music streaming providers into one unified
- [Feishin](https://github.com/jeffvli/feishin) (Mac/Windows/Linux) - [Feishin](https://github.com/jeffvli/feishin) (Mac/Windows/Linux)
- [Musiver](https://music.aqzscn.cn/en/) (Android/IOS/Windows/Android) - [Musiver](https://music.aqzscn.cn/en/) (Android/IOS/Windows/Android)
- [Finamp](https://github.com/jmshrv/finamp) ()
_Working on getting more currently_ _Working on getting more currently_
@@ -334,7 +335,7 @@ Subsonic__EnableExternalPlaylists=false
### Spotify Playlist Injection (Jellyfin Only) ### Spotify Playlist Injection (Jellyfin Only)
Allstarr automatically fills your Spotify playlists (like Release Radar and Discover Weekly) with tracks from your configured streaming provider (SquidWTF, Deezer, or Qobuz). This works by intercepting playlists created by the Jellyfin Spotify Import plugin and matching missing tracks with your streaming service. Allstarr can automatically fill your Spotify playlists (like Release Radar and Discover Weekly) with tracks from your configured streaming provider (SquidWTF, Deezer, or Qobuz). This feature works by intercepting playlists created by the Jellyfin Spotify Import plugin and matching missing tracks with your streaming service.
#### Prerequisites #### Prerequisites
@@ -348,112 +349,136 @@ Allstarr automatically fills your Spotify playlists (like Release Radar and Disc
- Go to Jellyfin Dashboard → Plugins → Spotify Import - Go to Jellyfin Dashboard → Plugins → Spotify Import
- Connect your Spotify account - Connect your Spotify account
- Select which playlists to sync (e.g., Release Radar, Discover Weekly) - Select which playlists to sync (e.g., Release Radar, Discover Weekly)
- Set a sync schedule (the plugin will create playlists in Jellyfin) - Set a daily sync schedule (e.g., 4:15 PM daily)
- The plugin will create playlists in Jellyfin and generate "missing tracks" files for songs not in your library
3. **Configure Allstarr** 3. **Configure Allstarr**
- Enable Spotify Import in Allstarr (see configuration below) - Allstarr needs to know when the plugin runs and which playlists to intercept
- Link your Jellyfin playlists to Spotify playlists via the Web UI - Uses your existing `JELLYFIN_URL` and `JELLYFIN_API_KEY` settings (no additional credentials needed)
- Uses your existing `JELLYFIN_URL` and `JELLYFIN_API_KEY` settings
#### Configuration #### Configuration
| Setting | Description | | Setting | Description |
|---------|-------------| |---------|-------------|
| `SpotifyImport:Enabled` | Enable Spotify playlist injection (default: `false`) | | `SpotifyImport:Enabled` | Enable Spotify playlist injection (default: `false`) |
| `SpotifyImport:MatchingIntervalHours` | How often to run track matching in hours (default: 24, set to 0 for startup only) | | `SpotifyImport:SyncStartHour` | Hour when the Spotify Import plugin runs (24-hour format, 0-23) |
| `SpotifyImport:Playlists` | JSON array of playlists (managed via Web UI) | | `SpotifyImport:SyncStartMinute` | Minute when the plugin runs (0-59) |
| `SpotifyImport:SyncWindowHours` | Hours to search for missing tracks files after sync time (default: 2) |
| `SpotifyImport:PlaylistIds` | Comma-separated Jellyfin playlist IDs to intercept |
| `SpotifyImport:PlaylistNames` | Comma-separated playlist names (must match order of IDs) |
**Environment variables example:** **Environment variables example:**
```bash ```bash
# Enable the feature # Enable the feature
SPOTIFY_IMPORT_ENABLED=true SPOTIFY_IMPORT_ENABLED=true
# Matching interval (24 hours = once per day) # Sync window settings (optional - used to prevent fetching too frequently)
SPOTIFY_IMPORT_MATCHING_INTERVAL_HOURS=24 # The fetcher searches backwards from current time for the last 48 hours
SPOTIFY_IMPORT_SYNC_START_HOUR=16
SPOTIFY_IMPORT_SYNC_START_MINUTE=15
SPOTIFY_IMPORT_SYNC_WINDOW_HOURS=2
# Playlists (use Web UI to manage instead of editing manually) # Get playlist IDs from Jellyfin URLs: https://jellyfin.example.com/web/#/details?id=PLAYLIST_ID
SPOTIFY_IMPORT_PLAYLISTS=[["Discover Weekly","37i9dQZEVXcV6s7Dm7RXsU","first"],["Release Radar","37i9dQZEVXbng2vDHnfQlC","first"]] SPOTIFY_IMPORT_PLAYLIST_IDS=ba50e26c867ec9d57ab2f7bf24cfd6b0,4383a46d8bcac3be2ef9385053ea18df
# Names must match exactly as they appear in Jellyfin (used to find missing tracks files)
SPOTIFY_IMPORT_PLAYLIST_NAMES=Release Radar,Discover Weekly
``` ```
#### How It Works #### How It Works
1. **Spotify Import Plugin Runs** 1. **Spotify Import Plugin Runs** (e.g., daily at 4:15 PM)
- Plugin fetches your Spotify playlists - Plugin fetches your Spotify playlists
- Creates/updates playlists in Jellyfin with tracks already in your library - Creates/updates playlists in Jellyfin with tracks already in your library
- Generates "missing tracks" JSON files for songs not found locally - Generates "missing tracks" JSON files for songs not found locally
- Files are named like: `Release Radar_missing_2026-02-01_16-15.json`
2. **Allstarr Matches Tracks** (on startup + every 24 hours by default) 2. **Allstarr Fetches Missing Tracks** (within sync window)
- Reads missing tracks files from the Jellyfin plugin - Searches for missing tracks files from the Jellyfin plugin
- Searches **+24 hours forward first** (newest files), then **-48 hours backward** if not found
- This efficiently finds the most recent file regardless of timezone differences
- Example: Server time 12 PM EST, file timestamped 9 PM UTC (same day) → Found in forward search
- Caches the list of missing tracks in Redis + file cache
- Runs automatically on startup (if needed) and every 5 minutes during the sync window
3. **Allstarr Matches Tracks** (2 minutes after startup, then configurable interval)
- For each missing track, searches your streaming provider (SquidWTF, Deezer, or Qobuz) - For each missing track, searches your streaming provider (SquidWTF, Deezer, or Qobuz)
- Uses fuzzy matching to find the best match (title + artist similarity) - Uses fuzzy matching to find the best match (title + artist similarity)
- Rate-limited to avoid overwhelming the service (150ms delay between searches) - Rate-limited to avoid overwhelming the service (150ms delay between searches)
- Pre-builds playlist cache for instant loading - Caches matched results for 1 hour
- **Pre-builds playlist items cache** for instant serving (no "on the fly" building)
- Default interval: 24 hours (configurable via `SPOTIFY_IMPORT_MATCHING_INTERVAL_HOURS`)
- Set to 0 to only run once on startup (manual trigger via admin UI still works)
3. **You Open the Playlist in Jellyfin** 4. **You Open the Playlist in Jellyfin**
- Allstarr intercepts the request - Allstarr intercepts the request
- Returns a merged list: local tracks + matched streaming tracks - Returns a merged list: local tracks + matched streaming tracks
- Loads instantly from cache! - Loads instantly from cache (no searching needed!)
4. **You Play a Track** 5. **You Play a Track**
- Local tracks stream from Jellyfin normally - If it's a local track, streams from Jellyfin normally
- Matched tracks download from streaming provider on-demand - If it's a matched track, downloads from streaming provider on-demand
- Downloaded tracks are saved to your library for future use - Downloaded tracks are saved to your library for future use
#### Manual API Triggers #### Manual Triggers
You can manually trigger operations via the admin API: You can manually trigger syncing and matching via API:
```bash ```bash
# Get API key from your .env file
API_KEY="your-api-key-here"
# Fetch missing tracks from Jellyfin plugin # Fetch missing tracks from Jellyfin plugin
curl "http://localhost:5274/spotify/sync?api_key=$API_KEY" curl "https://your-jellyfin-proxy.com/spotify/sync?api_key=YOUR_API_KEY"
# Trigger track matching (searches streaming provider) # Trigger track matching (searches streaming provider)
curl "http://localhost:5274/spotify/match?api_key=$API_KEY" curl "https://your-jellyfin-proxy.com/spotify/match?api_key=YOUR_API_KEY"
# Match all playlists (refresh all matches) # Clear cache to force re-matching
curl "http://localhost:5274/spotify/match-all?api_key=$API_KEY" curl "https://your-jellyfin-proxy.com/spotify/clear-cache?api_key=YOUR_API_KEY"
# Clear cache and rebuild
curl "http://localhost:5274/spotify/clear-cache?api_key=$API_KEY"
# Refresh specific playlist
curl "http://localhost:5274/spotify/refresh-playlist?playlistId=PLAYLIST_ID&api_key=$API_KEY"
``` ```
#### Web UI Management #### Startup Behavior
The easiest way to manage Spotify playlists is through the Web UI at `http://localhost:5275`: When Allstarr starts with Spotify Import enabled:
1. **Link Playlists Tab**: Link Jellyfin playlists to Spotify playlists **Smart Cache Check:**
2. **Active Playlists Tab**: View status, trigger matching, and manage playlists - Checks if today's sync window has passed (e.g., if sync is at 4 PM + 2 hour window = 6 PM)
3. **Configuration Tab**: Enable/disable Spotify Import and adjust settings - If before 6 PM and yesterday's cache exists → **Skips fetch** (cache is still current)
- If after 6 PM or no cache exists → **Fetches missing tracks** from Jellyfin plugin
**Track Matching:**
- **T+2min**: Matches tracks with streaming provider (with rate limiting)
- Only matches playlists that don't already have cached matches
- **Result**: Playlists load instantly when you open them!
**Example Timeline:**
- Plugin runs daily at 4:15 PM, creates files at ~4:16 PM
- You restart Allstarr at 12:00 PM (noon) the next day
- Startup check: "Today's sync window ends at 6 PM, and I have yesterday's 4:16 PM file"
- **Decision**: Skip fetch, use existing cache
- At 6:01 PM: Next scheduled check will search for new files
#### Troubleshooting #### Troubleshooting
**Playlists are empty:** **Playlists are empty:**
- Check that the Spotify Import plugin is running and creating playlists - Check that the Spotify Import plugin is running and creating playlists
- Verify playlists are linked in the Web UI - Verify `SPOTIFY_IMPORT_PLAYLIST_IDS` match your Jellyfin playlist IDs
- Check logs: `docker-compose logs -f allstarr | grep -i spotify` - Check logs: `docker-compose logs -f allstarr | grep -i spotify`
**Tracks aren't matching:** **Tracks aren't matching:**
- Ensure your streaming provider is configured (`MUSIC_SERVICE`, credentials) - Ensure your streaming provider is configured (`MUSIC_SERVICE`, credentials)
- Manually trigger matching via Web UI or API - Check that playlist names in `SPOTIFY_IMPORT_PLAYLIST_NAMES` match exactly
- Check that the Jellyfin plugin generated missing tracks files - Manually trigger matching: `curl "https://your-proxy.com/spotify/match?api_key=KEY"`
**Performance:** **Sync timing issues:**
- Matching runs in background with rate limiting (150ms between searches) - Set `SPOTIFY_IMPORT_SYNC_START_HOUR/MINUTE` to match your plugin schedule
- First match may take a few minutes for large playlists - Increase `SPOTIFY_IMPORT_SYNC_WINDOW_HOURS` if files aren't being found
- Subsequent loads are instant (served from cache) - Check Jellyfin plugin logs to confirm when it runs
#### Notes #### Notes
- Uses your existing `JELLYFIN_URL` and `JELLYFIN_API_KEY` settings - This feature uses your existing `JELLYFIN_URL` and `JELLYFIN_API_KEY` settings
- Matched tracks cached for fast loading - Matched tracks are cached for 1 hour to avoid repeated searches
- Missing tracks cache persists across restarts (Redis + file cache) - Missing tracks cache persists across restarts (stored in Redis + file cache)
- Rate limiting prevents overwhelming your streaming provider - Rate limiting prevents overwhelming your streaming provider (150ms between searches)
- Only works with Jellyfin backend (not Subsonic/Navidrome) - Only works with Jellyfin backend (not Subsonic/Navidrome)
### Getting Credentials ### Getting Credentials
@@ -567,46 +592,9 @@ If you prefer to run Allstarr without Docker:
## API Endpoints ## API Endpoints
### Jellyfin Backend (Primary Focus)
The proxy provides comprehensive Jellyfin API support with streaming provider integration:
| Endpoint | Description |
|----------|-------------|
| `GET /Items` | Search and browse library items (local + streaming providers) |
| `GET /Artists` | Browse artists with merged results from local + streaming |
| `GET /Artists/AlbumArtists` | Album artists with streaming provider results |
| `GET /Users/{userId}/Items` | User library items with external content |
| `GET /Audio/{id}/stream` | Stream audio, downloading from provider on-demand |
| `GET /Audio/{id}/Lyrics` | Lyrics from Jellyfin, Spotify, or LRCLib |
| `GET /Items/{id}/Images/{type}` | Proxy cover art for external content |
| `GET /Playlists/{id}/Items` | Playlist items (Spotify Import integration) |
| `POST /UserFavoriteItems/{id}` | Favorite items; copies external tracks to kept folder |
| `DELETE /UserFavoriteItems/{id}` | Unfavorite items |
| `POST /Sessions/Playing` | Playback reporting for external tracks |
| `POST /Sessions/Playing/Progress` | Playback progress tracking |
| `POST /Sessions/Playing/Stopped` | Playback stopped reporting |
| `WebSocket /socket` | Real-time session management and remote control |
**Admin API (Port 5275):**
| Endpoint | Description |
|----------|-------------|
| `GET /api/config` | Get current configuration |
| `POST /api/config` | Update configuration |
| `GET /api/playlists` | List Spotify Import playlists |
| `POST /api/playlists/link` | Link Jellyfin playlist to Spotify |
| `DELETE /api/playlists/{id}` | Unlink playlist |
| `POST /spotify/sync` | Fetch missing tracks from Jellyfin plugin |
| `POST /spotify/match` | Trigger track matching |
| `POST /spotify/match-all` | Match all playlists |
| `POST /spotify/clear-cache` | Clear playlist cache |
| `POST /spotify/refresh-playlist` | Refresh specific playlist |
All other Jellyfin API endpoints are passed through unchanged.
### Subsonic Backend ### Subsonic Backend
The proxy implements the Subsonic API with streaming provider integration: The proxy implements the Subsonic API and adds transparent streaming provider integration:
| Endpoint | Description | | Endpoint | Description |
|----------|-------------| |----------|-------------|
@@ -620,6 +608,20 @@ The proxy implements the Subsonic API with streaming provider integration:
All other Subsonic API endpoints are passed through to Navidrome unchanged. All other Subsonic API endpoints are passed through to Navidrome unchanged.
### Jellyfin Backend
The proxy implements a subset of the Jellyfin API:
| Endpoint | Description |
|----------|-------------|
| `GET /Items` | Search and browse library items |
| `GET /Artists` | Browse artists with streaming provider results |
| `GET /Audio/{id}/stream` | Stream audio, downloading from provider if needed |
| `GET /Items/{id}/Images/{type}` | Proxy cover art for external content |
| `POST /UserFavoriteItems/{id}` | Favorite items; triggers playlist download |
All other Jellyfin API endpoints are passed through unchanged.
## External ID Format ## External ID Format
External (streaming provider) content uses typed IDs: External (streaming provider) content uses typed IDs:
@@ -634,37 +636,25 @@ Legacy format `ext-deezer-{id}` is also supported (assumes song type).
## Download Folder Structure ## Download Folder Structure
All downloads are organized under a single base directory (default: `./downloads`): Downloaded music is organized as:
``` ```
downloads/ downloads/
├── permanent/ # Permanent downloads (STORAGE_MODE=Permanent) ├── Artist Name/
│ ├── Artist Name/ │ ├── Album Title/
│ │ ├── Album Title/ │ │ ├── 01 - Track One.mp3
│ │ ├── 01 - Track One.flac │ │ ├── 02 - Track Two.mp3
│ │ │ ├── 02 - Track Two.flac │ │ └── ...
│ │ └── ... └── Another Album/
└── Another Album/ └── ...
│ │ └── ... ├── Another Artist/
│ └── playlists/ │ └── ...
│ ├── My Favorite Songs.m3u └── playlists/
── Chill Vibes.m3u ── My Favorite Songs.m3u
├── cache/ # Temporary cache (STORAGE_MODE=Cache) ├── Chill Vibes.m3u
└── Artist Name/ └── ...
│ └── Album Title/
│ └── Track.flac
└── kept/ # Favorited external tracks (always permanent)
└── Artist Name/
└── Album Title/
└── Track.flac
``` ```
**Storage modes:** Playlists are stored as M3U files with relative paths to downloaded tracks, making them portable and compatible with most music players.
- **Permanent** (`downloads/permanent/`): Files saved permanently and registered in your media server
- **Cache** (`downloads/cache/`): Temporary files, auto-cleaned after `CACHE_DURATION_HOURS`
- **Kept** (`downloads/kept/`): External tracks you've favorited - always permanent, separate from cache
Playlists are stored as M3U files with relative paths, making them portable and compatible with most music players.
## Metadata Embedding ## Metadata Embedding
@@ -695,17 +685,10 @@ dotnet test
``` ```
allstarr/ allstarr/
├── Controllers/ ├── Controllers/
│ ├── AdminController.cs # Admin dashboard API │ ├── JellyfinController.cs # Jellyfin API controller (registered when Backend:Type=Jellyfin)
── JellyfinController.cs # Jellyfin API controller ── SubsonicController.cs # Subsonic API controller (registered when Backend:Type=Subsonic)
│ └── SubsonicController.cs # Subsonic API controller
├── Filters/
│ ├── AdminPortFilter.cs # Admin port access control
│ ├── ApiKeyAuthFilter.cs # API key authentication
│ └── JellyfinAuthFilter.cs # Jellyfin authentication
├── Middleware/ ├── Middleware/
── AdminStaticFilesMiddleware.cs # Admin UI static file serving ── GlobalExceptionHandler.cs # Global error handling
│ ├── GlobalExceptionHandler.cs # Global error handling
│ └── WebSocketProxyMiddleware.cs # WebSocket proxying for Jellyfin
├── Models/ ├── Models/
│ ├── Domain/ # Domain entities │ ├── Domain/ # Domain entities
│ │ ├── Song.cs │ │ ├── Song.cs
@@ -714,39 +697,18 @@ allstarr/
│ ├── Settings/ # Configuration models │ ├── Settings/ # Configuration models
│ │ ├── SubsonicSettings.cs │ │ ├── SubsonicSettings.cs
│ │ ├── DeezerSettings.cs │ │ ├── DeezerSettings.cs
│ │ ── QobuzSettings.cs │ │ ── QobuzSettings.cs
│ │ ├── SquidWTFSettings.cs
│ │ ├── SpotifyApiSettings.cs
│ │ ├── SpotifyImportSettings.cs
│ │ ├── MusicBrainzSettings.cs
│ │ └── RedisSettings.cs
│ ├── Download/ # Download-related models │ ├── Download/ # Download-related models
│ │ ├── DownloadInfo.cs │ │ ├── DownloadInfo.cs
│ │ └── DownloadStatus.cs │ │ └── DownloadStatus.cs
│ ├── Lyrics/
│ │ └── LyricsInfo.cs
│ ├── Search/ │ ├── Search/
│ │ └── SearchResult.cs │ │ └── SearchResult.cs
│ ├── Spotify/
│ │ ├── MissingTrack.cs
│ │ └── SpotifyPlaylistTrack.cs
│ └── Subsonic/ │ └── Subsonic/
│ ├── ExternalPlaylist.cs
│ └── ScanStatus.cs │ └── ScanStatus.cs
├── Services/ ├── Services/
│ ├── Common/ # Shared services │ ├── Common/ # Shared services
│ │ ├── BaseDownloadService.cs # Template method base class │ │ ├── BaseDownloadService.cs # Template method base class
│ │ ├── CacheCleanupService.cs # Cache cleanup background service
│ │ ├── CacheWarmingService.cs # Startup cache warming
│ │ ├── EndpointBenchmarkService.cs # Endpoint performance benchmarking
│ │ ├── FuzzyMatcher.cs # Fuzzy string matching
│ │ ├── GenreEnrichmentService.cs # MusicBrainz genre enrichment
│ │ ├── OdesliService.cs # Odesli/song.link conversion
│ │ ├── ParallelMetadataService.cs # Parallel metadata fetching
│ │ ├── PathHelper.cs # Path utilities │ │ ├── PathHelper.cs # Path utilities
│ │ ├── PlaylistIdHelper.cs # Playlist ID helpers
│ │ ├── RedisCacheService.cs # Redis caching
│ │ ├── RoundRobinFallbackHelper.cs # Load balancing and failover
│ │ ├── Result.cs # Result<T> pattern │ │ ├── Result.cs # Result<T> pattern
│ │ └── Error.cs # Error types │ │ └── Error.cs # Error types
│ ├── Deezer/ # Deezer provider │ ├── Deezer/ # Deezer provider
@@ -758,35 +720,12 @@ allstarr/
│ │ ├── QobuzMetadataService.cs │ │ ├── QobuzMetadataService.cs
│ │ ├── QobuzBundleService.cs │ │ ├── QobuzBundleService.cs
│ │ └── QobuzStartupValidator.cs │ │ └── QobuzStartupValidator.cs
│ ├── SquidWTF/ # SquidWTF provider
│ │ ├── SquidWTFDownloadService.cs
│ │ ├── SquidWTFMetadataService.cs
│ │ └── SquidWTFStartupValidator.cs
│ ├── Jellyfin/ # Jellyfin integration
│ │ ├── JellyfinModelMapper.cs # Model mapping
│ │ ├── JellyfinProxyService.cs # Request proxying
│ │ ├── JellyfinResponseBuilder.cs # Response building
│ │ ├── JellyfinSessionManager.cs # Session management
│ │ └── JellyfinStartupValidator.cs # Startup validation
│ ├── Lyrics/ # Lyrics services
│ │ ├── LrclibService.cs # LRCLIB lyrics
│ │ ├── LyricsPrefetchService.cs # Background lyrics prefetching
│ │ ├── LyricsStartupValidator.cs # Lyrics validation
│ │ └── SpotifyLyricsService.cs # Spotify lyrics
│ ├── MusicBrainz/
│ │ └── MusicBrainzService.cs # MusicBrainz metadata
│ ├── Spotify/ # Spotify integration
│ │ ├── SpotifyApiClient.cs # Spotify API client
│ │ ├── SpotifyMissingTracksFetcher.cs # Missing tracks fetcher
│ │ ├── SpotifyPlaylistFetcher.cs # Playlist fetcher
│ │ └── SpotifyTrackMatchingService.cs # Track matching
│ ├── Local/ # Local library │ ├── Local/ # Local library
│ │ ├── ILocalLibraryService.cs │ │ ├── ILocalLibraryService.cs
│ │ └── LocalLibraryService.cs │ │ └── LocalLibraryService.cs
│ ├── Subsonic/ # Subsonic API logic │ ├── Subsonic/ # Subsonic API logic
│ │ ├── PlaylistSyncService.cs # Playlist synchronization
│ │ ├── SubsonicModelMapper.cs # Model mapping
│ │ ├── SubsonicProxyService.cs # Request proxying │ │ ├── SubsonicProxyService.cs # Request proxying
│ │ ├── SubsonicModelMapper.cs # Model mapping
│ │ ├── SubsonicRequestParser.cs # Request parsing │ │ ├── SubsonicRequestParser.cs # Request parsing
│ │ └── SubsonicResponseBuilder.cs # Response building │ │ └── SubsonicResponseBuilder.cs # Response building
│ ├── Validation/ # Startup validation │ ├── Validation/ # Startup validation
@@ -798,17 +737,13 @@ allstarr/
│ ├── IDownloadService.cs # Download interface │ ├── IDownloadService.cs # Download interface
│ ├── IMusicMetadataService.cs # Metadata interface │ ├── IMusicMetadataService.cs # Metadata interface
│ └── StartupValidationService.cs │ └── StartupValidationService.cs
├── wwwroot/ # Admin UI static files
│ ├── index.html # Admin dashboard
│ └── placeholder.png # Placeholder image
├── Program.cs # Application entry point ├── Program.cs # Application entry point
└── appsettings.json # Configuration └── appsettings.json # Configuration
allstarr.Tests/ allstarr.Tests/
├── DeezerDownloadServiceTests.cs # Deezer download tests ├── DeezerDownloadServiceTests.cs # Deezer download tests
├── DeezerMetadataServiceTests.cs # Deezer metadata tests ├── DeezerMetadataServiceTests.cs # Deezer metadata tests
├── JellyfinResponseStructureTests.cs # Jellyfin response tests ├── QobuzDownloadServiceTests.cs # Qobuz download tests (127 tests)
├── QobuzDownloadServiceTests.cs # Qobuz download tests
├── LocalLibraryServiceTests.cs # Local library tests ├── LocalLibraryServiceTests.cs # Local library tests
├── SubsonicModelMapperTests.cs # Model mapping tests ├── SubsonicModelMapperTests.cs # Model mapping tests
├── SubsonicProxyServiceTests.cs # Proxy service tests ├── SubsonicProxyServiceTests.cs # Proxy service tests
@@ -882,7 +817,7 @@ We welcome contributions! Here's how to get started:
- Follow existing code patterns and conventions - Follow existing code patterns and conventions
- Add tests for new features - Add tests for new features
- Update documentation as needed - Update documentation as needed
- Keep commits feature focused - Keep commits focused and atomic
### Testing ### Testing
@@ -904,14 +839,8 @@ GPL-3.0
## Acknowledgments ## Acknowledgments
- [octo-fiesta](https://github.com/V1ck3s/octo-fiesta) - The original
- [octo-fiestarr](https://github.com/bransoned/octo-fiestarr) - The fork that introduced me to this idea based on the above
- [Jellyfin Spotify Import Plugin](https://github.com/Viperinius/jellyfin-plugin-spotify-import?tab=readme-ov-file) - The plugin that I **strongly** recommend using alongside this repo
- [Jellyfin](https://jellyfin.org/) - The free and open-source media server
- [Navidrome](https://www.navidrome.org/) - The excellent self-hosted music server - [Navidrome](https://www.navidrome.org/) - The excellent self-hosted music server
- [Subsonic API](http://www.subsonic.org/pages/api.jsp) - The API specification - [Jellyfin](https://jellyfin.org/) - The free and open-source media server
- [Hi-Fi API](https://github.com/binimum/hifi-api) - These people do some great work, and you should thank them for this even existing!
- [Deezer](https://www.deezer.com/) - Music streaming service - [Deezer](https://www.deezer.com/) - Music streaming service
- [Qobuz](https://www.qobuz.com/) - Hi-Res music streaming service - [Qobuz](https://www.qobuz.com/) - Hi-Res music streaming service
- [spotify-lyrics-api](https://github.com/akashrchandran/spotify-lyrics-api) - Thank them for the fact that we have access to Spotify's lyrics! - [Subsonic API](http://www.subsonic.org/pages/api.jsp) - The API specification
- [LRCLIB](https://github.com/tranxuanthang/lrclib) - The GOATS for giving us a free api for lyrics! They power LRCGET, which I'm sure some of you have heard of

View File

@@ -28,7 +28,6 @@ public class AdminController : ControllerBase
private readonly SpotifyApiSettings _spotifyApiSettings; private readonly SpotifyApiSettings _spotifyApiSettings;
private readonly SpotifyImportSettings _spotifyImportSettings; private readonly SpotifyImportSettings _spotifyImportSettings;
private readonly JellyfinSettings _jellyfinSettings; private readonly JellyfinSettings _jellyfinSettings;
private readonly SubsonicSettings _subsonicSettings;
private readonly DeezerSettings _deezerSettings; private readonly DeezerSettings _deezerSettings;
private readonly QobuzSettings _qobuzSettings; private readonly QobuzSettings _qobuzSettings;
private readonly SquidWTFSettings _squidWtfSettings; private readonly SquidWTFSettings _squidWtfSettings;
@@ -53,7 +52,6 @@ public class AdminController : ControllerBase
IOptions<SpotifyApiSettings> spotifyApiSettings, IOptions<SpotifyApiSettings> spotifyApiSettings,
IOptions<SpotifyImportSettings> spotifyImportSettings, IOptions<SpotifyImportSettings> spotifyImportSettings,
IOptions<JellyfinSettings> jellyfinSettings, IOptions<JellyfinSettings> jellyfinSettings,
IOptions<SubsonicSettings> subsonicSettings,
IOptions<DeezerSettings> deezerSettings, IOptions<DeezerSettings> deezerSettings,
IOptions<QobuzSettings> qobuzSettings, IOptions<QobuzSettings> qobuzSettings,
IOptions<SquidWTFSettings> squidWtfSettings, IOptions<SquidWTFSettings> squidWtfSettings,
@@ -71,7 +69,6 @@ public class AdminController : ControllerBase
_spotifyApiSettings = spotifyApiSettings.Value; _spotifyApiSettings = spotifyApiSettings.Value;
_spotifyImportSettings = spotifyImportSettings.Value; _spotifyImportSettings = spotifyImportSettings.Value;
_jellyfinSettings = jellyfinSettings.Value; _jellyfinSettings = jellyfinSettings.Value;
_subsonicSettings = subsonicSettings.Value;
_deezerSettings = deezerSettings.Value; _deezerSettings = deezerSettings.Value;
_qobuzSettings = qobuzSettings.Value; _qobuzSettings = qobuzSettings.Value;
_squidWtfSettings = squidWtfSettings.Value; _squidWtfSettings = squidWtfSettings.Value;
@@ -91,6 +88,8 @@ public class AdminController : ControllerBase
_envFilePath = _environment.IsDevelopment() _envFilePath = _environment.IsDevelopment()
? Path.Combine(_environment.ContentRootPath, "..", ".env") ? Path.Combine(_environment.ContentRootPath, "..", ".env")
: "/app/.env"; : "/app/.env";
_logger.LogInformation("Admin controller initialized. .env path: {EnvFilePath}", _envFilePath);
} }
private static List<string> DecodeSquidWtfUrls() private static List<string> DecodeSquidWtfUrls()
@@ -166,7 +165,8 @@ public class AdminController : ControllerBase
spotifyImport = new spotifyImport = new
{ {
enabled = _spotifyImportSettings.Enabled, enabled = _spotifyImportSettings.Enabled,
matchingIntervalHours = _spotifyImportSettings.MatchingIntervalHours, syncTime = $"{_spotifyImportSettings.SyncStartHour:D2}:{_spotifyImportSettings.SyncStartMinute:D2}",
syncWindowHours = _spotifyImportSettings.SyncWindowHours,
playlistCount = _spotifyImportSettings.Playlists.Count playlistCount = _spotifyImportSettings.Playlists.Count
}, },
deezer = new deezer = new
@@ -469,53 +469,30 @@ public class AdminController : ControllerBase
foreach (var track in spotifyTracks) foreach (var track in spotifyTracks)
{ {
var isLocal = false; var isLocal = false;
var hasExternalMapping = false;
// FIRST: Check for manual Jellyfin mapping if (localTracks.Count > 0)
var manualMappingKey = $"spotify:manual-map:{config.Name}:{track.SpotifyId}";
var manualJellyfinId = await _cache.GetAsync<string>(manualMappingKey);
if (!string.IsNullOrEmpty(manualJellyfinId))
{ {
// Manual Jellyfin mapping exists - this track is definitely local var bestMatch = localTracks
isLocal = true; .Select(local => new
}
else
{
// Check for external manual mapping
var externalMappingKey = $"spotify:external-map:{config.Name}:{track.SpotifyId}";
var externalMappingJson = await _cache.GetStringAsync(externalMappingKey);
if (!string.IsNullOrEmpty(externalMappingJson))
{
// External manual mapping exists
hasExternalMapping = true;
}
else if (localTracks.Count > 0)
{
// SECOND: No manual mapping, try fuzzy matching with local tracks
var bestMatch = localTracks
.Select(local => new
{
Local = local,
TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, local.Title),
ArtistScore = FuzzyMatcher.CalculateSimilarity(track.PrimaryArtist, local.Artist)
})
.Select(x => new
{
x.Local,
x.TitleScore,
x.ArtistScore,
TotalScore = (x.TitleScore * 0.7) + (x.ArtistScore * 0.3)
})
.OrderByDescending(x => x.TotalScore)
.FirstOrDefault();
// Use 70% threshold (same as playback matching)
if (bestMatch != null && bestMatch.TotalScore >= 70)
{ {
isLocal = true; Local = local,
} TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, local.Title),
ArtistScore = FuzzyMatcher.CalculateSimilarity(track.PrimaryArtist, local.Artist)
})
.Select(x => new
{
x.Local,
x.TitleScore,
x.ArtistScore,
TotalScore = (x.TitleScore * 0.7) + (x.ArtistScore * 0.3)
})
.OrderByDescending(x => x.TotalScore)
.FirstOrDefault();
// Use 70% threshold (same as playback matching)
if (bestMatch != null && bestMatch.TotalScore >= 70)
{
isLocal = true;
} }
} }
@@ -525,8 +502,8 @@ public class AdminController : ControllerBase
} }
else else
{ {
// Check if external track is matched (either manual mapping or auto-matched) // Check if external track is matched
if (hasExternalMapping || matchedSpotifyIds.Contains(track.SpotifyId)) if (matchedSpotifyIds.Contains(track.SpotifyId))
{ {
externalMatchedCount++; externalMatchedCount++;
} }
@@ -570,6 +547,54 @@ public class AdminController : ControllerBase
_logger.LogWarning("Playlist {Name} has no JellyfinId configured", config.Name); _logger.LogWarning("Playlist {Name} has no JellyfinId configured", config.Name);
} }
// Get lyrics completion status
try
{
var tracks = await _playlistFetcher.GetPlaylistTracksAsync(config.Name);
if (tracks.Count > 0)
{
var lyricsWithCount = 0;
var lyricsWithoutCount = 0;
foreach (var track in tracks)
{
var cacheKey = $"lyrics:{track.PrimaryArtist}:{track.Title}:{track.Album}:{track.DurationMs / 1000}";
var existingLyrics = await _cache.GetStringAsync(cacheKey);
if (!string.IsNullOrEmpty(existingLyrics))
{
lyricsWithCount++;
}
else
{
lyricsWithoutCount++;
}
}
playlistInfo["lyricsTotal"] = tracks.Count;
playlistInfo["lyricsCached"] = lyricsWithCount;
playlistInfo["lyricsMissing"] = lyricsWithoutCount;
playlistInfo["lyricsPercentage"] = tracks.Count > 0
? (int)Math.Round((double)lyricsWithCount / tracks.Count * 100)
: 0;
}
else
{
playlistInfo["lyricsTotal"] = 0;
playlistInfo["lyricsCached"] = 0;
playlistInfo["lyricsMissing"] = 0;
playlistInfo["lyricsPercentage"] = 0;
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to get lyrics completion for playlist {Name}", config.Name);
playlistInfo["lyricsTotal"] = 0;
playlistInfo["lyricsCached"] = 0;
playlistInfo["lyricsMissing"] = 0;
playlistInfo["lyricsPercentage"] = 0;
}
playlists.Add(playlistInfo); playlists.Add(playlistInfo);
} }
@@ -605,221 +630,236 @@ public class AdminController : ControllerBase
// Get Spotify tracks // Get Spotify tracks
var spotifyTracks = await _playlistFetcher.GetPlaylistTracksAsync(decodedName); var spotifyTracks = await _playlistFetcher.GetPlaylistTracksAsync(decodedName);
// Get the playlist config to find Jellyfin ID
var playlistConfig = _spotifyImportSettings.Playlists
.FirstOrDefault(p => p.Name.Equals(decodedName, StringComparison.OrdinalIgnoreCase));
var tracksWithStatus = new List<object>(); var tracksWithStatus = new List<object>();
// Use the pre-built playlist cache (same as GetPlaylists endpoint) if (!string.IsNullOrEmpty(playlistConfig?.JellyfinId))
// This cache includes all matched tracks with proper provider IDs
var playlistItemsCacheKey = $"spotify:playlist:items:{decodedName}";
List<Dictionary<string, object?>>? cachedPlaylistItems = null;
try
{ {
cachedPlaylistItems = await _cache.GetAsync<List<Dictionary<string, object?>>>(playlistItemsCacheKey); // Get existing tracks from Jellyfin to determine local/external status
} var userId = _jellyfinSettings.UserId;
catch (Exception cacheEx) if (!string.IsNullOrEmpty(userId))
{
_logger.LogWarning(cacheEx, "Failed to deserialize playlist cache for {Playlist}", decodedName);
}
_logger.LogInformation("GetPlaylistTracks for {Playlist}: Cache found: {Found}, Count: {Count}",
decodedName, cachedPlaylistItems != null, cachedPlaylistItems?.Count ?? 0);
if (cachedPlaylistItems != null && cachedPlaylistItems.Count > 0)
{
// Build a map of Spotify ID -> cached item for quick lookup
var spotifyIdToItem = new Dictionary<string, Dictionary<string, object?>>();
foreach (var item in cachedPlaylistItems)
{ {
if (item.TryGetValue("ProviderIds", out var providerIdsObj) && providerIdsObj != null) try
{ {
Dictionary<string, string>? providerIds = null; var url = $"{_jellyfinSettings.Url}/Playlists/{playlistConfig.JellyfinId}/Items?UserId={userId}";
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("X-Emby-Authorization", GetJellyfinAuthHeader());
if (providerIdsObj is Dictionary<string, string> dict) var response = await _jellyfinHttpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{ {
providerIds = dict; var json = await response.Content.ReadAsStringAsync();
} using var doc = JsonDocument.Parse(json);
else if (providerIdsObj is JsonElement jsonEl && jsonEl.ValueKind == JsonValueKind.Object)
{ // Build list of local tracks (match by name only - no Spotify IDs!)
providerIds = new Dictionary<string, string>(); var localTracks = new List<(string Title, string Artist)>();
foreach (var prop in jsonEl.EnumerateObject()) if (doc.RootElement.TryGetProperty("Items", out var items))
{ {
providerIds[prop.Name] = prop.Value.GetString() ?? ""; foreach (var item in items.EnumerateArray())
}
}
if (providerIds != null && providerIds.TryGetValue("Spotify", out var spotifyId) && !string.IsNullOrEmpty(spotifyId))
{
spotifyIdToItem[spotifyId] = item;
}
}
}
// Match each Spotify track to its cached item
foreach (var track in spotifyTracks)
{
bool? isLocal = null;
string? externalProvider = null;
bool isManualMapping = false;
string? manualMappingType = null;
string? manualMappingId = null;
if (spotifyIdToItem.TryGetValue(track.SpotifyId, out var cachedItem))
{
// Track is in the cache - determine if it's local or external
if (cachedItem.TryGetValue("ProviderIds", out var providerIdsObj) && providerIdsObj != null)
{
Dictionary<string, string>? providerIds = null;
if (providerIdsObj is Dictionary<string, string> dict)
{
providerIds = dict;
}
else if (providerIdsObj is JsonElement jsonEl && jsonEl.ValueKind == JsonValueKind.Object)
{
providerIds = new Dictionary<string, string>();
foreach (var prop in jsonEl.EnumerateObject())
{ {
providerIds[prop.Name] = prop.Value.GetString() ?? ""; var title = item.TryGetProperty("Name", out var nameEl) ? nameEl.GetString() ?? "" : "";
var artist = "";
if (item.TryGetProperty("Artists", out var artistsEl) && artistsEl.GetArrayLength() > 0)
{
artist = artistsEl[0].GetString() ?? "";
}
else if (item.TryGetProperty("AlbumArtist", out var albumArtistEl))
{
artist = albumArtistEl.GetString() ?? "";
}
if (!string.IsNullOrEmpty(title))
{
localTracks.Add((title, artist));
}
} }
} }
if (providerIds != null) _logger.LogInformation("Found {Count} local tracks in Jellyfin playlist {Playlist}",
localTracks.Count, decodedName);
// Get matched external tracks cache
var matchedTracksKey = $"spotify:matched:ordered:{decodedName}";
var matchedTracks = await _cache.GetAsync<List<MatchedTrack>>(matchedTracksKey);
var matchedSpotifyIds = new HashSet<string>(
matchedTracks?.Select(m => m.SpotifyId) ?? Enumerable.Empty<string>()
);
// Match Spotify tracks to local tracks by name (fuzzy matching)
foreach (var track in spotifyTracks)
{ {
_logger.LogDebug("Track {Title} has ProviderIds: {Keys}", track.Title, string.Join(", ", providerIds.Keys)); bool? isLocal = null;
string? externalProvider = null;
bool isManualMapping = false;
string? manualMappingType = null;
string? manualMappingId = null;
// Check for external provider keys (case-insensitive) // FIRST: Check for manual Jellyfin mapping
// External providers: squidwtf, deezer, qobuz, tidal (lowercase) var manualMappingKey = $"spotify:manual-map:{decodedName}:{track.SpotifyId}";
var providerKey = providerIds.Keys.FirstOrDefault(k => var manualJellyfinId = await _cache.GetAsync<string>(manualMappingKey);
k.Equals("squidwtf", StringComparison.OrdinalIgnoreCase) ||
k.Equals("SquidWTF", StringComparison.OrdinalIgnoreCase));
if (providerKey != null) if (!string.IsNullOrEmpty(manualJellyfinId))
{ {
isLocal = false; // Manual Jellyfin mapping exists - this track is definitely local
externalProvider = "SquidWTF"; isLocal = true;
_logger.LogDebug("✓ Track {Title} identified as SquidWTF", track.Title); isManualMapping = true;
} manualMappingType = "jellyfin";
else if ((providerKey = providerIds.Keys.FirstOrDefault(k => k.Equals("deezer", StringComparison.OrdinalIgnoreCase))) != null) manualMappingId = manualJellyfinId;
{ _logger.LogDebug("✓ Manual Jellyfin mapping found for {Title}: Jellyfin ID {Id}",
isLocal = false; track.Title, manualJellyfinId);
externalProvider = "Deezer";
_logger.LogDebug("✓ Track {Title} identified as Deezer", track.Title);
}
else if ((providerKey = providerIds.Keys.FirstOrDefault(k => k.Equals("qobuz", StringComparison.OrdinalIgnoreCase))) != null)
{
isLocal = false;
externalProvider = "Qobuz";
_logger.LogDebug("✓ Track {Title} identified as Qobuz", track.Title);
}
else if ((providerKey = providerIds.Keys.FirstOrDefault(k => k.Equals("tidal", StringComparison.OrdinalIgnoreCase))) != null)
{
isLocal = false;
externalProvider = "Tidal";
_logger.LogDebug("✓ Track {Title} identified as Tidal", track.Title);
} }
else else
{ {
// No external provider key found - it's a local track // Check for external manual mapping
// Local tracks have MusicBrainz, ISRC, Spotify IDs but no external provider var externalMappingKey = $"spotify:external-map:{decodedName}:{track.SpotifyId}";
isLocal = true; var externalMappingJson = await _cache.GetStringAsync(externalMappingKey);
_logger.LogDebug("✓ Track {Title} identified as LOCAL (has ProviderIds but no external provider)", track.Title);
}
}
else
{
_logger.LogWarning("Track {Title} has ProviderIds object but it's null after parsing", track.Title);
}
}
else
{
_logger.LogWarning("Track {Title} in cache but has NO ProviderIds - treating as missing", track.Title);
isLocal = null;
externalProvider = null;
}
// Check if this is a manual mapping if (!string.IsNullOrEmpty(externalMappingJson))
var manualJellyfinKey = $"spotify:manual-map:{decodedName}:{track.SpotifyId}";
var manualJellyfinId = await _cache.GetAsync<string>(manualJellyfinKey);
if (!string.IsNullOrEmpty(manualJellyfinId))
{
isManualMapping = true;
manualMappingType = "jellyfin";
manualMappingId = manualJellyfinId;
}
else
{
var externalMappingKey = $"spotify:external-map:{decodedName}:{track.SpotifyId}";
var externalMappingJson = await _cache.GetStringAsync(externalMappingKey);
if (!string.IsNullOrEmpty(externalMappingJson))
{
try
{
using var extDoc = JsonDocument.Parse(externalMappingJson);
var extRoot = extDoc.RootElement;
if (extRoot.TryGetProperty("id", out var idEl))
{ {
isManualMapping = true; try
manualMappingType = "external"; {
manualMappingId = idEl.GetString(); using var extDoc = JsonDocument.Parse(externalMappingJson);
var extRoot = extDoc.RootElement;
string? provider = null;
string? externalId = null;
if (extRoot.TryGetProperty("provider", out var providerEl))
{
provider = providerEl.GetString();
}
if (extRoot.TryGetProperty("id", out var idEl))
{
externalId = idEl.GetString();
}
if (!string.IsNullOrEmpty(provider) && !string.IsNullOrEmpty(externalId))
{
// External manual mapping exists
isLocal = false;
externalProvider = provider;
isManualMapping = true;
manualMappingType = "external";
manualMappingId = externalId;
_logger.LogDebug("✓ Manual external mapping found for {Title}: {Provider} {ExternalId}",
track.Title, provider, externalId);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to process external manual mapping for {Title}", track.Title);
}
}
else if (localTracks.Count > 0)
{
// SECOND: No manual mapping, try fuzzy matching with local tracks
var bestMatch = localTracks
.Select(local => new
{
Local = local,
TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, local.Title),
ArtistScore = FuzzyMatcher.CalculateSimilarity(track.PrimaryArtist, local.Artist)
})
.Select(x => new
{
x.Local,
x.TitleScore,
x.ArtistScore,
TotalScore = (x.TitleScore * 0.7) + (x.ArtistScore * 0.3)
})
.OrderByDescending(x => x.TotalScore)
.FirstOrDefault();
// Use 70% threshold (same as playback matching)
if (bestMatch != null && bestMatch.TotalScore >= 70)
{
isLocal = true;
}
} }
} }
catch { }
// If not local, check if it's externally matched or missing
if (isLocal != true)
{
// Check if there's a manual external mapping
if (isManualMapping && manualMappingType == "external")
{
// Track has manual external mapping - it's available externally
isLocal = false;
// externalProvider already set above
}
else if (matchedSpotifyIds.Contains(track.SpotifyId))
{
// Track is externally matched (search succeeded)
isLocal = false;
externalProvider = "SquidWTF"; // Default to SquidWTF for external matches
}
else
{
// Track is missing (search failed)
isLocal = null;
externalProvider = null;
}
}
// Check lyrics status (only from our cache - lrclib/Spotify)
// Note: For local tracks, Jellyfin may have embedded lyrics that we don't check here
// Those will be served directly by Jellyfin when requested
var cacheKey = $"lyrics:{track.PrimaryArtist}:{track.Title}:{track.Album}:{track.DurationMs / 1000}";
var existingLyrics = await _cache.GetStringAsync(cacheKey);
var hasLyrics = !string.IsNullOrEmpty(existingLyrics);
tracksWithStatus.Add(new
{
position = track.Position,
title = track.Title,
artists = track.Artists,
album = track.Album,
isrc = track.Isrc,
spotifyId = track.SpotifyId,
durationMs = track.DurationMs,
albumArtUrl = track.AlbumArtUrl,
isLocal = isLocal,
externalProvider = externalProvider,
searchQuery = isLocal != true ? $"{track.Title} {track.PrimaryArtist}" : null, // Set for both external and missing
isManualMapping = isManualMapping,
manualMappingType = manualMappingType,
manualMappingId = manualMappingId,
hasLyrics = hasLyrics
});
} }
return Ok(new
{
name = decodedName,
trackCount = spotifyTracks.Count,
tracks = tracksWithStatus
});
} }
} }
else catch (Exception ex)
{ {
// Track not in cache - it's missing _logger.LogWarning(ex, "Failed to get local track status for {Playlist}", decodedName);
isLocal = null;
externalProvider = null;
} }
// Check lyrics status
var cacheKey = $"lyrics:{track.PrimaryArtist}:{track.Title}:{track.Album}:{track.DurationMs / 1000}";
var existingLyrics = await _cache.GetStringAsync(cacheKey);
var hasLyrics = !string.IsNullOrEmpty(existingLyrics);
tracksWithStatus.Add(new
{
position = track.Position,
title = track.Title,
artists = track.Artists,
album = track.Album,
isrc = track.Isrc,
spotifyId = track.SpotifyId,
durationMs = track.DurationMs,
albumArtUrl = track.AlbumArtUrl,
isLocal = isLocal,
externalProvider = externalProvider,
searchQuery = isLocal != true ? $"{track.Title} {track.PrimaryArtist}" : null,
isManualMapping = isManualMapping,
manualMappingType = manualMappingType,
manualMappingId = manualMappingId,
hasLyrics = hasLyrics
});
} }
return Ok(new
{
name = decodedName,
trackCount = spotifyTracks.Count,
tracks = tracksWithStatus
});
} }
// Fallback: Cache not available, use matched tracks cache // If we get here, we couldn't get local tracks from Jellyfin
_logger.LogWarning("Playlist cache not available for {Playlist}, using fallback", decodedName); // Just return tracks with basic external/missing status based on cache
var fallbackMatchedTracksKey = $"spotify:matched:ordered:{decodedName}"; var fallbackMatchedTracksKey = $"spotify:matched:ordered:{decodedName}";
var fallbackMatchedTracks = await _cache.GetAsync<List<MatchedTrack>>(fallbackMatchedTracksKey); var fallbackMatchedTracks = await _cache.GetAsync<List<MatchedTrack>>(fallbackMatchedTracksKey);
var fallbackMatchedSpotifyIds = new HashSet<string>( var fallbackMatchedSpotifyIds = new HashSet<string>(
fallbackMatchedTracks?.Select(m => m.SpotifyId) ?? Enumerable.Empty<string>() fallbackMatchedTracks?.Select(m => m.SpotifyId) ?? Enumerable.Empty<string>()
); );
// Clear and reuse tracksWithStatus for fallback
tracksWithStatus.Clear();
foreach (var track in spotifyTracks) foreach (var track in spotifyTracks)
{ {
bool? isLocal = null; bool? isLocal = null;
@@ -866,11 +906,13 @@ public class AdminController : ControllerBase
} }
else if (fallbackMatchedSpotifyIds.Contains(track.SpotifyId)) else if (fallbackMatchedSpotifyIds.Contains(track.SpotifyId))
{ {
// Track is externally matched (search succeeded)
isLocal = false; isLocal = false;
externalProvider = "SquidWTF"; externalProvider = "SquidWTF"; // Default to SquidWTF for external matches
} }
else else
{ {
// Track is missing (search failed)
isLocal = null; isLocal = null;
externalProvider = null; externalProvider = null;
} }
@@ -888,7 +930,7 @@ public class AdminController : ControllerBase
albumArtUrl = track.AlbumArtUrl, albumArtUrl = track.AlbumArtUrl,
isLocal = isLocal, isLocal = isLocal,
externalProvider = externalProvider, externalProvider = externalProvider,
searchQuery = isLocal != true ? $"{track.Title} {track.PrimaryArtist}" : null searchQuery = isLocal != true ? $"{track.Title} {track.PrimaryArtist}" : null // Set for both external and missing
}); });
} }
@@ -1391,7 +1433,9 @@ public class AdminController : ControllerBase
spotifyImport = new spotifyImport = new
{ {
enabled = _spotifyImportSettings.Enabled, enabled = _spotifyImportSettings.Enabled,
matchingIntervalHours = _spotifyImportSettings.MatchingIntervalHours, syncStartHour = _spotifyImportSettings.SyncStartHour,
syncStartMinute = _spotifyImportSettings.SyncStartMinute,
syncWindowHours = _spotifyImportSettings.SyncWindowHours,
playlists = _spotifyImportSettings.Playlists.Select(p => new playlists = _spotifyImportSettings.Playlists.Select(p => new
{ {
name = p.Name, name = p.Name,
@@ -1406,16 +1450,6 @@ public class AdminController : ControllerBase
userId = _jellyfinSettings.UserId ?? "(not set)", userId = _jellyfinSettings.UserId ?? "(not set)",
libraryId = _jellyfinSettings.LibraryId libraryId = _jellyfinSettings.LibraryId
}, },
library = new
{
downloadPath = _subsonicSettings.StorageMode == StorageMode.Cache
? Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "cache")
: Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "permanent"),
keptPath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept"),
storageMode = _subsonicSettings.StorageMode.ToString(),
cacheDurationHours = _subsonicSettings.CacheDurationHours,
downloadMode = _subsonicSettings.DownloadMode.ToString()
},
deezer = new deezer = new
{ {
arl = MaskValue(_deezerSettings.Arl, showLast: 8), arl = MaskValue(_deezerSettings.Arl, showLast: 8),
@@ -1982,13 +2016,8 @@ public class AdminController : ControllerBase
var isConfigured = configuredPlaylist != null; var isConfigured = configuredPlaylist != null;
var linkedSpotifyId = configuredPlaylist?.Id; var linkedSpotifyId = configuredPlaylist?.Id;
// Only fetch detailed track stats for configured Spotify playlists // Fetch track details to categorize local vs external
// This avoids expensive queries for large non-Spotify playlists var trackStats = await GetPlaylistTrackStats(id!);
var trackStats = (LocalTracks: 0, ExternalTracks: 0, ExternalAvailable: 0);
if (isConfigured)
{
trackStats = await GetPlaylistTrackStats(id!);
}
playlists.Add(new playlists.Add(new
{ {
@@ -2697,11 +2726,7 @@ public class AdminController : ControllerBase
if (parts.Length >= 3) if (parts.Length >= 3)
{ {
var timestamp = parts[0]; var timestamp = parts[0];
var method = parts[1]; var endpoint = parts[1];
var endpoint = parts[2];
// Combine method and endpoint for better clarity
var fullEndpoint = $"{method} {endpoint}";
// Filter by date if specified // Filter by date if specified
if (sinceDate.HasValue && DateTime.TryParse(timestamp, out var logDate)) if (sinceDate.HasValue && DateTime.TryParse(timestamp, out var logDate))
@@ -2710,7 +2735,7 @@ public class AdminController : ControllerBase
continue; continue;
} }
usage[fullEndpoint] = usage.GetValueOrDefault(fullEndpoint, 0) + 1; usage[endpoint] = usage.GetValueOrDefault(endpoint, 0) + 1;
} }
} }
@@ -3242,6 +3267,7 @@ public class AdminController : ControllerBase
} }
#endregion #endregion
}
public class ManualMappingRequest public class ManualMappingRequest
{ {
@@ -3297,44 +3323,36 @@ public class LinkPlaylistRequest
public string SpotifyPlaylistId { get; set; } = string.Empty; public string SpotifyPlaylistId { get; set; } = string.Empty;
} }
/// <summary> /// <summary>
/// GET /api/admin/downloads /// GET /api/admin/downloads
/// Lists all downloaded files in the KEPT folder only (favorited tracks) /// Lists all downloaded files in the downloads directory
/// </summary> /// </summary>
[HttpGet("downloads")] [HttpGet("downloads")]
public IActionResult GetDownloads() public IActionResult GetDownloads()
{ {
try try
{ {
var keptPath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept"); var downloadPath = _configuration["Library:DownloadPath"] ?? "./downloads";
_logger.LogInformation("📂 Checking kept folder: {Path}", keptPath); if (!Directory.Exists(downloadPath))
_logger.LogInformation("📂 Directory exists: {Exists}", Directory.Exists(keptPath));
if (!Directory.Exists(keptPath))
{ {
_logger.LogWarning("Kept folder does not exist: {Path}", keptPath); return Ok(new { files = new List<object>(), totalSize = 0 });
return Ok(new { files = new List<object>(), totalSize = 0, count = 0 });
} }
var files = new List<object>(); var files = new List<object>();
long totalSize = 0; long totalSize = 0;
// Recursively get all audio files from kept folder // Recursively get all audio files
var audioExtensions = new[] { ".flac", ".mp3", ".m4a", ".opus" }; var audioExtensions = new[] { ".flac", ".mp3", ".m4a", ".opus" };
var allFiles = Directory.GetFiles(downloadPath, "*.*", SearchOption.AllDirectories)
var allFiles = Directory.GetFiles(keptPath, "*.*", SearchOption.AllDirectories)
.Where(f => audioExtensions.Contains(Path.GetExtension(f).ToLowerInvariant())) .Where(f => audioExtensions.Contains(Path.GetExtension(f).ToLowerInvariant()))
.ToList(); .ToList();
_logger.LogInformation("📂 Found {Count} audio files in kept folder", allFiles.Count);
foreach (var filePath in allFiles) foreach (var filePath in allFiles)
{ {
_logger.LogDebug("📂 Processing file: {Path}", filePath);
var fileInfo = new FileInfo(filePath); var fileInfo = new FileInfo(filePath);
var relativePath = Path.GetRelativePath(keptPath, filePath); var relativePath = Path.GetRelativePath(downloadPath, filePath);
// Parse artist/album/track from path structure // Parse artist/album/track from path structure
var parts = relativePath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); var parts = relativePath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
@@ -3358,8 +3376,6 @@ public class LinkPlaylistRequest
totalSize += fileInfo.Length; totalSize += fileInfo.Length;
} }
_logger.LogInformation("📂 Returning {Count} kept files, total size: {Size}", files.Count, FormatFileSize(totalSize));
return Ok(new return Ok(new
{ {
files = files.OrderBy(f => ((dynamic)f).artist).ThenBy(f => ((dynamic)f).album).ThenBy(f => ((dynamic)f).fileName), files = files.OrderBy(f => ((dynamic)f).artist).ThenBy(f => ((dynamic)f).album).ThenBy(f => ((dynamic)f).fileName),
@@ -3370,14 +3386,14 @@ public class LinkPlaylistRequest
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Failed to list kept downloads"); _logger.LogError(ex, "Failed to list downloads");
return StatusCode(500, new { error = "Failed to list kept downloads" }); return StatusCode(500, new { error = "Failed to list downloads" });
} }
} }
/// <summary> /// <summary>
/// DELETE /api/admin/downloads /// DELETE /api/admin/downloads
/// Deletes a specific kept file and cleans up empty folders /// Deletes a specific downloaded file
/// </summary> /// </summary>
[HttpDelete("downloads")] [HttpDelete("downloads")]
public IActionResult DeleteDownload([FromQuery] string path) public IActionResult DeleteDownload([FromQuery] string path)
@@ -3389,59 +3405,54 @@ public class LinkPlaylistRequest
return BadRequest(new { error = "Path is required" }); return BadRequest(new { error = "Path is required" });
} }
var keptPath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept"); var downloadPath = _configuration["Library:DownloadPath"] ?? "./downloads";
var fullPath = Path.Combine(keptPath, path); var fullPath = Path.Combine(downloadPath, path);
_logger.LogInformation("🗑️ Delete request for: {Path}", fullPath); // Security: Ensure the path is within the download directory
// Security: Ensure the path is within the kept directory
var normalizedFullPath = Path.GetFullPath(fullPath); var normalizedFullPath = Path.GetFullPath(fullPath);
var normalizedKeptPath = Path.GetFullPath(keptPath); var normalizedDownloadPath = Path.GetFullPath(downloadPath);
if (!normalizedFullPath.StartsWith(normalizedKeptPath)) if (!normalizedFullPath.StartsWith(normalizedDownloadPath))
{ {
_logger.LogWarning("🗑️ Invalid path (outside kept folder): {Path}", normalizedFullPath);
return BadRequest(new { error = "Invalid path" }); return BadRequest(new { error = "Invalid path" });
} }
if (!System.IO.File.Exists(fullPath)) if (!System.IO.File.Exists(fullPath))
{ {
_logger.LogWarning("🗑️ File not found: {Path}", fullPath);
return NotFound(new { error = "File not found" }); return NotFound(new { error = "File not found" });
} }
System.IO.File.Delete(fullPath); System.IO.File.Delete(fullPath);
_logger.LogInformation("🗑️ Deleted file: {Path}", fullPath); _logger.LogInformation("Deleted download: {Path}", path);
// Clean up empty directories (Album folder, then Artist folder if empty) // Clean up empty directories
var directory = Path.GetDirectoryName(fullPath); var directory = Path.GetDirectoryName(fullPath);
while (directory != null && directory != keptPath && directory.StartsWith(keptPath)) while (directory != null && directory != downloadPath)
{ {
if (Directory.Exists(directory) && !Directory.EnumerateFileSystemEntries(directory).Any()) if (Directory.Exists(directory) && !Directory.EnumerateFileSystemEntries(directory).Any())
{ {
Directory.Delete(directory); Directory.Delete(directory);
_logger.LogInformation("🗑️ Deleted empty directory: {Dir}", directory); _logger.LogDebug("Deleted empty directory: {Dir}", directory);
directory = Path.GetDirectoryName(directory);
} }
else else
{ {
_logger.LogDebug("🗑️ Directory not empty or doesn't exist, stopping cleanup: {Dir}", directory);
break; break;
} }
directory = Path.GetDirectoryName(directory);
} }
return Ok(new { success = true, message = "File deleted successfully" }); return Ok(new { success = true, message = "File deleted successfully" });
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Failed to delete file: {Path}", path); _logger.LogError(ex, "Failed to delete download: {Path}", path);
return StatusCode(500, new { error = "Failed to delete file" }); return StatusCode(500, new { error = "Failed to delete file" });
} }
} }
/// <summary> /// <summary>
/// GET /api/admin/downloads/file /// GET /api/admin/downloads/file
/// Downloads a specific file from the kept folder /// Downloads a specific file
/// </summary> /// </summary>
[HttpGet("downloads/file")] [HttpGet("downloads/file")]
public IActionResult DownloadFile([FromQuery] string path) public IActionResult DownloadFile([FromQuery] string path)
@@ -3453,14 +3464,14 @@ public class LinkPlaylistRequest
return BadRequest(new { error = "Path is required" }); return BadRequest(new { error = "Path is required" });
} }
var keptPath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept"); var downloadPath = _configuration["Library:DownloadPath"] ?? "./downloads";
var fullPath = Path.Combine(keptPath, path); var fullPath = Path.Combine(downloadPath, path);
// Security: Ensure the path is within the kept directory // Security: Ensure the path is within the download directory
var normalizedFullPath = Path.GetFullPath(fullPath); var normalizedFullPath = Path.GetFullPath(fullPath);
var normalizedKeptPath = Path.GetFullPath(keptPath); var normalizedDownloadPath = Path.GetFullPath(downloadPath);
if (!normalizedFullPath.StartsWith(normalizedKeptPath)) if (!normalizedFullPath.StartsWith(normalizedDownloadPath))
{ {
return BadRequest(new { error = "Invalid path" }); return BadRequest(new { error = "Invalid path" });
} }

View File

@@ -40,9 +40,7 @@ public class JellyfinController : ControllerBase
private readonly SpotifyPlaylistFetcher? _spotifyPlaylistFetcher; private readonly SpotifyPlaylistFetcher? _spotifyPlaylistFetcher;
private readonly SpotifyLyricsService? _spotifyLyricsService; private readonly SpotifyLyricsService? _spotifyLyricsService;
private readonly LrclibService? _lrclibService; private readonly LrclibService? _lrclibService;
private readonly OdesliService _odesliService;
private readonly RedisCacheService _cache; private readonly RedisCacheService _cache;
private readonly IConfiguration _configuration;
private readonly ILogger<JellyfinController> _logger; private readonly ILogger<JellyfinController> _logger;
public JellyfinController( public JellyfinController(
@@ -56,9 +54,7 @@ public class JellyfinController : ControllerBase
JellyfinModelMapper modelMapper, JellyfinModelMapper modelMapper,
JellyfinProxyService proxyService, JellyfinProxyService proxyService,
JellyfinSessionManager sessionManager, JellyfinSessionManager sessionManager,
OdesliService odesliService,
RedisCacheService cache, RedisCacheService cache,
IConfiguration configuration,
ILogger<JellyfinController> logger, ILogger<JellyfinController> logger,
ParallelMetadataService? parallelMetadataService = null, ParallelMetadataService? parallelMetadataService = null,
PlaylistSyncService? playlistSyncService = null, PlaylistSyncService? playlistSyncService = null,
@@ -81,9 +77,7 @@ public class JellyfinController : ControllerBase
_spotifyPlaylistFetcher = spotifyPlaylistFetcher; _spotifyPlaylistFetcher = spotifyPlaylistFetcher;
_spotifyLyricsService = spotifyLyricsService; _spotifyLyricsService = spotifyLyricsService;
_lrclibService = lrclibService; _lrclibService = lrclibService;
_odesliService = odesliService;
_cache = cache; _cache = cache;
_configuration = configuration;
_logger = logger; _logger = logger;
if (string.IsNullOrWhiteSpace(_settings.Url)) if (string.IsNullOrWhiteSpace(_settings.Url))
@@ -279,71 +273,63 @@ public class JellyfinController : ControllerBase
// Parse Jellyfin results into domain models // Parse Jellyfin results into domain models
var (localSongs, localAlbums, localArtists) = _modelMapper.ParseItemsResponse(jellyfinResult); var (localSongs, localAlbums, localArtists) = _modelMapper.ParseItemsResponse(jellyfinResult);
// Respect source ordering (SquidWTF/Tidal has better search ranking than our fuzzy matching) // Score and filter Jellyfin results by relevance
// Just interleave local and external results based on which source has better overall match 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);
// Calculate average match score for each source to determine which should come first // Score external results with a small boost
var localSongsAvgScore = localSongs.Any() var scoredExternalSongs = ScoreSearchResults(cleanQuery, externalResult.Songs, s => s.Title, s => s.Artist, s => s.Album, isExternal: true);
? localSongs.Average(s => FuzzyMatcher.CalculateSimilarity(cleanQuery, s.Title)) var scoredExternalAlbums = ScoreSearchResults(cleanQuery, externalResult.Albums, a => a.Title, a => a.Artist, _ => null, isExternal: true);
: 0.0; var scoredExternalArtists = ScoreSearchResults(cleanQuery, externalResult.Artists, a => a.Name, _ => null, _ => null, isExternal: true);
var externalSongsAvgScore = externalResult.Songs.Any()
? externalResult.Songs.Average(s => FuzzyMatcher.CalculateSimilarity(cleanQuery, s.Title))
: 0.0;
var localAlbumsAvgScore = localAlbums.Any() // Merge and sort by score (no filtering - just reorder by relevance)
? localAlbums.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Title)) var allSongs = scoredLocalSongs.Concat(scoredExternalSongs)
: 0.0; .OrderByDescending(x => x.Score)
var externalAlbumsAvgScore = externalResult.Albums.Any() .Select(x => x.Item)
? externalResult.Albums.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Title)) .ToList();
: 0.0;
var localArtistsAvgScore = localArtists.Any() var allAlbums = scoredLocalAlbums.Concat(scoredExternalAlbums)
? localArtists.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Name)) .OrderByDescending(x => x.Score)
: 0.0; .Select(x => x.Item)
var externalArtistsAvgScore = externalResult.Artists.Any() .ToList();
? externalResult.Artists.Average(a => FuzzyMatcher.CalculateSimilarity(cleanQuery, a.Name))
: 0.0;
// Interleave results: put better-matching source first, preserve original ordering within each source // NO deduplication - just merge and sort by relevance score
var allSongs = localSongsAvgScore >= externalSongsAvgScore // Show ALL matches (local + external) sorted by best match first
? localSongs.Concat(externalResult.Songs).ToList() var artistScores = scoredLocalArtists.Concat(scoredExternalArtists)
: externalResult.Songs.Concat(localSongs).ToList(); .OrderByDescending(x => x.Score)
.Select(x => x.Item)
var allAlbums = localAlbumsAvgScore >= externalAlbumsAvgScore .ToList();
? localAlbums.Concat(externalResult.Albums).ToList()
: externalResult.Albums.Concat(localAlbums).ToList();
var allArtists = localArtistsAvgScore >= externalArtistsAvgScore
? localArtists.Concat(externalResult.Artists).ToList()
: externalResult.Artists.Concat(localArtists).ToList();
// Log results for debugging // Log results for debugging
if (_logger.IsEnabled(LogLevel.Debug)) if (_logger.IsEnabled(LogLevel.Debug))
{ {
_logger.LogDebug("🎵 Songs: Local avg score={LocalScore:F2}, External avg score={ExtScore:F2}, Local first={LocalFirst}", var localArtistNames = scoredLocalArtists.Select(x => $"{x.Item.Name} (local, score: {x.Score:F2})").ToList();
localSongsAvgScore, externalSongsAvgScore, localSongsAvgScore >= externalSongsAvgScore); var externalArtistNames = scoredExternalArtists.Select(x => $"{x.Item.Name} ({x.Item.ExternalProvider}, score: {x.Score:F2})").ToList();
_logger.LogDebug("💿 Albums: Local avg score={LocalScore:F2}, External avg score={ExtScore:F2}, Local first={LocalFirst}", _logger.LogDebug("🎤 Artist results: Local={LocalArtists}, External={ExternalArtists}, Total={TotalCount}",
localAlbumsAvgScore, externalAlbumsAvgScore, localAlbumsAvgScore >= externalAlbumsAvgScore); string.Join(", ", localArtistNames),
_logger.LogDebug("🎤 Artists: Local avg score={LocalScore:F2}, External avg score={ExtScore:F2}, Local first={LocalFirst}", string.Join(", ", externalArtistNames),
localArtistsAvgScore, externalArtistsAvgScore, localArtistsAvgScore >= externalArtistsAvgScore); artistScores.Count);
} }
// Convert to Jellyfin format // Convert to Jellyfin format
var mergedSongs = allSongs.Select(s => _responseBuilder.ConvertSongToJellyfinItem(s)).ToList(); var mergedSongs = allSongs.Select(s => _responseBuilder.ConvertSongToJellyfinItem(s)).ToList();
var mergedAlbums = allAlbums.Select(a => _responseBuilder.ConvertAlbumToJellyfinItem(a)).ToList(); var mergedAlbums = allAlbums.Select(a => _responseBuilder.ConvertAlbumToJellyfinItem(a)).ToList();
var mergedArtists = allArtists.Select(a => _responseBuilder.ConvertArtistToJellyfinItem(a)).ToList(); var mergedArtists = artistScores.Select(a => _responseBuilder.ConvertArtistToJellyfinItem(a)).ToList();
// Add playlists (preserve their order too) // Add playlists (score them too)
if (playlistResult.Count > 0) if (playlistResult.Count > 0)
{ {
var playlistItems = playlistResult var scoredPlaylists = playlistResult
.Select(p => _responseBuilder.ConvertPlaylistToJellyfinItem(p)) .Select(p => new { Playlist = p, Score = FuzzyMatcher.CalculateSimilarity(cleanQuery, p.Name) })
.OrderByDescending(x => x.Score)
.Select(x => _responseBuilder.ConvertPlaylistToJellyfinItem(x.Playlist))
.ToList(); .ToList();
mergedAlbums.AddRange(playlistItems); mergedAlbums.AddRange(scoredPlaylists);
} }
_logger.LogInformation("Merged results (preserving source order): Songs={Songs}, Albums={Albums}, Artists={Artists}", _logger.LogInformation("Scored and filtered results: Songs={Songs}, Albums={Albums}, Artists={Artists}",
mergedSongs.Count, mergedAlbums.Count, mergedArtists.Count); mergedSongs.Count, mergedAlbums.Count, mergedArtists.Count);
// Pre-fetch lyrics for top 3 songs in background (don't await) // Pre-fetch lyrics for top 3 songs in background (don't await)
@@ -1144,8 +1130,6 @@ public class JellyfinController : ControllerBase
[HttpGet("Items/{itemId}/Lyrics")] [HttpGet("Items/{itemId}/Lyrics")]
public async Task<IActionResult> GetLyrics(string itemId) public async Task<IActionResult> GetLyrics(string itemId)
{ {
_logger.LogInformation("🎵 GetLyrics called for itemId: {ItemId}", itemId);
if (string.IsNullOrWhiteSpace(itemId)) if (string.IsNullOrWhiteSpace(itemId))
{ {
return NotFound(); return NotFound();
@@ -1153,9 +1137,6 @@ public class JellyfinController : ControllerBase
var (isExternal, provider, externalId) = _localLibraryService.ParseSongId(itemId); var (isExternal, provider, externalId) = _localLibraryService.ParseSongId(itemId);
_logger.LogInformation("🎵 Lyrics request: itemId={ItemId}, isExternal={IsExternal}, provider={Provider}, externalId={ExternalId}",
itemId, isExternal, provider, externalId);
// For local tracks, check if Jellyfin already has embedded lyrics // For local tracks, check if Jellyfin already has embedded lyrics
if (!isExternal) if (!isExternal)
{ {
@@ -1164,16 +1145,13 @@ public class JellyfinController : ControllerBase
// Try to get lyrics from Jellyfin first (it reads embedded lyrics from files) // Try to get lyrics from Jellyfin first (it reads embedded lyrics from files)
var (jellyfinLyrics, statusCode) = await _proxyService.GetJsonAsync($"Audio/{itemId}/Lyrics", null, Request.Headers); var (jellyfinLyrics, statusCode) = await _proxyService.GetJsonAsync($"Audio/{itemId}/Lyrics", null, Request.Headers);
_logger.LogInformation("Jellyfin lyrics check result: statusCode={StatusCode}, hasLyrics={HasLyrics}",
statusCode, jellyfinLyrics != null);
if (jellyfinLyrics != null && statusCode == 200) if (jellyfinLyrics != null && statusCode == 200)
{ {
_logger.LogInformation("Found embedded lyrics in Jellyfin for track {ItemId}", itemId); _logger.LogInformation("Found embedded lyrics in Jellyfin for track {ItemId}", itemId);
return new JsonResult(JsonSerializer.Deserialize<object>(jellyfinLyrics.RootElement.GetRawText())); return new JsonResult(JsonSerializer.Deserialize<object>(jellyfinLyrics.RootElement.GetRawText()));
} }
_logger.LogInformation("No embedded lyrics found in Jellyfin (status: {StatusCode}), trying Spotify/LRCLIB", statusCode); _logger.LogInformation("No embedded lyrics found in Jellyfin, trying Spotify/LRCLIB");
} }
// Get song metadata for lyrics search // Get song metadata for lyrics search
@@ -1184,15 +1162,9 @@ public class JellyfinController : ControllerBase
{ {
song = await _metadataService.GetSongAsync(provider!, externalId!); song = await _metadataService.GetSongAsync(provider!, externalId!);
// Use Spotify ID from song metadata if available (populated during GetSongAsync) // Try to find Spotify ID from matched tracks cache
if (song != null && !string.IsNullOrEmpty(song.SpotifyId)) // External tracks from playlists should have been matched and cached
{ if (song != null)
spotifyTrackId = song.SpotifyId;
_logger.LogInformation("Using Spotify ID {SpotifyId} from song metadata for {Provider}/{ExternalId}",
spotifyTrackId, provider, externalId);
}
// Fallback: Try to find Spotify ID from matched tracks cache
else if (song != null)
{ {
spotifyTrackId = await FindSpotifyIdForExternalTrackAsync(song); spotifyTrackId = await FindSpotifyIdForExternalTrackAsync(song);
if (!string.IsNullOrEmpty(spotifyTrackId)) if (!string.IsNullOrEmpty(spotifyTrackId))
@@ -1202,27 +1174,9 @@ public class JellyfinController : ControllerBase
} }
else else
{ {
// Last resort: Try to convert via Odesli/song.link // If no cached Spotify ID, try to convert via Odesli/song.link
if (provider == "squidwtf") // This works for SquidWTF (Tidal), Deezer, Qobuz, etc.
{ spotifyTrackId = await ConvertToSpotifyIdViaOdesliAsync(song, provider!, externalId!);
spotifyTrackId = await _odesliService.ConvertTidalToSpotifyIdAsync(externalId!, HttpContext.RequestAborted);
}
else
{
// For other providers, build the URL and convert
var sourceUrl = provider?.ToLowerInvariant() switch
{
"deezer" => $"https://www.deezer.com/track/{externalId}",
"qobuz" => $"https://www.qobuz.com/us-en/album/-/-/{externalId}",
_ => null
};
if (!string.IsNullOrEmpty(sourceUrl))
{
spotifyTrackId = await _odesliService.ConvertUrlToSpotifyIdAsync(sourceUrl, HttpContext.RequestAborted);
}
}
if (!string.IsNullOrEmpty(spotifyTrackId)) if (!string.IsNullOrEmpty(spotifyTrackId))
{ {
_logger.LogInformation("Converted {Provider}/{ExternalId} to Spotify ID {SpotifyId} via Odesli", _logger.LogInformation("Converted {Provider}/{ExternalId} to Spotify ID {SpotifyId} via Odesli",
@@ -1447,9 +1401,9 @@ public class JellyfinController : ControllerBase
spotifyTrackId = await FindSpotifyIdForExternalTrackAsync(song); spotifyTrackId = await FindSpotifyIdForExternalTrackAsync(song);
// If no cached Spotify ID, try Odesli conversion // If no cached Spotify ID, try Odesli conversion
if (string.IsNullOrEmpty(spotifyTrackId) && provider == "squidwtf") if (string.IsNullOrEmpty(spotifyTrackId))
{ {
spotifyTrackId = await _odesliService.ConvertTidalToSpotifyIdAsync(externalId, HttpContext.RequestAborted); spotifyTrackId = await ConvertToSpotifyIdViaOdesliAsync(song, provider, externalId);
} }
} }
} }
@@ -1873,83 +1827,53 @@ public class JellyfinController : ControllerBase
_logger.LogInformation("Authentication request received"); _logger.LogInformation("Authentication request received");
// DO NOT log request body or detailed headers - contains password // DO NOT log request body or detailed headers - contains password
// Forward to Jellyfin server with client headers - completely transparent proxy // Forward to Jellyfin server with client headers
var (result, statusCode) = await _proxyService.PostJsonAsync("Users/AuthenticateByName", body, Request.Headers); var (result, statusCode) = await _proxyService.PostJsonAsync("Users/AuthenticateByName", body, Request.Headers);
// Pass through Jellyfin's response exactly as-is (transparent proxy) if (result == null)
if (result != null)
{ {
var responseJson = result.RootElement.GetRawText(); _logger.LogWarning("Authentication failed - status {StatusCode}", statusCode);
if (statusCode == 401)
// On successful auth, extract access token and post session capabilities in background
if (statusCode == 200)
{ {
_logger.LogInformation("Authentication successful"); return Unauthorized(new { error = "Invalid username or password" });
}
return StatusCode(statusCode, new { error = "Authentication failed" });
}
// Extract access token from response for session capabilities _logger.LogInformation("Authentication successful");
string? accessToken = null;
if (result.RootElement.TryGetProperty("AccessToken", out var tokenEl))
{
accessToken = tokenEl.GetString();
}
// Post session capabilities in background if we have a token // Post session capabilities immediately after authentication
if (!string.IsNullOrEmpty(accessToken)) // This ensures Jellyfin creates a session that will show up in the dashboard
{ try
// Capture token in closure - don't use Request.Headers (will be disposed) {
var token = accessToken; _logger.LogInformation("🔧 Posting session capabilities after authentication");
_ = Task.Run(async () => var capabilities = new
{ {
try PlayableMediaTypes = new[] { "Audio" },
{ SupportedCommands = Array.Empty<string>(),
_logger.LogDebug("🔧 Posting session capabilities after authentication"); SupportsMediaControl = false,
SupportsPersistentIdentifier = true,
SupportsSync = false
};
// Build auth header with the new token var capabilitiesJson = JsonSerializer.Serialize(capabilities);
var authHeaders = new HeaderDictionary var (capResult, capStatus) = await _proxyService.PostJsonAsync("Sessions/Capabilities/Full", capabilitiesJson, Request.Headers);
{
["X-Emby-Token"] = token
};
var capabilities = new if (capStatus == 204 || capStatus == 200)
{ {
PlayableMediaTypes = new[] { "Audio" }, _logger.LogInformation("✓ Session capabilities posted after auth ({StatusCode})", capStatus);
SupportedCommands = Array.Empty<string>(),
SupportsMediaControl = false,
SupportsPersistentIdentifier = true,
SupportsSync = false
};
var capabilitiesJson = JsonSerializer.Serialize(capabilities);
var (capResult, capStatus) = await _proxyService.PostJsonAsync("Sessions/Capabilities/Full", capabilitiesJson, authHeaders);
if (capStatus == 204 || capStatus == 200)
{
_logger.LogDebug("✓ Session capabilities posted after auth ({StatusCode})", capStatus);
}
else
{
_logger.LogDebug("⚠ Session capabilities returned {StatusCode} after auth", capStatus);
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Failed to post session capabilities after auth");
}
});
}
} }
else else
{ {
_logger.LogWarning("Authentication failed - status {StatusCode}", statusCode); _logger.LogWarning("⚠ Session capabilities returned {StatusCode} after auth", capStatus);
} }
}
// Return Jellyfin's exact response catch (Exception ex)
return Content(responseJson, "application/json"); {
_logger.LogWarning(ex, "Failed to post session capabilities after auth, continuing anyway");
} }
// No response body from Jellyfin - return status code only return Content(result.RootElement.GetRawText(), "application/json");
_logger.LogWarning("Authentication request returned {StatusCode} with no response body", statusCode);
return StatusCode(statusCode);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -2183,7 +2107,7 @@ public class JellyfinController : ControllerBase
var method = Request.Method; var method = Request.Method;
var queryString = Request.QueryString.HasValue ? Request.QueryString.Value : ""; var queryString = Request.QueryString.HasValue ? Request.QueryString.Value : "";
_logger.LogDebug("📡 Session capabilities reported - Method: {Method}, Query: {Query}", method, queryString); _logger.LogInformation("📡 Session capabilities reported - Method: {Method}, Query: {Query}", method, queryString);
_logger.LogInformation("Headers: {Headers}", _logger.LogInformation("Headers: {Headers}",
string.Join(", ", Request.Headers.Where(h => h.Key.Contains("Auth", StringComparison.OrdinalIgnoreCase) || h.Key.Contains("Device", StringComparison.OrdinalIgnoreCase) || h.Key.Contains("Client", StringComparison.OrdinalIgnoreCase)) string.Join(", ", Request.Headers.Where(h => h.Key.Contains("Auth", StringComparison.OrdinalIgnoreCase) || h.Key.Contains("Device", StringComparison.OrdinalIgnoreCase) || h.Key.Contains("Client", StringComparison.OrdinalIgnoreCase))
.Select(h => $"{h.Key}={h.Value}"))); .Select(h => $"{h.Key}={h.Value}")));
@@ -2208,11 +2132,7 @@ public class JellyfinController : ControllerBase
if (statusCode == 204 || statusCode == 200) if (statusCode == 204 || statusCode == 200)
{ {
_logger.LogDebug("✓ Session capabilities forwarded to Jellyfin ({StatusCode})", statusCode); _logger.LogInformation("✓ Session capabilities forwarded to Jellyfin ({StatusCode})", statusCode);
}
else if (statusCode == 401)
{
_logger.LogDebug("⚠ Jellyfin returned 401 for capabilities (token expired)");
} }
else else
{ {
@@ -2246,7 +2166,7 @@ public class JellyfinController : ControllerBase
} }
Request.Body.Position = 0; Request.Body.Position = 0;
_logger.LogDebug("📻 Playback START reported"); _logger.LogInformation("📻 Playback START reported");
// Parse the body to check if it's an external track // Parse the body to check if it's an external track
var doc = JsonDocument.Parse(body); var doc = JsonDocument.Parse(body);
@@ -2298,37 +2218,35 @@ public class JellyfinController : ControllerBase
} }
}); });
// Create a ghost/fake item to report to Jellyfin so "Now Playing" shows up // CRITICAL: Create session for external tracks too!
// Generate a deterministic UUID from the external ID // Even though Jellyfin doesn't know about the track, we need a session
var ghostUuid = GenerateUuidFromString(itemId); // for the client to appear in the dashboard and receive remote control commands
if (!string.IsNullOrEmpty(deviceId))
// Build minimal playback start with just the ghost UUID
// Don't include the Item object - Jellyfin will just track the session without item details
var playbackStart = new
{ {
ItemId = ghostUuid, _logger.LogInformation("🔧 SESSION: Creating session for external track playback");
PositionTicks = positionTicks ?? 0, var sessionCreated = await _sessionManager.EnsureSessionAsync(
CanSeek = true, deviceId,
IsPaused = false, client ?? "Unknown",
IsMuted = false, device ?? "Unknown",
PlayMethod = "DirectPlay" version ?? "1.0",
}; Request.Headers);
var playbackJson = JsonSerializer.Serialize(playbackStart); if (sessionCreated)
_logger.LogDebug("📤 Sending ghost playback start for external track: {Json}", playbackJson); {
_logger.LogInformation("✓ SESSION: Session created for external track playback on device {DeviceId}", deviceId);
// Forward to Jellyfin with ghost UUID }
var (ghostResult, ghostStatusCode) = await _proxyService.PostJsonAsync("Sessions/Playing", playbackJson, Request.Headers); else
{
if (ghostStatusCode == 204 || ghostStatusCode == 200) _logger.LogWarning("⚠️ SESSION: Failed to create session for external track playback");
{ }
_logger.LogDebug("✓ Ghost playback start forwarded to Jellyfin for external track ({StatusCode})", ghostStatusCode);
} }
else else
{ {
_logger.LogWarning("⚠️ Ghost playback start returned status {StatusCode} for external track", ghostStatusCode); _logger.LogWarning("⚠️ SESSION: No device ID found for external track playback");
} }
// For external tracks, we can't report playback to Jellyfin since it doesn't know about them
// But the session is now active and will appear in the dashboard
return NoContent(); return NoContent();
} }
@@ -2350,7 +2268,7 @@ public class JellyfinController : ControllerBase
} }
// For local tracks, forward playback start to Jellyfin FIRST // For local tracks, forward playback start to Jellyfin FIRST
_logger.LogDebug("Forwarding playback start to Jellyfin..."); _logger.LogInformation("Forwarding playback start to Jellyfin...");
// Fetch full item details to include in playback report // Fetch full item details to include in playback report
try try
@@ -2376,7 +2294,7 @@ public class JellyfinController : ControllerBase
if (statusCode == 204 || statusCode == 200) if (statusCode == 204 || statusCode == 200)
{ {
_logger.LogDebug("✓ Playback start forwarded to Jellyfin ({StatusCode})", statusCode); _logger.LogInformation("✓ Playback start forwarded to Jellyfin ({StatusCode})", statusCode);
// NOW ensure session exists with capabilities (after playback is reported) // NOW ensure session exists with capabilities (after playback is reported)
if (!string.IsNullOrEmpty(deviceId)) if (!string.IsNullOrEmpty(deviceId))
@@ -2384,7 +2302,7 @@ public class JellyfinController : ControllerBase
var sessionCreated = await _sessionManager.EnsureSessionAsync(deviceId, client ?? "Unknown", device ?? "Unknown", version ?? "1.0", Request.Headers); var sessionCreated = await _sessionManager.EnsureSessionAsync(deviceId, client ?? "Unknown", device ?? "Unknown", version ?? "1.0", Request.Headers);
if (sessionCreated) if (sessionCreated)
{ {
_logger.LogDebug("✓ SESSION: Session ensured for device {DeviceId} after playback start", deviceId); _logger.LogWarning("✓ SESSION: Session ensured for device {DeviceId} after playback start", deviceId);
} }
else else
{ {
@@ -2408,7 +2326,7 @@ public class JellyfinController : ControllerBase
var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Playing", body, Request.Headers); var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Playing", body, Request.Headers);
if (statusCode == 204 || statusCode == 200) if (statusCode == 204 || statusCode == 200)
{ {
_logger.LogDebug("✓ Basic playback start forwarded to Jellyfin ({StatusCode})", statusCode); _logger.LogInformation("✓ Basic playback start forwarded to Jellyfin ({StatusCode})", statusCode);
} }
} }
} }
@@ -2481,36 +2399,25 @@ public class JellyfinController : ControllerBase
if (isExternal) if (isExternal)
{ {
// For external tracks, report progress with ghost UUID to Jellyfin // For external tracks, update session activity to keep it alive
var ghostUuid = GenerateUuidFromString(itemId); // This ensures the session remains visible in Jellyfin dashboard
if (!string.IsNullOrEmpty(deviceId))
// Build progress report with ghost UUID
var progressReport = new
{ {
ItemId = ghostUuid, _sessionManager.UpdateActivity(deviceId);
PositionTicks = positionTicks ?? 0,
IsPaused = false,
IsMuted = false,
CanSeek = true,
PlayMethod = "DirectPlay"
};
var progressJson = JsonSerializer.Serialize(progressReport); // Log progress occasionally for debugging (every ~30 seconds)
if (positionTicks.HasValue)
// Forward to Jellyfin with ghost UUID
var (progressResult, progressStatusCode) = await _proxyService.PostJsonAsync("Sessions/Playing/Progress", progressJson, Request.Headers);
// Log progress occasionally for debugging (every ~30 seconds)
if (positionTicks.HasValue)
{
var position = TimeSpan.FromTicks(positionTicks.Value);
if (position.Seconds % 30 == 0 && position.Milliseconds < 500)
{ {
_logger.LogDebug("▶️ External track progress: {Position:mm\\:ss} ({Provider}/{ExternalId}) - Status: {StatusCode}", var position = TimeSpan.FromTicks(positionTicks.Value);
position, provider, externalId, progressStatusCode); if (position.Seconds % 30 == 0 && position.Milliseconds < 500)
{
_logger.LogDebug("▶️ External track progress: {Position:mm\\:ss} ({Provider}/{ExternalId})",
position, provider, externalId);
}
} }
} }
// Just acknowledge (no reporting to Jellyfin for external tracks)
return NoContent(); return NoContent();
} }
@@ -2521,14 +2428,12 @@ public class JellyfinController : ControllerBase
// Only log at 10-second intervals // Only log at 10-second intervals
if (position.Seconds % 10 == 0 && position.Milliseconds < 500) if (position.Seconds % 10 == 0 && position.Milliseconds < 500)
{ {
_logger.LogDebug("▶️ Progress: {Position:mm\\:ss} for item {ItemId}", position, itemId); _logger.LogInformation("▶️ Progress: {Position:mm\\:ss} for item {ItemId}", position, itemId);
} }
} }
} }
// For local tracks, forward to Jellyfin // For local tracks, forward to Jellyfin
_logger.LogDebug("📤 Sending playback progress body: {Body}", body);
var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Playing/Progress", body, Request.Headers); var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Playing/Progress", body, Request.Headers);
if (statusCode != 204 && statusCode != 200) if (statusCode != 204 && statusCode != 200)
@@ -2561,7 +2466,7 @@ public class JellyfinController : ControllerBase
} }
Request.Body.Position = 0; Request.Body.Position = 0;
_logger.LogDebug("⏹️ Playback STOPPED reported"); _logger.LogInformation("⏹️ Playback STOPPED reported");
// Parse the body to check if it's an external track // Parse the body to check if it's an external track
var doc = JsonDocument.Parse(body); var doc = JsonDocument.Parse(body);
@@ -2603,23 +2508,11 @@ public class JellyfinController : ControllerBase
_logger.LogInformation("🎵 External track playback stopped: {Name} at {Position} ({Provider}/{ExternalId})", _logger.LogInformation("🎵 External track playback stopped: {Name} at {Position} ({Provider}/{ExternalId})",
itemName ?? "Unknown", position, provider, externalId); itemName ?? "Unknown", position, provider, externalId);
// Report stop to Jellyfin with ghost UUID // Mark session as potentially ended after playback stops
var ghostUuid = GenerateUuidFromString(itemId); // Wait 50 seconds for next song to start before cleaning up
if (!string.IsNullOrEmpty(deviceId))
var stopInfo = new
{ {
ItemId = ghostUuid, _sessionManager.MarkSessionPotentiallyEnded(deviceId, TimeSpan.FromSeconds(50));
PositionTicks = positionTicks ?? 0
};
var stopJson = JsonSerializer.Serialize(stopInfo);
_logger.LogDebug("📤 Sending ghost playback stop for external track: {Json}", stopJson);
var (stopResult, stopStatusCode) = await _proxyService.PostJsonAsync("Sessions/Playing/Stopped", stopJson, Request.Headers);
if (stopStatusCode == 204 || stopStatusCode == 200)
{
_logger.LogDebug("✓ Ghost playback stop forwarded to Jellyfin ({StatusCode})", stopStatusCode);
} }
return NoContent(); return NoContent();
@@ -2630,34 +2523,12 @@ public class JellyfinController : ControllerBase
} }
// For local tracks, forward to Jellyfin // For local tracks, forward to Jellyfin
_logger.LogDebug("Forwarding playback stop to Jellyfin..."); _logger.LogInformation("Forwarding playback stop to Jellyfin...");
// Log the body being sent for debugging
_logger.LogInformation("📤 Sending playback stop body: {Body}", body);
// Validate that body is not empty
if (string.IsNullOrWhiteSpace(body) || body == "{}")
{
_logger.LogWarning("⚠️ Playback stop body is empty, building minimal valid payload");
// Build a minimal valid PlaybackStopInfo
var stopInfo = new
{
ItemId = itemId,
PositionTicks = positionTicks ?? 0
};
body = JsonSerializer.Serialize(stopInfo);
_logger.LogInformation("📤 Built playback stop body: {Body}", body);
}
var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Playing/Stopped", body, Request.Headers); var (result, statusCode) = await _proxyService.PostJsonAsync("Sessions/Playing/Stopped", body, Request.Headers);
if (statusCode == 204 || statusCode == 200) if (statusCode == 204 || statusCode == 200)
{ {
_logger.LogDebug("✓ Playback stop forwarded to Jellyfin ({StatusCode})", statusCode); _logger.LogInformation("✓ Playback stop forwarded to Jellyfin ({StatusCode})", statusCode);
}
else if (statusCode == 401)
{
_logger.LogDebug("Playback stop returned 401 (token expired)");
} }
else else
{ {
@@ -2716,7 +2587,7 @@ public class JellyfinController : ControllerBase
var queryString = Request.QueryString.HasValue ? Request.QueryString.Value : ""; var queryString = Request.QueryString.HasValue ? Request.QueryString.Value : "";
var endpoint = string.IsNullOrEmpty(path) ? $"Sessions{queryString}" : $"Sessions/{path}{queryString}"; var endpoint = string.IsNullOrEmpty(path) ? $"Sessions{queryString}" : $"Sessions/{path}{queryString}";
_logger.LogDebug("🔄 Proxying session request: {Method} {Endpoint}", method, endpoint); _logger.LogInformation("🔄 Proxying session request: {Method} {Endpoint}", method, endpoint);
_logger.LogDebug("Session proxy headers: {Headers}", _logger.LogDebug("Session proxy headers: {Headers}",
string.Join(", ", Request.Headers.Where(h => h.Key.Contains("Auth", StringComparison.OrdinalIgnoreCase)) string.Join(", ", Request.Headers.Where(h => h.Key.Contains("Auth", StringComparison.OrdinalIgnoreCase))
.Select(h => $"{h.Key}={h.Value}"))); .Select(h => $"{h.Key}={h.Value}")));
@@ -2746,11 +2617,11 @@ public class JellyfinController : ControllerBase
if (result != null) if (result != null)
{ {
_logger.LogDebug("✓ Session request proxied successfully ({StatusCode})", statusCode); _logger.LogInformation("✓ Session request proxied successfully ({StatusCode})", statusCode);
return new JsonResult(result.RootElement.Clone()); return new JsonResult(result.RootElement.Clone());
} }
_logger.LogDebug("✓ Session request proxied ({StatusCode}, no body)", statusCode); _logger.LogInformation("✓ Session request proxied ({StatusCode}, no body)", statusCode);
return StatusCode(statusCode); return StatusCode(statusCode);
} }
catch (Exception ex) catch (Exception ex)
@@ -2808,7 +2679,7 @@ public class JellyfinController : ControllerBase
if (path.Contains("session", StringComparison.OrdinalIgnoreCase) || if (path.Contains("session", StringComparison.OrdinalIgnoreCase) ||
path.Contains("capabilit", StringComparison.OrdinalIgnoreCase)) path.Contains("capabilit", StringComparison.OrdinalIgnoreCase))
{ {
_logger.LogDebug("🔍 SESSION/CAPABILITY REQUEST: {Method} /{Path}{Query}", Request.Method, path, Request.QueryString); _logger.LogWarning("🔍 SESSION/CAPABILITY REQUEST: {Method} /{Path}{Query}", Request.Method, path, Request.QueryString);
} }
else else
{ {
@@ -3822,7 +3693,8 @@ public class JellyfinController : ControllerBase
{ {
Song = song, Song = song,
TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, song.Title), TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, song.Title),
ArtistScore = FuzzyMatcher.CalculateArtistMatchScore(track.Artists, song.Artist, song.Contributors) // Calculate artist score by checking ALL artists match
ArtistScore = CalculateArtistMatchScore(track.Artists, song.Artist, song.Contributors)
}) })
.Select(x => new .Select(x => new
{ {
@@ -3915,8 +3787,8 @@ public class JellyfinController : ControllerBase
return; return;
} }
// Build kept folder path: Artist/Album/ // Build kept folder path: /app/kept/Artist/Album/
var keptBasePath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept"); var keptBasePath = "/app/kept";
var keptArtistPath = Path.Combine(keptBasePath, PathHelper.SanitizeFileName(song.Artist)); var keptArtistPath = Path.Combine(keptBasePath, PathHelper.SanitizeFileName(song.Artist));
var keptAlbumPath = Path.Combine(keptArtistPath, PathHelper.SanitizeFileName(song.Album)); var keptAlbumPath = Path.Combine(keptArtistPath, PathHelper.SanitizeFileName(song.Album));
@@ -4212,7 +4084,7 @@ public class JellyfinController : ControllerBase
var song = await _metadataService.GetSongAsync(provider!, externalId!); var song = await _metadataService.GetSongAsync(provider!, externalId!);
if (song == null) return; if (song == null) return;
var keptBasePath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept"); var keptBasePath = "/app/kept";
var keptArtistPath = Path.Combine(keptBasePath, PathHelper.SanitizeFileName(song.Artist)); var keptArtistPath = Path.Combine(keptBasePath, PathHelper.SanitizeFileName(song.Artist));
var keptAlbumPath = Path.Combine(keptArtistPath, PathHelper.SanitizeFileName(song.Album)); var keptAlbumPath = Path.Combine(keptArtistPath, PathHelper.SanitizeFileName(song.Album));
@@ -4513,24 +4385,6 @@ public class JellyfinController : ControllerBase
return (deviceId, client, device, version); return (deviceId, client, device, version);
} }
/// <summary>
/// Generates a deterministic UUID (v5) from a string.
/// This allows us to create consistent UUIDs for external track IDs.
/// </summary>
private string GenerateUuidFromString(string input)
{
// Use MD5 hash to generate a deterministic UUID
using var md5 = System.Security.Cryptography.MD5.Create();
var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
// Convert to UUID format (version 5, namespace-based)
hash[6] = (byte)((hash[6] & 0x0F) | 0x50); // Version 5
hash[8] = (byte)((hash[8] & 0x3F) | 0x80); // Variant
var guid = new Guid(hash);
return guid.ToString();
}
/// <summary> /// <summary>
/// Finds the Spotify ID for an external track by searching through all playlist matched tracks caches. /// Finds the Spotify ID for an external track by searching through all playlist matched tracks caches.
/// This allows us to get Spotify lyrics for external tracks that were matched from Spotify playlists. /// This allows us to get Spotify lyrics for external tracks that were matched from Spotify playlists.
@@ -4575,6 +4429,122 @@ public class JellyfinController : ControllerBase
return null; return null;
} }
} }
/// <summary>
/// Converts an external track URL (Tidal/Deezer/Qobuz) to a Spotify track ID using Odesli/song.link API.
/// This enables Spotify lyrics for external tracks that aren't in injected playlists.
/// </summary>
private async Task<string?> ConvertToSpotifyIdViaOdesliAsync(Song song, string provider, string externalId)
{
try
{
// Build the source URL based on provider
string? sourceUrl = null;
switch (provider.ToLowerInvariant())
{
case "squidwtf":
// SquidWTF uses Tidal IDs
sourceUrl = $"https://tidal.com/browse/track/{externalId}";
break;
case "deezer":
sourceUrl = $"https://www.deezer.com/track/{externalId}";
break;
case "qobuz":
sourceUrl = $"https://www.qobuz.com/us-en/album/-/-/{externalId}";
break;
default:
_logger.LogDebug("Provider {Provider} not supported for Odesli conversion", provider);
return null;
}
// Check cache first (cache for 30 days since these mappings don't change)
var cacheKey = $"odesli:{provider}:{externalId}";
var cachedSpotifyId = await _cache.GetStringAsync(cacheKey);
if (!string.IsNullOrEmpty(cachedSpotifyId))
{
_logger.LogDebug("Returning cached Odesli conversion: {Provider}/{ExternalId} → {SpotifyId}",
provider, externalId, cachedSpotifyId);
return cachedSpotifyId;
}
// RATE LIMITING: Odesli allows 10 requests per minute
// Use a simple semaphore-based rate limiter
await OdesliRateLimiter.WaitAsync();
try
{
// Call Odesli API
var odesliUrl = $"https://api.song.link/v1-alpha.1/links?url={Uri.EscapeDataString(sourceUrl)}&userCountry=US";
_logger.LogDebug("Calling Odesli API: {Url}", odesliUrl);
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(5);
var response = await httpClient.GetAsync(odesliUrl);
if (!response.IsSuccessStatusCode)
{
_logger.LogDebug("Odesli API returned {StatusCode} for {Provider}/{ExternalId}",
response.StatusCode, provider, externalId);
return null;
}
var json = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
// Extract Spotify URL from linksByPlatform.spotify.url
if (root.TryGetProperty("linksByPlatform", out var platforms) &&
platforms.TryGetProperty("spotify", out var spotify) &&
spotify.TryGetProperty("url", out var spotifyUrlEl))
{
var spotifyUrl = spotifyUrlEl.GetString();
if (!string.IsNullOrEmpty(spotifyUrl))
{
// Extract Spotify ID from URL: https://open.spotify.com/track/{id}
var match = System.Text.RegularExpressions.Regex.Match(spotifyUrl, @"spotify\.com/track/([a-zA-Z0-9]+)");
if (match.Success)
{
var spotifyId = match.Groups[1].Value;
// Cache the result (30 days)
await _cache.SetStringAsync(cacheKey, spotifyId, TimeSpan.FromDays(30));
_logger.LogInformation("✓ Odesli converted {Provider}/{ExternalId} → Spotify ID {SpotifyId}",
provider, externalId, spotifyId);
return spotifyId;
}
}
}
_logger.LogDebug("No Spotify link found in Odesli response for {Provider}/{ExternalId}", provider, externalId);
return null;
}
finally
{
// Release rate limiter after 6 seconds (10 requests per 60 seconds = 1 request per 6 seconds)
_ = Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(6));
OdesliRateLimiter.Release();
});
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Error converting {Provider}/{ExternalId} via Odesli", provider, externalId);
return null;
}
}
// Static rate limiter for Odesli API (10 requests per minute = 1 request per 6 seconds)
private static readonly SemaphoreSlim OdesliRateLimiter = new SemaphoreSlim(10, 10);
} }
// force rebuild Sun Jan 25 13:22:47 EST 2026 // force rebuild Sun Jan 25 13:22:47 EST 2026

View File

@@ -2,44 +2,239 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using allstarr.Models.Settings; using allstarr.Models.Settings;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace allstarr.Filters; namespace allstarr.Filters;
/// <summary> /// <summary>
/// REMOVED: Authentication filter for Jellyfin API endpoints. /// Authentication filter for Jellyfin API endpoints.
/// /// Validates client credentials against configured username and API key.
/// This filter has been removed because Allstarr acts as a TRANSPARENT PROXY. /// Clients can authenticate via:
/// Clients authenticate directly with Jellyfin through the proxy, not with the proxy itself. /// - Authorization header: MediaBrowser Token="apikey"
/// /// - X-Emby-Token header
/// Authentication flow: /// - Query parameter: api_key
/// 1. Client sends credentials to /Users/AuthenticateByName /// - JSON body (for login endpoints): Username/Pw fields
/// 2. Proxy forwards request to Jellyfin (no validation)
/// 3. Jellyfin validates credentials and returns AccessToken
/// 4. Client uses AccessToken in subsequent requests
/// 5. Proxy forwards token to Jellyfin for validation
///
/// The proxy NEVER validates credentials or tokens - that's Jellyfin's job.
/// The proxy only forwards authentication headers transparently.
///
/// If you need to restrict access to the proxy itself, use network-level controls
/// (firewall, VPN, reverse proxy with auth) instead of application-level auth.
/// </summary> /// </summary>
public class JellyfinAuthFilter : IAsyncActionFilter public partial class JellyfinAuthFilter : IAsyncActionFilter
{ {
private readonly JellyfinSettings _settings;
private readonly ILogger<JellyfinAuthFilter> _logger; private readonly ILogger<JellyfinAuthFilter> _logger;
public JellyfinAuthFilter(ILogger<JellyfinAuthFilter> logger) public JellyfinAuthFilter(
IOptions<JellyfinSettings> settings,
ILogger<JellyfinAuthFilter> logger)
{ {
_settings = settings.Value;
_logger = logger; _logger = logger;
} }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{ {
// This filter is now a no-op - all authentication is handled by Jellyfin // Skip auth if no credentials configured (open mode)
// Keeping the class for backwards compatibility but it does nothing if (string.IsNullOrEmpty(_settings.ClientUsername) || string.IsNullOrEmpty(_settings.ApiKey))
{
_logger.LogDebug("Auth skipped - no client credentials configured");
await next();
return;
}
_logger.LogTrace("JellyfinAuthFilter: Transparent proxy mode - no authentication check"); var request = context.HttpContext.Request;
// Try to extract credentials from various sources
var (username, token) = await ExtractCredentialsAsync(request);
// Validate credentials
if (!ValidateCredentials(username, token))
{
_logger.LogWarning("Authentication failed for user '{Username}' from {IP}",
username ?? "unknown",
context.HttpContext.Connection.RemoteIpAddress);
context.Result = new UnauthorizedObjectResult(new
{
error = "Invalid credentials",
message = "Authentication required. Provide valid username and API key."
});
return;
}
_logger.LogDebug("Authentication successful for user '{Username}'", username);
await next(); await next();
} }
private async Task<(string? username, string? token)> ExtractCredentialsAsync(HttpRequest request)
{
string? username = null;
string? token = null;
// 1. Check Authorization header (MediaBrowser format)
if (request.Headers.TryGetValue("Authorization", out var authHeader))
{
var authValue = authHeader.ToString();
// Parse MediaBrowser auth header: MediaBrowser Client="...", Token="..."
if (authValue.StartsWith("MediaBrowser", StringComparison.OrdinalIgnoreCase))
{
token = ExtractTokenFromMediaBrowser(authValue);
username = ExtractUserIdFromMediaBrowser(authValue);
}
// Basic auth: Basic base64(username:password)
else if (authValue.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase))
{
(username, token) = ParseBasicAuth(authValue);
}
}
// 2. Check X-Emby-Token header
if (string.IsNullOrEmpty(token) && request.Headers.TryGetValue("X-Emby-Token", out var embyToken))
{
token = embyToken.ToString();
}
// 3. Check X-MediaBrowser-Token header
if (string.IsNullOrEmpty(token) && request.Headers.TryGetValue("X-MediaBrowser-Token", out var mbToken))
{
token = mbToken.ToString();
}
// 4. Check X-Emby-Authorization header (alternative format)
if (string.IsNullOrEmpty(token) && request.Headers.TryGetValue("X-Emby-Authorization", out var embyAuth))
{
token = ExtractTokenFromMediaBrowser(embyAuth.ToString());
if (string.IsNullOrEmpty(username))
{
username = ExtractUserIdFromMediaBrowser(embyAuth.ToString());
}
}
// 5. Check query parameters
if (string.IsNullOrEmpty(token))
{
token = request.Query["api_key"].FirstOrDefault()
?? request.Query["ApiKey"].FirstOrDefault()
?? request.Query["X-Emby-Token"].FirstOrDefault();
}
if (string.IsNullOrEmpty(username))
{
username = request.Query["userId"].FirstOrDefault()
?? request.Query["UserId"].FirstOrDefault()
?? request.Query["u"].FirstOrDefault();
}
// 6. Check JSON body for login endpoints (Jellyfin: Username/Pw, Navidrome: username/password)
if ((string.IsNullOrEmpty(username) || string.IsNullOrEmpty(token)) &&
request.ContentType?.Contains("application/json", StringComparison.OrdinalIgnoreCase) == true &&
request.ContentLength > 0)
{
var (bodyUsername, bodyPassword) = await ExtractCredentialsFromBodyAsync(request);
if (string.IsNullOrEmpty(username)) username = bodyUsername;
if (string.IsNullOrEmpty(token)) token = bodyPassword;
}
return (username, token);
}
private async Task<(string? username, string? password)> ExtractCredentialsFromBodyAsync(HttpRequest request)
{
try
{
request.EnableBuffering();
request.Body.Position = 0;
using var reader = new StreamReader(request.Body, leaveOpen: true);
var body = await reader.ReadToEndAsync();
request.Body.Position = 0;
if (string.IsNullOrEmpty(body)) return (null, null);
using var doc = JsonDocument.Parse(body);
var root = doc.RootElement;
// Try Jellyfin format: Username, Pw
string? username = null;
string? password = null;
if (root.TryGetProperty("Username", out var usernameProp))
username = usernameProp.GetString();
else if (root.TryGetProperty("username", out var usernameLowerProp))
username = usernameLowerProp.GetString();
if (root.TryGetProperty("Pw", out var pwProp))
password = pwProp.GetString();
else if (root.TryGetProperty("pw", out var pwLowerProp))
password = pwLowerProp.GetString();
else if (root.TryGetProperty("Password", out var passwordProp))
password = passwordProp.GetString();
else if (root.TryGetProperty("password", out var passwordLowerProp))
password = passwordLowerProp.GetString();
return (username, password);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Failed to parse credentials from request body");
return (null, null);
}
}
private string? ExtractTokenFromMediaBrowser(string header)
{
var match = TokenRegex().Match(header);
return match.Success ? match.Groups[1].Value : null;
}
private string? ExtractUserIdFromMediaBrowser(string header)
{
var match = UserIdRegex().Match(header);
return match.Success ? match.Groups[1].Value : null;
}
private static (string? username, string? password) ParseBasicAuth(string authHeader)
{
try
{
var base64 = authHeader["Basic ".Length..].Trim();
var bytes = Convert.FromBase64String(base64);
var credentials = System.Text.Encoding.UTF8.GetString(bytes);
var parts = credentials.Split(':', 2);
return parts.Length == 2 ? (parts[0], parts[1]) : (null, null);
}
catch
{
return (null, null);
}
}
private bool ValidateCredentials(string? username, string? token)
{
// Must have token (API key used as password)
if (string.IsNullOrEmpty(token))
{
return false;
}
// Token must match API key
if (!string.Equals(token, _settings.ApiKey, StringComparison.Ordinal))
{
return false;
}
// If username provided, it must match configured client username
if (!string.IsNullOrEmpty(username) &&
!string.Equals(username, _settings.ClientUsername, StringComparison.OrdinalIgnoreCase))
{
return false;
}
return true;
}
[GeneratedRegex(@"Token=""([^""]+)""", RegexOptions.IgnoreCase)]
private static partial Regex TokenRegex();
[GeneratedRegex(@"UserId=""([^""]+)""", RegexOptions.IgnoreCase)]
private static partial Regex UserIdRegex();
} }

View File

@@ -54,7 +54,7 @@ public class WebSocketProxyMiddleware
if (context.Request.Path.StartsWithSegments("/socket", StringComparison.OrdinalIgnoreCase) && if (context.Request.Path.StartsWithSegments("/socket", StringComparison.OrdinalIgnoreCase) &&
context.WebSockets.IsWebSocketRequest) context.WebSockets.IsWebSocketRequest)
{ {
_logger.LogDebug("🔌 WEBSOCKET: WebSocket connection request received from {RemoteIp}", _logger.LogInformation("🔌 WEBSOCKET: WebSocket connection request received from {RemoteIp}",
context.Connection.RemoteIpAddress); context.Connection.RemoteIpAddress);
await HandleWebSocketProxyAsync(context); await HandleWebSocketProxyAsync(context);
@@ -142,7 +142,7 @@ public class WebSocketProxyMiddleware
serverWebSocket.Options.SetRequestHeader("User-Agent", "Allstarr/1.0"); serverWebSocket.Options.SetRequestHeader("User-Agent", "Allstarr/1.0");
await serverWebSocket.ConnectAsync(new Uri(jellyfinWsUrl), context.RequestAborted); await serverWebSocket.ConnectAsync(new Uri(jellyfinWsUrl), context.RequestAborted);
_logger.LogDebug("✓ WEBSOCKET: Connected to Jellyfin WebSocket"); _logger.LogInformation("✓ WEBSOCKET: Connected to Jellyfin WebSocket");
// Start bidirectional proxying // Start bidirectional proxying
var clientToServer = ProxyMessagesAsync(clientWebSocket, serverWebSocket, "Client→Server", context.RequestAborted); var clientToServer = ProxyMessagesAsync(clientWebSocket, serverWebSocket, "Client→Server", context.RequestAborted);
@@ -155,15 +155,7 @@ public class WebSocketProxyMiddleware
} }
catch (WebSocketException wsEx) catch (WebSocketException wsEx)
{ {
// 403 is expected when tokens expire or session ends - don't spam logs _logger.LogWarning(wsEx, "⚠️ WEBSOCKET: WebSocket error: {Message}", wsEx.Message);
if (wsEx.Message.Contains("403"))
{
_logger.LogDebug("WEBSOCKET: Connection rejected with 403 (token expired or session ended)");
}
else
{
_logger.LogWarning(wsEx, "⚠️ WEBSOCKET: WebSocket error: {Message}", wsEx.Message);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -202,7 +194,7 @@ public class WebSocketProxyMiddleware
// CRITICAL: Notify session manager that client disconnected // CRITICAL: Notify session manager that client disconnected
if (!string.IsNullOrEmpty(deviceId)) if (!string.IsNullOrEmpty(deviceId))
{ {
_logger.LogDebug("🧹 WEBSOCKET: Client disconnected, removing session for device {DeviceId}", deviceId); _logger.LogInformation("🧹 WEBSOCKET: Client disconnected, removing session for device {DeviceId}", deviceId);
await _sessionManager.RemoveSessionAsync(deviceId); await _sessionManager.RemoveSessionAsync(deviceId);
} }
@@ -247,7 +239,7 @@ public class WebSocketProxyMiddleware
if (direction == "Server→Client") if (direction == "Server→Client")
{ {
var messageText = System.Text.Encoding.UTF8.GetString(messageBytes); var messageText = System.Text.Encoding.UTF8.GetString(messageBytes);
_logger.LogTrace("📥 WEBSOCKET {Direction}: {Preview}", _logger.LogInformation("📥 WEBSOCKET {Direction}: {Preview}",
direction, direction,
messageText.Length > 500 ? messageText[..500] + "..." : messageText); messageText.Length > 500 ? messageText[..500] + "..." : messageText);
} }
@@ -282,7 +274,7 @@ public class WebSocketProxyMiddleware
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogDebug(ex, "WEBSOCKET {Direction}: Error proxying messages (connection closed)", direction); _logger.LogWarning(ex, "⚠️ WEBSOCKET {Direction}: Error proxying messages", direction);
} }
} }
} }

View File

@@ -44,11 +44,6 @@ public class Song
/// </summary> /// </summary>
public string? Isrc { get; set; } public string? Isrc { get; set; }
/// <summary>
/// Spotify track ID (for lyrics and matching)
/// </summary>
public string? SpotifyId { get; set; }
/// <summary> /// <summary>
/// Full release date (format: YYYY-MM-DD) /// Full release date (format: YYYY-MM-DD)
/// </summary> /// </summary>

View File

@@ -59,6 +59,27 @@ public class SpotifyImportSettings
/// </summary> /// </summary>
public bool Enabled { get; set; } public bool Enabled { get; set; }
/// <summary>
/// Hour when Spotify Import plugin runs (24-hour format, 0-23)
/// NOTE: This setting is now optional and only used for the sync window check.
/// The fetcher will search backwards from current time for the last 48 hours,
/// so timezone confusion is avoided.
/// </summary>
public int SyncStartHour { get; set; } = 16;
/// <summary>
/// Minute when Spotify Import plugin runs (0-59)
/// NOTE: This setting is now optional and only used for the sync window check.
/// </summary>
public int SyncStartMinute { get; set; } = 15;
/// <summary>
/// How many hours to search for missing tracks files after sync start time
/// This prevents the fetcher from running too frequently.
/// Set to 0 to disable the sync window check and always search on startup.
/// </summary>
public int SyncWindowHours { get; set; } = 2;
/// <summary> /// <summary>
/// How often to run track matching in hours. /// How often to run track matching in hours.
/// Spotify playlists like Discover Weekly update once per week, Release Radar updates weekly. /// Spotify playlists like Discover Weekly update once per week, Release Radar updates weekly.

View File

@@ -22,21 +22,16 @@ static List<string> DecodeSquidWtfUrls()
{ {
var encodedUrls = new[] var encodedUrls = new[]
{ {
"aHR0cHM6Ly90cml0b24uc3F1aWQud3Rm", // triton.squid.wtf "aHR0cHM6Ly90cml0b24uc3F1aWQud3Rm", // triton
"aHR0cHM6Ly90aWRhbC1hcGkuYmluaW11bS5vcmc=", // tidal-api.binimum.org "aHR0cHM6Ly90aWRhbC1hcGkuYmluaW11bS5vcmc=", // binimum
"aHR0cHM6Ly90aWRhbC5raW5vcGx1cy5vbmxpbmU=", // tidal.kinoplus.online "aHR0cHM6Ly90aWRhbC5raW5vcGx1cy5vbmxpbmU=", // kinoplus
"aHR0cHM6Ly9oaWZpLXR3by5zcG90aXNhdmVyLm5ldA==", // hifi-two.spotisaver.net "aHR0cHM6Ly9oaWZpLXR3by5zcG90aXNhdmVyLm5ldA==", // spoti-2
"aHR0cHM6Ly9oaWZpLW9uZS5zcG90aXNhdmVyLm5ldA==", // hifi-one.spotisaver.net "aHR0cHM6Ly9oaWZpLW9uZS5zcG90aXNhdmVyLm5ldA==", // spoti-1
"aHR0cHM6Ly93b2xmLnFxZGwuc2l0ZQ==", // wolf.qqdl.site "aHR0cHM6Ly93b2xmLnFxZGwuc2l0ZQ==", // wolf
"aHR0cDovL2h1bmQucXFkbC5zaXRl", // hund.qqdl.site (http) "aHR0cDovL2h1bmQucXFkbC5zaXRl", // hund
"aHR0cHM6Ly9rYXR6ZS5xcWRsLnNpdGU=", // katze.qqdl.site "aHR0cHM6Ly9rYXR6ZS5xcWRsLnNpdGU=", // katze
"aHR0cHM6Ly92b2dlbC5xcWRsLnNpdGU=", // vogel.qqdl.site "aHR0cHM6Ly92b2dlbC5xcWRsLnNpdGU=", // vogel
"aHR0cHM6Ly9tYXVzLnFxZGwuc2l0ZQ==", // maus.qqdl.site "aHR0cHM6Ly9tYXVzLnFxZGwuc2l0ZQ==" // maus
"aHR0cHM6Ly9ldS1jZW50cmFsLm1vbm9jaHJvbWUudGY=", // eu-central.monochrome.tf
"aHR0cHM6Ly91cy13ZXN0Lm1vbm9jaHJvbWUudGY=", // us-west.monochrome.tf
"aHR0cHM6Ly9hcnJhbi5tb25vY2hyb21lLnRm", // arran.monochrome.tf
"aHR0cHM6Ly9hcGkubW9ub2Nocm9tZS50Zg==", // api.monochrome.tf
"aHR0cHM6Ly9odW5kLnFxZGwuc2l0ZQ==" // hund.qqdl.site (https)
}; };
return encodedUrls return encodedUrls
@@ -358,7 +353,7 @@ builder.Services.Configure<SpotifyImportSettings>(options =>
} }
// Log configuration at startup // Log configuration at startup
Console.WriteLine($"Spotify Import: Enabled={options.Enabled}, MatchingInterval={options.MatchingIntervalHours}h"); Console.WriteLine($"Spotify Import: Enabled={options.Enabled}, SyncHour={options.SyncStartHour}:{options.SyncStartMinute:D2}, WindowHours={options.SyncWindowHours}");
Console.WriteLine($"Spotify Import Playlists: {options.Playlists.Count} configured"); Console.WriteLine($"Spotify Import Playlists: {options.Playlists.Count} configured");
foreach (var playlist in options.Playlists) foreach (var playlist in options.Playlists)
{ {
@@ -384,7 +379,6 @@ else
// Business services - shared across backends // Business services - shared across backends
builder.Services.AddSingleton<RedisCacheService>(); builder.Services.AddSingleton<RedisCacheService>();
builder.Services.AddSingleton<OdesliService>();
builder.Services.AddSingleton<ILocalLibraryService, LocalLibraryService>(); builder.Services.AddSingleton<ILocalLibraryService, LocalLibraryService>();
builder.Services.AddSingleton<LrclibService>(); builder.Services.AddSingleton<LrclibService>();
@@ -465,7 +459,6 @@ else if (musicService == MusicService.SquidWTF)
sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<SquidWTFSettings>>(), sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<SquidWTFSettings>>(),
sp, sp,
sp.GetRequiredService<ILogger<SquidWTFDownloadService>>(), sp.GetRequiredService<ILogger<SquidWTFDownloadService>>(),
sp.GetRequiredService<OdesliService>(),
squidWtfApiUrls)); squidWtfApiUrls));
} }
@@ -482,18 +475,13 @@ else
builder.Services.AddSingleton<IStartupValidator, SubsonicStartupValidator>(); builder.Services.AddSingleton<IStartupValidator, SubsonicStartupValidator>();
} }
// Register endpoint benchmark service
builder.Services.AddSingleton<EndpointBenchmarkService>();
builder.Services.AddSingleton<IStartupValidator, DeezerStartupValidator>(); builder.Services.AddSingleton<IStartupValidator, DeezerStartupValidator>();
builder.Services.AddSingleton<IStartupValidator, QobuzStartupValidator>(); builder.Services.AddSingleton<IStartupValidator, QobuzStartupValidator>();
builder.Services.AddSingleton<IStartupValidator>(sp => builder.Services.AddSingleton<IStartupValidator>(sp =>
new SquidWTFStartupValidator( new SquidWTFStartupValidator(
sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<SquidWTFSettings>>(), sp.GetRequiredService<Microsoft.Extensions.Options.IOptions<SquidWTFSettings>>(),
sp.GetRequiredService<IHttpClientFactory>().CreateClient(), sp.GetRequiredService<IHttpClientFactory>().CreateClient(),
squidWtfApiUrls, squidWtfApiUrls));
sp.GetRequiredService<EndpointBenchmarkService>(),
sp.GetRequiredService<ILogger<SquidWTFStartupValidator>>()));
builder.Services.AddSingleton<IStartupValidator, LyricsStartupValidator>(); builder.Services.AddSingleton<IStartupValidator, LyricsStartupValidator>();
// Register orchestrator as hosted service // Register orchestrator as hosted service
@@ -580,9 +568,8 @@ builder.Services.AddSingleton<allstarr.Services.Spotify.SpotifyTrackMatchingServ
builder.Services.AddHostedService(sp => sp.GetRequiredService<allstarr.Services.Spotify.SpotifyTrackMatchingService>()); builder.Services.AddHostedService(sp => sp.GetRequiredService<allstarr.Services.Spotify.SpotifyTrackMatchingService>());
// Register lyrics prefetch service (prefetches lyrics for all playlist tracks) // Register lyrics prefetch service (prefetches lyrics for all playlist tracks)
// DISABLED - No need to prefetch since Jellyfin and Spotify lyrics are fast builder.Services.AddSingleton<allstarr.Services.Lyrics.LyricsPrefetchService>();
// builder.Services.AddSingleton<allstarr.Services.Lyrics.LyricsPrefetchService>(); builder.Services.AddHostedService(sp => sp.GetRequiredService<allstarr.Services.Lyrics.LyricsPrefetchService>());
// builder.Services.AddHostedService(sp => sp.GetRequiredService<allstarr.Services.Lyrics.LyricsPrefetchService>());
// Register MusicBrainz service for metadata enrichment // Register MusicBrainz service for metadata enrichment
builder.Services.Configure<allstarr.Models.Settings.MusicBrainzSettings>(options => builder.Services.Configure<allstarr.Models.Settings.MusicBrainzSettings>(options =>

View File

@@ -31,11 +31,6 @@ public abstract class BaseDownloadService : IDownloadService
protected readonly ConcurrentDictionary<string, DownloadInfo> ActiveDownloads = new(); protected readonly ConcurrentDictionary<string, DownloadInfo> ActiveDownloads = new();
protected readonly SemaphoreSlim DownloadLock = new(1, 1); protected readonly SemaphoreSlim DownloadLock = new(1, 1);
// Rate limiting fields
private readonly SemaphoreSlim _requestLock = new(1, 1);
private DateTime _lastRequestTime = DateTime.MinValue;
private readonly int _minRequestIntervalMs = 200;
/// <summary> /// <summary>
/// Lazy-loaded PlaylistSyncService to avoid circular dependency /// Lazy-loaded PlaylistSyncService to avoid circular dependency
/// </summary> /// </summary>
@@ -95,88 +90,22 @@ public abstract class BaseDownloadService : IDownloadService
public async Task<Stream> DownloadAndStreamAsync(string externalProvider, string externalId, CancellationToken cancellationToken = default) public async Task<Stream> DownloadAndStreamAsync(string externalProvider, string externalId, CancellationToken cancellationToken = default)
{ {
var startTime = DateTime.UtcNow;
// Check if already downloaded locally // Check if already downloaded locally
var localPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId); var localPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId);
if (localPath != null && IOFile.Exists(localPath)) if (localPath != null && IOFile.Exists(localPath))
{ {
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds; Logger.LogInformation("Streaming from local cache: {Path}", localPath);
Logger.LogInformation("Streaming from local cache ({ElapsedMs}ms): {Path}", elapsed, localPath);
// Update access time for cache cleanup
if (SubsonicSettings.StorageMode == StorageMode.Cache)
{
IOFile.SetLastAccessTime(localPath, DateTime.UtcNow);
}
// Start background Odesli conversion for lyrics (if not already cached)
StartBackgroundOdesliConversion(externalProvider, externalId);
return IOFile.OpenRead(localPath); return IOFile.OpenRead(localPath);
} }
// Download to disk first to ensure complete file with metadata // For on-demand streaming, download to disk first to ensure complete file
// This is necessary because: // This is necessary because:
// 1. Clients may seek to arbitrary positions (requires full file) // 1. Clients may seek to arbitrary positions (requires full file)
// 2. Metadata embedding requires complete file // 2. Metadata embedding requires complete file
// 3. Caching for future plays // 3. Caching for future plays
Logger.LogInformation("Downloading song for streaming: {Provider}:{ExternalId}", externalProvider, externalId); Logger.LogInformation("Downloading song for streaming: {Provider}:{ExternalId}", externalProvider, externalId);
localPath = await DownloadSongInternalAsync(externalProvider, externalId, triggerAlbumDownload: true, cancellationToken);
try return IOFile.OpenRead(localPath);
{
localPath = await DownloadSongInternalAsync(externalProvider, externalId, triggerAlbumDownload: true, cancellationToken);
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
Logger.LogInformation("Download completed, starting stream ({ElapsedMs}ms total): {Path}", elapsed, localPath);
// Start background Odesli conversion for lyrics (after stream starts)
StartBackgroundOdesliConversion(externalProvider, externalId);
return IOFile.OpenRead(localPath);
}
catch (OperationCanceledException)
{
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
Logger.LogWarning("Download cancelled by client after {ElapsedMs}ms for {Provider}:{ExternalId}", elapsed, externalProvider, externalId);
throw;
}
catch (Exception ex)
{
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
Logger.LogError(ex, "Download failed after {ElapsedMs}ms for {Provider}:{ExternalId}", elapsed, externalProvider, externalId);
throw;
}
}
/// <summary>
/// Starts background Odesli conversion for lyrics support.
/// This is called AFTER streaming starts so it doesn't block the client.
/// </summary>
private void StartBackgroundOdesliConversion(string externalProvider, string externalId)
{
_ = Task.Run(async () =>
{
try
{
// Provider-specific conversion (override in subclasses if needed)
await ConvertToSpotifyIdAsync(externalProvider, externalId);
}
catch (Exception ex)
{
Logger.LogDebug(ex, "Background Spotify ID conversion failed for {Provider}:{ExternalId}", externalProvider, externalId);
}
});
}
/// <summary>
/// Converts external track ID to Spotify ID for lyrics support.
/// Override in provider-specific services if needed.
/// </summary>
protected virtual Task ConvertToSpotifyIdAsync(string externalProvider, string externalId)
{
// Default implementation does nothing
// Provider-specific services can override this
return Task.CompletedTask;
} }
public DownloadInfo? GetDownloadStatus(string songId) public DownloadInfo? GetDownloadStatus(string songId)
@@ -192,13 +121,20 @@ public abstract class BaseDownloadService : IDownloadService
return null; return null;
} }
// Check local library (works for both cache and permanent storage) // Check local library
var localPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId); var localPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId);
if (localPath != null && IOFile.Exists(localPath)) if (localPath != null && IOFile.Exists(localPath))
{ {
return localPath; return localPath;
} }
// Check cache directory
var cachedPath = GetCachedFilePath(externalProvider, externalId);
if (cachedPath != null && IOFile.Exists(cachedPath))
{
return cachedPath;
}
return null; return null;
} }
@@ -267,44 +203,47 @@ public abstract class BaseDownloadService : IDownloadService
try try
{ {
// Check if already downloaded (works for both cache and permanent modes) // Check if already downloaded (skip for cache mode as we want to check cache folder)
var existingPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId); if (!isCache)
if (existingPath != null && IOFile.Exists(existingPath))
{ {
Logger.LogInformation("Song already downloaded: {Path}", existingPath); var existingPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId);
if (existingPath != null && IOFile.Exists(existingPath))
// For cache mode, update file access time for cache cleanup logic
if (isCache)
{ {
IOFile.SetLastAccessTime(existingPath, DateTime.UtcNow); Logger.LogInformation("Song already downloaded: {Path}", existingPath);
return existingPath;
}
}
else
{
// For cache mode, check if file exists in cache directory
var cachedPath = GetCachedFilePath(externalProvider, externalId);
if (cachedPath != null && IOFile.Exists(cachedPath))
{
Logger.LogInformation("Song found in cache: {Path}", cachedPath);
// Update file access time for cache cleanup logic
IOFile.SetLastAccessTime(cachedPath, DateTime.UtcNow);
return cachedPath;
} }
return existingPath;
} }
// Check if download in progress // Check if download in progress
if (ActiveDownloads.TryGetValue(songId, out var activeDownload) && activeDownload.Status == DownloadStatus.InProgress) if (ActiveDownloads.TryGetValue(songId, out var activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
{ {
Logger.LogDebug("Download already in progress for {SongId}, waiting for completion...", songId); Logger.LogInformation("Download already in progress for {SongId}, waiting...", songId);
// Release lock while waiting // Release lock while waiting
DownloadLock.Release(); DownloadLock.Release();
// Wait for download to complete, checking every 100ms (faster than 500ms)
// Also respect cancellation token so client timeouts are handled immediately
while (ActiveDownloads.TryGetValue(songId, out activeDownload) && activeDownload.Status == DownloadStatus.InProgress) while (ActiveDownloads.TryGetValue(songId, out activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
{ {
cancellationToken.ThrowIfCancellationRequested(); await Task.Delay(500, cancellationToken);
await Task.Delay(100, cancellationToken);
} }
if (activeDownload?.Status == DownloadStatus.Completed && activeDownload.LocalPath != null) if (activeDownload?.Status == DownloadStatus.Completed && activeDownload.LocalPath != null)
{ {
Logger.LogDebug("Download completed while waiting, returning path: {Path}", activeDownload.LocalPath);
return activeDownload.LocalPath; return activeDownload.LocalPath;
} }
// Download failed or was cancelled throw new Exception(activeDownload?.ErrorMessage ?? "Download failed");
throw new Exception(activeDownload?.ErrorMessage ?? "Download failed while waiting");
} }
// Get metadata // Get metadata
@@ -638,34 +577,29 @@ public abstract class BaseDownloadService : IDownloadService
} }
} }
#endregion
#region Rate Limiting
/// <summary> /// <summary>
/// Queues a request with rate limiting to prevent overwhelming the API. /// Gets the cached file path for a given provider and external ID
/// Ensures minimum interval between requests. /// Returns null if no cached file exists
/// </summary> /// </summary>
protected async Task<T> QueueRequestAsync<T>(Func<Task<T>> action) protected string? GetCachedFilePath(string provider, string externalId)
{ {
await _requestLock.WaitAsync();
try try
{ {
var now = DateTime.UtcNow; // Search for cached files matching the pattern: {provider}_{externalId}.*
var timeSinceLastRequest = (now - _lastRequestTime).TotalMilliseconds; var pattern = $"{provider}_{externalId}.*";
var files = Directory.GetFiles(CachePath, pattern, SearchOption.AllDirectories);
if (timeSinceLastRequest < _minRequestIntervalMs) if (files.Length > 0)
{ {
await Task.Delay((int)(_minRequestIntervalMs - timeSinceLastRequest)); return files[0]; // Return first match
} }
_lastRequestTime = DateTime.UtcNow; return null;
return await action();
} }
finally catch (Exception ex)
{ {
_requestLock.Release(); Logger.LogWarning(ex, "Failed to search for cached file: {Provider}_{ExternalId}", provider, externalId);
return null;
} }
} }

View File

@@ -94,16 +94,16 @@ public class CacheCleanupService : BackgroundService
{ {
var fileInfo = new FileInfo(filePath); var fileInfo = new FileInfo(filePath);
// Use last write time (when file was created/downloaded) to determine if file should be deleted // Use last access time to determine if file should be deleted
// LastAccessTime is unreliable on many filesystems (noatime mount option) // This gets updated when a cached file is streamed
if (fileInfo.LastWriteTimeUtc < cutoffTime) if (fileInfo.LastAccessTimeUtc < cutoffTime)
{ {
var size = fileInfo.Length; var size = fileInfo.Length;
File.Delete(filePath); File.Delete(filePath);
deletedCount++; deletedCount++;
totalSize += size; totalSize += size;
_logger.LogDebug("Deleted cached file: {Path} (age: {Age:F1} hours)", _logger.LogDebug("Deleted cached file: {Path} (last accessed: {LastAccess})",
filePath, (DateTime.UtcNow - fileInfo.LastWriteTimeUtc).TotalHours); filePath, fileInfo.LastAccessTimeUtc);
} }
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -1,135 +0,0 @@
using System.Diagnostics;
namespace allstarr.Services.Common;
/// <summary>
/// Benchmarks API endpoints on startup and maintains performance metrics.
/// Used to prioritize faster endpoints in racing scenarios.
/// </summary>
public class EndpointBenchmarkService
{
private readonly ILogger<EndpointBenchmarkService> _logger;
private readonly Dictionary<string, EndpointMetrics> _metrics = new();
private readonly SemaphoreSlim _lock = new(1, 1);
public EndpointBenchmarkService(ILogger<EndpointBenchmarkService> logger)
{
_logger = logger;
}
/// <summary>
/// Benchmarks a list of endpoints by making test requests.
/// Returns endpoints sorted by average response time (fastest first).
/// </summary>
public async Task<List<string>> BenchmarkEndpointsAsync(
List<string> endpoints,
Func<string, CancellationToken, Task<bool>> testFunc,
int pingCount = 3,
CancellationToken cancellationToken = default)
{
_logger.LogInformation("🏁 Benchmarking {Count} endpoints with {Pings} pings each...", endpoints.Count, pingCount);
var tasks = endpoints.Select(async endpoint =>
{
var sw = Stopwatch.StartNew();
var successCount = 0;
var totalMs = 0L;
for (int i = 0; i < pingCount; i++)
{
try
{
var pingStart = Stopwatch.GetTimestamp();
var success = await testFunc(endpoint, cancellationToken);
var pingMs = Stopwatch.GetElapsedTime(pingStart).TotalMilliseconds;
if (success)
{
successCount++;
totalMs += (long)pingMs;
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Benchmark ping failed for {Endpoint}", endpoint);
}
// Small delay between pings
if (i < pingCount - 1)
{
await Task.Delay(100, cancellationToken);
}
}
sw.Stop();
var avgMs = successCount > 0 ? totalMs / successCount : long.MaxValue;
var metrics = new EndpointMetrics
{
Endpoint = endpoint,
AverageResponseMs = avgMs,
SuccessRate = (double)successCount / pingCount,
LastBenchmark = DateTime.UtcNow
};
await _lock.WaitAsync(cancellationToken);
try
{
_metrics[endpoint] = metrics;
}
finally
{
_lock.Release();
}
_logger.LogInformation(" {Endpoint}: {AvgMs}ms avg, {SuccessRate:P0} success rate",
endpoint, avgMs, metrics.SuccessRate);
return metrics;
}).ToList();
var results = await Task.WhenAll(tasks);
// Sort by: success rate first (must be > 0), then by average response time
var sorted = results
.Where(m => m.SuccessRate > 0)
.OrderByDescending(m => m.SuccessRate)
.ThenBy(m => m.AverageResponseMs)
.Select(m => m.Endpoint)
.ToList();
_logger.LogInformation("✅ Benchmark complete. Fastest: {Fastest} ({Ms}ms)",
sorted.FirstOrDefault() ?? "none",
results.Where(m => m.SuccessRate > 0).MinBy(m => m.AverageResponseMs)?.AverageResponseMs ?? 0);
return sorted;
}
/// <summary>
/// Gets the metrics for a specific endpoint.
/// </summary>
public EndpointMetrics? GetMetrics(string endpoint)
{
_metrics.TryGetValue(endpoint, out var metrics);
return metrics;
}
/// <summary>
/// Gets all endpoint metrics sorted by performance.
/// </summary>
public List<EndpointMetrics> GetAllMetrics()
{
return _metrics.Values
.OrderByDescending(m => m.SuccessRate)
.ThenBy(m => m.AverageResponseMs)
.ToList();
}
}
public class EndpointMetrics
{
public string Endpoint { get; set; } = string.Empty;
public long AverageResponseMs { get; set; }
public double SuccessRate { get; set; }
public DateTime LastBenchmark { get; set; }
}

View File

@@ -221,54 +221,4 @@ public static class FuzzyMatcher
return distance[sourceLength, targetLength]; return distance[sourceLength, targetLength];
} }
/// <summary>
/// Calculates artist match score between Spotify artists and local song artists.
/// Checks bidirectional matching and penalizes mismatches.
/// Penalizes if artist counts don't match or if any artist is missing.
/// Returns score 0-100.
/// </summary>
public static double CalculateArtistMatchScore(List<string> spotifyArtists, string songMainArtist, List<string> songContributors)
{
if (spotifyArtists.Count == 0 || string.IsNullOrEmpty(songMainArtist))
return 0;
// Build list of all song artists (main + contributors)
var allSongArtists = new List<string> { songMainArtist };
allSongArtists.AddRange(songContributors);
// If artist counts differ significantly, penalize
var countDiff = Math.Abs(spotifyArtists.Count - allSongArtists.Count);
if (countDiff > 1) // Allow 1 artist difference (sometimes features are listed differently)
return 0;
// Check that each Spotify artist has a good match in song artists
var spotifyScores = new List<double>();
foreach (var spotifyArtist in spotifyArtists)
{
var bestMatch = allSongArtists.Max(songArtist =>
CalculateSimilarity(spotifyArtist, songArtist));
spotifyScores.Add(bestMatch);
}
// Check that each song artist has a good match in Spotify artists
var songScores = new List<double>();
foreach (var songArtist in allSongArtists)
{
var bestMatch = spotifyArtists.Max(spotifyArtist =>
CalculateSimilarity(songArtist, spotifyArtist));
songScores.Add(bestMatch);
}
// Average all scores - this ensures ALL artists must match well
var allScores = spotifyScores.Concat(songScores);
var avgScore = allScores.Average();
// Penalize if any individual artist match is poor (< 70)
var minScore = allScores.Min();
if (minScore < 70)
avgScore *= 0.7; // 30% penalty for poor individual match
return avgScore;
}
} }

View File

@@ -1,143 +0,0 @@
using System.Text.Json;
using Microsoft.Extensions.Logging;
namespace allstarr.Services.Common;
/// <summary>
/// Service for converting music URLs between platforms using Odesli/song.link API
/// </summary>
public class OdesliService
{
private readonly HttpClient _httpClient;
private readonly ILogger<OdesliService> _logger;
private readonly RedisCacheService _cache;
public OdesliService(
IHttpClientFactory httpClientFactory,
ILogger<OdesliService> logger,
RedisCacheService cache)
{
_httpClient = httpClientFactory.CreateClient();
_logger = logger;
_cache = cache;
}
/// <summary>
/// Converts a Tidal track ID to a Spotify track ID using Odesli
/// Results are cached for 7 days
/// </summary>
public async Task<string?> ConvertTidalToSpotifyIdAsync(string tidalTrackId, CancellationToken cancellationToken = default)
{
// Check cache first (7 day TTL - these mappings don't change)
var cacheKey = $"odesli:tidal-to-spotify:{tidalTrackId}";
var cached = await _cache.GetAsync<string>(cacheKey);
if (!string.IsNullOrEmpty(cached))
{
_logger.LogDebug("✓ Using cached Spotify ID for Tidal track {TidalId}", tidalTrackId);
return cached;
}
try
{
var tidalUrl = $"https://tidal.com/browse/track/{tidalTrackId}";
var odesliUrl = $"https://api.song.link/v1-alpha.1/links?url={Uri.EscapeDataString(tidalUrl)}&userCountry=US";
_logger.LogDebug("🔗 Converting Tidal track {TidalId} to Spotify ID via Odesli", tidalTrackId);
var odesliResponse = await _httpClient.GetAsync(odesliUrl, cancellationToken);
if (odesliResponse.IsSuccessStatusCode)
{
var odesliJson = await odesliResponse.Content.ReadAsStringAsync(cancellationToken);
var odesliDoc = JsonDocument.Parse(odesliJson);
// Extract Spotify track ID from the Spotify URL
if (odesliDoc.RootElement.TryGetProperty("linksByPlatform", out var platforms) &&
platforms.TryGetProperty("spotify", out var spotifyPlatform) &&
spotifyPlatform.TryGetProperty("url", out var spotifyUrlEl))
{
var spotifyUrl = spotifyUrlEl.GetString();
if (!string.IsNullOrEmpty(spotifyUrl))
{
// Extract ID from URL: https://open.spotify.com/track/{id}
var match = System.Text.RegularExpressions.Regex.Match(spotifyUrl, @"spotify\.com/track/([a-zA-Z0-9]+)");
if (match.Success)
{
var spotifyId = match.Groups[1].Value;
_logger.LogInformation("✓ Converted Tidal/{TidalId} → Spotify ID {SpotifyId}", tidalTrackId, spotifyId);
// Cache for 7 days
await _cache.SetAsync(cacheKey, spotifyId, TimeSpan.FromDays(7));
return spotifyId;
}
}
}
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Failed to convert Tidal track to Spotify ID via Odesli");
}
return null;
}
/// <summary>
/// Converts any music URL to a Spotify track ID using Odesli
/// Results are cached for 7 days
/// </summary>
public async Task<string?> ConvertUrlToSpotifyIdAsync(string musicUrl, CancellationToken cancellationToken = default)
{
// Check cache first
var cacheKey = $"odesli:url-to-spotify:{musicUrl}";
var cached = await _cache.GetAsync<string>(cacheKey);
if (!string.IsNullOrEmpty(cached))
{
_logger.LogDebug("✓ Using cached Spotify ID for URL {Url}", musicUrl);
return cached;
}
try
{
var odesliUrl = $"https://api.song.link/v1-alpha.1/links?url={Uri.EscapeDataString(musicUrl)}&userCountry=US";
_logger.LogDebug("🔗 Converting URL to Spotify ID via Odesli: {Url}", musicUrl);
var odesliResponse = await _httpClient.GetAsync(odesliUrl, cancellationToken);
if (odesliResponse.IsSuccessStatusCode)
{
var odesliJson = await odesliResponse.Content.ReadAsStringAsync(cancellationToken);
var odesliDoc = JsonDocument.Parse(odesliJson);
// Extract Spotify track ID from the Spotify URL
if (odesliDoc.RootElement.TryGetProperty("linksByPlatform", out var platforms) &&
platforms.TryGetProperty("spotify", out var spotifyPlatform) &&
spotifyPlatform.TryGetProperty("url", out var spotifyUrlEl))
{
var spotifyUrl = spotifyUrlEl.GetString();
if (!string.IsNullOrEmpty(spotifyUrl))
{
// Extract ID from URL: https://open.spotify.com/track/{id}
var match = System.Text.RegularExpressions.Regex.Match(spotifyUrl, @"spotify\.com/track/([a-zA-Z0-9]+)");
if (match.Success)
{
var spotifyId = match.Groups[1].Value;
_logger.LogInformation("✓ Converted URL → Spotify ID {SpotifyId}", spotifyId);
// Cache for 7 days
await _cache.SetAsync(cacheKey, spotifyId, TimeSpan.FromDays(7));
return spotifyId;
}
}
}
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Failed to convert URL to Spotify ID via Odesli");
}
return null;
}
}

View File

@@ -1,313 +0,0 @@
namespace allstarr.Services.Common;
/// <summary>
/// Helper for round-robin load balancing with fallback across multiple API endpoints.
/// Distributes load evenly while maintaining reliability through automatic failover.
/// </summary>
public class RoundRobinFallbackHelper
{
private readonly List<string> _apiUrls;
private int _currentUrlIndex = 0;
private readonly object _urlIndexLock = new object();
private readonly ILogger _logger;
private readonly string _serviceName;
private readonly HttpClient _healthCheckClient;
// Cache health check results for 30 seconds to avoid excessive checks
private readonly Dictionary<string, (bool isHealthy, DateTime checkedAt)> _healthCache = new();
private readonly object _healthCacheLock = new object();
private readonly TimeSpan _healthCacheExpiry = TimeSpan.FromSeconds(30);
public int EndpointCount => _apiUrls.Count;
public RoundRobinFallbackHelper(List<string> apiUrls, ILogger logger, string serviceName)
{
_apiUrls = apiUrls ?? throw new ArgumentNullException(nameof(apiUrls));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_serviceName = serviceName ?? "Service";
if (_apiUrls.Count == 0)
{
throw new ArgumentException("API URLs list cannot be empty", nameof(apiUrls));
}
// Create a dedicated HttpClient for health checks with short timeout
_healthCheckClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(3) // Quick health check timeout
};
}
/// <summary>
/// Quickly checks if an endpoint is healthy (responds within 3 seconds).
/// Results are cached for 30 seconds to avoid excessive health checks.
/// </summary>
private async Task<bool> IsEndpointHealthyAsync(string baseUrl)
{
// Check cache first
lock (_healthCacheLock)
{
if (_healthCache.TryGetValue(baseUrl, out var cached))
{
if (DateTime.UtcNow - cached.checkedAt < _healthCacheExpiry)
{
return cached.isHealthy;
}
}
}
// Perform health check
try
{
var response = await _healthCheckClient.GetAsync(baseUrl, HttpCompletionOption.ResponseHeadersRead);
var isHealthy = response.IsSuccessStatusCode;
// Cache result
lock (_healthCacheLock)
{
_healthCache[baseUrl] = (isHealthy, DateTime.UtcNow);
}
if (!isHealthy)
{
_logger.LogDebug("{Service} endpoint {Endpoint} health check failed: {StatusCode}",
_serviceName, baseUrl, response.StatusCode);
}
return isHealthy;
}
catch (Exception ex)
{
_logger.LogDebug(ex, "{Service} endpoint {Endpoint} health check failed", _serviceName, baseUrl);
// Cache as unhealthy
lock (_healthCacheLock)
{
_healthCache[baseUrl] = (false, DateTime.UtcNow);
}
return false;
}
}
/// <summary>
/// Gets a list of healthy endpoints, checking them in parallel.
/// Falls back to all endpoints if none are healthy.
/// </summary>
private async Task<List<string>> GetHealthyEndpointsAsync()
{
var healthCheckTasks = _apiUrls.Select(async url => new
{
Url = url,
IsHealthy = await IsEndpointHealthyAsync(url)
}).ToList();
var results = await Task.WhenAll(healthCheckTasks);
var healthyEndpoints = results.Where(r => r.IsHealthy).Select(r => r.Url).ToList();
if (healthyEndpoints.Count == 0)
{
_logger.LogWarning("{Service} health check: no healthy endpoints found, will try all", _serviceName);
return _apiUrls;
}
_logger.LogDebug("{Service} health check: {Healthy}/{Total} endpoints healthy",
_serviceName, healthyEndpoints.Count, _apiUrls.Count);
return healthyEndpoints;
}
/// <summary>
/// Updates the endpoint order based on benchmark results (fastest first).
/// </summary>
public void SetEndpointOrder(List<string> orderedEndpoints)
{
lock (_urlIndexLock)
{
// Reorder _apiUrls to match the benchmarked order
var reordered = orderedEndpoints.Where(e => _apiUrls.Contains(e)).ToList();
// Add any endpoints that weren't benchmarked (shouldn't happen, but be safe)
foreach (var url in _apiUrls.Where(u => !reordered.Contains(u)))
{
reordered.Add(url);
}
_apiUrls.Clear();
_apiUrls.AddRange(reordered);
_currentUrlIndex = 0;
_logger.LogInformation("📊 {Service} endpoints reordered by benchmark: {Endpoints}",
_serviceName, string.Join(", ", _apiUrls.Take(3)));
}
}
/// <summary>
/// Tries the request with the next provider in round-robin, then falls back to others on failure.
/// This distributes load evenly across all providers while maintaining reliability.
/// Performs quick health checks first to avoid wasting time on dead endpoints.
/// Throws exception if all endpoints fail.
/// </summary>
public async Task<T> TryWithFallbackAsync<T>(Func<string, Task<T>> action)
{
// Get healthy endpoints first (with caching to avoid excessive checks)
var healthyEndpoints = await GetHealthyEndpointsAsync();
// Start with the next URL in round-robin to distribute load
var startIndex = 0;
lock (_urlIndexLock)
{
startIndex = _currentUrlIndex;
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
}
// Try healthy endpoints first, then fall back to all if needed
var endpointsToTry = healthyEndpoints.Count < _apiUrls.Count
? healthyEndpoints.Concat(_apiUrls.Except(healthyEndpoints)).ToList()
: healthyEndpoints;
// Try all URLs starting from the round-robin selected one
for (int attempt = 0; attempt < endpointsToTry.Count; attempt++)
{
var urlIndex = (startIndex + attempt) % endpointsToTry.Count;
var baseUrl = endpointsToTry[urlIndex];
try
{
_logger.LogDebug("Trying {Service} endpoint {Endpoint} (attempt {Attempt}/{Total})",
_serviceName, baseUrl, attempt + 1, endpointsToTry.Count);
return await action(baseUrl);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "{Service} request failed with endpoint {Endpoint}, trying next...",
_serviceName, baseUrl);
// Mark as unhealthy in cache
lock (_healthCacheLock)
{
_healthCache[baseUrl] = (false, DateTime.UtcNow);
}
if (attempt == endpointsToTry.Count - 1)
{
_logger.LogError("All {Count} {Service} endpoints failed", endpointsToTry.Count, _serviceName);
throw;
}
}
}
throw new Exception($"All {_serviceName} endpoints failed");
}
/// <summary>
/// Races all endpoints in parallel and returns the first successful result.
/// Cancels remaining requests once one succeeds. Great for latency-sensitive operations.
/// </summary>
public async Task<T> RaceAllEndpointsAsync<T>(Func<string, CancellationToken, Task<T>> action, CancellationToken cancellationToken = default)
{
if (_apiUrls.Count == 1)
{
// No point racing with one endpoint
return await action(_apiUrls[0], cancellationToken);
}
using var raceCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var tasks = new List<Task<(T result, string endpoint, bool success)>>();
// Start all requests in parallel
foreach (var baseUrl in _apiUrls)
{
var task = Task.Run(async () =>
{
try
{
_logger.LogDebug("Racing {Service} endpoint {Endpoint}", _serviceName, baseUrl);
var result = await action(baseUrl, raceCts.Token);
return (result, baseUrl, true);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "{Service} race failed for endpoint {Endpoint}", _serviceName, baseUrl);
return (default(T)!, baseUrl, false);
}
}, raceCts.Token);
tasks.Add(task);
}
// Wait for first successful completion
while (tasks.Count > 0)
{
var completedTask = await Task.WhenAny(tasks);
var (result, endpoint, success) = await completedTask;
if (success)
{
_logger.LogInformation("🏁 {Service} race won by {Endpoint}, canceling others", _serviceName, endpoint);
raceCts.Cancel(); // Cancel all other requests
return result;
}
tasks.Remove(completedTask);
}
throw new Exception($"All {_serviceName} endpoints failed in race");
}
/// <summary>
/// Tries the request with the next provider in round-robin, then falls back to others on failure.
/// Performs quick health checks first to avoid wasting time on dead endpoints.
/// Returns default value if all endpoints fail (does not throw).
/// </summary>
public async Task<T> TryWithFallbackAsync<T>(Func<string, Task<T>> action, T defaultValue)
{
// Get healthy endpoints first (with caching to avoid excessive checks)
var healthyEndpoints = await GetHealthyEndpointsAsync();
// Start with the next URL in round-robin to distribute load
var startIndex = 0;
lock (_urlIndexLock)
{
startIndex = _currentUrlIndex;
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
}
// Try healthy endpoints first, then fall back to all if needed
var endpointsToTry = healthyEndpoints.Count < _apiUrls.Count
? healthyEndpoints.Concat(_apiUrls.Except(healthyEndpoints)).ToList()
: healthyEndpoints;
// Try all URLs starting from the round-robin selected one
for (int attempt = 0; attempt < endpointsToTry.Count; attempt++)
{
var urlIndex = (startIndex + attempt) % endpointsToTry.Count;
var baseUrl = endpointsToTry[urlIndex];
try
{
_logger.LogDebug("Trying {Service} endpoint {Endpoint} (attempt {Attempt}/{Total})",
_serviceName, baseUrl, attempt + 1, endpointsToTry.Count);
return await action(baseUrl);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "{Service} request failed with endpoint {Endpoint}, trying next...",
_serviceName, baseUrl);
// Mark as unhealthy in cache
lock (_healthCacheLock)
{
_healthCache[baseUrl] = (false, DateTime.UtcNow);
}
if (attempt == endpointsToTry.Count - 1)
{
_logger.LogError("All {Count} {Service} endpoints failed, returning default value",
endpointsToTry.Count, _serviceName);
return defaultValue;
}
}
}
return defaultValue;
}
}

View File

@@ -24,6 +24,7 @@ namespace allstarr.Services.Deezer;
public class DeezerDownloadService : BaseDownloadService public class DeezerDownloadService : BaseDownloadService
{ {
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly SemaphoreSlim _requestLock = new(1, 1);
private readonly string? _arl; private readonly string? _arl;
private readonly string? _arlFallback; private readonly string? _arlFallback;
@@ -32,6 +33,9 @@ public class DeezerDownloadService : BaseDownloadService
private string? _apiToken; private string? _apiToken;
private string? _licenseToken; private string? _licenseToken;
private DateTime _lastRequestTime = DateTime.MinValue;
private readonly int _minRequestIntervalMs = 200;
private const string DeezerApiBase = "https://api.deezer.com"; private const string DeezerApiBase = "https://api.deezer.com";
// Deezer's standard Blowfish CBC encryption key for track decryption // Deezer's standard Blowfish CBC encryption key for track decryption
@@ -107,10 +111,7 @@ public class DeezerDownloadService : BaseDownloadService
// Build organized folder structure: Artist/Album/Track using AlbumArtist (fallback to Artist for singles) // Build organized folder structure: Artist/Album/Track using AlbumArtist (fallback to Artist for singles)
var artistForPath = song.AlbumArtist ?? song.Artist; var artistForPath = song.AlbumArtist ?? song.Artist;
// Cache mode uses downloads/cache/ folder, Permanent mode uses downloads/permanent/ var basePath = SubsonicSettings.StorageMode == StorageMode.Cache ? CachePath : DownloadPath;
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
? Path.Combine("downloads", "cache")
: Path.Combine("downloads", "permanent");
var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension); var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension);
// Create directories if they don't exist // Create directories if they don't exist
@@ -493,6 +494,27 @@ public class DeezerDownloadService : BaseDownloadService
await RetryWithBackoffAsync<bool>(action, maxRetries, initialDelayMs); await RetryWithBackoffAsync<bool>(action, maxRetries, initialDelayMs);
} }
private async Task<T> QueueRequestAsync<T>(Func<Task<T>> action)
{
await _requestLock.WaitAsync();
try
{
var now = DateTime.UtcNow;
var timeSinceLastRequest = (now - _lastRequestTime).TotalMilliseconds;
if (timeSinceLastRequest < _minRequestIntervalMs)
{
await Task.Delay((int)(_minRequestIntervalMs - timeSinceLastRequest));
}
_lastRequestTime = DateTime.UtcNow;
return await action();
}
finally
{
_requestLock.Release();
}
}
#endregion #endregion

View File

@@ -168,11 +168,6 @@ public class JellyfinProxyService
(h.Value.ToString().Contains("image", StringComparison.OrdinalIgnoreCase) || (h.Value.ToString().Contains("image", StringComparison.OrdinalIgnoreCase) ||
h.Value.ToString().Contains("document", StringComparison.OrdinalIgnoreCase))) == true); h.Value.ToString().Contains("document", StringComparison.OrdinalIgnoreCase))) == true);
// Check if this is a public endpoint that doesn't require authentication
bool isPublicEndpoint = url.Contains("/System/Info/Public", StringComparison.OrdinalIgnoreCase) ||
url.Contains("/Branding/", StringComparison.OrdinalIgnoreCase) ||
url.Contains("/Startup/", StringComparison.OrdinalIgnoreCase);
// Forward authentication headers from client if provided // Forward authentication headers from client if provided
if (clientHeaders != null && clientHeaders.Count > 0) if (clientHeaders != null && clientHeaders.Count > 0)
{ {
@@ -184,27 +179,11 @@ public class JellyfinProxyService
var headerValue = header.Value.ToString(); var headerValue = header.Value.ToString();
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue); request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
authHeaderAdded = true; authHeaderAdded = true;
_logger.LogTrace("Forwarded X-Emby-Authorization header"); _logger.LogInformation("Forwarded X-Emby-Authorization: {Value}", headerValue);
break; break;
} }
} }
// Try X-Emby-Token (simpler format used by some clients)
if (!authHeaderAdded)
{
foreach (var header in clientHeaders)
{
if (header.Key.Equals("X-Emby-Token", StringComparison.OrdinalIgnoreCase))
{
var headerValue = header.Value.ToString();
request.Headers.TryAddWithoutValidation("X-Emby-Token", headerValue);
authHeaderAdded = true;
_logger.LogTrace("Forwarded X-Emby-Token header");
break;
}
}
}
// If no X-Emby-Authorization, check if Authorization header contains MediaBrowser format // If no X-Emby-Authorization, check if Authorization header contains MediaBrowser format
// Some clients send it as "Authorization" instead of "X-Emby-Authorization" // Some clients send it as "Authorization" instead of "X-Emby-Authorization"
if (!authHeaderAdded) if (!authHeaderAdded)
@@ -222,32 +201,37 @@ public class JellyfinProxyService
// Forward as X-Emby-Authorization (Jellyfin's expected header) // Forward as X-Emby-Authorization (Jellyfin's expected header)
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue); request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
authHeaderAdded = true; authHeaderAdded = true;
_logger.LogTrace("Converted Authorization to X-Emby-Authorization"); _logger.LogInformation("Converted Authorization to X-Emby-Authorization: {Value}", headerValue);
} }
else else
{ {
// Standard Bearer token - forward as-is // Standard Bearer token - forward as-is
request.Headers.TryAddWithoutValidation("Authorization", headerValue); request.Headers.TryAddWithoutValidation("Authorization", headerValue);
authHeaderAdded = true; authHeaderAdded = true;
_logger.LogTrace("Forwarded Authorization header"); _logger.LogInformation("Forwarded Authorization (Bearer): {Value}", headerValue);
} }
break; break;
} }
} }
} }
// Check for api_key query parameter (some clients use this) // Only log warnings for non-browser static requests
if (!authHeaderAdded && url.Contains("api_key=", StringComparison.OrdinalIgnoreCase)) if (!authHeaderAdded && !isBrowserStaticRequest)
{ {
authHeaderAdded = true; // It's in the URL, no need to add header _logger.LogWarning("✗ No auth header found. Available headers: {Headers}",
_logger.LogTrace("Using api_key from query string"); string.Join(", ", clientHeaders.Select(h => $"{h.Key}={h.Value}")));
} }
} }
else if (!isBrowserStaticRequest)
// Only log warnings for non-public, non-browser requests without auth
if (!authHeaderAdded && !isBrowserStaticRequest && !isPublicEndpoint)
{ {
_logger.LogDebug("No client auth provided for {Url} - Jellyfin will handle authentication", url); _logger.LogWarning("No client headers provided for {Url}", url);
}
// 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 && !isBrowserStaticRequest)
{
_logger.LogInformation("No client auth provided for {Url} - forwarding without auth", url);
} }
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
@@ -264,28 +248,14 @@ public class JellyfinProxyService
{ {
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{ {
// 401 means token expired or invalid - client needs to re-authenticate _logger.LogWarning("Jellyfin returned 401 Unauthorized for {Url} - passing through to client", url);
_logger.LogInformation("Jellyfin returned 401 Unauthorized for {Url} - client should re-authenticate", url);
} }
else if (!isBrowserStaticRequest && !isPublicEndpoint) else if (!isBrowserStaticRequest) // Don't log 404s for browser static requests
{ {
_logger.LogWarning("Jellyfin request failed: {StatusCode} for {Url}", response.StatusCode, url); _logger.LogWarning("Jellyfin request failed: {StatusCode} for {Url}", response.StatusCode, url);
} }
// Try to parse error response to pass through to client // Return null body with the actual status code
if (!string.IsNullOrWhiteSpace(content))
{
try
{
var errorDoc = JsonDocument.Parse(content);
return (errorDoc, statusCode);
}
catch
{
// Not valid JSON, return null
}
}
return (null, statusCode); return (null, statusCode);
} }
@@ -327,10 +297,8 @@ public class JellyfinProxyService
request.Content = new StringContent(bodyToSend, System.Text.Encoding.UTF8, "application/json"); request.Content = new StringContent(bodyToSend, System.Text.Encoding.UTF8, "application/json");
bool authHeaderAdded = false; bool authHeaderAdded = false;
bool isAuthEndpoint = endpoint.Contains("Authenticate", StringComparison.OrdinalIgnoreCase);
// Forward authentication headers from client (case-insensitive) // Forward authentication headers from client (case-insensitive)
// Try X-Emby-Authorization first
foreach (var header in clientHeaders) foreach (var header in clientHeaders)
{ {
if (header.Key.Equals("X-Emby-Authorization", StringComparison.OrdinalIgnoreCase)) if (header.Key.Equals("X-Emby-Authorization", StringComparison.OrdinalIgnoreCase))
@@ -338,28 +306,11 @@ public class JellyfinProxyService
var headerValue = header.Value.ToString(); var headerValue = header.Value.ToString();
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue); request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
authHeaderAdded = true; authHeaderAdded = true;
_logger.LogTrace("Forwarded X-Emby-Authorization header"); _logger.LogDebug("Forwarded X-Emby-Authorization from client");
break; break;
} }
} }
// Try X-Emby-Token
if (!authHeaderAdded)
{
foreach (var header in clientHeaders)
{
if (header.Key.Equals("X-Emby-Token", StringComparison.OrdinalIgnoreCase))
{
var headerValue = header.Value.ToString();
request.Headers.TryAddWithoutValidation("X-Emby-Token", headerValue);
authHeaderAdded = true;
_logger.LogTrace("Forwarded X-Emby-Token header");
break;
}
}
}
// Try Authorization header
if (!authHeaderAdded) if (!authHeaderAdded)
{ {
foreach (var header in clientHeaders) foreach (var header in clientHeaders)
@@ -374,13 +325,13 @@ public class JellyfinProxyService
{ {
// Forward as X-Emby-Authorization // Forward as X-Emby-Authorization
request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue); request.Headers.TryAddWithoutValidation("X-Emby-Authorization", headerValue);
_logger.LogTrace("Converted Authorization to X-Emby-Authorization"); _logger.LogDebug("Converted Authorization to X-Emby-Authorization");
} }
else else
{ {
// Standard Bearer token // Standard Bearer token
request.Headers.TryAddWithoutValidation("Authorization", headerValue); request.Headers.TryAddWithoutValidation("Authorization", headerValue);
_logger.LogTrace("Forwarded Authorization header"); _logger.LogDebug("Forwarded Authorization header");
} }
authHeaderAdded = true; authHeaderAdded = true;
break; break;
@@ -388,23 +339,30 @@ public class JellyfinProxyService
} }
} }
// For authentication endpoints, credentials are in the body, not headers // DO NOT use server credentials as fallback
// For other endpoints without auth, let Jellyfin reject the request // Exception: For auth endpoints, client provides their own credentials in the body
if (!authHeaderAdded && !isAuthEndpoint) // For all other endpoints, if client doesn't provide auth, let Jellyfin reject it
if (!authHeaderAdded)
{ {
_logger.LogDebug("No client auth provided for POST {Url} - Jellyfin will handle authentication", url); _logger.LogInformation("No client auth provided for POST {Url} - forwarding without auth", url);
} }
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// DO NOT log the body for auth endpoints - it contains passwords! // DO NOT log the body for auth endpoints - it contains passwords!
if (isAuthEndpoint) if (endpoint.Contains("Authenticate", StringComparison.OrdinalIgnoreCase))
{ {
_logger.LogDebug("POST to Jellyfin: {Url} (auth request - body not logged)", url); _logger.LogDebug("POST to Jellyfin: {Url} (auth request - body not logged)", url);
} }
else else
{ {
_logger.LogTrace("POST to Jellyfin: {Url}, body length: {Length} bytes", url, bodyToSend.Length); _logger.LogInformation("POST to Jellyfin: {Url}, body length: {Length} bytes", url, bodyToSend.Length);
// Log body content for playback endpoints to debug
if (endpoint.Contains("Playing", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation("Sending body to Jellyfin: {Body}", bodyToSend);
}
} }
var response = await _httpClient.SendAsync(request); var response = await _httpClient.SendAsync(request);
@@ -414,39 +372,15 @@ public class JellyfinProxyService
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
var errorContent = await response.Content.ReadAsStringAsync(); var errorContent = await response.Content.ReadAsStringAsync();
_logger.LogWarning("❌ SESSION: Jellyfin POST request failed: {StatusCode} for {Url}. Response: {Response}",
// 401 is expected when tokens expire - don't spam logs response.StatusCode, url, errorContent);
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
_logger.LogInformation("Jellyfin POST returned 401 for {Url} - client should re-authenticate", url);
}
else
{
_logger.LogWarning("Jellyfin POST request failed: {StatusCode} for {Url}. Response: {Response}",
response.StatusCode, url, errorContent.Length > 200 ? errorContent[..200] + "..." : errorContent);
}
// Try to parse error response as JSON to pass through to client
if (!string.IsNullOrWhiteSpace(errorContent))
{
try
{
var errorDoc = JsonDocument.Parse(errorContent);
return (errorDoc, statusCode);
}
catch
{
// Not valid JSON, return null
}
}
return (null, statusCode); return (null, statusCode);
} }
// Log successful session-related responses // Log successful session-related responses
if (endpoint.Contains("Sessions", StringComparison.OrdinalIgnoreCase)) if (endpoint.Contains("Sessions", StringComparison.OrdinalIgnoreCase))
{ {
_logger.LogTrace("Jellyfin responded {StatusCode} for {Endpoint}", statusCode, endpoint); _logger.LogWarning("✓ SESSION: Jellyfin responded {StatusCode} for {Endpoint}", statusCode, endpoint);
} }
// Handle 204 No Content responses (e.g., /sessions/playing, /sessions/playing/progress) // Handle 204 No Content responses (e.g., /sessions/playing, /sessions/playing/progress)
@@ -463,6 +397,13 @@ public class JellyfinProxyService
return (null, statusCode); return (null, statusCode);
} }
// Log response content for session endpoints
if (endpoint.Contains("Sessions", StringComparison.OrdinalIgnoreCase) && !string.IsNullOrWhiteSpace(responseContent))
{
var preview = responseContent.Length > 200 ? responseContent[..200] + "..." : responseContent;
_logger.LogWarning("📥 SESSION: Jellyfin response body: {Body}", preview);
}
return (JsonDocument.Parse(responseContent), statusCode); return (JsonDocument.Parse(responseContent), statusCode);
} }

View File

@@ -38,13 +38,12 @@ public class JellyfinSessionManager : IDisposable
/// <summary> /// <summary>
/// Ensures a session exists for the given device. Creates one if needed. /// Ensures a session exists for the given device. Creates one if needed.
/// Returns false if token is expired (401), indicating client needs to re-authenticate.
/// </summary> /// </summary>
public async Task<bool> EnsureSessionAsync(string deviceId, string client, string device, string version, IHeaderDictionary headers) public async Task<bool> EnsureSessionAsync(string deviceId, string client, string device, string version, IHeaderDictionary headers)
{ {
if (string.IsNullOrEmpty(deviceId)) if (string.IsNullOrEmpty(deviceId))
{ {
_logger.LogWarning("Cannot create session - no device ID"); _logger.LogWarning("⚠️ SESSION: Cannot create session - no device ID");
return false; return false;
} }
@@ -52,37 +51,25 @@ public class JellyfinSessionManager : IDisposable
if (_sessions.TryGetValue(deviceId, out var existingSession)) if (_sessions.TryGetValue(deviceId, out var existingSession))
{ {
existingSession.LastActivity = DateTime.UtcNow; existingSession.LastActivity = DateTime.UtcNow;
_logger.LogTrace("Session already exists for device {DeviceId}", deviceId); _logger.LogDebug("✓ SESSION: Session already exists for device {DeviceId}", deviceId);
// Refresh capabilities to keep session alive // Refresh capabilities to keep session alive
// If this returns false (401), the token expired and client needs to re-auth await PostCapabilitiesAsync(headers);
var success = await PostCapabilitiesAsync(headers);
if (!success)
{
// Token expired - remove the stale session
_logger.LogInformation("Token expired for device {DeviceId} - removing session", deviceId);
await RemoveSessionAsync(deviceId);
return false;
}
return true; return true;
} }
_logger.LogDebug("Creating new session for device: {DeviceId} ({Client} on {Device})", deviceId, client, device); _logger.LogInformation("🔧 SESSION: Creating new session for device: {DeviceId} ({Client} on {Device})", deviceId, client, device);
// Log the headers we received for debugging
_logger.LogDebug("🔍 SESSION: Headers received for session creation: {Headers}",
string.Join(", ", headers.Select(h => $"{h.Key}={h.Value.ToString().Substring(0, Math.Min(30, h.Value.ToString().Length))}...")));
try try
{ {
// Post session capabilities to Jellyfin - this creates the session // Post session capabilities to Jellyfin - this creates the session
var success = await PostCapabilitiesAsync(headers); await PostCapabilitiesAsync(headers);
if (!success) _logger.LogInformation("✓ SESSION: Session created for {DeviceId}", deviceId);
{
// Token expired or invalid - client needs to re-authenticate
_logger.LogInformation("Failed to create session for {DeviceId} - token may be expired", deviceId);
return false;
}
_logger.LogDebug("Session created for {DeviceId}", deviceId);
// Track this session // Track this session
_sessions[deviceId] = new SessionInfo _sessions[deviceId] = new SessionInfo
@@ -102,16 +89,15 @@ public class JellyfinSessionManager : IDisposable
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error creating session for {DeviceId}", deviceId); _logger.LogError(ex, "❌ SESSION: Error creating session for {DeviceId}", deviceId);
return false; return false;
} }
} }
/// <summary> /// <summary>
/// Posts session capabilities to Jellyfin. /// Posts session capabilities to Jellyfin.
/// Returns true if successful, false if token expired (401).
/// </summary> /// </summary>
private async Task<bool> PostCapabilitiesAsync(IHeaderDictionary headers) private async Task PostCapabilitiesAsync(IHeaderDictionary headers)
{ {
var capabilities = new var capabilities = new
{ {
@@ -132,19 +118,12 @@ public class JellyfinSessionManager : IDisposable
if (statusCode == 204 || statusCode == 200) if (statusCode == 204 || statusCode == 200)
{ {
_logger.LogTrace("Posted capabilities successfully ({StatusCode})", statusCode); _logger.LogDebug("✓ SESSION: Posted capabilities successfully ({StatusCode})", statusCode);
return true;
}
else if (statusCode == 401)
{
// Token expired - this is expected, client needs to re-authenticate
_logger.LogDebug("Capabilities returned 401 (token expired) - client should re-authenticate");
return false;
} }
else else
{ {
_logger.LogDebug("Capabilities post returned {StatusCode}", statusCode); // 401 is common when cached headers have expired - not a critical error
return false; _logger.LogDebug("SESSION: Capabilities post returned {StatusCode} (may be expected if token expired)", statusCode);
} }
} }
@@ -199,7 +178,7 @@ public class JellyfinSessionManager : IDisposable
if (_sessions.TryGetValue(deviceId, out var currentSession) && if (_sessions.TryGetValue(deviceId, out var currentSession) &&
currentSession.LastActivity <= markedTime) currentSession.LastActivity <= markedTime)
{ {
_logger.LogDebug("🧹 SESSION: Auto-removing inactive session {DeviceId} after playback stop", deviceId); _logger.LogInformation("🧹 SESSION: Auto-removing inactive session {DeviceId} after playback stop", deviceId);
await RemoveSessionAsync(deviceId); await RemoveSessionAsync(deviceId);
} }
else else
@@ -244,7 +223,7 @@ public class JellyfinSessionManager : IDisposable
{ {
if (_sessions.TryRemove(deviceId, out var session)) if (_sessions.TryRemove(deviceId, out var session))
{ {
_logger.LogDebug("🗑️ SESSION: Removing session for device {DeviceId}", deviceId); _logger.LogInformation("🗑️ SESSION: Removing session for device {DeviceId}", deviceId);
// Close WebSocket if it exists // Close WebSocket if it exists
if (session.WebSocket != null && session.WebSocket.State == WebSocketState.Open) if (session.WebSocket != null && session.WebSocket.State == WebSocketState.Open)
@@ -256,7 +235,7 @@ public class JellyfinSessionManager : IDisposable
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogDebug(ex, "WEBSOCKET: Error closing WebSocket for {DeviceId}", deviceId); _logger.LogWarning(ex, "⚠️ WEBSOCKET: Error closing WebSocket for {DeviceId}", deviceId);
} }
finally finally
{ {
@@ -276,7 +255,7 @@ public class JellyfinSessionManager : IDisposable
}; };
var stopJson = JsonSerializer.Serialize(stopPayload); var stopJson = JsonSerializer.Serialize(stopPayload);
await _proxyService.PostJsonAsync("Sessions/Playing/Stopped", stopJson, session.Headers); await _proxyService.PostJsonAsync("Sessions/Playing/Stopped", stopJson, session.Headers);
_logger.LogDebug("🛑 SESSION: Reported playback stopped for {DeviceId} (ItemId: {ItemId}, Position: {Position})", _logger.LogInformation("🛑 SESSION: Reported playback stopped for {DeviceId} (ItemId: {ItemId}, Position: {Position})",
deviceId, session.LastPlayingItemId, session.LastPlayingPositionTicks); deviceId, session.LastPlayingItemId, session.LastPlayingPositionTicks);
} }
@@ -319,23 +298,20 @@ public class JellyfinSessionManager : IDisposable
webSocket = new ClientWebSocket(); webSocket = new ClientWebSocket();
session.WebSocket = webSocket; session.WebSocket = webSocket;
// Use stored session headers instead of parameter (parameter might be disposed)
var sessionHeaders = session.Headers;
// Log available headers for debugging // Log available headers for debugging
_logger.LogDebug("🔍 WEBSOCKET: Available headers for {DeviceId}: {Headers}", _logger.LogDebug("🔍 WEBSOCKET: Available headers for {DeviceId}: {Headers}",
deviceId, string.Join(", ", sessionHeaders.Keys)); deviceId, string.Join(", ", headers.Keys));
// Forward authentication headers from the CLIENT - this is critical for session to appear under the right user // Forward authentication headers from the CLIENT - this is critical for session to appear under the right user
bool authFound = false; bool authFound = false;
if (sessionHeaders.TryGetValue("X-Emby-Authorization", out var embyAuth)) if (headers.TryGetValue("X-Emby-Authorization", out var embyAuth))
{ {
webSocket.Options.SetRequestHeader("X-Emby-Authorization", embyAuth.ToString()); webSocket.Options.SetRequestHeader("X-Emby-Authorization", embyAuth.ToString());
_logger.LogDebug("🔑 WEBSOCKET: Using X-Emby-Authorization for {DeviceId}: {Auth}", _logger.LogDebug("🔑 WEBSOCKET: Using X-Emby-Authorization for {DeviceId}: {Auth}",
deviceId, embyAuth.ToString().Length > 50 ? embyAuth.ToString()[..50] + "..." : embyAuth.ToString()); deviceId, embyAuth.ToString().Length > 50 ? embyAuth.ToString()[..50] + "..." : embyAuth.ToString());
authFound = true; authFound = true;
} }
else if (sessionHeaders.TryGetValue("Authorization", out var auth)) else if (headers.TryGetValue("Authorization", out var auth))
{ {
var authValue = auth.ToString(); var authValue = auth.ToString();
if (authValue.Contains("MediaBrowser", StringComparison.OrdinalIgnoreCase)) if (authValue.Contains("MediaBrowser", StringComparison.OrdinalIgnoreCase))
@@ -360,11 +336,11 @@ public class JellyfinSessionManager : IDisposable
if (!string.IsNullOrEmpty(_settings.ApiKey)) if (!string.IsNullOrEmpty(_settings.ApiKey))
{ {
jellyfinWsUrl += $"?api_key={_settings.ApiKey}"; jellyfinWsUrl += $"?api_key={_settings.ApiKey}";
_logger.LogDebug("WEBSOCKET: No client auth found in headers, falling back to server API key for {DeviceId}", deviceId); _logger.LogWarning("⚠️ WEBSOCKET: No client auth found in headers, falling back to server API key for {DeviceId}", deviceId);
} }
else else
{ {
_logger.LogWarning("❌ WEBSOCKET: No authentication available for {DeviceId} - WebSocket will fail", deviceId); _logger.LogWarning("❌ WEBSOCKET: No authentication available for {DeviceId}!", deviceId);
} }
} }
@@ -375,7 +351,7 @@ public class JellyfinSessionManager : IDisposable
// Connect to Jellyfin // Connect to Jellyfin
await webSocket.ConnectAsync(new Uri(jellyfinWsUrl), CancellationToken.None); await webSocket.ConnectAsync(new Uri(jellyfinWsUrl), CancellationToken.None);
_logger.LogDebug("✓ WEBSOCKET: Connected to Jellyfin for device {DeviceId}", deviceId); _logger.LogInformation("✓ WEBSOCKET: Connected to Jellyfin for device {DeviceId}", deviceId);
// CRITICAL: Send ForceKeepAlive message to initialize session in Jellyfin // CRITICAL: Send ForceKeepAlive message to initialize session in Jellyfin
// This tells Jellyfin to create/show the session in the dashboard // This tells Jellyfin to create/show the session in the dashboard
@@ -431,8 +407,8 @@ public class JellyfinSessionManager : IDisposable
} }
else else
{ {
// Log other message types at trace level // Log other message types at info level
_logger.LogTrace("📥 WEBSOCKET: {DeviceId}: {Message}", _logger.LogInformation("📥 WEBSOCKET: {DeviceId}: {Message}",
deviceId, message.Length > 100 ? message[..100] + "..." : message); deviceId, message.Length > 100 ? message[..100] + "..." : message);
} }
} }
@@ -454,7 +430,7 @@ public class JellyfinSessionManager : IDisposable
} }
catch (WebSocketException wsEx) catch (WebSocketException wsEx)
{ {
_logger.LogDebug(wsEx, "WEBSOCKET: Connection closed for device {DeviceId}", deviceId); _logger.LogWarning(wsEx, "⚠️ WEBSOCKET: WebSocket error for device {DeviceId}", deviceId);
break; break;
} }
} }
@@ -490,7 +466,6 @@ public class JellyfinSessionManager : IDisposable
/// <summary> /// <summary>
/// Periodically pings Jellyfin to keep sessions alive. /// Periodically pings Jellyfin to keep sessions alive.
/// Note: This is a backup mechanism. The WebSocket connection is the primary keep-alive. /// Note: This is a backup mechanism. The WebSocket connection is the primary keep-alive.
/// Removes sessions with expired tokens (401 responses).
/// </summary> /// </summary>
private async void KeepSessionsAlive(object? state) private async void KeepSessionsAlive(object? state)
{ {
@@ -502,43 +477,29 @@ public class JellyfinSessionManager : IDisposable
return; return;
} }
_logger.LogTrace("Keeping {Count} sessions alive", activeSessions.Count); _logger.LogDebug("💓 SESSION: Keeping {Count} sessions alive", activeSessions.Count);
var expiredSessions = new List<string>();
foreach (var session in activeSessions) foreach (var session in activeSessions)
{ {
try try
{ {
// Post capabilities again to keep session alive // Post capabilities again to keep session alive
// If this returns false (401), the token has expired // Note: This may fail with 401 if the client's token has expired
var success = await PostCapabilitiesAsync(session.Headers); // That's okay - the WebSocket connection keeps the session alive anyway
await PostCapabilitiesAsync(session.Headers);
if (!success)
{
_logger.LogInformation("Token expired for device {DeviceId} during keep-alive - marking for removal", session.DeviceId);
expiredSessions.Add(session.DeviceId);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogDebug(ex, "Error keeping session alive for {DeviceId}", session.DeviceId); _logger.LogDebug(ex, "SESSION: Error keeping session alive for {DeviceId} (WebSocket still active)", session.DeviceId);
} }
} }
// Remove sessions with expired tokens
foreach (var deviceId in expiredSessions)
{
_logger.LogInformation("Removing session with expired token: {DeviceId}", deviceId);
await RemoveSessionAsync(deviceId);
}
// Clean up stale sessions after 3 minutes of inactivity // Clean up stale sessions after 3 minutes of inactivity
// This balances cleaning up finished sessions with allowing brief pauses/network issues // This balances cleaning up finished sessions with allowing brief pauses/network issues
var staleSessions = _sessions.Where(kvp => now - kvp.Value.LastActivity > TimeSpan.FromMinutes(3)).ToList(); var staleSessions = _sessions.Where(kvp => now - kvp.Value.LastActivity > TimeSpan.FromMinutes(3)).ToList();
foreach (var stale in staleSessions) foreach (var stale in staleSessions)
{ {
_logger.LogDebug("Removing stale session for {DeviceId} (inactive for {Minutes:F1} minutes)", _logger.LogInformation("🧹 SESSION: Removing stale session for {DeviceId} (inactive for {Minutes:F1} minutes)",
stale.Key, (now - stale.Value.LastActivity).TotalMinutes); stale.Key, (now - stale.Value.LastActivity).TotalMinutes);
await RemoveSessionAsync(stale.Key); await RemoveSessionAsync(stale.Key);
} }

View File

@@ -406,9 +406,9 @@ public class LyricsPrefetchService : BackgroundService
} }
// Directly check if this track has lyrics using the item ID // Directly check if this track has lyrics using the item ID
// Use internal method with server API key since this is a background operation var (lyricsResult, lyricsStatusCode) = await proxyService.GetJsonAsync(
var (lyricsResult, lyricsStatusCode) = await proxyService.GetJsonAsyncInternal(
$"Audio/{jellyfinItemId}/Lyrics", $"Audio/{jellyfinItemId}/Lyrics",
null,
null); null);
if (lyricsResult != null && lyricsStatusCode == 200) if (lyricsResult != null && lyricsStatusCode == 200)
@@ -455,7 +455,7 @@ public class LyricsPrefetchService : BackgroundService
["limit"] = "5" // Get a few results to find best match ["limit"] = "5" // Get a few results to find best match
}; };
var (searchResult, statusCode) = await proxyService.GetJsonAsyncInternal("Items", searchParams); var (searchResult, statusCode) = await proxyService.GetJsonAsync("Items", searchParams, null);
if (searchResult == null || statusCode != 200) if (searchResult == null || statusCode != 200)
{ {
@@ -511,9 +511,9 @@ public class LyricsPrefetchService : BackgroundService
} }
// Check if this track has lyrics // Check if this track has lyrics
// Use internal method with server API key since this is a background operation var (lyricsResult, lyricsStatusCode) = await proxyService.GetJsonAsync(
var (lyricsResult, lyricsStatusCode) = await proxyService.GetJsonAsyncInternal(
$"Audio/{bestMatchId}/Lyrics", $"Audio/{bestMatchId}/Lyrics",
null,
null); null);
if (lyricsResult != null && lyricsStatusCode == 200) if (lyricsResult != null && lyricsStatusCode == 200)

View File

@@ -63,7 +63,15 @@ public class SpotifyLyricsService
// Normalize track ID (remove URI prefix if present) // Normalize track ID (remove URI prefix if present)
spotifyTrackId = ExtractTrackId(spotifyTrackId); spotifyTrackId = ExtractTrackId(spotifyTrackId);
// NO CACHING - Spotify lyrics come from local Docker container (fast) // Check cache
var cacheKey = $"spotify:lyrics:{spotifyTrackId}";
var cached = await _cache.GetAsync<SpotifyLyricsResult>(cacheKey);
if (cached != null)
{
_logger.LogDebug("Returning cached Spotify lyrics for track {TrackId}", spotifyTrackId);
return cached;
}
try try
{ {
var url = $"{_settings.LyricsApiUrl}/?trackid={spotifyTrackId}&format=id3"; var url = $"{_settings.LyricsApiUrl}/?trackid={spotifyTrackId}&format=id3";
@@ -84,6 +92,8 @@ public class SpotifyLyricsService
if (result != null) if (result != null)
{ {
// Cache for 30 days (lyrics don't change)
await _cache.SetAsync(cacheKey, result, TimeSpan.FromDays(30));
_logger.LogInformation("Got Spotify lyrics from sidecar for track {TrackId} ({LineCount} lines)", _logger.LogInformation("Got Spotify lyrics from sidecar for track {TrackId} ({LineCount} lines)",
spotifyTrackId, result.Lines.Count); spotifyTrackId, result.Lines.Count);
} }

View File

@@ -110,10 +110,7 @@ public class QobuzDownloadService : BaseDownloadService
// Build organized folder structure using AlbumArtist (fallback to Artist for singles) // Build organized folder structure using AlbumArtist (fallback to Artist for singles)
var artistForPath = song.AlbumArtist ?? song.Artist; var artistForPath = song.AlbumArtist ?? song.Artist;
// Cache mode uses downloads/cache/ folder, Permanent mode uses downloads/permanent/ var basePath = SubsonicSettings.StorageMode == StorageMode.Cache ? CachePath : DownloadPath;
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
? Path.Combine(DownloadPath, "cache")
: Path.Combine(DownloadPath, "permanent");
var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension); var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension);
var albumFolder = Path.GetDirectoryName(outputPath)!; var albumFolder = Path.GetDirectoryName(outputPath)!;

View File

@@ -44,7 +44,7 @@ public class SpotifyMissingTracksFetcher : BackgroundService
public async Task TriggerFetchAsync() public async Task TriggerFetchAsync()
{ {
_logger.LogInformation("Manual fetch triggered"); _logger.LogInformation("Manual fetch triggered");
await FetchMissingTracksAsync(CancellationToken.None); await FetchMissingTracksAsync(CancellationToken.None, bypassSyncWindowCheck: true);
} }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -84,7 +84,20 @@ public class SpotifyMissingTracksFetcher : BackgroundService
_logger.LogInformation("Spotify Import ENABLED"); _logger.LogInformation("Spotify Import ENABLED");
_logger.LogInformation("Configured Playlists: {Count}", _spotifySettings.Value.Playlists.Count); _logger.LogInformation("Configured Playlists: {Count}", _spotifySettings.Value.Playlists.Count);
_logger.LogInformation("Background check interval: 5 minutes");
// Log the search schedule
var settings = _spotifySettings.Value;
var syncTime = DateTime.Today
.AddHours(settings.SyncStartHour)
.AddMinutes(settings.SyncStartMinute);
var syncEndTime = syncTime.AddHours(settings.SyncWindowHours);
_logger.LogInformation("Search Schedule:");
_logger.LogInformation(" Plugin sync time: {Time:HH:mm} UTC (configured)", syncTime);
_logger.LogInformation(" Search window: {Start:HH:mm} - {End:HH:mm} UTC ({Hours}h window)",
syncTime, syncEndTime, settings.SyncWindowHours);
_logger.LogInformation(" Will search for new files once per day after sync window ends");
_logger.LogInformation(" Background check interval: 5 minutes");
// Fetch playlist names from Jellyfin // Fetch playlist names from Jellyfin
await LoadPlaylistNamesAsync(); await LoadPlaylistNamesAsync();
@@ -96,7 +109,7 @@ public class SpotifyMissingTracksFetcher : BackgroundService
} }
_logger.LogInformation("========================================"); _logger.LogInformation("========================================");
// Run on startup if we don't have cache // Check if we should run on startup
if (!_hasRunOnce) if (!_hasRunOnce)
{ {
var shouldRun = await ShouldRunOnStartupAsync(); var shouldRun = await ShouldRunOnStartupAsync();
@@ -105,7 +118,7 @@ public class SpotifyMissingTracksFetcher : BackgroundService
_logger.LogInformation("Running initial fetch on startup"); _logger.LogInformation("Running initial fetch on startup");
try try
{ {
await FetchMissingTracksAsync(stoppingToken); await FetchMissingTracksAsync(stoppingToken, bypassSyncWindowCheck: true);
_hasRunOnce = true; _hasRunOnce = true;
} }
catch (Exception ex) catch (Exception ex)
@@ -115,20 +128,21 @@ public class SpotifyMissingTracksFetcher : BackgroundService
} }
else else
{ {
_logger.LogInformation("Skipping startup fetch - already have cached files"); _logger.LogInformation("Skipping startup fetch - already have current files");
_hasRunOnce = true; _hasRunOnce = true;
} }
} }
// Background loop - check for new files every 5 minutes
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
try try
{ {
// Only fetch if we're past today's sync window AND we haven't fetched today yet
var shouldFetch = await ShouldFetchNowAsync(); var shouldFetch = await ShouldFetchNowAsync();
if (shouldFetch) if (shouldFetch)
{ {
await FetchMissingTracksAsync(stoppingToken); await FetchMissingTracksAsync(stoppingToken);
_hasRunOnce = true;
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -142,29 +156,42 @@ public class SpotifyMissingTracksFetcher : BackgroundService
private async Task<bool> ShouldFetchNowAsync() private async Task<bool> ShouldFetchNowAsync()
{ {
// Check if we have recent cache files (within last 24 hours) var settings = _spotifySettings.Value;
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var cacheThreshold = now.AddHours(-24);
// Calculate today's sync window
var todaySync = now.Date
.AddHours(settings.SyncStartHour)
.AddMinutes(settings.SyncStartMinute);
var todaySyncEnd = todaySync.AddHours(settings.SyncWindowHours);
// Only fetch if we're past today's sync window
if (now < todaySyncEnd)
{
return false;
}
// Check if we already have today's files
foreach (var playlistName in _playlistIdToName.Values) foreach (var playlistName in _playlistIdToName.Values)
{ {
var filePath = GetCacheFilePath(playlistName); var filePath = GetCacheFilePath(playlistName);
if (!File.Exists(filePath)) if (File.Exists(filePath))
{ {
// Missing cache file for this playlist var fileTime = File.GetLastWriteTimeUtc(filePath);
return true;
// If file is from today's sync or later, we already have it
if (fileTime >= todaySync)
{
continue;
}
} }
var fileTime = File.GetLastWriteTimeUtc(filePath); // Missing today's file for this playlist
if (fileTime < cacheThreshold) return true;
{
// Cache file is older than 24 hours
return true;
}
} }
// All playlists have recent cache files // All playlists have today's files
return false; return false;
} }
@@ -183,43 +210,120 @@ public class SpotifyMissingTracksFetcher : BackgroundService
{ {
_logger.LogInformation("=== STARTUP CACHE CHECK ==="); _logger.LogInformation("=== STARTUP CACHE CHECK ===");
var allPlaylistsHaveCache = true; var settings = _spotifySettings.Value;
var now = DateTime.UtcNow;
foreach (var playlistName in _playlistIdToName.Values) // Calculate today's sync window
var todaySync = now.Date
.AddHours(settings.SyncStartHour)
.AddMinutes(settings.SyncStartMinute);
var todaySyncEnd = todaySync.AddHours(settings.SyncWindowHours);
_logger.LogInformation("Today's sync window: {Start:yyyy-MM-dd HH:mm} - {End:yyyy-MM-dd HH:mm} UTC",
todaySync, todaySyncEnd);
_logger.LogInformation("Current time: {Now:yyyy-MM-dd HH:mm} UTC", now);
// If we're still before today's sync window end, we should have yesterday's or today's file
// Don't search again until after today's sync window ends
if (now < todaySyncEnd)
{ {
var filePath = GetCacheFilePath(playlistName); _logger.LogInformation("We're before today's sync window end - checking if we have recent cache...");
var cacheKey = $"spotify:missing:{playlistName}";
// Check file cache var allPlaylistsHaveCache = true;
if (File.Exists(filePath))
foreach (var playlistName in _playlistIdToName.Values)
{ {
var fileAge = DateTime.UtcNow - File.GetLastWriteTimeUtc(filePath); var filePath = GetCacheFilePath(playlistName);
_logger.LogInformation(" {Playlist}: Found file cache (age: {Age:F1}h)", playlistName, fileAge.TotalHours); var cacheKey = $"spotify:missing:{playlistName}";
// Load into Redis if not already there // Check file cache
if (!await _cache.ExistsAsync(cacheKey)) if (File.Exists(filePath))
{ {
await LoadFromFileCache(playlistName); var fileAge = DateTime.UtcNow - File.GetLastWriteTimeUtc(filePath);
_logger.LogInformation(" {Playlist}: Found file cache (age: {Age:F1}h)", playlistName, fileAge.TotalHours);
// Load into Redis if not already there
if (!await _cache.ExistsAsync(cacheKey))
{
await LoadFromFileCache(playlistName);
}
continue;
} }
continue;
// Check Redis cache
if (await _cache.ExistsAsync(cacheKey))
{
_logger.LogInformation(" {Playlist}: Found in Redis cache", playlistName);
continue;
}
// No cache found for this playlist
_logger.LogInformation(" {Playlist}: No cache found", playlistName);
allPlaylistsHaveCache = false;
} }
// Check Redis cache if (allPlaylistsHaveCache)
if (await _cache.ExistsAsync(cacheKey))
{ {
_logger.LogInformation(" {Playlist}: Found in Redis cache", playlistName); _logger.LogInformation("=== ALL PLAYLISTS HAVE CACHE - SKIPPING STARTUP FETCH ===");
continue; _logger.LogInformation("Will search again after {Time:yyyy-MM-dd HH:mm} UTC", todaySyncEnd);
return false;
} }
// No cache found for this playlist
_logger.LogInformation(" {Playlist}: No cache found", playlistName);
allPlaylistsHaveCache = false;
} }
if (allPlaylistsHaveCache) // If we're after today's sync window end, check if we already have today's file
if (now >= todaySyncEnd)
{ {
_logger.LogInformation("=== ALL PLAYLISTS HAVE CACHE - SKIPPING STARTUP FETCH ==="); _logger.LogInformation("We're after today's sync window end - checking if we already fetched today's files...");
return false;
var allPlaylistsHaveTodaysFile = true;
foreach (var playlistName in _playlistIdToName.Values)
{
var filePath = GetCacheFilePath(playlistName);
var cacheKey = $"spotify:missing:{playlistName}";
// Check if file exists and was created today (after sync start)
if (File.Exists(filePath))
{
var fileTime = File.GetLastWriteTimeUtc(filePath);
// File should be from today's sync window or later
if (fileTime >= todaySync)
{
var fileAge = DateTime.UtcNow - fileTime;
_logger.LogInformation(" {Playlist}: Have today's file (created {Time:yyyy-MM-dd HH:mm}, age: {Age:F1}h)",
playlistName, fileTime, fileAge.TotalHours);
// Load into Redis if not already there
if (!await _cache.ExistsAsync(cacheKey))
{
await LoadFromFileCache(playlistName);
}
continue;
}
else
{
_logger.LogInformation(" {Playlist}: File is old (from {Time:yyyy-MM-dd HH:mm}, before today's sync)",
playlistName, fileTime);
}
}
else
{
_logger.LogInformation(" {Playlist}: No file found", playlistName);
}
allPlaylistsHaveTodaysFile = false;
}
if (allPlaylistsHaveTodaysFile)
{
_logger.LogInformation("=== ALL PLAYLISTS HAVE TODAY'S FILES - SKIPPING STARTUP FETCH ===");
// Calculate when to search next (tomorrow after sync window)
var tomorrowSyncEnd = todaySyncEnd.AddDays(1);
_logger.LogInformation("Will search again after {Time:yyyy-MM-dd HH:mm} UTC", tomorrowSyncEnd);
return false;
}
} }
_logger.LogInformation("=== WILL FETCH ON STARTUP ==="); _logger.LogInformation("=== WILL FETCH ON STARTUP ===");
@@ -276,9 +380,32 @@ public class SpotifyMissingTracksFetcher : BackgroundService
} }
} }
private async Task FetchMissingTracksAsync(CancellationToken cancellationToken) private async Task FetchMissingTracksAsync(CancellationToken cancellationToken, bool bypassSyncWindowCheck = false)
{ {
_logger.LogInformation("=== FETCHING MISSING TRACKS ==="); 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); _logger.LogInformation("Processing {Count} playlists", _playlistIdToName.Count);
// Track when we find files to optimize search for other playlists // Track when we find files to optimize search for other playlists

View File

@@ -513,7 +513,6 @@ public class SpotifyTrackMatchingService : BackgroundService
matchedTracks.Count, tracksToMatch.Count, playlistName, isrcMatches, fuzzyMatches, noMatch); matchedTracks.Count, tracksToMatch.Count, playlistName, isrcMatches, fuzzyMatches, noMatch);
// Pre-build playlist items cache for instant serving // Pre-build playlist items cache for instant serving
// This is what makes the UI show all matched tracks at once
await PreBuildPlaylistItemsCacheAsync(playlistName, playlistConfig?.JellyfinId, spotifyTracks, matchedTracks, cancellationToken); await PreBuildPlaylistItemsCacheAsync(playlistName, playlistConfig?.JellyfinId, spotifyTracks, matchedTracks, cancellationToken);
} }
else else
@@ -554,7 +553,7 @@ public class SpotifyTrackMatchingService : BackgroundService
Song = song, Song = song,
// Use aggressive matching which follows optimal order internally // Use aggressive matching which follows optimal order internally
TitleScore = FuzzyMatcher.CalculateSimilarityAggressive(title, song.Title), TitleScore = FuzzyMatcher.CalculateSimilarityAggressive(title, song.Title),
ArtistScore = FuzzyMatcher.CalculateArtistMatchScore(artists, song.Artist, song.Contributors) ArtistScore = CalculateArtistMatchScore(artists, song.Artist, song.Contributors)
}) })
.Select(x => new .Select(x => new
{ {
@@ -640,7 +639,7 @@ public class SpotifyTrackMatchingService : BackgroundService
Song = song, Song = song,
// Use aggressive matching which follows optimal order internally // Use aggressive matching which follows optimal order internally
TitleScore = FuzzyMatcher.CalculateSimilarityAggressive(title, song.Title), TitleScore = FuzzyMatcher.CalculateSimilarityAggressive(title, song.Title),
ArtistScore = FuzzyMatcher.CalculateArtistMatchScore(artists, song.Artist, song.Contributors) ArtistScore = CalculateArtistMatchScore(artists, song.Artist, song.Contributors)
}) })
.Select(x => new .Select(x => new
{ {
@@ -744,7 +743,7 @@ public class SpotifyTrackMatchingService : BackgroundService
Song = song, Song = song,
TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, song.Title), TitleScore = FuzzyMatcher.CalculateSimilarity(track.Title, song.Title),
// Calculate artist score by checking ALL artists match // Calculate artist score by checking ALL artists match
ArtistScore = FuzzyMatcher.CalculateArtistMatchScore(track.Artists, song.Artist, song.Contributors) ArtistScore = CalculateArtistMatchScore(track.Artists, song.Artist, song.Contributors)
}) })
.Select(x => new .Select(x => new
{ {

View File

@@ -7,7 +7,6 @@ using allstarr.Models.Search;
using allstarr.Models.Subsonic; using allstarr.Models.Subsonic;
using allstarr.Services.Local; using allstarr.Services.Local;
using allstarr.Services.Common; using allstarr.Services.Common;
using allstarr.Services.Lyrics;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using IOFile = System.IO.File; using IOFile = System.IO.File;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@@ -15,48 +14,21 @@ using Microsoft.Extensions.Logging;
namespace allstarr.Services.SquidWTF; namespace allstarr.Services.SquidWTF;
/// <summary> /// <summary>
/// Handles track downloading from tidal.squid.wtf (no encryption, no auth required). /// Handles track downloading from tidal.squid.wtf (no encryption, no auth required)
/// /// Downloads are direct from Tidal's CDN via the squid.wtf proxy
/// Downloads are direct from Tidal's CDN via the squid.wtf proxy. The service:
/// 1. Fetches download info from hifi-api /track/ endpoint
/// 2. Decodes base64 manifest to get actual Tidal CDN URL
/// 3. Downloads directly from Tidal CDN (no decryption needed)
/// 4. Converts Tidal track ID to Spotify ID in parallel (for lyrics matching)
/// 5. Writes ID3/FLAC metadata tags and embeds cover art
///
/// Per hifi-api spec, the /track/ endpoint returns:
/// { "version": "2.0", "data": {
/// trackId, assetPresentation, audioMode, audioQuality,
/// manifestMimeType: "application/vnd.tidal.bts",
/// manifest: "base64-encoded-json",
/// albumReplayGain, trackReplayGain, bitDepth, sampleRate
/// }}
///
/// The manifest decodes to:
/// { "mimeType": "audio/flac", "codecs": "flac", "encryptionType": "NONE",
/// "urls": ["https://lgf.audio.tidal.com/mediatracks/..."] }
///
/// Quality Mapping:
/// - HI_RES → HI_RES_LOSSLESS (24-bit/192kHz FLAC)
/// - FLAC/LOSSLESS → LOSSLESS (16-bit/44.1kHz FLAC)
/// - HIGH → HIGH (320kbps AAC)
/// - LOW → LOW (96kbps AAC)
///
/// Features:
/// - Racing multiple endpoints for fastest download
/// - Automatic failover to backup endpoints
/// - Parallel Spotify ID conversion via Odesli
/// - Organized folder structure: Artist/Album/Track
/// - Unique filename resolution for duplicates
/// - Support for both cache and permanent storage modes
/// </summary> /// </summary>
public class SquidWTFDownloadService : BaseDownloadService public class SquidWTFDownloadService : BaseDownloadService
{ {
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly SemaphoreSlim _requestLock = new(1, 1);
private readonly SquidWTFSettings _squidwtfSettings; private readonly SquidWTFSettings _squidwtfSettings;
private readonly OdesliService _odesliService;
private readonly RoundRobinFallbackHelper _fallbackHelper; private DateTime _lastRequestTime = DateTime.MinValue;
private readonly IServiceProvider _serviceProvider; private readonly int _minRequestIntervalMs = 200;
private readonly List<string> _apiUrls;
private int _currentUrlIndex = 0;
private readonly object _urlIndexLock = new object();
protected override string ProviderName => "squidwtf"; protected override string ProviderName => "squidwtf";
@@ -69,26 +41,59 @@ public class SquidWTFDownloadService : BaseDownloadService
IOptions<SquidWTFSettings> SquidWTFSettings, IOptions<SquidWTFSettings> SquidWTFSettings,
IServiceProvider serviceProvider, IServiceProvider serviceProvider,
ILogger<SquidWTFDownloadService> logger, ILogger<SquidWTFDownloadService> logger,
OdesliService odesliService,
List<string> apiUrls) List<string> apiUrls)
: base(configuration, localLibraryService, metadataService, subsonicSettings.Value, serviceProvider, logger) : base(configuration, localLibraryService, metadataService, subsonicSettings.Value, serviceProvider, logger)
{ {
_httpClient = httpClientFactory.CreateClient(); _httpClient = httpClientFactory.CreateClient();
_squidwtfSettings = SquidWTFSettings.Value; _squidwtfSettings = SquidWTFSettings.Value;
_odesliService = odesliService; _apiUrls = apiUrls;
_fallbackHelper = new RoundRobinFallbackHelper(apiUrls, logger, "SquidWTF");
_serviceProvider = serviceProvider;
// Increase timeout for large downloads and slow endpoints
_httpClient.Timeout = TimeSpan.FromMinutes(5);
} }
/// <summary>
/// Tries the request with the next provider in round-robin, then falls back to others on failure.
/// This distributes load evenly across all providers while maintaining reliability.
/// </summary>
private async Task<T> TryWithFallbackAsync<T>(Func<string, Task<T>> action)
{
// Start with the next URL in round-robin to distribute load
var startIndex = 0;
lock (_urlIndexLock)
{
startIndex = _currentUrlIndex;
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
}
// Try all URLs starting from the round-robin selected one
for (int attempt = 0; attempt < _apiUrls.Count; attempt++)
{
var urlIndex = (startIndex + attempt) % _apiUrls.Count;
var baseUrl = _apiUrls[urlIndex];
try
{
Logger.LogDebug("Trying endpoint {Endpoint} (attempt {Attempt}/{Total})",
baseUrl, attempt + 1, _apiUrls.Count);
return await action(baseUrl);
}
catch (Exception ex)
{
Logger.LogWarning(ex, "Request failed with endpoint {Endpoint}, trying next...", baseUrl);
if (attempt == _apiUrls.Count - 1)
{
Logger.LogError("All {Count} SquidWTF endpoints failed", _apiUrls.Count);
throw;
}
}
}
throw new Exception("All SquidWTF endpoints failed");
}
#region BaseDownloadService Implementation #region BaseDownloadService Implementation
public override async Task<bool> IsAvailableAsync() public override async Task<bool> IsAvailableAsync()
{ {
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
var response = await _httpClient.GetAsync(baseUrl); var response = await _httpClient.GetAsync(baseUrl);
Console.WriteLine($"Response code from is available async: {response.IsSuccessStatusCode}"); Console.WriteLine($"Response code from is available async: {response.IsSuccessStatusCode}");
@@ -111,8 +116,8 @@ public class SquidWTFDownloadService : BaseDownloadService
{ {
var downloadInfo = await GetTrackDownloadInfoAsync(trackId, cancellationToken); var downloadInfo = await GetTrackDownloadInfoAsync(trackId, cancellationToken);
Logger.LogInformation("Track download URL obtained from hifi-api: {Url}", downloadInfo.DownloadUrl); Logger.LogInformation("Track token obtained: {Url}", downloadInfo.DownloadUrl);
Logger.LogInformation("Using format: {Format} (Quality: {Quality})", downloadInfo.MimeType, downloadInfo.AudioQuality); Logger.LogInformation("Using format: {Format}", downloadInfo.MimeType);
// Determine extension from MIME type // Determine extension from MIME type
var extension = downloadInfo.MimeType?.ToLower() switch var extension = downloadInfo.MimeType?.ToLower() switch
@@ -125,10 +130,7 @@ public class SquidWTFDownloadService : BaseDownloadService
// Build organized folder structure: Artist/Album/Track using AlbumArtist (fallback to Artist for singles) // Build organized folder structure: Artist/Album/Track using AlbumArtist (fallback to Artist for singles)
var artistForPath = song.AlbumArtist ?? song.Artist; var artistForPath = song.AlbumArtist ?? song.Artist;
// Cache mode uses downloads/cache/ folder, Permanent mode uses downloads/permanent/ var basePath = SubsonicSettings.StorageMode == StorageMode.Cache ? CachePath : DownloadPath;
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
? Path.Combine("downloads", "cache")
: Path.Combine("downloads", "permanent");
var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension); var outputPath = PathHelper.BuildTrackPath(basePath, artistForPath, song.Album, song.Title, song.Track, extension);
// Create directories if they don't exist // Create directories if they don't exist
@@ -138,53 +140,10 @@ public class SquidWTFDownloadService : BaseDownloadService
// Resolve unique path if file already exists // Resolve unique path if file already exists
outputPath = PathHelper.ResolveUniquePath(outputPath); outputPath = PathHelper.ResolveUniquePath(outputPath);
// Use round-robin with fallback for downloads to reduce CPU usage // Download from Tidal CDN (no authentication needed, token is in URL)
Logger.LogDebug("Using round-robin endpoint selection for download"); var response = await QueueRequestAsync(async () =>
var response = await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) =>
{ {
// Map quality settings to Tidal's quality levels per hifi-api spec using var request = new HttpRequestMessage(HttpMethod.Get, downloadInfo.DownloadUrl);
var quality = _squidwtfSettings.Quality?.ToUpperInvariant() switch
{
"FLAC" => "LOSSLESS",
"HI_RES" => "HI_RES_LOSSLESS",
"LOSSLESS" => "LOSSLESS",
"HIGH" => "HIGH",
"LOW" => "LOW",
_ => "LOSSLESS"
};
var url = $"{baseUrl}/track/?id={trackId}&quality={quality}";
// Get download info from this endpoint
var infoResponse = await _httpClient.GetAsync(url, cancellationToken);
infoResponse.EnsureSuccessStatusCode();
var json = await infoResponse.Content.ReadAsStringAsync(cancellationToken);
var doc = JsonDocument.Parse(json);
if (!doc.RootElement.TryGetProperty("data", out var data))
{
throw new Exception("Invalid response from API");
}
var manifestBase64 = data.GetProperty("manifest").GetString()
?? throw new Exception("No manifest in response");
// Decode base64 manifest to get actual CDN URL
var manifestJson = Encoding.UTF8.GetString(Convert.FromBase64String(manifestBase64));
var manifest = JsonDocument.Parse(manifestJson);
if (!manifest.RootElement.TryGetProperty("urls", out var urls) || urls.GetArrayLength() == 0)
{
throw new Exception("No download URLs in manifest");
}
var downloadUrl = urls[0].GetString()
?? throw new Exception("Download URL is null");
// Start the actual download from Tidal CDN (no encryption - squid.wtf handles everything)
using var request = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
request.Headers.Add("User-Agent", "Mozilla/5.0"); request.Headers.Add("User-Agent", "Mozilla/5.0");
request.Headers.Add("Accept", "*/*"); request.Headers.Add("Accept", "*/*");
@@ -202,26 +161,7 @@ public class SquidWTFDownloadService : BaseDownloadService
// Close file before writing metadata // Close file before writing metadata
await outputFile.DisposeAsync(); await outputFile.DisposeAsync();
// Start Spotify ID conversion in background (for lyrics support) // Write metadata and cover art
// This doesn't block streaming - lyrics endpoint will fetch it on-demand if needed
_ = Task.Run(async () =>
{
try
{
var spotifyId = await _odesliService.ConvertTidalToSpotifyIdAsync(trackId, CancellationToken.None);
if (!string.IsNullOrEmpty(spotifyId))
{
Logger.LogDebug("Background Spotify ID obtained for Tidal/{TrackId}: {SpotifyId}", trackId, spotifyId);
// Spotify ID is cached by Odesli service for future lyrics requests
}
}
catch (Exception ex)
{
Logger.LogDebug(ex, "Background Spotify ID conversion failed for Tidal/{TrackId}", trackId);
}
});
// Write metadata and cover art (without Spotify ID - it's only needed for lyrics)
await WriteMetadataAsync(outputPath, song, cancellationToken); await WriteMetadataAsync(outputPath, song, cancellationToken);
return outputPath; return outputPath;
@@ -231,22 +171,13 @@ public class SquidWTFDownloadService : BaseDownloadService
#region SquidWTF API Methods #region SquidWTF API Methods
/// <summary>
/// Gets track download information from hifi-api /track/ endpoint.
/// Per hifi-api spec: GET /track/?id={trackId}&quality={quality}
/// Returns: { "version": "2.0", "data": { trackId, assetPresentation, audioMode, audioQuality,
/// manifestMimeType, manifestHash, manifest (base64), albumReplayGain, trackReplayGain, bitDepth, sampleRate } }
/// The manifest is base64-encoded JSON containing: { mimeType, codecs, encryptionType, urls: [downloadUrl] }
/// Quality options: HI_RES_LOSSLESS (24-bit/192kHz FLAC), LOSSLESS (16-bit/44.1kHz FLAC), HIGH (320kbps AAC), LOW (96kbps AAC)
/// </summary>
private async Task<DownloadResult> GetTrackDownloadInfoAsync(string trackId, CancellationToken cancellationToken) private async Task<DownloadResult> GetTrackDownloadInfoAsync(string trackId, CancellationToken cancellationToken)
{ {
return await QueueRequestAsync(async () => return await QueueRequestAsync(async () =>
{ {
// Use round-robin with fallback instead of racing to reduce CPU usage return await TryWithFallbackAsync(async (baseUrl) =>
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) =>
{ {
// Map quality settings to Tidal's quality levels per hifi-api spec // Map quality settings to Tidal's quality levels
var quality = _squidwtfSettings.Quality?.ToUpperInvariant() switch var quality = _squidwtfSettings.Quality?.ToUpperInvariant() switch
{ {
"FLAC" => "LOSSLESS", "FLAC" => "LOSSLESS",
@@ -259,7 +190,7 @@ public class SquidWTFDownloadService : BaseDownloadService
var url = $"{baseUrl}/track/?id={trackId}&quality={quality}"; var url = $"{baseUrl}/track/?id={trackId}&quality={quality}";
Logger.LogDebug("Fetching track download info from: {Url}", url); Console.WriteLine($"%%%%%%%%%%%%%%%%%%% URL For downloads??: {url}");
var response = await _httpClient.GetAsync(url, cancellationToken); var response = await _httpClient.GetAsync(url, cancellationToken);
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
@@ -297,7 +228,8 @@ public class SquidWTFDownloadService : BaseDownloadService
? audioQualityEl.GetString() ? audioQualityEl.GetString()
: "LOSSLESS"; : "LOSSLESS";
Logger.LogInformation("Track download URL obtained from hifi-api: {Url}", downloadUrl); Logger.LogDebug("Decoded manifest - URL: {Url}, MIME: {MimeType}, Quality: {Quality}",
downloadUrl, mimeType, audioQuality);
return new DownloadResult return new DownloadResult
{ {
@@ -309,56 +241,29 @@ public class SquidWTFDownloadService : BaseDownloadService
}); });
} }
#endregion #endregion
#region Utility Methods #region Utility Methods
/// <summary> private async Task<T> QueueRequestAsync<T>(Func<Task<T>> action)
/// Converts Tidal track ID to Spotify ID for lyrics support.
/// Called in background after streaming starts.
/// Also prefetches lyrics immediately after conversion.
/// </summary>
protected override async Task ConvertToSpotifyIdAsync(string externalProvider, string externalId)
{ {
if (externalProvider != "squidwtf") await _requestLock.WaitAsync();
try
{ {
return; var now = DateTime.UtcNow;
} var timeSinceLastRequest = (now - _lastRequestTime).TotalMilliseconds;
var spotifyId = await _odesliService.ConvertTidalToSpotifyIdAsync(externalId, CancellationToken.None); if (timeSinceLastRequest < _minRequestIntervalMs)
if (!string.IsNullOrEmpty(spotifyId))
{
Logger.LogDebug("Background Spotify ID obtained for Tidal/{TrackId}: {SpotifyId}", externalId, spotifyId);
// Immediately prefetch lyrics now that we have the Spotify ID
// This ensures lyrics are cached and ready when the client requests them
_ = Task.Run(async () =>
{ {
try await Task.Delay((int)(_minRequestIntervalMs - timeSinceLastRequest));
{ }
using var scope = _serviceProvider.CreateScope();
var spotifyLyricsService = scope.ServiceProvider.GetService<SpotifyLyricsService>();
if (spotifyLyricsService != null) _lastRequestTime = DateTime.UtcNow;
{ return await action();
var lyrics = await spotifyLyricsService.GetLyricsByTrackIdAsync(spotifyId); }
if (lyrics != null && lyrics.Lines.Count > 0) finally
{ {
Logger.LogDebug("Background lyrics prefetched for Spotify/{SpotifyId}: {LineCount} lines", _requestLock.Release();
spotifyId, lyrics.Lines.Count);
}
else
{
Logger.LogDebug("No lyrics available for Spotify/{SpotifyId}", spotifyId);
}
}
}
catch (Exception ex)
{
Logger.LogDebug(ex, "Background lyrics prefetch failed for Spotify/{SpotifyId}", spotifyId);
}
});
} }
} }

View File

@@ -12,41 +12,7 @@ using System.Text.Json.Nodes;
namespace allstarr.Services.SquidWTF; namespace allstarr.Services.SquidWTF;
/// <summary> /// <summary>
/// Metadata service implementation using the SquidWTF API (free, no key required). /// Metadata service implementation using the SquidWTF API (free, no key required)
///
/// SquidWTF is a proxy to Tidal's API that provides free access to Tidal's music catalog.
/// This implementation follows the hifi-api specification documented at the forked repository.
///
/// API Endpoints (per hifi-api spec):
/// - GET /search/?s={query} - Search tracks (returns data.items array)
/// - GET /search/?a={query} - Search artists (returns data.artists.items array)
/// - GET /search/?al={query} - Search albums (returns data.albums.items array, undocumented)
/// - GET /search/?p={query} - Search playlists (returns data.playlists.items array, undocumented)
/// - GET /info/?id={trackId} - Get track metadata (returns data object with full track info)
/// - GET /track/?id={trackId}&quality={quality} - Get track download info (returns manifest)
/// - GET /album/?id={albumId} - Get album with tracks (undocumented, returns data.items array)
/// - GET /artist/?f={artistId} - Get artist with albums (undocumented, returns albums.items array)
/// - GET /playlist/?id={playlistId} - Get playlist with tracks (undocumented)
///
/// Quality Options:
/// - HI_RES_LOSSLESS: 24-bit/192kHz FLAC
/// - LOSSLESS: 16-bit/44.1kHz FLAC
/// - HIGH: 320kbps AAC
/// - LOW: 96kbps AAC
///
/// Response Structure:
/// All responses follow: { "version": "2.0", "data": { ... } }
/// Track objects include: id, title, duration, trackNumber, volumeNumber, explicit, bpm, isrc,
/// artist (singular), artists (array), album (object with id, title, cover UUID)
/// Cover art URLs: https://resources.tidal.com/images/{uuid-with-slashes}/{size}.jpg
///
/// Features:
/// - Round-robin load balancing across multiple mirror endpoints
/// - Automatic failover to backup endpoints on failure
/// - Racing endpoints for fastest response on latency-sensitive operations
/// - Redis caching for albums and artists (24-hour TTL)
/// - Explicit content filtering support
/// - Parallel Spotify ID conversion via Odesli for lyrics matching
/// </summary> /// </summary>
public class SquidWTFMetadataService : IMusicMetadataService public class SquidWTFMetadataService : IMusicMetadataService
@@ -55,7 +21,9 @@ public class SquidWTFMetadataService : IMusicMetadataService
private readonly SubsonicSettings _settings; private readonly SubsonicSettings _settings;
private readonly ILogger<SquidWTFMetadataService> _logger; private readonly ILogger<SquidWTFMetadataService> _logger;
private readonly RedisCacheService _cache; private readonly RedisCacheService _cache;
private readonly RoundRobinFallbackHelper _fallbackHelper; private readonly List<string> _apiUrls;
private int _currentUrlIndex = 0;
private readonly object _urlIndexLock = new object();
public SquidWTFMetadataService( public SquidWTFMetadataService(
IHttpClientFactory httpClientFactory, IHttpClientFactory httpClientFactory,
@@ -69,33 +37,79 @@ public class SquidWTFMetadataService : IMusicMetadataService
_settings = settings.Value; _settings = settings.Value;
_logger = logger; _logger = logger;
_cache = cache; _cache = cache;
_fallbackHelper = new RoundRobinFallbackHelper(apiUrls, logger, "SquidWTF"); _apiUrls = apiUrls;
// Set up default headers // Set up default headers
_httpClient.DefaultRequestHeaders.Add("User-Agent", _httpClient.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0"); "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0");
// Increase timeout for large artist/album responses (some artists have 100+ albums)
_httpClient.Timeout = TimeSpan.FromMinutes(5);
} }
/// <summary>
/// Gets the next URL in round-robin fashion to distribute load across providers
/// </summary>
private string GetNextBaseUrl()
{
lock (_urlIndexLock)
{
var url = _apiUrls[_currentUrlIndex];
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
return url;
}
}
/// <summary>
/// Tries the request with the next provider in round-robin, then falls back to others on failure.
/// This distributes load evenly across all providers while maintaining reliability.
/// </summary>
private async Task<T> TryWithFallbackAsync<T>(Func<string, Task<T>> action, T defaultValue)
{
// Start with the next URL in round-robin to distribute load
var startIndex = 0;
lock (_urlIndexLock)
{
startIndex = _currentUrlIndex;
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
}
// Try all URLs starting from the round-robin selected one
for (int attempt = 0; attempt < _apiUrls.Count; attempt++)
{
var urlIndex = (startIndex + attempt) % _apiUrls.Count;
var baseUrl = _apiUrls[urlIndex];
try
{
_logger.LogDebug("Trying endpoint {Endpoint} (attempt {Attempt}/{Total})",
baseUrl, attempt + 1, _apiUrls.Count);
return await action(baseUrl);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Request failed with endpoint {Endpoint}, trying next...", baseUrl);
if (attempt == _apiUrls.Count - 1)
{
_logger.LogError("All {Count} SquidWTF endpoints failed", _apiUrls.Count);
return defaultValue;
}
}
}
return defaultValue;
}
public async Task<List<Song>> SearchSongsAsync(string query, int limit = 20) public async Task<List<Song>> SearchSongsAsync(string query, int limit = 20)
{ {
// Race all endpoints for fastest search results return await TryWithFallbackAsync(async (baseUrl) =>
return await _fallbackHelper.RaceAllEndpointsAsync(async (baseUrl, ct) =>
{ {
// Use 's' parameter for track search as per hifi-api spec
var url = $"{baseUrl}/search/?s={Uri.EscapeDataString(query)}"; var url = $"{baseUrl}/search/?s={Uri.EscapeDataString(query)}";
var response = await _httpClient.GetAsync(url, ct); var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
throw new HttpRequestException($"HTTP {response.StatusCode}"); throw new HttpRequestException($"HTTP {response.StatusCode}");
} }
var json = await response.Content.ReadAsStringAsync(ct); var json = await response.Content.ReadAsStringAsync();
// Check for error in response body // Check for error in response body
var result = JsonDocument.Parse(json); var result = JsonDocument.Parse(json);
@@ -106,7 +120,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
} }
var songs = new List<Song>(); var songs = new List<Song>();
// Per hifi-api spec: track search returns data.items array
if (result.RootElement.TryGetProperty("data", out var data) && if (result.RootElement.TryGetProperty("data", out var data) &&
data.TryGetProperty("items", out var items)) data.TryGetProperty("items", out var items))
{ {
@@ -116,36 +129,30 @@ public class SquidWTFMetadataService : IMusicMetadataService
if (count >= limit) break; if (count >= limit) break;
var song = ParseTidalTrack(track); var song = ParseTidalTrack(track);
if (ShouldIncludeSong(song)) songs.Add(song);
{
songs.Add(song);
}
count++; count++;
} }
} }
return songs; return songs;
}); }, new List<Song>());
} }
public async Task<List<Album>> SearchAlbumsAsync(string query, int limit = 20) public async Task<List<Album>> SearchAlbumsAsync(string query, int limit = 20)
{ {
// Race all endpoints for fastest search results return await TryWithFallbackAsync(async (baseUrl) =>
return await _fallbackHelper.RaceAllEndpointsAsync(async (baseUrl, ct) =>
{ {
// Note: hifi-api doesn't document album search, but 'al' parameter is commonly used
var url = $"{baseUrl}/search/?al={Uri.EscapeDataString(query)}"; var url = $"{baseUrl}/search/?al={Uri.EscapeDataString(query)}";
var response = await _httpClient.GetAsync(url, ct); var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
throw new HttpRequestException($"HTTP {response.StatusCode}"); return new List<Album>();
} }
var json = await response.Content.ReadAsStringAsync(ct); var json = await response.Content.ReadAsStringAsync();
var result = JsonDocument.Parse(json); var result = JsonDocument.Parse(json);
var albums = new List<Album>(); var albums = new List<Album>();
// Per hifi-api spec: album search returns data.albums.items array
if (result.RootElement.TryGetProperty("data", out var data) && if (result.RootElement.TryGetProperty("data", out var data) &&
data.TryGetProperty("albums", out var albumsObj) && data.TryGetProperty("albums", out var albumsObj) &&
albumsObj.TryGetProperty("items", out var items)) albumsObj.TryGetProperty("items", out var items))
@@ -161,31 +168,28 @@ public class SquidWTFMetadataService : IMusicMetadataService
} }
return albums; return albums;
}); }, new List<Album>());
} }
public async Task<List<Artist>> SearchArtistsAsync(string query, int limit = 20) public async Task<List<Artist>> SearchArtistsAsync(string query, int limit = 20)
{ {
// Race all endpoints for fastest search results return await TryWithFallbackAsync(async (baseUrl) =>
return await _fallbackHelper.RaceAllEndpointsAsync(async (baseUrl, ct) =>
{ {
// Per hifi-api spec: use 'a' parameter for artist search
var url = $"{baseUrl}/search/?a={Uri.EscapeDataString(query)}"; var url = $"{baseUrl}/search/?a={Uri.EscapeDataString(query)}";
_logger.LogInformation("🔍 SQUIDWTF: Searching artists with URL: {Url}", url); _logger.LogInformation("🔍 SQUIDWTF: Searching artists with URL: {Url}", url);
var response = await _httpClient.GetAsync(url, ct); var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
_logger.LogWarning("⚠️ SQUIDWTF: Artist search failed with status {StatusCode}", response.StatusCode); _logger.LogWarning("⚠️ SQUIDWTF: Artist search failed with status {StatusCode}", response.StatusCode);
throw new HttpRequestException($"HTTP {response.StatusCode}"); return new List<Artist>();
} }
var json = await response.Content.ReadAsStringAsync(ct); var json = await response.Content.ReadAsStringAsync();
var result = JsonDocument.Parse(json); var result = JsonDocument.Parse(json);
var artists = new List<Artist>(); var artists = new List<Artist>();
// Per hifi-api spec: artist search returns data.artists.items array
if (result.RootElement.TryGetProperty("data", out var data) && if (result.RootElement.TryGetProperty("data", out var data) &&
data.TryGetProperty("artists", out var artistsObj) && data.TryGetProperty("artists", out var artistsObj) &&
artistsObj.TryGetProperty("items", out var items)) artistsObj.TryGetProperty("items", out var items))
@@ -204,14 +208,13 @@ public class SquidWTFMetadataService : IMusicMetadataService
_logger.LogInformation("✓ SQUIDWTF: Artist search returned {Count} results", artists.Count); _logger.LogInformation("✓ SQUIDWTF: Artist search returned {Count} results", artists.Count);
return artists; return artists;
}); }, new List<Artist>());
} }
public async Task<List<ExternalPlaylist>> SearchPlaylistsAsync(string query, int limit = 20) public async Task<List<ExternalPlaylist>> SearchPlaylistsAsync(string query, int limit = 20)
{ {
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
// Per hifi-api spec: use 'p' parameter for playlist search
var url = $"{baseUrl}/search/?p={Uri.EscapeDataString(query)}"; var url = $"{baseUrl}/search/?p={Uri.EscapeDataString(query)}";
var response = await _httpClient.GetAsync(url); var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode) return new List<ExternalPlaylist>(); if (!response.IsSuccessStatusCode) return new List<ExternalPlaylist>();
@@ -220,20 +223,15 @@ public class SquidWTFMetadataService : IMusicMetadataService
var result = JsonDocument.Parse(json); var result = JsonDocument.Parse(json);
var playlists = new List<ExternalPlaylist>(); var playlists = new List<ExternalPlaylist>();
// Per hifi-api spec: playlist search returns data.playlists.items array
if (result.RootElement.TryGetProperty("data", out var data) && if (result.RootElement.TryGetProperty("data", out var data) &&
data.TryGetProperty("playlists", out var playlistObj) && data.TryGetProperty("playlists", out var playlistObj) &&
playlistObj.TryGetProperty("items", out var items)) playlistObj.TryGetProperty("items", out var items))
{ {
int count = 0;
foreach(var playlist in items.EnumerateArray()) foreach(var playlist in items.EnumerateArray())
{ {
if (count >= limit) break;
try try
{ {
playlists.Add(ParseTidalPlaylist(playlist)); playlists.Add(ParseTidalPlaylist(playlist));
count++;
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -269,9 +267,8 @@ public class SquidWTFMetadataService : IMusicMetadataService
{ {
if (externalProvider != "squidwtf") return null; if (externalProvider != "squidwtf") return null;
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
// Per hifi-api spec: GET /info/?id={trackId} returns track metadata
var url = $"{baseUrl}/info/?id={externalId}"; var url = $"{baseUrl}/info/?id={externalId}";
var response = await _httpClient.GetAsync(url); var response = await _httpClient.GetAsync(url);
@@ -280,16 +277,10 @@ public class SquidWTFMetadataService : IMusicMetadataService
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
var result = JsonDocument.Parse(json); var result = JsonDocument.Parse(json);
// Per hifi-api spec: response is { "version": "2.0", "data": { track object } }
if (!result.RootElement.TryGetProperty("data", out var track)) if (!result.RootElement.TryGetProperty("data", out var track))
return null; return null;
var song = ParseTidalTrackFull(track); return ParseTidalTrackFull(track);
// NOTE: Spotify ID conversion happens during download (in SquidWTFDownloadService)
// This avoids redundant conversions and ensures it's done in parallel with the download
return song;
}, (Song?)null); }, (Song?)null);
} }
@@ -302,9 +293,8 @@ public class SquidWTFMetadataService : IMusicMetadataService
var cached = await _cache.GetAsync<Album>(cacheKey); var cached = await _cache.GetAsync<Album>(cacheKey);
if (cached != null) return cached; if (cached != null) return cached;
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
// Note: hifi-api doesn't document album endpoint, but /album/?id={albumId} is commonly used
var url = $"{baseUrl}/album/?id={externalId}"; var url = $"{baseUrl}/album/?id={externalId}";
var response = await _httpClient.GetAsync(url); var response = await _httpClient.GetAsync(url);
@@ -313,18 +303,17 @@ public class SquidWTFMetadataService : IMusicMetadataService
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
var result = JsonDocument.Parse(json); var result = JsonDocument.Parse(json);
// Response structure: { "data": { album object with "items" array of tracks } }
if (!result.RootElement.TryGetProperty("data", out var albumElement)) if (!result.RootElement.TryGetProperty("data", out var albumElement))
return null; return null;
var album = ParseTidalAlbum(albumElement); var album = ParseTidalAlbum(albumElement);
// Get album tracks from items array // Get album tracks
if (albumElement.TryGetProperty("items", out var tracks)) if (albumElement.TryGetProperty("items", out var tracks))
{ {
foreach (var trackWrapper in tracks.EnumerateArray()) foreach (var trackWrapper in tracks.EnumerateArray())
{ {
// Each item is wrapped: { "item": { track object } }
if (trackWrapper.TryGetProperty("item", out var track)) if (trackWrapper.TryGetProperty("item", out var track))
{ {
var song = ParseTidalTrack(track); var song = ParseTidalTrack(track);
@@ -358,9 +347,8 @@ public class SquidWTFMetadataService : IMusicMetadataService
return cached; return cached;
} }
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
// Note: hifi-api doesn't document artist endpoint, but /artist/?f={artistId} is commonly used
var url = $"{baseUrl}/artist/?f={externalId}"; var url = $"{baseUrl}/artist/?f={externalId}";
_logger.LogInformation("Fetching artist from {Url}", url); _logger.LogInformation("Fetching artist from {Url}", url);
@@ -378,8 +366,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
JsonElement? artistSource = null; JsonElement? artistSource = null;
int albumCount = 0; int albumCount = 0;
// Response structure: { "albums": { "items": [ album objects ] }, "tracks": [ track objects ] } // Try to get artist from albums.items[0].artist
// Extract artist info from albums.items[0].artist (most reliable source)
if (result.RootElement.TryGetProperty("albums", out var albums) && if (result.RootElement.TryGetProperty("albums", out var albums) &&
albums.TryGetProperty("items", out var albumItems) && albums.TryGetProperty("items", out var albumItems) &&
albumItems.GetArrayLength() > 0) albumItems.GetArrayLength() > 0)
@@ -411,7 +398,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
} }
var artistElement = artistSource.Value; var artistElement = artistSource.Value;
// Normalize artist data to include album count
var normalizedArtist = new JsonObject var normalizedArtist = new JsonObject
{ {
["id"] = artistElement.GetProperty("id").GetInt64(), ["id"] = artistElement.GetProperty("id").GetInt64(),
@@ -436,11 +422,10 @@ public class SquidWTFMetadataService : IMusicMetadataService
{ {
if (externalProvider != "squidwtf") return new List<Album>(); if (externalProvider != "squidwtf") return new List<Album>();
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
_logger.LogInformation("GetArtistAlbumsAsync called for SquidWTF artist {ExternalId}", externalId); _logger.LogInformation("GetArtistAlbumsAsync called for SquidWTF artist {ExternalId}", externalId);
// Note: hifi-api doesn't document artist endpoint, but /artist/?f={artistId} is commonly used
var url = $"{baseUrl}/artist/?f={externalId}"; var url = $"{baseUrl}/artist/?f={externalId}";
_logger.LogInformation("Fetching artist albums from URL: {Url}", url); _logger.LogInformation("Fetching artist albums from URL: {Url}", url);
var response = await _httpClient.GetAsync(url); var response = await _httpClient.GetAsync(url);
@@ -457,7 +442,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
var albums = new List<Album>(); var albums = new List<Album>();
// Response structure: { "albums": { "items": [ album objects ] } }
if (result.RootElement.TryGetProperty("albums", out var albumsObj) && if (result.RootElement.TryGetProperty("albums", out var albumsObj) &&
albumsObj.TryGetProperty("items", out var items)) albumsObj.TryGetProperty("items", out var items))
{ {
@@ -483,9 +467,8 @@ public class SquidWTFMetadataService : IMusicMetadataService
{ {
if (externalProvider != "squidwtf") return null; if (externalProvider != "squidwtf") return null;
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
// Note: hifi-api doesn't document playlist endpoint, but /playlist/?id={playlistId} is commonly used
var url = $"{baseUrl}/playlist/?id={externalId}"; var url = $"{baseUrl}/playlist/?id={externalId}";
var response = await _httpClient.GetAsync(url); var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode) return null; if (!response.IsSuccessStatusCode) return null;
@@ -493,10 +476,8 @@ public class SquidWTFMetadataService : IMusicMetadataService
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
var playlistElement = JsonDocument.Parse(json).RootElement; var playlistElement = JsonDocument.Parse(json).RootElement;
// Check for error response
if (playlistElement.TryGetProperty("error", out _)) return null; if (playlistElement.TryGetProperty("error", out _)) return null;
// Response structure: { "playlist": { playlist object }, "items": [ track wrappers ] }
return ParseTidalPlaylist(playlistElement); return ParseTidalPlaylist(playlistElement);
}, (ExternalPlaylist?)null); }, (ExternalPlaylist?)null);
} }
@@ -505,9 +486,8 @@ public class SquidWTFMetadataService : IMusicMetadataService
{ {
if (externalProvider != "squidwtf") return new List<Song>(); if (externalProvider != "squidwtf") return new List<Song>();
return await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => return await TryWithFallbackAsync(async (baseUrl) =>
{ {
// Note: hifi-api doesn't document playlist endpoint, but /playlist/?id={playlistId} is commonly used
var url = $"{baseUrl}/playlist/?id={externalId}"; var url = $"{baseUrl}/playlist/?id={externalId}";
var response = await _httpClient.GetAsync(url); var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode) return new List<Song>(); if (!response.IsSuccessStatusCode) return new List<Song>();
@@ -515,13 +495,11 @@ public class SquidWTFMetadataService : IMusicMetadataService
var json = await response.Content.ReadAsStringAsync(); var json = await response.Content.ReadAsStringAsync();
var playlistElement = JsonDocument.Parse(json).RootElement; var playlistElement = JsonDocument.Parse(json).RootElement;
// Check for error response
if (playlistElement.TryGetProperty("error", out _)) return new List<Song>(); if (playlistElement.TryGetProperty("error", out _)) return new List<Song>();
JsonElement? playlist = null; JsonElement? playlist = null;
JsonElement? tracks = null; JsonElement? tracks = null;
// Response structure: { "playlist": { playlist object }, "items": [ track wrappers ] }
if (playlistElement.TryGetProperty("playlist", out var playlistEl)) if (playlistElement.TryGetProperty("playlist", out var playlistEl))
{ {
playlist = playlistEl; playlist = playlistEl;
@@ -544,7 +522,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
int trackIndex = 1; int trackIndex = 1;
foreach (var entry in tracks.Value.EnumerateArray()) foreach (var entry in tracks.Value.EnumerateArray())
{ {
// Each item is wrapped: { "item": { track object } }
if (!entry.TryGetProperty("item", out var track)) if (!entry.TryGetProperty("item", out var track))
continue; continue;
@@ -567,14 +544,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
// --- Parser functions start here --- // --- Parser functions start here ---
/// <summary>
/// Parses a Tidal track object from hifi-api search/album/playlist responses.
/// Per hifi-api spec, track objects contain: id, title, duration, trackNumber, volumeNumber,
/// explicit, artist (singular), artists (array), album (object with id, title, cover).
/// </summary>
/// <param name="track">JSON element containing track data</param>
/// <param name="fallbackTrackNumber">Optional track number to use if not present in JSON</param>
/// <returns>Parsed Song object</returns>
private Song ParseTidalTrack(JsonElement track, int? fallbackTrackNumber = null) private Song ParseTidalTrack(JsonElement track, int? fallbackTrackNumber = null)
{ {
var externalId = track.GetProperty("id").GetInt64().ToString(); var externalId = track.GetProperty("id").GetInt64().ToString();
@@ -664,13 +633,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
}; };
} }
/// <summary>
/// Parses a full Tidal track object from hifi-api /info/ endpoint.
/// Per hifi-api spec, full track objects include additional metadata: bpm, isrc, key, keyScale,
/// streamStartDate (for year), copyright, replayGain, peak, audioQuality, audioModes.
/// </summary>
/// <param name="track">JSON element containing full track data</param>
/// <returns>Parsed Song object with extended metadata</returns>
private Song ParseTidalTrackFull(JsonElement track) private Song ParseTidalTrackFull(JsonElement track)
{ {
var externalId = track.GetProperty("id").GetInt64().ToString(); var externalId = track.GetProperty("id").GetInt64().ToString();
@@ -792,13 +754,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
}; };
} }
/// <summary>
/// Parses a Tidal album object from hifi-api responses.
/// Per hifi-api spec, album objects contain: id, title, releaseDate, numberOfTracks,
/// cover (UUID), artist (object) or artists (array).
/// </summary>
/// <param name="album">JSON element containing album data</param>
/// <returns>Parsed Album object</returns>
private Album ParseTidalAlbum(JsonElement album) private Album ParseTidalAlbum(JsonElement album)
{ {
var externalId = album.GetProperty("id").GetInt64().ToString(); var externalId = album.GetProperty("id").GetInt64().ToString();
@@ -852,13 +807,8 @@ public class SquidWTFMetadataService : IMusicMetadataService
}; };
} }
/// <summary> // TODO: Think of a way to implement album count when this function is called by search function
/// Parses a Tidal artist object from hifi-api responses. // as the API endpoint in search does not include this data
/// Per hifi-api spec, artist objects contain: id, name, picture (UUID).
/// Note: albums_count is not in the standard API response but is added by GetArtistAsync.
/// </summary>
/// <param name="artist">JSON element containing artist data</param>
/// <returns>Parsed Artist object</returns>
private Artist ParseTidalArtist(JsonElement artist) private Artist ParseTidalArtist(JsonElement artist)
{ {
var externalId = artist.GetProperty("id").GetInt64().ToString(); var externalId = artist.GetProperty("id").GetInt64().ToString();
@@ -884,14 +834,6 @@ public class SquidWTFMetadataService : IMusicMetadataService
}; };
} }
/// <summary>
/// Parses a Tidal playlist from hifi-api /playlist/ endpoint response.
/// Per hifi-api spec (undocumented), response structure is:
/// { "playlist": { uuid, title, description, creator, created, numberOfTracks, duration, squareImage },
/// "items": [ { "item": { track object } } ] }
/// </summary>
/// <param name="playlistElement">Root JSON element containing playlist and items</param>
/// <returns>Parsed ExternalPlaylist object</returns>
private ExternalPlaylist ParseTidalPlaylist(JsonElement playlistElement) private ExternalPlaylist ParseTidalPlaylist(JsonElement playlistElement)
{ {
JsonElement? playlist = null; JsonElement? playlist = null;

View File

@@ -3,7 +3,6 @@ using System.Text.Json;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using allstarr.Models.Settings; using allstarr.Models.Settings;
using allstarr.Services.Validation; using allstarr.Services.Validation;
using allstarr.Services.Common;
namespace allstarr.Services.SquidWTF; namespace allstarr.Services.SquidWTF;
@@ -13,26 +12,56 @@ namespace allstarr.Services.SquidWTF;
public class SquidWTFStartupValidator : BaseStartupValidator public class SquidWTFStartupValidator : BaseStartupValidator
{ {
private readonly SquidWTFSettings _settings; private readonly SquidWTFSettings _settings;
private readonly RoundRobinFallbackHelper _fallbackHelper; private readonly List<string> _apiUrls;
private readonly EndpointBenchmarkService _benchmarkService; private int _currentUrlIndex = 0;
private readonly ILogger<SquidWTFStartupValidator> _logger; private readonly object _urlIndexLock = new object();
public override string ServiceName => "SquidWTF"; public override string ServiceName => "SquidWTF";
public SquidWTFStartupValidator( public SquidWTFStartupValidator(IOptions<SquidWTFSettings> settings, HttpClient httpClient, List<string> apiUrls)
IOptions<SquidWTFSettings> settings,
HttpClient httpClient,
List<string> apiUrls,
EndpointBenchmarkService benchmarkService,
ILogger<SquidWTFStartupValidator> logger)
: base(httpClient) : base(httpClient)
{ {
_settings = settings.Value; _settings = settings.Value;
_fallbackHelper = new RoundRobinFallbackHelper(apiUrls, logger, "SquidWTF"); _apiUrls = apiUrls;
_benchmarkService = benchmarkService;
_logger = logger;
} }
/// <summary>
/// Tries the request with the next provider in round-robin, then falls back to others on failure.
/// This distributes load evenly across all providers while maintaining reliability.
/// </summary>
private async Task<T> TryWithFallbackAsync<T>(Func<string, Task<T>> action, T defaultValue)
{
// Start with the next URL in round-robin to distribute load
var startIndex = 0;
lock (_urlIndexLock)
{
startIndex = _currentUrlIndex;
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
}
// Try all URLs starting from the round-robin selected one
for (int attempt = 0; attempt < _apiUrls.Count; attempt++)
{
var urlIndex = (startIndex + attempt) % _apiUrls.Count;
var baseUrl = _apiUrls[urlIndex];
try
{
return await action(baseUrl);
}
catch
{
WriteDetail($"Endpoint {baseUrl} failed, trying next...");
if (attempt == _apiUrls.Count - 1)
{
WriteDetail($"All {_apiUrls.Count} endpoints failed");
return defaultValue;
}
}
}
return defaultValue;
}
public override async Task<ValidationResult> ValidateAsync(CancellationToken cancellationToken) public override async Task<ValidationResult> ValidateAsync(CancellationToken cancellationToken)
{ {
@@ -50,49 +79,8 @@ public class SquidWTFStartupValidator : BaseStartupValidator
WriteStatus("SquidWTF Quality", quality, ConsoleColor.Cyan); WriteStatus("SquidWTF Quality", quality, ConsoleColor.Cyan);
// Benchmark all endpoints to determine fastest
var apiUrls = _fallbackHelper.EndpointCount > 0
? Enumerable.Range(0, _fallbackHelper.EndpointCount).Select(_ => "").ToList() // Placeholder, we'll get actual URLs from fallback helper
: new List<string>();
// Get the actual API URLs by reflection (not ideal, but works for now)
var fallbackHelperType = _fallbackHelper.GetType();
var apiUrlsField = fallbackHelperType.GetField("_apiUrls", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (apiUrlsField != null)
{
apiUrls = (List<string>)apiUrlsField.GetValue(_fallbackHelper)!;
}
if (apiUrls.Count > 1)
{
WriteStatus("Benchmarking Endpoints", $"{apiUrls.Count} endpoints", ConsoleColor.Cyan);
var orderedEndpoints = await _benchmarkService.BenchmarkEndpointsAsync(
apiUrls,
async (endpoint, ct) =>
{
try
{
var response = await _httpClient.GetAsync(endpoint, ct);
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
},
pingCount: 2,
cancellationToken);
if (orderedEndpoints.Count > 0)
{
_fallbackHelper.SetEndpointOrder(orderedEndpoints);
WriteDetail($"Fastest endpoint: {orderedEndpoints.First()}");
}
}
// Test connectivity with fallback // Test connectivity with fallback
var result = await _fallbackHelper.TryWithFallbackAsync(async (baseUrl) => var result = await TryWithFallbackAsync(async (baseUrl) =>
{ {
var response = await _httpClient.GetAsync(baseUrl, cancellationToken); var response = await _httpClient.GetAsync(baseUrl, cancellationToken);

View File

@@ -32,8 +32,7 @@
"EnableExternalPlaylists": true "EnableExternalPlaylists": true
}, },
"Library": { "Library": {
"DownloadPath": "./downloads", "DownloadPath": "./downloads"
"KeptPath": "/app/kept"
}, },
"Qobuz": { "Qobuz": {
"UserAuthToken": "your-qobuz-token", "UserAuthToken": "your-qobuz-token",

View File

@@ -539,7 +539,6 @@
<div class="tab" data-tab="jellyfin-playlists">Link Playlists</div> <div class="tab" data-tab="jellyfin-playlists">Link Playlists</div>
<div class="tab" data-tab="playlists">Active Playlists</div> <div class="tab" data-tab="playlists">Active Playlists</div>
<div class="tab" data-tab="config">Configuration</div> <div class="tab" data-tab="config">Configuration</div>
<div class="tab" data-tab="endpoints">API Analytics</div>
</div> </div>
<!-- Dashboard Tab --> <!-- Dashboard Tab -->
@@ -645,18 +644,12 @@
<!-- Active Playlists Tab --> <!-- Active Playlists Tab -->
<div class="tab-content" id="tab-playlists"> <div class="tab-content" id="tab-playlists">
<!-- Warning Banner (hidden by default) -->
<div id="matching-warning-banner" style="display:none;background:#f59e0b;color:#000;padding:16px;border-radius:8px;margin-bottom:16px;font-weight:600;text-align:center;box-shadow:0 4px 6px rgba(0,0,0,0.1);">
⚠️ TRACK MATCHING IN PROGRESS - Please wait for matching to complete before making changes to playlists or mappings!
</div>
<div class="card"> <div class="card">
<h2> <h2>
Active Spotify Playlists Active Spotify Playlists
<div class="actions"> <div class="actions">
<button onclick="matchAllPlaylists()" title="Match tracks for all playlists against your local library and external providers. This may take several minutes.">Match All Tracks</button> <button onclick="matchAllPlaylists()">Match All Tracks</button>
<button onclick="refreshPlaylists()" title="Fetch the latest playlist data from Spotify without re-matching tracks.">Refresh All</button> <button onclick="refreshPlaylists()">Refresh All</button>
<button onclick="refreshAndMatchAll()" title="Clear caches, fetch fresh data from Spotify, and match all tracks. This is a full rebuild and may take several minutes." style="background:var(--accent);border-color:var(--accent);">Refresh & Match All</button>
</div> </div>
</h2> </h2>
<p style="color: var(--text-secondary); margin-bottom: 12px;"> <p style="color: var(--text-secondary); margin-bottom: 12px;">
@@ -669,13 +662,14 @@
<th>Spotify ID</th> <th>Spotify ID</th>
<th>Tracks</th> <th>Tracks</th>
<th>Completion</th> <th>Completion</th>
<th>Lyrics</th>
<th>Cache Age</th> <th>Cache Age</th>
<th>Actions</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
<tbody id="playlist-table-body"> <tbody id="playlist-table-body">
<tr> <tr>
<td colspan="6" class="loading"> <td colspan="7" class="loading">
<span class="spinner"></span> Loading playlists... <span class="spinner"></span> Loading playlists...
</td> </td>
</tr> </tr>
@@ -926,22 +920,6 @@
</div> </div>
</div> </div>
<div class="card">
<h2>Library Settings</h2>
<div class="config-section">
<div class="config-item">
<span class="label">Download Path (Cache)</span>
<span class="value" id="config-download-path">-</span>
<button onclick="openEditSetting('LIBRARY_DOWNLOAD_PATH', 'Download Path', 'text')">Edit</button>
</div>
<div class="config-item">
<span class="label">Kept Path (Favorited)</span>
<span class="value" id="config-kept-path">-</span>
<button onclick="openEditSetting('LIBRARY_KEPT_PATH', 'Kept Path', 'text')">Edit</button>
</div>
</div>
</div>
<div class="card"> <div class="card">
<h2>Sync Schedule</h2> <h2>Sync Schedule</h2>
<div class="config-section"> <div class="config-section">
@@ -981,85 +959,6 @@
</div> </div>
</div> </div>
</div> </div>
<!-- API Analytics Tab -->
<div class="tab-content" id="tab-endpoints">
<div class="card">
<h2>
API Endpoint Usage
<div class="actions">
<button onclick="fetchEndpointUsage()">Refresh</button>
<button class="danger" onclick="clearEndpointUsage()">Clear Data</button>
</div>
</h2>
<p style="color: var(--text-secondary); margin-bottom: 16px;">
Track which Jellyfin API endpoints are being called most frequently. Useful for debugging and understanding client behavior.
</p>
<div id="endpoints-summary" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 20px;">
<div style="background: var(--bg-tertiary); padding: 16px; border-radius: 8px;">
<div style="color: var(--text-secondary); font-size: 0.85rem; margin-bottom: 4px;">Total Requests</div>
<div style="font-size: 1.8rem; font-weight: 600; color: var(--accent);" id="endpoints-total-requests">0</div>
</div>
<div style="background: var(--bg-tertiary); padding: 16px; border-radius: 8px;">
<div style="color: var(--text-secondary); font-size: 0.85rem; margin-bottom: 4px;">Unique Endpoints</div>
<div style="font-size: 1.8rem; font-weight: 600; color: var(--success);" id="endpoints-unique-count">0</div>
</div>
<div style="background: var(--bg-tertiary); padding: 16px; border-radius: 8px;">
<div style="color: var(--text-secondary); font-size: 0.85rem; margin-bottom: 4px;">Most Called</div>
<div style="font-size: 1.1rem; font-weight: 600; color: var(--text-primary); word-break: break-all;" id="endpoints-most-called">-</div>
</div>
</div>
<div style="margin-bottom: 16px;">
<label style="display: block; margin-bottom: 8px; color: var(--text-secondary); font-size: 0.9rem;">Show Top</label>
<select id="endpoints-top-select" onchange="fetchEndpointUsage()" style="padding: 8px; background: var(--bg-tertiary); border: 1px solid var(--border); border-radius: 6px; color: var(--text-primary);">
<option value="25">Top 25</option>
<option value="50" selected>Top 50</option>
<option value="100">Top 100</option>
<option value="500">Top 500</option>
</select>
</div>
<div style="max-height: 600px; overflow-y: auto;">
<table class="playlist-table">
<thead>
<tr>
<th style="width: 60px;">#</th>
<th>Endpoint</th>
<th style="width: 120px; text-align: right;">Requests</th>
<th style="width: 120px; text-align: right;">% of Total</th>
</tr>
</thead>
<tbody id="endpoints-table-body">
<tr>
<td colspan="4" class="loading">
<span class="spinner"></span> Loading endpoint usage data...
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="card">
<h2>About Endpoint Tracking</h2>
<p style="color: var(--text-secondary); line-height: 1.6;">
Allstarr logs every Jellyfin API endpoint call to help you understand how clients interact with your server.
This data is stored in <code style="background: var(--bg-tertiary); padding: 2px 6px; border-radius: 4px;">/app/cache/endpoint-usage/endpoints.csv</code>
and persists across restarts.
<br><br>
<strong>Common Endpoints:</strong>
<ul style="margin-top: 8px; margin-left: 20px;">
<li><code>/Users/{userId}/Items</code> - Browse library items</li>
<li><code>/Items/{itemId}</code> - Get item details</li>
<li><code>/Audio/{itemId}/stream</code> - Stream audio</li>
<li><code>/Sessions/Playing</code> - Report playback status</li>
<li><code>/Search/Hints</code> - Search functionality</li>
</ul>
</p>
</div>
</div>
</div> </div>
<!-- Add Playlist Modal --> <!-- Add Playlist Modal -->
@@ -1281,37 +1180,8 @@
if (hash) { if (hash) {
switchTab(hash); switchTab(hash);
} }
// Start auto-refresh for playlists tab (every 5 seconds)
startPlaylistAutoRefresh();
}); });
// Auto-refresh functionality for playlists
let playlistAutoRefreshInterval = null;
function startPlaylistAutoRefresh() {
// Clear any existing interval
if (playlistAutoRefreshInterval) {
clearInterval(playlistAutoRefreshInterval);
}
// Refresh every 5 seconds when on playlists tab
playlistAutoRefreshInterval = setInterval(() => {
const playlistsTab = document.getElementById('tab-playlists');
if (playlistsTab && playlistsTab.classList.contains('active')) {
// Silently refresh without showing loading state
fetchPlaylists(true);
}
}, 5000);
}
function stopPlaylistAutoRefresh() {
if (playlistAutoRefreshInterval) {
clearInterval(playlistAutoRefreshInterval);
playlistAutoRefreshInterval = null;
}
}
// Toast notification // Toast notification
function showToast(message, type = 'success', duration = 3000) { function showToast(message, type = 'success', duration = 3000) {
const toast = document.createElement('div'); const toast = document.createElement('div');
@@ -1451,7 +1321,7 @@
} }
} }
async function fetchPlaylists(silent = false) { async function fetchPlaylists() {
try { try {
const res = await fetch('/api/admin/playlists'); const res = await fetch('/api/admin/playlists');
const data = await res.json(); const data = await res.json();
@@ -1459,9 +1329,7 @@
const tbody = document.getElementById('playlist-table-body'); const tbody = document.getElementById('playlist-table-body');
if (data.playlists.length === 0) { if (data.playlists.length === 0) {
if (!silent) { tbody.innerHTML = '<tr><td colspan="7" style="text-align:center;color:var(--text-secondary);padding:40px;">No playlists configured. Link playlists from the Jellyfin Playlists tab.</td></tr>';
tbody.innerHTML = '<tr><td colspan="6" style="text-align:center;color:var(--text-secondary);padding:40px;">No playlists configured. Link playlists from the Jellyfin Playlists tab.</td></tr>';
}
return; return;
} }
@@ -1529,10 +1397,21 @@
<span style="font-size:0.85rem;color:${completionColor};font-weight:500;min-width:40px;">${completionPct}%</span> <span style="font-size:0.85rem;color:${completionColor};font-weight:500;min-width:40px;">${completionPct}%</span>
</div> </div>
</td> </td>
<td>
${p.lyricsTotal > 0 ? `
<div style="display:flex;align-items:center;gap:8px;">
<div style="flex:1;background:var(--bg-tertiary);height:12px;border-radius:6px;overflow:hidden;">
<div style="width:${p.lyricsPercentage}%;height:100%;background:${p.lyricsPercentage === 100 ? '#10b981' : '#3b82f6'};transition:width 0.3s;" title="${p.lyricsCached} lyrics cached"></div>
</div>
<span style="font-size:0.85rem;color:var(--text-secondary);font-weight:500;min-width:40px;">${p.lyricsPercentage}%</span>
</div>
` : '<span style="color:var(--text-secondary);font-size:0.85rem;">-</span>'}
</td>
<td class="cache-age">${p.cacheAge || '-'}</td> <td class="cache-age">${p.cacheAge || '-'}</td>
<td> <td>
<button onclick="clearPlaylistCache('${escapeJs(p.name)}')">Clear Cache & Rebuild</button> <button onclick="clearPlaylistCache('${escapeJs(p.name)}')">Clear Cache & Rebuild</button>
<button onclick="matchPlaylistTracks('${escapeJs(p.name)}')">Match Tracks</button> <button onclick="matchPlaylistTracks('${escapeJs(p.name)}')">Match Tracks</button>
<button onclick="prefetchLyrics('${escapeJs(p.name)}')">Prefetch Lyrics</button>
<button onclick="viewTracks('${escapeJs(p.name)}')">View</button> <button onclick="viewTracks('${escapeJs(p.name)}')">View</button>
<button class="danger" onclick="removePlaylist('${escapeJs(p.name)}')">Remove</button> <button class="danger" onclick="removePlaylist('${escapeJs(p.name)}')">Remove</button>
</td> </td>
@@ -1667,20 +1546,16 @@
} }
tbody.innerHTML = missingTracks.map(t => { tbody.innerHTML = missingTracks.map(t => {
const artist = (t.artists && t.artists.length > 0) ? t.artists.join(', ') : '';
const searchQuery = `${t.title} ${artist}`;
return ` return `
<tr> <tr>
<td><strong>${escapeHtml(t.playlist)}</strong></td> <td><strong>${escapeHtml(t.playlist)}</strong></td>
<td>${escapeHtml(t.title)}</td> <td>${escapeHtml(t.title)}</td>
<td>${escapeHtml(artist)}</td> <td>${escapeHtml(t.artist)}</td>
<td style="color:var(--text-secondary);">${t.album ? escapeHtml(t.album) : '-'}</td> <td style="color:var(--text-secondary);">${t.album ? escapeHtml(t.album) : '-'}</td>
<td> <td>
<button onclick="searchProvider('${escapeJs(searchQuery)}', 'squidwtf')" <button onclick="openMapToLocal('${escapeJs(t.playlist)}', '${escapeJs(t.spotifyId)}', '${escapeJs(t.title)}', '${escapeJs(t.artist)}')"
style="margin-right:4px;font-size:0.75rem;padding:4px 8px;background:#3b82f6;border-color:#3b82f6;color:white;">🔍 Search</button>
<button onclick="openMapToLocal('${escapeJs(t.playlist)}', '${escapeJs(t.spotifyId)}', '${escapeJs(t.title)}', '${escapeJs(artist)}')"
style="margin-right:4px;font-size:0.75rem;padding:4px 8px;background:var(--success);border-color:var(--success);">Map to Local</button> style="margin-right:4px;font-size:0.75rem;padding:4px 8px;background:var(--success);border-color:var(--success);">Map to Local</button>
<button onclick="openMapToExternal('${escapeJs(t.playlist)}', '${escapeJs(t.spotifyId)}', '${escapeJs(t.title)}', '${escapeJs(artist)}')" <button onclick="openMapToExternal('${escapeJs(t.playlist)}', '${escapeJs(t.spotifyId)}', '${escapeJs(t.title)}', '${escapeJs(t.artist)}')"
style="font-size:0.75rem;padding:4px 8px;background:var(--warning);border-color:var(--warning);">Map to External</button> style="font-size:0.75rem;padding:4px 8px;background:var(--warning);border-color:var(--warning);">Map to External</button>
</td> </td>
</tr> </tr>
@@ -1812,10 +1687,6 @@
document.getElementById('config-jellyfin-user-id').textContent = data.jellyfin.userId || '(not set)'; document.getElementById('config-jellyfin-user-id').textContent = data.jellyfin.userId || '(not set)';
document.getElementById('config-jellyfin-library-id').textContent = data.jellyfin.libraryId || '-'; document.getElementById('config-jellyfin-library-id').textContent = data.jellyfin.libraryId || '-';
// Library settings
document.getElementById('config-download-path').textContent = data.library?.downloadPath || './downloads';
document.getElementById('config-kept-path').textContent = data.library?.keptPath || '/app/kept';
// Sync settings // Sync settings
const syncHour = data.spotifyImport.syncStartHour; const syncHour = data.spotifyImport.syncStartHour;
const syncMin = data.spotifyImport.syncStartMinute; const syncMin = data.spotifyImport.syncStartMinute;
@@ -2023,9 +1894,6 @@
if (!confirm(`Clear cache and rebuild for "${name}"?\n\nThis will:\n• Clear Redis cache\n• Delete file caches\n• Rebuild with latest Spotify IDs\n\nThis may take a minute.`)) return; if (!confirm(`Clear cache and rebuild for "${name}"?\n\nThis will:\n• Clear Redis cache\n• Delete file caches\n• Rebuild with latest Spotify IDs\n\nThis may take a minute.`)) return;
try { try {
// Show warning banner
document.getElementById('matching-warning-banner').style.display = 'block';
showToast(`Clearing cache for ${name}...`, 'info'); showToast(`Clearing cache for ${name}...`, 'info');
const res = await fetch(`/api/admin/playlists/${encodeURIComponent(name)}/clear-cache`, { method: 'POST' }); const res = await fetch(`/api/admin/playlists/${encodeURIComponent(name)}/clear-cache`, { method: 'POST' });
const data = await res.json(); const data = await res.json();
@@ -2033,26 +1901,17 @@
if (res.ok) { if (res.ok) {
showToast(`${data.message} (Cleared ${data.clearedKeys} cache keys, ${data.clearedFiles} files)`, 'success', 5000); showToast(`${data.message} (Cleared ${data.clearedKeys} cache keys, ${data.clearedFiles} files)`, 'success', 5000);
// Refresh the playlists table after a delay to show updated counts // Refresh the playlists table after a delay to show updated counts
setTimeout(() => { setTimeout(fetchPlaylists, 3000);
fetchPlaylists();
// Hide warning banner after refresh
document.getElementById('matching-warning-banner').style.display = 'none';
}, 3000);
} else { } else {
showToast(data.error || 'Failed to clear cache', 'error'); showToast(data.error || 'Failed to clear cache', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} catch (error) { } catch (error) {
showToast('Failed to clear cache', 'error'); showToast('Failed to clear cache', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} }
async function matchPlaylistTracks(name) { async function matchPlaylistTracks(name) {
try { try {
// Show warning banner
document.getElementById('matching-warning-banner').style.display = 'block';
showToast(`Matching tracks for ${name}...`, 'success'); showToast(`Matching tracks for ${name}...`, 'success');
const res = await fetch(`/api/admin/playlists/${encodeURIComponent(name)}/match`, { method: 'POST' }); const res = await fetch(`/api/admin/playlists/${encodeURIComponent(name)}/match`, { method: 'POST' });
const data = await res.json(); const data = await res.json();
@@ -2060,18 +1919,12 @@
if (res.ok) { if (res.ok) {
showToast(`${data.message}`, 'success'); showToast(`${data.message}`, 'success');
// Refresh the playlists table after a delay to show updated counts // Refresh the playlists table after a delay to show updated counts
setTimeout(() => { setTimeout(fetchPlaylists, 2000);
fetchPlaylists();
// Hide warning banner after refresh
document.getElementById('matching-warning-banner').style.display = 'none';
}, 2000);
} else { } else {
showToast(data.error || 'Failed to match tracks', 'error'); showToast(data.error || 'Failed to match tracks', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} catch (error) { } catch (error) {
showToast('Failed to match tracks', 'error'); showToast('Failed to match tracks', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} }
@@ -2079,9 +1932,6 @@
if (!confirm('Match tracks for ALL playlists? This may take a few minutes.')) return; if (!confirm('Match tracks for ALL playlists? This may take a few minutes.')) return;
try { try {
// Show warning banner
document.getElementById('matching-warning-banner').style.display = 'block';
showToast('Matching tracks for all playlists...', 'success'); showToast('Matching tracks for all playlists...', 'success');
const res = await fetch('/api/admin/playlists/match-all', { method: 'POST' }); const res = await fetch('/api/admin/playlists/match-all', { method: 'POST' });
const data = await res.json(); const data = await res.json();
@@ -2089,68 +1939,32 @@
if (res.ok) { if (res.ok) {
showToast(`${data.message}`, 'success'); showToast(`${data.message}`, 'success');
// Refresh the playlists table after a delay to show updated counts // Refresh the playlists table after a delay to show updated counts
setTimeout(() => { setTimeout(fetchPlaylists, 2000);
fetchPlaylists();
// Hide warning banner after refresh
document.getElementById('matching-warning-banner').style.display = 'none';
}, 2000);
} else { } else {
showToast(data.error || 'Failed to match tracks', 'error'); showToast(data.error || 'Failed to match tracks', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} catch (error) { } catch (error) {
showToast('Failed to match tracks', 'error'); showToast('Failed to match tracks', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} }
async function refreshAndMatchAll() { async function prefetchLyrics(name) {
if (!confirm('Clear caches, refresh from Spotify, and match all tracks?\n\nThis will:\n• Clear all playlist caches\n• Fetch fresh data from Spotify\n• Match all tracks against local library and external providers\n\nThis may take several minutes.')) return;
try { try {
// Show warning banner showToast(`Prefetching lyrics for ${name}...`, 'info', 5000);
document.getElementById('matching-warning-banner').style.display = 'block'; const res = await fetch(`/api/admin/playlists/${encodeURIComponent(name)}/prefetch-lyrics`, { method: 'POST' });
showToast('Starting full refresh and match...', 'info', 3000);
// Step 1: Clear all caches
showToast('Step 1/3: Clearing caches...', 'info', 2000);
await fetch('/api/admin/cache/clear', { method: 'POST' });
// Wait for cache to be fully cleared
await new Promise(resolve => setTimeout(resolve, 2000));
// Step 2: Refresh playlists from Spotify
showToast('Step 2/3: Fetching from Spotify...', 'info', 2000);
await fetch('/api/admin/playlists/refresh', { method: 'POST' });
// Wait for Spotify fetch to complete
await new Promise(resolve => setTimeout(resolve, 5000));
// Step 3: Match all tracks
showToast('Step 3/3: Matching all tracks (this may take several minutes)...', 'info', 3000);
const res = await fetch('/api/admin/playlists/match-all', { method: 'POST' });
const data = await res.json(); const data = await res.json();
if (res.ok) { if (res.ok) {
showToast(`✓ Full refresh and match complete!`, 'success', 5000); const summary = `Fetched: ${data.fetched}, Cached: ${data.cached}, Missing: ${data.missing}`;
// Refresh the playlists table after a delay showToast(`✓ Lyrics prefetch complete for ${name}. ${summary}`, 'success', 8000);
setTimeout(() => {
fetchPlaylists();
// Hide warning banner after refresh
document.getElementById('matching-warning-banner').style.display = 'none';
}, 3000);
} else { } else {
showToast(data.error || 'Failed to match tracks', 'error'); showToast(data.error || 'Failed to prefetch lyrics', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} catch (error) { } catch (error) {
showToast('Failed to complete refresh and match', 'error'); showToast('Failed to prefetch lyrics', 'error');
document.getElementById('matching-warning-banner').style.display = 'none';
} }
} }
async function searchProvider(query, provider) { async function searchProvider(query, provider) {
// Use SquidWTF HiFi API with round-robin base URLs for all searches // Use SquidWTF HiFi API with round-robin base URLs for all searches
// Get a random base URL from the backend // Get a random base URL from the backend
@@ -2944,7 +2758,6 @@
fetchJellyfinUsers(); fetchJellyfinUsers();
fetchJellyfinPlaylists(); fetchJellyfinPlaylists();
fetchConfig(); fetchConfig();
fetchEndpointUsage();
// Auto-refresh every 30 seconds // Auto-refresh every 30 seconds
setInterval(() => { setInterval(() => {
@@ -2953,102 +2766,7 @@
fetchTrackMappings(); fetchTrackMappings();
fetchMissingTracks(); fetchMissingTracks();
fetchDownloads(); fetchDownloads();
// Refresh endpoint usage if on that tab
const endpointsTab = document.getElementById('tab-endpoints');
if (endpointsTab && endpointsTab.classList.contains('active')) {
fetchEndpointUsage();
}
}, 30000); }, 30000);
// Endpoint Usage Functions
async function fetchEndpointUsage() {
try {
const topSelect = document.getElementById('endpoints-top-select');
const top = topSelect ? topSelect.value : 50;
const res = await fetch(`/api/admin/debug/endpoint-usage?top=${top}`);
const data = await res.json();
// Update summary stats
document.getElementById('endpoints-total-requests').textContent = data.totalRequests?.toLocaleString() || '0';
document.getElementById('endpoints-unique-count').textContent = data.totalEndpoints?.toLocaleString() || '0';
const mostCalled = data.endpoints && data.endpoints.length > 0
? data.endpoints[0].endpoint
: '-';
document.getElementById('endpoints-most-called').textContent = mostCalled;
// Update table
const tbody = document.getElementById('endpoints-table-body');
if (!data.endpoints || data.endpoints.length === 0) {
tbody.innerHTML = '<tr><td colspan="4" style="text-align:center;color:var(--text-secondary);padding:40px;">No endpoint usage data available yet. Data will appear as clients make requests.</td></tr>';
return;
}
tbody.innerHTML = data.endpoints.map((ep, index) => {
const percentage = data.totalRequests > 0
? ((ep.count / data.totalRequests) * 100).toFixed(1)
: '0.0';
// Color code based on usage
let countColor = 'var(--text-primary)';
if (ep.count > 1000) countColor = 'var(--error)';
else if (ep.count > 100) countColor = 'var(--warning)';
else if (ep.count > 10) countColor = 'var(--accent)';
// Highlight common patterns
let endpointDisplay = ep.endpoint;
if (ep.endpoint.includes('/stream')) {
endpointDisplay = `<span style="color:var(--success)">${escapeHtml(ep.endpoint)}</span>`;
} else if (ep.endpoint.includes('/Playing')) {
endpointDisplay = `<span style="color:var(--accent)">${escapeHtml(ep.endpoint)}</span>`;
} else if (ep.endpoint.includes('/Search')) {
endpointDisplay = `<span style="color:var(--warning)">${escapeHtml(ep.endpoint)}</span>`;
} else {
endpointDisplay = escapeHtml(ep.endpoint);
}
return `
<tr>
<td style="color:var(--text-secondary);text-align:center;">${index + 1}</td>
<td style="font-family:monospace;font-size:0.85rem;">${endpointDisplay}</td>
<td style="text-align:right;font-weight:600;color:${countColor}">${ep.count.toLocaleString()}</td>
<td style="text-align:right;color:var(--text-secondary)">${percentage}%</td>
</tr>
`;
}).join('');
} catch (error) {
console.error('Failed to fetch endpoint usage:', error);
const tbody = document.getElementById('endpoints-table-body');
tbody.innerHTML = '<tr><td colspan="4" style="text-align:center;color:var(--error);padding:40px;">Failed to load endpoint usage data</td></tr>';
}
}
async function clearEndpointUsage() {
if (!confirm('Are you sure you want to clear all endpoint usage data? This cannot be undone.')) {
return;
}
try {
const res = await fetch('/api/admin/debug/endpoint-usage', { method: 'DELETE' });
const data = await res.json();
showToast(data.message || 'Endpoint usage data cleared', 'success');
fetchEndpointUsage();
} catch (error) {
console.error('Failed to clear endpoint usage:', error);
showToast('Failed to clear endpoint usage data', 'error');
}
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
</script> </script>
</body> </body>
</html> </html>