mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
Compare commits
35 Commits
f741cc5297
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
6ea03b8005
|
|||
|
1369d09cbd
|
|||
|
838151741f
|
|||
|
88bf083386
|
|||
|
670544a9d6
|
|||
|
0dca6b792d
|
|||
|
f135db3f60
|
|||
| 2b76fa9e6f | |||
|
6949f8aed4
|
|||
|
a37f7e0b1d
|
|||
|
2b4cd35cf7
|
|||
|
faa07c2791
|
|||
|
bdd753fd02
|
|||
|
0a07804833
|
|||
|
6c14fc299c
|
|||
|
b03a4b85c9
|
|||
|
565cb46b72
|
|||
|
6357b524da
|
|||
|
aa9f0d0345
|
|||
|
b0e07404c9
|
|||
|
8dbf37f6a3
|
|||
|
baab1e88a5
|
|||
|
972756159d
|
|||
|
f59f265ad4
|
|||
|
bc0467b1ff
|
|||
|
e057f365f4
|
|||
|
e8eb095a23
|
|||
|
591fd5e8e1
|
|||
|
3e840f987b
|
|||
|
56bc9d4ea9
|
|||
|
f1dd01f6d5
|
|||
|
6c06c59f61
|
|||
|
56f2eca867
|
|||
|
248ab804f3
|
|||
|
b1769a35bf
|
37
.env.example
37
.env.example
@@ -18,27 +18,29 @@ 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 authentication (get from Jellyfin Dashboard > API Keys)
|
# API key for SERVER-SIDE operations only (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 (get from Jellyfin Dashboard > Users > click user > check URL)
|
# User ID for SERVER-SIDE library queries (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
|
||||||
|
|
||||||
# Path where downloaded songs will be stored on the host (only applies if STORAGE_MODE=Permanent)
|
# Base directory for all downloads (default: ./downloads)
|
||||||
DOWNLOAD_PATH=./downloads
|
# This creates three subdirectories:
|
||||||
|
# - downloads/permanent/ - Permanently downloaded tracks (STORAGE_MODE=Permanent)
|
||||||
# Path where favorited external tracks are permanently kept
|
# - downloads/cache/ - Temporarily cached tracks (STORAGE_MODE=Cache)
|
||||||
KEPT_PATH=./kept
|
# - downloads/kept/ - Favorited external tracks (always permanent)
|
||||||
|
Library__DownloadPath=./downloads
|
||||||
# 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
|
||||||
@@ -108,27 +110,14 @@ 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 (Release Radar, Discover Weekly) and fills them
|
# This feature intercepts Spotify Import plugin playlists and fills them with tracks from external providers
|
||||||
# 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
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -104,3 +104,6 @@ originals/
|
|||||||
|
|
||||||
# Sample missing playlists for Spotify integration testing
|
# Sample missing playlists for Spotify integration testing
|
||||||
sampleMissingPlaylists/
|
sampleMissingPlaylists/
|
||||||
|
|
||||||
|
# Migration guide (local only)
|
||||||
|
MIGRATION.md
|
||||||
342
README.md
342
README.md
@@ -5,11 +5,7 @@
|
|||||||
[](https://github.com/SoPat712/allstarr/pkgs/container/allstarr)
|
[](https://github.com/SoPat712/allstarr/pkgs/container/allstarr)
|
||||||
[](LICENSE)
|
[](LICENSE)
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
**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
|
||||||
|
|
||||||
@@ -40,15 +36,17 @@ The proxy will be available at `http://localhost:5274`.
|
|||||||
|
|
||||||
## Web Dashboard
|
## Web Dashboard
|
||||||
|
|
||||||
Allstarr includes a web-based dashboard for easy configuration and playlist management, accessible at `http://localhost:5275` (internal port, not exposed through reverse proxy).
|
Allstarr includes a web UI for easy configuration and playlist management, accessible at `http://localhost:5275`
|
||||||
|
<img width="1664" height="1101" alt="image" src="https://github.com/user-attachments/assets/9159100b-7e11-449e-8530-517d336d6bd2" />
|
||||||
|
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
- **Real-time Status**: Monitor Spotify authentication, cookie age, and playlist sync status
|
- **Playlist Management**: Link Jellyfin playlists to Spotify playlists with just a few clicks
|
||||||
- **Playlist Management**: Link Jellyfin playlists to Spotify playlists with a few clicks
|
- **Provider Matching**: It should fill in the gaps of your Jellyfin library with tracks from your selected provider
|
||||||
- **Configuration Editor**: Update settings without manually editing .env files
|
- **WebUI**: Update settings without manually editing .env files
|
||||||
- **Track Viewer**: Browse tracks in your configured playlists
|
- **Music**: Using multiple sources for music (optimized for SquidWTF right now, though)
|
||||||
- **Cache Management**: Clear cached data and restart the container
|
- **Lyrics**: Using multiple sources for lyrics, first Jellyfin Lyrics, then Spotify Lyrics, then LrcLib as a last resort
|
||||||
|
|
||||||
### Quick Setup with Web UI
|
### Quick Setup with Web UI
|
||||||
|
|
||||||
@@ -65,18 +63,18 @@ Allstarr includes a web-based dashboard for easy configuration and playlist mana
|
|||||||
- `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 (button in Configuration tab)
|
4. **Restart** to apply changes (should be a banner)
|
||||||
|
|
||||||
### Why Two Playlist Tabs?
|
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.
|
||||||
|
|
||||||
- **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`.
|
||||||
|
|
||||||
**Recommended workflow**: Use the `sp_dc` cookie method (simpler and more reliable than the Jellyfin Spotify Import plugin).
|
There's an environment variable to modify this.
|
||||||
|
|
||||||
|
|
||||||
|
**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)
|
||||||
|
|
||||||
@@ -141,7 +139,14 @@ This project brings together all the music streaming providers into one unified
|
|||||||
**Compatible Jellyfin clients:**
|
**Compatible Jellyfin clients:**
|
||||||
|
|
||||||
- [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)
|
<img width="1691" height="1128" alt="image" src="https://github.com/user-attachments/assets/c602f71c-c4dd-49a9-b533-1558e24a9f45" />
|
||||||
|
|
||||||
|
|
||||||
|
- [Musiver](https://music.aqzscn.cn/en/) (Android/iOS/Windows/Android)
|
||||||
|
<img width="523" height="1025" alt="image" src="https://github.com/user-attachments/assets/135e2721-5fd7-482f-bb06-b0736003cfe7" />
|
||||||
|
|
||||||
|
|
||||||
|
- [Finamp](https://github.com/jmshrv/finamp) (Android/iOS)
|
||||||
|
|
||||||
_Working on getting more currently_
|
_Working on getting more currently_
|
||||||
|
|
||||||
@@ -335,7 +340,10 @@ Subsonic__EnableExternalPlaylists=false
|
|||||||
|
|
||||||
### Spotify Playlist Injection (Jellyfin Only)
|
### Spotify Playlist Injection (Jellyfin Only)
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
<img width="1649" height="3764" alt="image" src="https://github.com/user-attachments/assets/a4d3d79c-7741-427f-8c01-ffc90f3a579b" />
|
||||||
|
|
||||||
|
|
||||||
#### Prerequisites
|
#### Prerequisites
|
||||||
|
|
||||||
@@ -349,136 +357,112 @@ Allstarr can automatically fill your Spotify playlists (like Release Radar and D
|
|||||||
- 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 daily sync schedule (e.g., 4:15 PM daily)
|
- Set a sync schedule (the plugin will create playlists in Jellyfin)
|
||||||
- The plugin will create playlists in Jellyfin and generate "missing tracks" files for songs not in your library
|
|
||||||
|
|
||||||
3. **Configure Allstarr**
|
3. **Configure Allstarr**
|
||||||
- Allstarr needs to know when the plugin runs and which playlists to intercept
|
- Enable Spotify Import in Allstarr (see configuration below)
|
||||||
- Uses your existing `JELLYFIN_URL` and `JELLYFIN_API_KEY` settings (no additional credentials needed)
|
- Link your Jellyfin playlists to Spotify playlists via the Web UI
|
||||||
|
- 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:SyncStartHour` | Hour when the Spotify Import plugin runs (24-hour format, 0-23) |
|
| `SpotifyImport:MatchingIntervalHours` | How often to run track matching in hours (default: 24, set to 0 for startup only) |
|
||||||
| `SpotifyImport:SyncStartMinute` | Minute when the plugin runs (0-59) |
|
| `SpotifyImport:Playlists` | JSON array of playlists (managed via Web UI) |
|
||||||
| `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
|
||||||
|
|
||||||
# Sync window settings (optional - used to prevent fetching too frequently)
|
# Matching interval (24 hours = once per day)
|
||||||
# The fetcher searches backwards from current time for the last 48 hours
|
SPOTIFY_IMPORT_MATCHING_INTERVAL_HOURS=24
|
||||||
SPOTIFY_IMPORT_SYNC_START_HOUR=16
|
|
||||||
SPOTIFY_IMPORT_SYNC_START_MINUTE=15
|
|
||||||
SPOTIFY_IMPORT_SYNC_WINDOW_HOURS=2
|
|
||||||
|
|
||||||
# Get playlist IDs from Jellyfin URLs: https://jellyfin.example.com/web/#/details?id=PLAYLIST_ID
|
# Playlists (use Web UI to manage instead of editing manually)
|
||||||
SPOTIFY_IMPORT_PLAYLIST_IDS=ba50e26c867ec9d57ab2f7bf24cfd6b0,4383a46d8bcac3be2ef9385053ea18df
|
SPOTIFY_IMPORT_PLAYLISTS=[["Discover Weekly","37i9dQZEVXcV6s7Dm7RXsU","first"],["Release Radar","37i9dQZEVXbng2vDHnfQlC","first"]]
|
||||||
|
|
||||||
# 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** (e.g., daily at 4:15 PM)
|
1. **Spotify Import Plugin Runs**
|
||||||
- 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 Fetches Missing Tracks** (within sync window)
|
2. **Allstarr Matches Tracks** (on startup + every 24 hours by default)
|
||||||
- Searches for missing tracks files from the Jellyfin plugin
|
- Reads 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)
|
||||||
- Caches matched results for 1 hour
|
- Pre-builds playlist cache for instant loading
|
||||||
- **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)
|
|
||||||
|
|
||||||
4. **You Open the Playlist in Jellyfin**
|
3. **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 (no searching needed!)
|
- Loads instantly from cache!
|
||||||
|
|
||||||
5. **You Play a Track**
|
4. **You Play a Track**
|
||||||
- If it's a local track, streams from Jellyfin normally
|
- Local tracks stream from Jellyfin normally
|
||||||
- If it's a matched track, downloads from streaming provider on-demand
|
- Matched tracks download 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 Triggers
|
#### Manual API Triggers
|
||||||
|
|
||||||
You can manually trigger syncing and matching via API:
|
You can manually trigger operations via the admin 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 "https://your-jellyfin-proxy.com/spotify/sync?api_key=YOUR_API_KEY"
|
curl "http://localhost:5274/spotify/sync?api_key=$API_KEY"
|
||||||
|
|
||||||
# Trigger track matching (searches streaming provider)
|
# Trigger track matching (searches streaming provider)
|
||||||
curl "https://your-jellyfin-proxy.com/spotify/match?api_key=YOUR_API_KEY"
|
curl "http://localhost:5274/spotify/match?api_key=$API_KEY"
|
||||||
|
|
||||||
# Clear cache to force re-matching
|
# Match all playlists (refresh all matches)
|
||||||
curl "https://your-jellyfin-proxy.com/spotify/clear-cache?api_key=YOUR_API_KEY"
|
curl "http://localhost:5274/spotify/match-all?api_key=$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"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Startup Behavior
|
#### Web UI Management
|
||||||
|
|
||||||
When Allstarr starts with Spotify Import enabled:
|
The easiest way to manage Spotify playlists is through the Web UI at `http://localhost:5275`:
|
||||||
|
|
||||||
**Smart Cache Check:**
|
1. **Link Playlists Tab**: Link Jellyfin playlists to Spotify playlists
|
||||||
- Checks if today's sync window has passed (e.g., if sync is at 4 PM + 2 hour window = 6 PM)
|
2. **Active Playlists Tab**: View status, trigger matching, and manage playlists
|
||||||
- If before 6 PM and yesterday's cache exists → **Skips fetch** (cache is still current)
|
3. **Configuration Tab**: Enable/disable Spotify Import and adjust settings
|
||||||
- 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 `SPOTIFY_IMPORT_PLAYLIST_IDS` match your Jellyfin playlist IDs
|
- Verify playlists are linked in the Web UI
|
||||||
- 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)
|
||||||
- Check that playlist names in `SPOTIFY_IMPORT_PLAYLIST_NAMES` match exactly
|
- Manually trigger matching via Web UI or API
|
||||||
- Manually trigger matching: `curl "https://your-proxy.com/spotify/match?api_key=KEY"`
|
- Check that the Jellyfin plugin generated missing tracks files
|
||||||
|
|
||||||
**Sync timing issues:**
|
**Performance:**
|
||||||
- Set `SPOTIFY_IMPORT_SYNC_START_HOUR/MINUTE` to match your plugin schedule
|
- Matching runs in background with rate limiting (150ms between searches)
|
||||||
- Increase `SPOTIFY_IMPORT_SYNC_WINDOW_HOURS` if files aren't being found
|
- First match may take a few minutes for large playlists
|
||||||
- Check Jellyfin plugin logs to confirm when it runs
|
- Subsequent loads are instant (served from cache)
|
||||||
|
|
||||||
#### Notes
|
#### Notes
|
||||||
|
|
||||||
- This feature uses your existing `JELLYFIN_URL` and `JELLYFIN_API_KEY` settings
|
- Uses your existing `JELLYFIN_URL` and `JELLYFIN_API_KEY` settings
|
||||||
- Matched tracks are cached for 1 hour to avoid repeated searches
|
- Matched tracks cached for fast loading
|
||||||
- Missing tracks cache persists across restarts (stored in Redis + file cache)
|
- Missing tracks cache persists across restarts (Redis + file cache)
|
||||||
- Rate limiting prevents overwhelming your streaming provider (150ms between searches)
|
- Rate limiting prevents overwhelming your streaming provider
|
||||||
- Only works with Jellyfin backend (not Subsonic/Navidrome)
|
- Only works with Jellyfin backend (not Subsonic/Navidrome)
|
||||||
|
|
||||||
### Getting Credentials
|
### Getting Credentials
|
||||||
@@ -592,9 +576,46 @@ 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 and adds transparent streaming provider integration:
|
The proxy implements the Subsonic API with streaming provider integration:
|
||||||
|
|
||||||
| Endpoint | Description |
|
| Endpoint | Description |
|
||||||
|----------|-------------|
|
|----------|-------------|
|
||||||
@@ -608,20 +629,6 @@ The proxy implements the Subsonic API and adds transparent streaming provider in
|
|||||||
|
|
||||||
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:
|
||||||
@@ -636,25 +643,37 @@ Legacy format `ext-deezer-{id}` is also supported (assumes song type).
|
|||||||
|
|
||||||
## Download Folder Structure
|
## Download Folder Structure
|
||||||
|
|
||||||
Downloaded music is organized as:
|
All downloads are organized under a single base directory (default: `./downloads`):
|
||||||
|
|
||||||
```
|
```
|
||||||
downloads/
|
downloads/
|
||||||
├── Artist Name/
|
├── permanent/ # Permanent downloads (STORAGE_MODE=Permanent)
|
||||||
│ ├── Album Title/
|
│ ├── Artist Name/
|
||||||
│ │ ├── 01 - Track One.mp3
|
│ │ ├── Album Title/
|
||||||
│ │ ├── 02 - Track Two.mp3
|
│ │ │ ├── 01 - Track One.flac
|
||||||
|
│ │ │ ├── 02 - Track Two.flac
|
||||||
|
│ │ │ └── ...
|
||||||
|
│ │ └── Another Album/
|
||||||
│ │ └── ...
|
│ │ └── ...
|
||||||
│ └── Another Album/
|
│ └── playlists/
|
||||||
│ └── ...
|
│ ├── My Favorite Songs.m3u
|
||||||
├── Another Artist/
|
│ └── Chill Vibes.m3u
|
||||||
│ └── ...
|
├── cache/ # Temporary cache (STORAGE_MODE=Cache)
|
||||||
└── playlists/
|
│ └── Artist Name/
|
||||||
├── My Favorite Songs.m3u
|
│ └── Album Title/
|
||||||
├── Chill Vibes.m3u
|
│ └── Track.flac
|
||||||
└── ...
|
└── kept/ # Favorited external tracks (always permanent)
|
||||||
|
└── Artist Name/
|
||||||
|
└── Album Title/
|
||||||
|
└── Track.flac
|
||||||
```
|
```
|
||||||
|
|
||||||
Playlists are stored as M3U files with relative paths to downloaded tracks, making them portable and compatible with most music players.
|
**Storage modes:**
|
||||||
|
- **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
|
||||||
|
|
||||||
@@ -685,10 +704,17 @@ dotnet test
|
|||||||
```
|
```
|
||||||
allstarr/
|
allstarr/
|
||||||
├── Controllers/
|
├── Controllers/
|
||||||
│ ├── JellyfinController.cs # Jellyfin API controller (registered when Backend:Type=Jellyfin)
|
│ ├── AdminController.cs # Admin dashboard API
|
||||||
│ └── SubsonicController.cs # Subsonic API controller (registered when Backend:Type=Subsonic)
|
│ ├── JellyfinController.cs # Jellyfin API controller
|
||||||
|
│ └── SubsonicController.cs # Subsonic API controller
|
||||||
|
├── Filters/
|
||||||
|
│ ├── AdminPortFilter.cs # Admin port access control
|
||||||
|
│ ├── ApiKeyAuthFilter.cs # API key authentication
|
||||||
|
│ └── JellyfinAuthFilter.cs # Jellyfin authentication
|
||||||
├── Middleware/
|
├── Middleware/
|
||||||
│ └── GlobalExceptionHandler.cs # Global error handling
|
│ ├── AdminStaticFilesMiddleware.cs # Admin UI static file serving
|
||||||
|
│ ├── GlobalExceptionHandler.cs # Global error handling
|
||||||
|
│ └── WebSocketProxyMiddleware.cs # WebSocket proxying for Jellyfin
|
||||||
├── Models/
|
├── Models/
|
||||||
│ ├── Domain/ # Domain entities
|
│ ├── Domain/ # Domain entities
|
||||||
│ │ ├── Song.cs
|
│ │ ├── Song.cs
|
||||||
@@ -697,18 +723,39 @@ 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
|
||||||
@@ -720,12 +767,35 @@ 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
|
||||||
│ │ ├── SubsonicProxyService.cs # Request proxying
|
│ │ ├── PlaylistSyncService.cs # Playlist synchronization
|
||||||
│ │ ├── SubsonicModelMapper.cs # Model mapping
|
│ │ ├── SubsonicModelMapper.cs # Model mapping
|
||||||
|
│ │ ├── SubsonicProxyService.cs # Request proxying
|
||||||
│ │ ├── SubsonicRequestParser.cs # Request parsing
|
│ │ ├── SubsonicRequestParser.cs # Request parsing
|
||||||
│ │ └── SubsonicResponseBuilder.cs # Response building
|
│ │ └── SubsonicResponseBuilder.cs # Response building
|
||||||
│ ├── Validation/ # Startup validation
|
│ ├── Validation/ # Startup validation
|
||||||
@@ -737,13 +807,17 @@ 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
|
||||||
├── QobuzDownloadServiceTests.cs # Qobuz download tests (127 tests)
|
├── JellyfinResponseStructureTests.cs # Jellyfin response 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
|
||||||
@@ -817,7 +891,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 focused and atomic
|
- Keep commits feature focused
|
||||||
|
|
||||||
### Testing
|
### Testing
|
||||||
|
|
||||||
@@ -839,8 +913,14 @@ GPL-3.0
|
|||||||
|
|
||||||
## Acknowledgments
|
## Acknowledgments
|
||||||
|
|
||||||
- [Navidrome](https://www.navidrome.org/) - The excellent self-hosted music server
|
- [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
|
- [Jellyfin](https://jellyfin.org/) - The free and open-source media server
|
||||||
|
- [Navidrome](https://www.navidrome.org/) - The excellent self-hosted music server
|
||||||
|
- [Subsonic API](http://www.subsonic.org/pages/api.jsp) - The API specification
|
||||||
|
- [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
|
||||||
- [Subsonic API](http://www.subsonic.org/pages/api.jsp) - The API specification
|
- [spotify-lyrics-api](https://github.com/akashrchandran/spotify-lyrics-api) - Thank them for the fact that we have access to Spotify's lyrics!
|
||||||
|
- [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
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ 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;
|
||||||
@@ -52,6 +53,7 @@ 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,
|
||||||
@@ -69,6 +71,7 @@ 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;
|
||||||
@@ -163,8 +166,7 @@ public class AdminController : ControllerBase
|
|||||||
spotifyImport = new
|
spotifyImport = new
|
||||||
{
|
{
|
||||||
enabled = _spotifyImportSettings.Enabled,
|
enabled = _spotifyImportSettings.Enabled,
|
||||||
syncTime = $"{_spotifyImportSettings.SyncStartHour:D2}:{_spotifyImportSettings.SyncStartMinute:D2}",
|
matchingIntervalHours = _spotifyImportSettings.MatchingIntervalHours,
|
||||||
syncWindowHours = _spotifyImportSettings.SyncWindowHours,
|
|
||||||
playlistCount = _spotifyImportSettings.Playlists.Count
|
playlistCount = _spotifyImportSettings.Playlists.Count
|
||||||
},
|
},
|
||||||
deezer = new
|
deezer = new
|
||||||
@@ -257,6 +259,7 @@ public class AdminController : ControllerBase
|
|||||||
["id"] = config.Id,
|
["id"] = config.Id,
|
||||||
["jellyfinId"] = config.JellyfinId,
|
["jellyfinId"] = config.JellyfinId,
|
||||||
["localTracksPosition"] = config.LocalTracksPosition.ToString(),
|
["localTracksPosition"] = config.LocalTracksPosition.ToString(),
|
||||||
|
["syncSchedule"] = config.SyncSchedule ?? "0 8 * * 1",
|
||||||
["trackCount"] = 0,
|
["trackCount"] = 0,
|
||||||
["localTracks"] = 0,
|
["localTracks"] = 0,
|
||||||
["externalTracks"] = 0,
|
["externalTracks"] = 0,
|
||||||
@@ -1377,6 +1380,12 @@ public class AdminController : ControllerBase
|
|||||||
{
|
{
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
|
backendType = _configuration.GetValue<string>("Backend:Type") ?? "Jellyfin",
|
||||||
|
musicService = _configuration.GetValue<string>("MusicService") ?? "SquidWTF",
|
||||||
|
explicitFilter = _configuration.GetValue<string>("ExplicitFilter") ?? "All",
|
||||||
|
enableExternalPlaylists = _configuration.GetValue<bool>("EnableExternalPlaylists", false),
|
||||||
|
playlistsDirectory = _configuration.GetValue<string>("PlaylistsDirectory") ?? "(not set)",
|
||||||
|
redisEnabled = _configuration.GetValue<bool>("Redis:Enabled", false),
|
||||||
spotifyApi = new
|
spotifyApi = new
|
||||||
{
|
{
|
||||||
enabled = _spotifyApiSettings.Enabled,
|
enabled = _spotifyApiSettings.Enabled,
|
||||||
@@ -1389,9 +1398,7 @@ public class AdminController : ControllerBase
|
|||||||
spotifyImport = new
|
spotifyImport = new
|
||||||
{
|
{
|
||||||
enabled = _spotifyImportSettings.Enabled,
|
enabled = _spotifyImportSettings.Enabled,
|
||||||
syncStartHour = _spotifyImportSettings.SyncStartHour,
|
matchingIntervalHours = _spotifyImportSettings.MatchingIntervalHours,
|
||||||
syncStartMinute = _spotifyImportSettings.SyncStartMinute,
|
|
||||||
syncWindowHours = _spotifyImportSettings.SyncWindowHours,
|
|
||||||
playlists = _spotifyImportSettings.Playlists.Select(p => new
|
playlists = _spotifyImportSettings.Playlists.Select(p => new
|
||||||
{
|
{
|
||||||
name = p.Name,
|
name = p.Name,
|
||||||
@@ -1408,8 +1415,13 @@ public class AdminController : ControllerBase
|
|||||||
},
|
},
|
||||||
library = new
|
library = new
|
||||||
{
|
{
|
||||||
downloadPath = _configuration["Library:DownloadPath"] ?? "./downloads",
|
downloadPath = _subsonicSettings.StorageMode == StorageMode.Cache
|
||||||
keptPath = _configuration["Library:KeptPath"] ?? "/app/kept"
|
? 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
|
||||||
{
|
{
|
||||||
@@ -1516,6 +1528,12 @@ public class AdminController : ControllerBase
|
|||||||
|
|
||||||
_logger.LogInformation("Config file updated successfully at {Path}", _envFilePath);
|
_logger.LogInformation("Config file updated successfully at {Path}", _envFilePath);
|
||||||
|
|
||||||
|
// Invalidate playlist summary cache if playlists were updated
|
||||||
|
if (appliedUpdates.Contains("SPOTIFY_IMPORT_PLAYLISTS"))
|
||||||
|
{
|
||||||
|
InvalidatePlaylistSummaryCache();
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(new
|
return Ok(new
|
||||||
{
|
{
|
||||||
message = "Configuration updated. Restart container to apply changes.",
|
message = "Configuration updated. Restart container to apply changes.",
|
||||||
@@ -1914,6 +1932,53 @@ public class AdminController : ControllerBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get all playlists from the user's Spotify account
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("spotify/user-playlists")]
|
||||||
|
public async Task<IActionResult> GetSpotifyUserPlaylists()
|
||||||
|
{
|
||||||
|
if (!_spotifyApiSettings.Enabled || string.IsNullOrEmpty(_spotifyApiSettings.SessionCookie))
|
||||||
|
{
|
||||||
|
return BadRequest(new { error = "Spotify API not configured. Please set sp_dc session cookie." });
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Get list of already-configured Spotify playlist IDs
|
||||||
|
var configuredPlaylists = await ReadPlaylistsFromEnvFile();
|
||||||
|
var linkedSpotifyIds = new HashSet<string>(
|
||||||
|
configuredPlaylists.Select(p => p.Id),
|
||||||
|
StringComparer.OrdinalIgnoreCase
|
||||||
|
);
|
||||||
|
|
||||||
|
// Use SpotifyApiClient's GraphQL method - much less rate-limited than REST API
|
||||||
|
var spotifyPlaylists = await _spotifyClient.GetUserPlaylistsAsync(searchName: null);
|
||||||
|
|
||||||
|
if (spotifyPlaylists == null || spotifyPlaylists.Count == 0)
|
||||||
|
{
|
||||||
|
return Ok(new { playlists = new List<object>() });
|
||||||
|
}
|
||||||
|
|
||||||
|
var playlists = spotifyPlaylists.Select(p => new
|
||||||
|
{
|
||||||
|
id = p.SpotifyId,
|
||||||
|
name = p.Name,
|
||||||
|
trackCount = p.TotalTracks,
|
||||||
|
owner = p.OwnerName ?? "",
|
||||||
|
isPublic = p.Public,
|
||||||
|
isLinked = linkedSpotifyIds.Contains(p.SpotifyId)
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
return Ok(new { playlists });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error fetching Spotify user playlists");
|
||||||
|
return StatusCode(500, new { error = "Failed to fetch Spotify playlists", details = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all playlists from Jellyfin
|
/// Get all playlists from Jellyfin
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1985,11 +2050,16 @@ public class AdminController : ControllerBase
|
|||||||
trackStats = await GetPlaylistTrackStats(id!);
|
trackStats = await GetPlaylistTrackStats(id!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use actual track stats for configured playlists, otherwise use Jellyfin's count
|
||||||
|
var actualTrackCount = isConfigured
|
||||||
|
? trackStats.LocalTracks + trackStats.ExternalTracks
|
||||||
|
: childCount;
|
||||||
|
|
||||||
playlists.Add(new
|
playlists.Add(new
|
||||||
{
|
{
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
trackCount = childCount,
|
trackCount = actualTrackCount,
|
||||||
linkedSpotifyId,
|
linkedSpotifyId,
|
||||||
isConfigured,
|
isConfigured,
|
||||||
localTracks = trackStats.LocalTracks,
|
localTracks = trackStats.LocalTracks,
|
||||||
@@ -2158,12 +2228,19 @@ public class AdminController : ControllerBase
|
|||||||
Name = request.Name,
|
Name = request.Name,
|
||||||
Id = request.SpotifyPlaylistId,
|
Id = request.SpotifyPlaylistId,
|
||||||
JellyfinId = jellyfinPlaylistId,
|
JellyfinId = jellyfinPlaylistId,
|
||||||
LocalTracksPosition = LocalTracksPosition.First // Use Spotify order
|
LocalTracksPosition = LocalTracksPosition.First, // Use Spotify order
|
||||||
|
SyncSchedule = request.SyncSchedule ?? "0 8 * * 1" // Default to Monday 8 AM
|
||||||
});
|
});
|
||||||
|
|
||||||
// Convert to JSON format for env var: [["Name","SpotifyId","JellyfinId","first|last"],...]
|
// Convert to JSON format for env var: [["Name","SpotifyId","JellyfinId","first|last","cronSchedule"],...]
|
||||||
var playlistsJson = JsonSerializer.Serialize(
|
var playlistsJson = JsonSerializer.Serialize(
|
||||||
currentPlaylists.Select(p => new[] { p.Name, p.Id, p.JellyfinId, p.LocalTracksPosition.ToString().ToLower() }).ToArray()
|
currentPlaylists.Select(p => new[] {
|
||||||
|
p.Name,
|
||||||
|
p.Id,
|
||||||
|
p.JellyfinId,
|
||||||
|
p.LocalTracksPosition.ToString().ToLower(),
|
||||||
|
p.SyncSchedule ?? "0 8 * * 1"
|
||||||
|
}).ToArray()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update .env file
|
// Update .env file
|
||||||
@@ -2188,6 +2265,60 @@ public class AdminController : ControllerBase
|
|||||||
return await RemovePlaylist(decodedName);
|
return await RemovePlaylist(decodedName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update playlist sync schedule
|
||||||
|
/// </summary>
|
||||||
|
[HttpPut("playlists/{name}/schedule")]
|
||||||
|
public async Task<IActionResult> UpdatePlaylistSchedule(string name, [FromBody] UpdateScheduleRequest request)
|
||||||
|
{
|
||||||
|
var decodedName = Uri.UnescapeDataString(name);
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(request.SyncSchedule))
|
||||||
|
{
|
||||||
|
return BadRequest(new { error = "SyncSchedule is required" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Basic cron validation
|
||||||
|
var cronParts = request.SyncSchedule.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (cronParts.Length != 5)
|
||||||
|
{
|
||||||
|
return BadRequest(new { error = "Invalid cron format. Expected: minute hour day month dayofweek" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read current playlists
|
||||||
|
var currentPlaylists = await ReadPlaylistsFromEnvFile();
|
||||||
|
var playlist = currentPlaylists.FirstOrDefault(p => p.Name.Equals(decodedName, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
if (playlist == null)
|
||||||
|
{
|
||||||
|
return NotFound(new { error = $"Playlist '{decodedName}' not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the schedule
|
||||||
|
playlist.SyncSchedule = request.SyncSchedule.Trim();
|
||||||
|
|
||||||
|
// Save back to .env
|
||||||
|
var playlistsJson = JsonSerializer.Serialize(
|
||||||
|
currentPlaylists.Select(p => new[] {
|
||||||
|
p.Name,
|
||||||
|
p.Id,
|
||||||
|
p.JellyfinId,
|
||||||
|
p.LocalTracksPosition.ToString().ToLower(),
|
||||||
|
p.SyncSchedule ?? "0 8 * * 1"
|
||||||
|
}).ToArray()
|
||||||
|
);
|
||||||
|
|
||||||
|
var updateRequest = new ConfigUpdateRequest
|
||||||
|
{
|
||||||
|
Updates = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["SPOTIFY_IMPORT_PLAYLISTS"] = playlistsJson
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return await UpdateConfig(updateRequest);
|
||||||
|
}
|
||||||
|
|
||||||
private string GetJellyfinAuthHeader()
|
private string GetJellyfinAuthHeader()
|
||||||
{
|
{
|
||||||
return $"MediaBrowser Client=\"Allstarr\", Device=\"Server\", DeviceId=\"allstarr-admin\", Version=\"1.0.0\", Token=\"{_jellyfinSettings.ApiKey}\"";
|
return $"MediaBrowser Client=\"Allstarr\", Device=\"Server\", DeviceId=\"allstarr-admin\", Version=\"1.0.0\", Token=\"{_jellyfinSettings.ApiKey}\"";
|
||||||
@@ -2219,7 +2350,7 @@ public class AdminController : ControllerBase
|
|||||||
return playlists;
|
return playlists;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse JSON array format: [["Name","SpotifyId","JellyfinId","first|last"],...]
|
// Parse JSON array format: [["Name","SpotifyId","JellyfinId","first|last","cronSchedule"],...]
|
||||||
var playlistArrays = JsonSerializer.Deserialize<string[][]>(value);
|
var playlistArrays = JsonSerializer.Deserialize<string[][]>(value);
|
||||||
if (playlistArrays != null)
|
if (playlistArrays != null)
|
||||||
{
|
{
|
||||||
@@ -2235,7 +2366,8 @@ public class AdminController : ControllerBase
|
|||||||
LocalTracksPosition = arr.Length >= 4 &&
|
LocalTracksPosition = arr.Length >= 4 &&
|
||||||
arr[3].Trim().Equals("last", StringComparison.OrdinalIgnoreCase)
|
arr[3].Trim().Equals("last", StringComparison.OrdinalIgnoreCase)
|
||||||
? LocalTracksPosition.Last
|
? LocalTracksPosition.Last
|
||||||
: LocalTracksPosition.First
|
: LocalTracksPosition.First,
|
||||||
|
SyncSchedule = arr.Length >= 5 ? arr[4].Trim() : "0 8 * * 1"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2692,7 +2824,11 @@ public class AdminController : ControllerBase
|
|||||||
if (parts.Length >= 3)
|
if (parts.Length >= 3)
|
||||||
{
|
{
|
||||||
var timestamp = parts[0];
|
var timestamp = parts[0];
|
||||||
var endpoint = parts[1];
|
var method = 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))
|
||||||
@@ -2701,7 +2837,7 @@ public class AdminController : ControllerBase
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
usage[endpoint] = usage.GetValueOrDefault(endpoint, 0) + 1;
|
usage[fullEndpoint] = usage.GetValueOrDefault(fullEndpoint, 0) + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3286,6 +3422,12 @@ public class LinkPlaylistRequest
|
|||||||
{
|
{
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
public string SpotifyPlaylistId { get; set; } = string.Empty;
|
public string SpotifyPlaylistId { get; set; } = string.Empty;
|
||||||
|
public string SyncSchedule { get; set; } = "0 8 * * 1"; // Default: 8 AM every Monday
|
||||||
|
}
|
||||||
|
|
||||||
|
public class UpdateScheduleRequest
|
||||||
|
{
|
||||||
|
public string SyncSchedule { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -3297,7 +3439,7 @@ public class LinkPlaylistRequest
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var keptPath = _configuration["Library:KeptPath"] ?? "/app/kept";
|
var keptPath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept");
|
||||||
|
|
||||||
_logger.LogInformation("📂 Checking kept folder: {Path}", keptPath);
|
_logger.LogInformation("📂 Checking kept folder: {Path}", keptPath);
|
||||||
_logger.LogInformation("📂 Directory exists: {Exists}", Directory.Exists(keptPath));
|
_logger.LogInformation("📂 Directory exists: {Exists}", Directory.Exists(keptPath));
|
||||||
@@ -3380,7 +3522,7 @@ public class LinkPlaylistRequest
|
|||||||
return BadRequest(new { error = "Path is required" });
|
return BadRequest(new { error = "Path is required" });
|
||||||
}
|
}
|
||||||
|
|
||||||
var keptPath = _configuration["Library:KeptPath"] ?? "/app/kept";
|
var keptPath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept");
|
||||||
var fullPath = Path.Combine(keptPath, path);
|
var fullPath = Path.Combine(keptPath, path);
|
||||||
|
|
||||||
_logger.LogInformation("🗑️ Delete request for: {Path}", fullPath);
|
_logger.LogInformation("🗑️ Delete request for: {Path}", fullPath);
|
||||||
@@ -3444,7 +3586,7 @@ public class LinkPlaylistRequest
|
|||||||
return BadRequest(new { error = "Path is required" });
|
return BadRequest(new { error = "Path is required" });
|
||||||
}
|
}
|
||||||
|
|
||||||
var keptPath = _configuration["Library:KeptPath"] ?? "/app/kept";
|
var keptPath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "kept");
|
||||||
var fullPath = Path.Combine(keptPath, path);
|
var fullPath = Path.Combine(keptPath, path);
|
||||||
|
|
||||||
// Security: Ensure the path is within the kept directory
|
// Security: Ensure the path is within the kept directory
|
||||||
|
|||||||
@@ -1144,6 +1144,8 @@ 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();
|
||||||
@@ -1151,6 +1153,9 @@ 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)
|
||||||
{
|
{
|
||||||
@@ -1159,13 +1164,16 @@ 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, trying Spotify/LRCLIB");
|
_logger.LogInformation("No embedded lyrics found in Jellyfin (status: {StatusCode}), trying Spotify/LRCLIB", statusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get song metadata for lyrics search
|
// Get song metadata for lyrics search
|
||||||
@@ -2175,7 +2183,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.LogInformation("📡 Session capabilities reported - Method: {Method}, Query: {Query}", method, queryString);
|
_logger.LogDebug("📡 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}")));
|
||||||
@@ -2200,7 +2208,11 @@ public class JellyfinController : ControllerBase
|
|||||||
|
|
||||||
if (statusCode == 204 || statusCode == 200)
|
if (statusCode == 204 || statusCode == 200)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("✓ Session capabilities forwarded to Jellyfin ({StatusCode})", statusCode);
|
_logger.LogDebug("✓ Session capabilities forwarded to Jellyfin ({StatusCode})", statusCode);
|
||||||
|
}
|
||||||
|
else if (statusCode == 401)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("⚠ Jellyfin returned 401 for capabilities (token expired)");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -2234,7 +2246,7 @@ public class JellyfinController : ControllerBase
|
|||||||
}
|
}
|
||||||
Request.Body.Position = 0;
|
Request.Body.Position = 0;
|
||||||
|
|
||||||
_logger.LogInformation("📻 Playback START reported");
|
_logger.LogDebug("📻 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);
|
||||||
@@ -2310,7 +2322,7 @@ public class JellyfinController : ControllerBase
|
|||||||
|
|
||||||
if (ghostStatusCode == 204 || ghostStatusCode == 200)
|
if (ghostStatusCode == 204 || ghostStatusCode == 200)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("✓ Ghost playback start forwarded to Jellyfin for external track ({StatusCode})", ghostStatusCode);
|
_logger.LogDebug("✓ Ghost playback start forwarded to Jellyfin for external track ({StatusCode})", ghostStatusCode);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -2338,7 +2350,7 @@ public class JellyfinController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For local tracks, forward playback start to Jellyfin FIRST
|
// For local tracks, forward playback start to Jellyfin FIRST
|
||||||
_logger.LogInformation("Forwarding playback start to Jellyfin...");
|
_logger.LogDebug("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
|
||||||
@@ -2364,7 +2376,7 @@ public class JellyfinController : ControllerBase
|
|||||||
|
|
||||||
if (statusCode == 204 || statusCode == 200)
|
if (statusCode == 204 || statusCode == 200)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("✓ Playback start forwarded to Jellyfin ({StatusCode})", statusCode);
|
_logger.LogDebug("✓ 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))
|
||||||
@@ -2372,7 +2384,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.LogWarning("✓ SESSION: Session ensured for device {DeviceId} after playback start", deviceId);
|
_logger.LogDebug("✓ SESSION: Session ensured for device {DeviceId} after playback start", deviceId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -2396,7 +2408,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.LogInformation("✓ Basic playback start forwarded to Jellyfin ({StatusCode})", statusCode);
|
_logger.LogDebug("✓ Basic playback start forwarded to Jellyfin ({StatusCode})", statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2509,7 +2521,7 @@ 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.LogInformation("▶️ Progress: {Position:mm\\:ss} for item {ItemId}", position, itemId);
|
_logger.LogDebug("▶️ Progress: {Position:mm\\:ss} for item {ItemId}", position, itemId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2549,7 +2561,7 @@ public class JellyfinController : ControllerBase
|
|||||||
}
|
}
|
||||||
Request.Body.Position = 0;
|
Request.Body.Position = 0;
|
||||||
|
|
||||||
_logger.LogInformation("⏹️ Playback STOPPED reported");
|
_logger.LogDebug("⏹️ 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);
|
||||||
@@ -2607,7 +2619,7 @@ public class JellyfinController : ControllerBase
|
|||||||
|
|
||||||
if (stopStatusCode == 204 || stopStatusCode == 200)
|
if (stopStatusCode == 204 || stopStatusCode == 200)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("✓ Ghost playback stop forwarded to Jellyfin ({StatusCode})", stopStatusCode);
|
_logger.LogDebug("✓ Ghost playback stop forwarded to Jellyfin ({StatusCode})", stopStatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NoContent();
|
return NoContent();
|
||||||
@@ -2618,7 +2630,7 @@ public class JellyfinController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For local tracks, forward to Jellyfin
|
// For local tracks, forward to Jellyfin
|
||||||
_logger.LogInformation("Forwarding playback stop to Jellyfin...");
|
_logger.LogDebug("Forwarding playback stop to Jellyfin...");
|
||||||
|
|
||||||
// Log the body being sent for debugging
|
// Log the body being sent for debugging
|
||||||
_logger.LogInformation("📤 Sending playback stop body: {Body}", body);
|
_logger.LogInformation("📤 Sending playback stop body: {Body}", body);
|
||||||
@@ -2641,7 +2653,11 @@ public class JellyfinController : ControllerBase
|
|||||||
|
|
||||||
if (statusCode == 204 || statusCode == 200)
|
if (statusCode == 204 || statusCode == 200)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("✓ Playback stop forwarded to Jellyfin ({StatusCode})", statusCode);
|
_logger.LogDebug("✓ Playback stop forwarded to Jellyfin ({StatusCode})", statusCode);
|
||||||
|
}
|
||||||
|
else if (statusCode == 401)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Playback stop returned 401 (token expired)");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -2700,7 +2716,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.LogInformation("🔄 Proxying session request: {Method} {Endpoint}", method, endpoint);
|
_logger.LogDebug("🔄 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}")));
|
||||||
@@ -2730,11 +2746,11 @@ public class JellyfinController : ControllerBase
|
|||||||
|
|
||||||
if (result != null)
|
if (result != null)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("✓ Session request proxied successfully ({StatusCode})", statusCode);
|
_logger.LogDebug("✓ Session request proxied successfully ({StatusCode})", statusCode);
|
||||||
return new JsonResult(result.RootElement.Clone());
|
return new JsonResult(result.RootElement.Clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("✓ Session request proxied ({StatusCode}, no body)", statusCode);
|
_logger.LogDebug("✓ Session request proxied ({StatusCode}, no body)", statusCode);
|
||||||
return StatusCode(statusCode);
|
return StatusCode(statusCode);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -2792,7 +2808,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.LogWarning("🔍 SESSION/CAPABILITY REQUEST: {Method} /{Path}{Query}", Request.Method, path, Request.QueryString);
|
_logger.LogDebug("🔍 SESSION/CAPABILITY REQUEST: {Method} /{Path}{Query}", Request.Method, path, Request.QueryString);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -3900,7 +3916,7 @@ public class JellyfinController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build kept folder path: Artist/Album/
|
// Build kept folder path: Artist/Album/
|
||||||
var keptBasePath = _configuration["Library:KeptPath"] ?? "/app/kept";
|
var keptBasePath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "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));
|
||||||
|
|
||||||
@@ -4196,7 +4212,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 = _configuration["Library:KeptPath"] ?? "/app/kept";
|
var keptBasePath = Path.Combine(_configuration["Library:DownloadPath"] ?? "./downloads", "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));
|
||||||
|
|
||||||
|
|||||||
@@ -2,239 +2,44 @@ 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>
|
||||||
/// Authentication filter for Jellyfin API endpoints.
|
/// REMOVED: Authentication filter for Jellyfin API endpoints.
|
||||||
/// Validates client credentials against configured username and API key.
|
///
|
||||||
/// Clients can authenticate via:
|
/// This filter has been removed because Allstarr acts as a TRANSPARENT PROXY.
|
||||||
/// - Authorization header: MediaBrowser Token="apikey"
|
/// Clients authenticate directly with Jellyfin through the proxy, not with the proxy itself.
|
||||||
/// - X-Emby-Token header
|
///
|
||||||
/// - Query parameter: api_key
|
/// Authentication flow:
|
||||||
/// - JSON body (for login endpoints): Username/Pw fields
|
/// 1. Client sends credentials to /Users/AuthenticateByName
|
||||||
|
/// 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 partial class JellyfinAuthFilter : IAsyncActionFilter
|
public class JellyfinAuthFilter : IAsyncActionFilter
|
||||||
{
|
{
|
||||||
private readonly JellyfinSettings _settings;
|
|
||||||
private readonly ILogger<JellyfinAuthFilter> _logger;
|
private readonly ILogger<JellyfinAuthFilter> _logger;
|
||||||
|
|
||||||
public JellyfinAuthFilter(
|
public JellyfinAuthFilter(ILogger<JellyfinAuthFilter> logger)
|
||||||
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)
|
||||||
{
|
{
|
||||||
// Skip auth if no credentials configured (open mode)
|
// This filter is now a no-op - all authentication is handled by Jellyfin
|
||||||
if (string.IsNullOrEmpty(_settings.ClientUsername) || string.IsNullOrEmpty(_settings.ApiKey))
|
// Keeping the class for backwards compatibility but it does nothing
|
||||||
{
|
|
||||||
_logger.LogDebug("Auth skipped - no client credentials configured");
|
|
||||||
await next();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var request = context.HttpContext.Request;
|
_logger.LogTrace("JellyfinAuthFilter: Transparent proxy mode - no authentication check");
|
||||||
|
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.LogInformation("🔌 WEBSOCKET: WebSocket connection request received from {RemoteIp}",
|
_logger.LogDebug("🔌 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.LogInformation("✓ WEBSOCKET: Connected to Jellyfin WebSocket");
|
_logger.LogDebug("✓ 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);
|
||||||
@@ -154,9 +154,17 @@ public class WebSocketProxyMiddleware
|
|||||||
_logger.LogDebug("🔌 WEBSOCKET: WebSocket proxy connection closed");
|
_logger.LogDebug("🔌 WEBSOCKET: WebSocket proxy connection closed");
|
||||||
}
|
}
|
||||||
catch (WebSocketException wsEx)
|
catch (WebSocketException wsEx)
|
||||||
|
{
|
||||||
|
// 403 is expected when tokens expire or session ends - don't spam logs
|
||||||
|
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);
|
_logger.LogWarning(wsEx, "⚠️ WEBSOCKET: WebSocket error: {Message}", wsEx.Message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "❌ WEBSOCKET: Error in WebSocket proxy");
|
_logger.LogError(ex, "❌ WEBSOCKET: Error in WebSocket proxy");
|
||||||
@@ -194,7 +202,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.LogInformation("🧹 WEBSOCKET: Client disconnected, removing session for device {DeviceId}", deviceId);
|
_logger.LogDebug("🧹 WEBSOCKET: Client disconnected, removing session for device {DeviceId}", deviceId);
|
||||||
await _sessionManager.RemoveSessionAsync(deviceId);
|
await _sessionManager.RemoveSessionAsync(deviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,7 +247,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.LogInformation("📥 WEBSOCKET {Direction}: {Preview}",
|
_logger.LogTrace("📥 WEBSOCKET {Direction}: {Preview}",
|
||||||
direction,
|
direction,
|
||||||
messageText.Length > 500 ? messageText[..500] + "..." : messageText);
|
messageText.Length > 500 ? messageText[..500] + "..." : messageText);
|
||||||
}
|
}
|
||||||
@@ -274,7 +282,7 @@ public class WebSocketProxyMiddleware
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(ex, "⚠️ WEBSOCKET {Direction}: Error proxying messages", direction);
|
_logger.LogDebug(ex, "WEBSOCKET {Direction}: Error proxying messages (connection closed)", direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,12 @@ public class Song
|
|||||||
/// All artists for this track (main + featured). For display in Jellyfin clients.
|
/// All artists for this track (main + featured). For display in Jellyfin clients.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<string> Artists { get; set; } = new();
|
public List<string> Artists { get; set; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// All artist IDs corresponding to the Artists list. Index-matched with Artists.
|
||||||
|
/// </summary>
|
||||||
|
public List<string> ArtistIds { get; set; } = new();
|
||||||
|
|
||||||
public string Album { get; set; } = string.Empty;
|
public string Album { get; set; } = string.Empty;
|
||||||
public string? AlbumId { get; set; }
|
public string? AlbumId { get; set; }
|
||||||
public int? Duration { get; set; } // In seconds
|
public int? Duration { get; set; } // In seconds
|
||||||
|
|||||||
@@ -45,6 +45,14 @@ public class SpotifyPlaylistConfig
|
|||||||
/// Where to position local tracks: "first" or "last"
|
/// Where to position local tracks: "first" or "last"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public LocalTracksPosition LocalTracksPosition { get; set; } = LocalTracksPosition.First;
|
public LocalTracksPosition LocalTracksPosition { get; set; } = LocalTracksPosition.First;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cron schedule for syncing this playlist with Spotify
|
||||||
|
/// Format: minute hour day month dayofweek
|
||||||
|
/// Example: "0 8 * * 1" = 8 AM every Monday
|
||||||
|
/// Default: "0 8 * * 1" (weekly on Monday at 8 AM)
|
||||||
|
/// </summary>
|
||||||
|
public string SyncSchedule { get; set; } = "0 8 * * 1";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -59,27 +67,6 @@ 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.
|
||||||
|
|||||||
@@ -13,25 +13,49 @@ using allstarr.Middleware;
|
|||||||
using allstarr.Filters;
|
using allstarr.Filters;
|
||||||
using Microsoft.Extensions.Http;
|
using Microsoft.Extensions.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Configure forwarded headers for reverse proxy support (nginx, etc.)
|
||||||
|
// This allows ASP.NET Core to read X-Forwarded-For, X-Real-IP, etc.
|
||||||
|
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||||
|
{
|
||||||
|
options.ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor
|
||||||
|
| Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto
|
||||||
|
| Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedHost;
|
||||||
|
|
||||||
|
// Clear known networks and proxies to accept headers from any proxy
|
||||||
|
// This is safe when running behind a trusted reverse proxy (nginx)
|
||||||
|
options.KnownIPNetworks.Clear();
|
||||||
|
options.KnownProxies.Clear();
|
||||||
|
|
||||||
|
// Trust X-Forwarded-* headers from any source
|
||||||
|
// Only do this if your reverse proxy is properly configured and trusted
|
||||||
|
options.ForwardLimit = null;
|
||||||
|
});
|
||||||
|
|
||||||
// Decode SquidWTF API base URLs once at startup
|
// Decode SquidWTF API base URLs once at startup
|
||||||
var squidWtfApiUrls = DecodeSquidWtfUrls();
|
var squidWtfApiUrls = DecodeSquidWtfUrls();
|
||||||
static List<string> DecodeSquidWtfUrls()
|
static List<string> DecodeSquidWtfUrls()
|
||||||
{
|
{
|
||||||
var encodedUrls = new[]
|
var encodedUrls = new[]
|
||||||
{
|
{
|
||||||
"aHR0cHM6Ly90cml0b24uc3F1aWQud3Rm", // triton
|
"aHR0cHM6Ly90cml0b24uc3F1aWQud3Rm", // triton.squid.wtf
|
||||||
"aHR0cHM6Ly90aWRhbC1hcGkuYmluaW11bS5vcmc=", // binimum
|
"aHR0cHM6Ly90aWRhbC1hcGkuYmluaW11bS5vcmc=", // tidal-api.binimum.org
|
||||||
"aHR0cHM6Ly90aWRhbC5raW5vcGx1cy5vbmxpbmU=", // kinoplus
|
"aHR0cHM6Ly90aWRhbC5raW5vcGx1cy5vbmxpbmU=", // tidal.kinoplus.online
|
||||||
"aHR0cHM6Ly9oaWZpLXR3by5zcG90aXNhdmVyLm5ldA==", // spoti-2
|
"aHR0cHM6Ly9oaWZpLXR3by5zcG90aXNhdmVyLm5ldA==", // hifi-two.spotisaver.net
|
||||||
"aHR0cHM6Ly9oaWZpLW9uZS5zcG90aXNhdmVyLm5ldA==", // spoti-1
|
"aHR0cHM6Ly9oaWZpLW9uZS5zcG90aXNhdmVyLm5ldA==", // hifi-one.spotisaver.net
|
||||||
"aHR0cHM6Ly93b2xmLnFxZGwuc2l0ZQ==", // wolf
|
"aHR0cHM6Ly93b2xmLnFxZGwuc2l0ZQ==", // wolf.qqdl.site
|
||||||
"aHR0cDovL2h1bmQucXFkbC5zaXRl", // hund
|
"aHR0cDovL2h1bmQucXFkbC5zaXRl", // hund.qqdl.site (http)
|
||||||
"aHR0cHM6Ly9rYXR6ZS5xcWRsLnNpdGU=", // katze
|
"aHR0cHM6Ly9rYXR6ZS5xcWRsLnNpdGU=", // katze.qqdl.site
|
||||||
"aHR0cHM6Ly92b2dlbC5xcWRsLnNpdGU=", // vogel
|
"aHR0cHM6Ly92b2dlbC5xcWRsLnNpdGU=", // vogel.qqdl.site
|
||||||
"aHR0cHM6Ly9tYXVzLnFxZGwuc2l0ZQ==" // maus
|
"aHR0cHM6Ly9tYXVzLnFxZGwuc2l0ZQ==", // maus.qqdl.site
|
||||||
|
"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
|
||||||
@@ -353,7 +377,7 @@ builder.Services.Configure<SpotifyImportSettings>(options =>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Log configuration at startup
|
// Log configuration at startup
|
||||||
Console.WriteLine($"Spotify Import: Enabled={options.Enabled}, SyncHour={options.SyncStartHour}:{options.SyncStartMinute:D2}, WindowHours={options.SyncWindowHours}");
|
Console.WriteLine($"Spotify Import: Enabled={options.Enabled}, MatchingInterval={options.MatchingIntervalHours}h");
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -621,7 +645,23 @@ builder.Services.AddCors(options =>
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Migrate old .env file format on startup
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var migrationService = new EnvMigrationService(app.Services.GetRequiredService<ILogger<EnvMigrationService>>());
|
||||||
|
migrationService.MigrateEnvFile();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
app.Logger.LogWarning(ex, "Failed to run .env migration");
|
||||||
|
}
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|
||||||
|
// IMPORTANT: UseForwardedHeaders must be called BEFORE other middleware
|
||||||
|
// This processes X-Forwarded-For, X-Real-IP, etc. from nginx
|
||||||
|
app.UseForwardedHeaders();
|
||||||
|
|
||||||
app.UseExceptionHandler(_ => { }); // Global exception handler
|
app.UseExceptionHandler(_ => { }); // Global exception handler
|
||||||
|
|
||||||
// Enable response compression EARLY in the pipeline
|
// Enable response compression EARLY in the pipeline
|
||||||
|
|||||||
@@ -95,23 +95,89 @@ 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))
|
||||||
{
|
{
|
||||||
Logger.LogInformation("Streaming from local cache: {Path}", localPath);
|
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For on-demand streaming, download to disk first to ensure complete file
|
// Download to disk first to ensure complete file with metadata
|
||||||
// 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);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
localPath = await DownloadSongInternalAsync(externalProvider, externalId, triggerAlbumDownload: true, cancellationToken);
|
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);
|
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)
|
||||||
{
|
{
|
||||||
@@ -198,6 +264,7 @@ public abstract class BaseDownloadService : IDownloadService
|
|||||||
|
|
||||||
// Acquire lock BEFORE checking existence to prevent race conditions with concurrent requests
|
// Acquire lock BEFORE checking existence to prevent race conditions with concurrent requests
|
||||||
await DownloadLock.WaitAsync(cancellationToken);
|
await DownloadLock.WaitAsync(cancellationToken);
|
||||||
|
var lockHeld = true;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -219,21 +286,27 @@ public abstract class BaseDownloadService : IDownloadService
|
|||||||
// 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.LogInformation("Download already in progress for {SongId}, waiting...", songId);
|
Logger.LogDebug("Download already in progress for {SongId}, waiting for completion...", songId);
|
||||||
// Release lock while waiting
|
// Release lock while waiting
|
||||||
DownloadLock.Release();
|
DownloadLock.Release();
|
||||||
|
lockHeld = false;
|
||||||
|
|
||||||
|
// 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)
|
||||||
{
|
{
|
||||||
await Task.Delay(500, cancellationToken);
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Exception(activeDownload?.ErrorMessage ?? "Download failed");
|
// Download failed or was cancelled
|
||||||
|
throw new Exception(activeDownload?.ErrorMessage ?? "Download failed while waiting");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get metadata
|
// Get metadata
|
||||||
@@ -372,10 +445,13 @@ public abstract class BaseDownloadService : IDownloadService
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
{
|
||||||
|
if (lockHeld)
|
||||||
{
|
{
|
||||||
DownloadLock.Release();
|
DownloadLock.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected async Task DownloadRemainingAlbumTracksAsync(string albumExternalId, string excludeTrackExternalId)
|
protected async Task DownloadRemainingAlbumTracksAsync(string albumExternalId, string excludeTrackExternalId)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -66,7 +66,9 @@ public class CacheCleanupService : BackgroundService
|
|||||||
|
|
||||||
private async Task CleanupOldCachedFilesAsync(CancellationToken cancellationToken)
|
private async Task CleanupOldCachedFilesAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var cachePath = PathHelper.GetCachePath();
|
// Get the actual cache path used by download services
|
||||||
|
var downloadPath = _configuration["Library:DownloadPath"] ?? "downloads";
|
||||||
|
var cachePath = Path.Combine(downloadPath, "cache");
|
||||||
|
|
||||||
if (!Directory.Exists(cachePath))
|
if (!Directory.Exists(cachePath))
|
||||||
{
|
{
|
||||||
@@ -78,7 +80,7 @@ public class CacheCleanupService : BackgroundService
|
|||||||
var deletedCount = 0;
|
var deletedCount = 0;
|
||||||
var totalSize = 0L;
|
var totalSize = 0L;
|
||||||
|
|
||||||
_logger.LogInformation("Starting cache cleanup: deleting files older than {CutoffTime}", cutoffTime);
|
_logger.LogInformation("Starting cache cleanup: deleting files older than {CutoffTime} from {Path}", cutoffTime, cachePath);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -94,16 +96,16 @@ public class CacheCleanupService : BackgroundService
|
|||||||
{
|
{
|
||||||
var fileInfo = new FileInfo(filePath);
|
var fileInfo = new FileInfo(filePath);
|
||||||
|
|
||||||
// Use last access time to determine if file should be deleted
|
// Use last write time (when file was created/downloaded) to determine if file should be deleted
|
||||||
// This gets updated when a cached file is streamed
|
// LastAccessTime is unreliable on many filesystems (noatime mount option)
|
||||||
if (fileInfo.LastAccessTimeUtc < cutoffTime)
|
if (fileInfo.LastWriteTimeUtc < 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} (last accessed: {LastAccess})",
|
_logger.LogDebug("Deleted cached file: {Path} (age: {Age:F1} hours)",
|
||||||
filePath, fileInfo.LastAccessTimeUtc);
|
filePath, (DateTime.UtcNow - fileInfo.LastWriteTimeUtc).TotalHours);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ public class EndpointBenchmarkService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Benchmarks a list of endpoints by making test requests.
|
/// Benchmarks a list of endpoints by making test requests.
|
||||||
/// Returns endpoints sorted by average response time (fastest first).
|
/// Returns endpoints sorted by average response time (fastest first).
|
||||||
|
///
|
||||||
|
/// IMPORTANT: The testFunc should implement its own timeout to prevent slow endpoints
|
||||||
|
/// from blocking startup. Recommended: 5-10 second timeout per ping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<List<string>> BenchmarkEndpointsAsync(
|
public async Task<List<string>> BenchmarkEndpointsAsync(
|
||||||
List<string> endpoints,
|
List<string> endpoints,
|
||||||
|
|||||||
59
allstarr/Services/Common/EnvMigrationService.cs
Normal file
59
allstarr/Services/Common/EnvMigrationService.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
namespace allstarr.Services.Common;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Service that runs on startup to migrate old .env file format to new format
|
||||||
|
/// </summary>
|
||||||
|
public class EnvMigrationService
|
||||||
|
{
|
||||||
|
private readonly ILogger<EnvMigrationService> _logger;
|
||||||
|
private readonly string _envFilePath;
|
||||||
|
|
||||||
|
public EnvMigrationService(ILogger<EnvMigrationService> logger)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_envFilePath = Path.Combine(Directory.GetCurrentDirectory(), ".env");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MigrateEnvFile()
|
||||||
|
{
|
||||||
|
if (!File.Exists(_envFilePath))
|
||||||
|
{
|
||||||
|
_logger.LogDebug("No .env file found, skipping migration");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var lines = File.ReadAllLines(_envFilePath);
|
||||||
|
var modified = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < lines.Length; i++)
|
||||||
|
{
|
||||||
|
var line = lines[i].Trim();
|
||||||
|
|
||||||
|
// Skip comments and empty lines
|
||||||
|
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Migrate DOWNLOAD_PATH to Library__DownloadPath
|
||||||
|
if (line.StartsWith("DOWNLOAD_PATH="))
|
||||||
|
{
|
||||||
|
var value = line.Substring("DOWNLOAD_PATH=".Length);
|
||||||
|
lines[i] = $"Library__DownloadPath={value}";
|
||||||
|
modified = true;
|
||||||
|
_logger.LogInformation("Migrated DOWNLOAD_PATH to Library__DownloadPath in .env file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modified)
|
||||||
|
{
|
||||||
|
File.WriteAllLines(_envFilePath, lines);
|
||||||
|
_logger.LogInformation("✅ .env file migration completed successfully");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Failed to migrate .env file - please manually update DOWNLOAD_PATH to Library__DownloadPath");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,12 @@ public class RoundRobinFallbackHelper
|
|||||||
private readonly object _urlIndexLock = new object();
|
private readonly object _urlIndexLock = new object();
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly string _serviceName;
|
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 int EndpointCount => _apiUrls.Count;
|
||||||
|
|
||||||
@@ -24,6 +30,91 @@ public class RoundRobinFallbackHelper
|
|||||||
{
|
{
|
||||||
throw new ArgumentException("API URLs list cannot be empty", nameof(apiUrls));
|
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>
|
/// <summary>
|
||||||
@@ -54,10 +145,14 @@ public class RoundRobinFallbackHelper
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries the request with the next provider in round-robin, then falls back to others on failure.
|
/// 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.
|
/// 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.
|
/// Throws exception if all endpoints fail.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<T> TryWithFallbackAsync<T>(Func<string, Task<T>> action)
|
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
|
// Start with the next URL in round-robin to distribute load
|
||||||
var startIndex = 0;
|
var startIndex = 0;
|
||||||
lock (_urlIndexLock)
|
lock (_urlIndexLock)
|
||||||
@@ -66,16 +161,21 @@ public class RoundRobinFallbackHelper
|
|||||||
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
|
_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
|
// Try all URLs starting from the round-robin selected one
|
||||||
for (int attempt = 0; attempt < _apiUrls.Count; attempt++)
|
for (int attempt = 0; attempt < endpointsToTry.Count; attempt++)
|
||||||
{
|
{
|
||||||
var urlIndex = (startIndex + attempt) % _apiUrls.Count;
|
var urlIndex = (startIndex + attempt) % endpointsToTry.Count;
|
||||||
var baseUrl = _apiUrls[urlIndex];
|
var baseUrl = endpointsToTry[urlIndex];
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Trying {Service} endpoint {Endpoint} (attempt {Attempt}/{Total})",
|
_logger.LogDebug("Trying {Service} endpoint {Endpoint} (attempt {Attempt}/{Total})",
|
||||||
_serviceName, baseUrl, attempt + 1, _apiUrls.Count);
|
_serviceName, baseUrl, attempt + 1, endpointsToTry.Count);
|
||||||
return await action(baseUrl);
|
return await action(baseUrl);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -83,9 +183,15 @@ public class RoundRobinFallbackHelper
|
|||||||
_logger.LogWarning(ex, "{Service} request failed with endpoint {Endpoint}, trying next...",
|
_logger.LogWarning(ex, "{Service} request failed with endpoint {Endpoint}, trying next...",
|
||||||
_serviceName, baseUrl);
|
_serviceName, baseUrl);
|
||||||
|
|
||||||
if (attempt == _apiUrls.Count - 1)
|
// Mark as unhealthy in cache
|
||||||
|
lock (_healthCacheLock)
|
||||||
{
|
{
|
||||||
_logger.LogError("All {Count} {Service} endpoints failed", _apiUrls.Count, _serviceName);
|
_healthCache[baseUrl] = (false, DateTime.UtcNow);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (attempt == endpointsToTry.Count - 1)
|
||||||
|
{
|
||||||
|
_logger.LogError("All {Count} {Service} endpoints failed", endpointsToTry.Count, _serviceName);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,10 +256,14 @@ public class RoundRobinFallbackHelper
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tries the request with the next provider in round-robin, then falls back to others on failure.
|
/// 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).
|
/// Returns default value if all endpoints fail (does not throw).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<T> TryWithFallbackAsync<T>(Func<string, Task<T>> action, T defaultValue)
|
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
|
// Start with the next URL in round-robin to distribute load
|
||||||
var startIndex = 0;
|
var startIndex = 0;
|
||||||
lock (_urlIndexLock)
|
lock (_urlIndexLock)
|
||||||
@@ -162,16 +272,21 @@ public class RoundRobinFallbackHelper
|
|||||||
_currentUrlIndex = (_currentUrlIndex + 1) % _apiUrls.Count;
|
_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
|
// Try all URLs starting from the round-robin selected one
|
||||||
for (int attempt = 0; attempt < _apiUrls.Count; attempt++)
|
for (int attempt = 0; attempt < endpointsToTry.Count; attempt++)
|
||||||
{
|
{
|
||||||
var urlIndex = (startIndex + attempt) % _apiUrls.Count;
|
var urlIndex = (startIndex + attempt) % endpointsToTry.Count;
|
||||||
var baseUrl = _apiUrls[urlIndex];
|
var baseUrl = endpointsToTry[urlIndex];
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Trying {Service} endpoint {Endpoint} (attempt {Attempt}/{Total})",
|
_logger.LogDebug("Trying {Service} endpoint {Endpoint} (attempt {Attempt}/{Total})",
|
||||||
_serviceName, baseUrl, attempt + 1, _apiUrls.Count);
|
_serviceName, baseUrl, attempt + 1, endpointsToTry.Count);
|
||||||
return await action(baseUrl);
|
return await action(baseUrl);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -179,10 +294,16 @@ public class RoundRobinFallbackHelper
|
|||||||
_logger.LogWarning(ex, "{Service} request failed with endpoint {Endpoint}, trying next...",
|
_logger.LogWarning(ex, "{Service} request failed with endpoint {Endpoint}, trying next...",
|
||||||
_serviceName, baseUrl);
|
_serviceName, baseUrl);
|
||||||
|
|
||||||
if (attempt == _apiUrls.Count - 1)
|
// 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",
|
_logger.LogError("All {Count} {Service} endpoints failed, returning default value",
|
||||||
_apiUrls.Count, _serviceName);
|
endpointsToTry.Count, _serviceName);
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,10 +107,10 @@ 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 cache/Music folder (cleaned up after 24h), Permanent mode uses downloads folder
|
// Cache mode uses downloads/cache/ folder, Permanent mode uses downloads/permanent/
|
||||||
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
|
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
|
||||||
? Path.Combine("cache", "Music")
|
? Path.Combine("downloads", "cache")
|
||||||
: "downloads";
|
: 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
|
||||||
|
|||||||
@@ -384,17 +384,23 @@ public class DeezerMetadataService : IMusicMetadataService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contributors
|
// Contributors (all artists including features)
|
||||||
var contributors = new List<string>();
|
var contributors = new List<string>();
|
||||||
|
var contributorIds = new List<string>();
|
||||||
if (track.TryGetProperty("contributors", out var contribs))
|
if (track.TryGetProperty("contributors", out var contribs))
|
||||||
{
|
{
|
||||||
foreach (var contrib in contribs.EnumerateArray())
|
foreach (var contrib in contribs.EnumerateArray())
|
||||||
{
|
{
|
||||||
if (contrib.TryGetProperty("name", out var contribName))
|
if (contrib.TryGetProperty("name", out var contribName) &&
|
||||||
|
contrib.TryGetProperty("id", out var contribId))
|
||||||
{
|
{
|
||||||
var name = contribName.GetString();
|
var name = contribName.GetString();
|
||||||
|
var id = contribId.GetInt64();
|
||||||
if (!string.IsNullOrEmpty(name))
|
if (!string.IsNullOrEmpty(name))
|
||||||
|
{
|
||||||
contributors.Add(name);
|
contributors.Add(name);
|
||||||
|
contributorIds.Add($"ext-deezer-artist-{id}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -437,6 +443,8 @@ public class DeezerMetadataService : IMusicMetadataService
|
|||||||
ArtistId = track.TryGetProperty("artist", out var artistForId)
|
ArtistId = track.TryGetProperty("artist", out var artistForId)
|
||||||
? $"ext-deezer-artist-{artistForId.GetProperty("id").GetInt64()}"
|
? $"ext-deezer-artist-{artistForId.GetProperty("id").GetInt64()}"
|
||||||
: null,
|
: null,
|
||||||
|
Artists = contributors.Count > 0 ? contributors : new List<string>(),
|
||||||
|
ArtistIds = contributorIds.Count > 0 ? contributorIds : new List<string>(),
|
||||||
Album = track.TryGetProperty("album", out var album)
|
Album = track.TryGetProperty("album", out var album)
|
||||||
? album.GetProperty("title").GetString() ?? ""
|
? album.GetProperty("title").GetString() ?? ""
|
||||||
: "",
|
: "",
|
||||||
|
|||||||
@@ -168,6 +168,11 @@ 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)
|
||||||
{
|
{
|
||||||
@@ -179,11 +184,27 @@ 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.LogInformation("✓ Forwarded X-Emby-Authorization: {Value}", headerValue);
|
_logger.LogTrace("Forwarded X-Emby-Authorization header");
|
||||||
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)
|
||||||
@@ -201,37 +222,32 @@ 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.LogInformation("✓ Converted Authorization to X-Emby-Authorization: {Value}", headerValue);
|
_logger.LogTrace("Converted Authorization to X-Emby-Authorization");
|
||||||
}
|
}
|
||||||
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.LogInformation("✓ Forwarded Authorization (Bearer): {Value}", headerValue);
|
_logger.LogTrace("Forwarded Authorization header");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only log warnings for non-browser static requests
|
// Check for api_key query parameter (some clients use this)
|
||||||
if (!authHeaderAdded && !isBrowserStaticRequest)
|
if (!authHeaderAdded && url.Contains("api_key=", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
_logger.LogWarning("✗ No auth header found. Available headers: {Headers}",
|
authHeaderAdded = true; // It's in the URL, no need to add header
|
||||||
string.Join(", ", clientHeaders.Select(h => $"{h.Key}={h.Value}")));
|
_logger.LogTrace("Using api_key from query string");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!isBrowserStaticRequest)
|
|
||||||
{
|
|
||||||
_logger.LogWarning("✗ No client headers provided for {Url}", url);
|
|
||||||
}
|
|
||||||
|
|
||||||
// DO NOT use server API key as fallback - let Jellyfin handle unauthenticated requests
|
// Only log warnings for non-public, non-browser requests without auth
|
||||||
// If client doesn't provide auth, they get what they deserve (401 from Jellyfin)
|
if (!authHeaderAdded && !isBrowserStaticRequest && !isPublicEndpoint)
|
||||||
if (!authHeaderAdded && !isBrowserStaticRequest)
|
|
||||||
{
|
{
|
||||||
_logger.LogInformation("No client auth provided for {Url} - forwarding without auth", url);
|
_logger.LogDebug("No client auth provided for {Url} - Jellyfin will handle authentication", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||||
@@ -248,14 +264,28 @@ public class JellyfinProxyService
|
|||||||
{
|
{
|
||||||
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Jellyfin returned 401 Unauthorized for {Url} - passing through to client", url);
|
// 401 means token expired or invalid - client needs to re-authenticate
|
||||||
|
_logger.LogInformation("Jellyfin returned 401 Unauthorized for {Url} - client should re-authenticate", url);
|
||||||
}
|
}
|
||||||
else if (!isBrowserStaticRequest) // Don't log 404s for browser static requests
|
else if (!isBrowserStaticRequest && !isPublicEndpoint)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Jellyfin request failed: {StatusCode} for {Url}", response.StatusCode, url);
|
_logger.LogWarning("Jellyfin request failed: {StatusCode} for {Url}", response.StatusCode, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return null body with the actual status code
|
// Try to parse error response to pass through to client
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,8 +327,10 @@ 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))
|
||||||
@@ -306,11 +338,28 @@ 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.LogDebug("Forwarded X-Emby-Authorization from client");
|
_logger.LogTrace("Forwarded X-Emby-Authorization header");
|
||||||
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)
|
||||||
@@ -325,13 +374,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.LogDebug("Converted Authorization to X-Emby-Authorization");
|
_logger.LogTrace("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.LogDebug("Forwarded Authorization header");
|
_logger.LogTrace("Forwarded Authorization header");
|
||||||
}
|
}
|
||||||
authHeaderAdded = true;
|
authHeaderAdded = true;
|
||||||
break;
|
break;
|
||||||
@@ -339,30 +388,23 @@ public class JellyfinProxyService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DO NOT use server credentials as fallback
|
// For authentication endpoints, credentials are in the body, not headers
|
||||||
// Exception: For auth endpoints, client provides their own credentials in the body
|
// For other endpoints without auth, let Jellyfin reject the request
|
||||||
// For all other endpoints, if client doesn't provide auth, let Jellyfin reject it
|
if (!authHeaderAdded && !isAuthEndpoint)
|
||||||
if (!authHeaderAdded)
|
|
||||||
{
|
{
|
||||||
_logger.LogInformation("No client auth provided for POST {Url} - forwarding without auth", url);
|
_logger.LogDebug("No client auth provided for POST {Url} - Jellyfin will handle authentication", 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 (endpoint.Contains("Authenticate", StringComparison.OrdinalIgnoreCase))
|
if (isAuthEndpoint)
|
||||||
{
|
{
|
||||||
_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.LogInformation("POST to Jellyfin: {Url}, body length: {Length} bytes", url, bodyToSend.Length);
|
_logger.LogTrace("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);
|
||||||
@@ -372,8 +414,17 @@ 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}",
|
|
||||||
response.StatusCode, url, errorContent);
|
// 401 is expected when tokens expire - don't spam logs
|
||||||
|
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
|
// Try to parse error response as JSON to pass through to client
|
||||||
if (!string.IsNullOrWhiteSpace(errorContent))
|
if (!string.IsNullOrWhiteSpace(errorContent))
|
||||||
@@ -395,7 +446,7 @@ public class JellyfinProxyService
|
|||||||
// Log successful session-related responses
|
// Log successful session-related responses
|
||||||
if (endpoint.Contains("Sessions", StringComparison.OrdinalIgnoreCase))
|
if (endpoint.Contains("Sessions", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
_logger.LogWarning("✓ SESSION: Jellyfin responded {StatusCode} for {Endpoint}", statusCode, endpoint);
|
_logger.LogTrace("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)
|
||||||
@@ -412,13 +463,6 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -299,13 +299,11 @@ public class JellyfinResponseBuilder
|
|||||||
["ItemId"] = song.Id
|
["ItemId"] = song.Id
|
||||||
},
|
},
|
||||||
["Artists"] = artistNames.Count > 0 ? artistNames.ToArray() : new[] { artistName ?? "" },
|
["Artists"] = artistNames.Count > 0 ? artistNames.ToArray() : new[] { artistName ?? "" },
|
||||||
["ArtistItems"] = artistNames.Count > 0
|
["ArtistItems"] = artistNames.Count > 0 && song.ArtistIds.Count == artistNames.Count
|
||||||
? artistNames.Select((name, index) => new Dictionary<string, object?>
|
? artistNames.Select((name, index) => new Dictionary<string, object?>
|
||||||
{
|
{
|
||||||
["Name"] = name,
|
["Name"] = name,
|
||||||
["Id"] = index == 0 && song.ArtistId != null
|
["Id"] = song.ArtistIds[index]
|
||||||
? song.ArtistId
|
|
||||||
: $"{song.Id}-artist-{index}"
|
|
||||||
}).ToArray()
|
}).ToArray()
|
||||||
: new[]
|
: new[]
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,12 +38,13 @@ 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("⚠️ SESSION: Cannot create session - no device ID");
|
_logger.LogWarning("Cannot create session - no device ID");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,27 +52,43 @@ 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.LogDebug("✓ SESSION: Session already exists for device {DeviceId}", deviceId);
|
_logger.LogTrace("Session already exists for device {DeviceId}", deviceId);
|
||||||
|
|
||||||
// Refresh capabilities to keep session alive
|
// Refresh capabilities to keep session alive
|
||||||
await PostCapabilitiesAsync(headers);
|
// If this returns false (401), the token expired and client needs to re-auth
|
||||||
|
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.LogInformation("🔧 SESSION: Creating new session for device: {DeviceId} ({Client} on {Device})", deviceId, client, device);
|
_logger.LogDebug("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
|
||||||
await PostCapabilitiesAsync(headers);
|
var success = await PostCapabilitiesAsync(headers);
|
||||||
|
|
||||||
_logger.LogInformation("✓ SESSION: Session created for {DeviceId}", deviceId);
|
if (!success)
|
||||||
|
{
|
||||||
|
// 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
|
||||||
|
var clientIp = headers["X-Forwarded-For"].FirstOrDefault()?.Split(',')[0].Trim()
|
||||||
|
?? headers["X-Real-IP"].FirstOrDefault()
|
||||||
|
?? "Unknown";
|
||||||
|
|
||||||
_sessions[deviceId] = new SessionInfo
|
_sessions[deviceId] = new SessionInfo
|
||||||
{
|
{
|
||||||
DeviceId = deviceId,
|
DeviceId = deviceId,
|
||||||
@@ -79,7 +96,8 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
Device = device,
|
Device = device,
|
||||||
Version = version,
|
Version = version,
|
||||||
LastActivity = DateTime.UtcNow,
|
LastActivity = DateTime.UtcNow,
|
||||||
Headers = CloneHeaders(headers)
|
Headers = CloneHeaders(headers),
|
||||||
|
ClientIp = clientIp
|
||||||
};
|
};
|
||||||
|
|
||||||
// Start a WebSocket connection to Jellyfin on behalf of this client
|
// Start a WebSocket connection to Jellyfin on behalf of this client
|
||||||
@@ -89,15 +107,16 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "❌ SESSION: Error creating session for {DeviceId}", deviceId);
|
_logger.LogError(ex, "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 PostCapabilitiesAsync(IHeaderDictionary headers)
|
private async Task<bool> PostCapabilitiesAsync(IHeaderDictionary headers)
|
||||||
{
|
{
|
||||||
var capabilities = new
|
var capabilities = new
|
||||||
{
|
{
|
||||||
@@ -118,12 +137,19 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
|
|
||||||
if (statusCode == 204 || statusCode == 200)
|
if (statusCode == 204 || statusCode == 200)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("✓ SESSION: Posted capabilities successfully ({StatusCode})", statusCode);
|
_logger.LogTrace("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
|
||||||
{
|
{
|
||||||
// 401 is common when cached headers have expired - not a critical error
|
_logger.LogDebug("Capabilities post returned {StatusCode}", statusCode);
|
||||||
_logger.LogDebug("SESSION: Capabilities post returned {StatusCode} (may be expected if token expired)", statusCode);
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,7 +204,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.LogInformation("🧹 SESSION: Auto-removing inactive session {DeviceId} after playback stop", deviceId);
|
_logger.LogDebug("🧹 SESSION: Auto-removing inactive session {DeviceId} after playback stop", deviceId);
|
||||||
await RemoveSessionAsync(deviceId);
|
await RemoveSessionAsync(deviceId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -201,6 +227,7 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
Client = s.Client,
|
Client = s.Client,
|
||||||
Device = s.Device,
|
Device = s.Device,
|
||||||
Version = s.Version,
|
Version = s.Version,
|
||||||
|
ClientIp = s.ClientIp,
|
||||||
LastActivity = s.LastActivity,
|
LastActivity = s.LastActivity,
|
||||||
InactiveMinutes = Math.Round((now - s.LastActivity).TotalMinutes, 1),
|
InactiveMinutes = Math.Round((now - s.LastActivity).TotalMinutes, 1),
|
||||||
HasWebSocket = s.WebSocket != null,
|
HasWebSocket = s.WebSocket != null,
|
||||||
@@ -223,7 +250,7 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
{
|
{
|
||||||
if (_sessions.TryRemove(deviceId, out var session))
|
if (_sessions.TryRemove(deviceId, out var session))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("🗑️ SESSION: Removing session for device {DeviceId}", deviceId);
|
_logger.LogDebug("🗑️ 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)
|
||||||
@@ -235,7 +262,7 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(ex, "⚠️ WEBSOCKET: Error closing WebSocket for {DeviceId}", deviceId);
|
_logger.LogDebug(ex, "WEBSOCKET: Error closing WebSocket for {DeviceId}", deviceId);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -255,7 +282,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.LogInformation("🛑 SESSION: Reported playback stopped for {DeviceId} (ItemId: {ItemId}, Position: {Position})",
|
_logger.LogDebug("🛑 SESSION: Reported playback stopped for {DeviceId} (ItemId: {ItemId}, Position: {Position})",
|
||||||
deviceId, session.LastPlayingItemId, session.LastPlayingPositionTicks);
|
deviceId, session.LastPlayingItemId, session.LastPlayingPositionTicks);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,11 +366,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.LogWarning("⚠️ WEBSOCKET: No client auth found in headers, falling back to server API key for {DeviceId}", deviceId);
|
_logger.LogDebug("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}!", deviceId);
|
_logger.LogWarning("❌ WEBSOCKET: No authentication available for {DeviceId} - WebSocket will fail", deviceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -354,7 +381,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.LogInformation("✓ WEBSOCKET: Connected to Jellyfin for device {DeviceId}", deviceId);
|
_logger.LogDebug("✓ 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
|
||||||
@@ -410,8 +437,8 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Log other message types at info level
|
// Log other message types at trace level
|
||||||
_logger.LogInformation("📥 WEBSOCKET: {DeviceId}: {Message}",
|
_logger.LogTrace("📥 WEBSOCKET: {DeviceId}: {Message}",
|
||||||
deviceId, message.Length > 100 ? message[..100] + "..." : message);
|
deviceId, message.Length > 100 ? message[..100] + "..." : message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -433,7 +460,7 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
}
|
}
|
||||||
catch (WebSocketException wsEx)
|
catch (WebSocketException wsEx)
|
||||||
{
|
{
|
||||||
_logger.LogWarning(wsEx, "⚠️ WEBSOCKET: WebSocket error for device {DeviceId}", deviceId);
|
_logger.LogDebug(wsEx, "WEBSOCKET: Connection closed for device {DeviceId}", deviceId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -469,6 +496,7 @@ 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)
|
||||||
{
|
{
|
||||||
@@ -480,29 +508,43 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogDebug("💓 SESSION: Keeping {Count} sessions alive", activeSessions.Count);
|
_logger.LogTrace("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
|
||||||
// Note: This may fail with 401 if the client's token has expired
|
// If this returns false (401), the token has expired
|
||||||
// That's okay - the WebSocket connection keeps the session alive anyway
|
var success = await PostCapabilitiesAsync(session.Headers);
|
||||||
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, "SESSION: Error keeping session alive for {DeviceId} (WebSocket still active)", session.DeviceId);
|
_logger.LogDebug(ex, "Error keeping session alive for {DeviceId}", 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.LogInformation("🧹 SESSION: Removing stale session for {DeviceId} (inactive for {Minutes:F1} minutes)",
|
_logger.LogDebug("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);
|
||||||
}
|
}
|
||||||
@@ -529,6 +571,7 @@ public class JellyfinSessionManager : IDisposable
|
|||||||
public ClientWebSocket? WebSocket { get; set; }
|
public ClientWebSocket? WebSocket { get; set; }
|
||||||
public string? LastPlayingItemId { get; set; }
|
public string? LastPlayingItemId { get; set; }
|
||||||
public long? LastPlayingPositionTicks { get; set; }
|
public long? LastPlayingPositionTicks { get; set; }
|
||||||
|
public string? ClientIp { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -110,10 +110,10 @@ 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 cache/Music folder (cleaned up after 24h), Permanent mode uses downloads folder
|
// Cache mode uses downloads/cache/ folder, Permanent mode uses downloads/permanent/
|
||||||
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
|
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
|
||||||
? Path.Combine("cache", "Music")
|
? Path.Combine(DownloadPath, "cache")
|
||||||
: "downloads";
|
: 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)!;
|
||||||
|
|||||||
@@ -349,6 +349,17 @@ public class SpotifyApiClient : IDisposable
|
|||||||
|
|
||||||
var response = await _webApiClient.SendAsync(request, cancellationToken);
|
var response = await _webApiClient.SendAsync(request, cancellationToken);
|
||||||
|
|
||||||
|
// Handle 429 rate limiting with exponential backoff
|
||||||
|
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
|
||||||
|
{
|
||||||
|
var retryAfter = response.Headers.RetryAfter?.Delta ?? TimeSpan.FromSeconds(5);
|
||||||
|
_logger.LogWarning("Spotify rate limit hit (429) when fetching playlist {PlaylistId}. Waiting {Seconds}s before retry...", playlistId, retryAfter.TotalSeconds);
|
||||||
|
await Task.Delay(retryAfter, cancellationToken);
|
||||||
|
|
||||||
|
// Retry the request
|
||||||
|
response = await _webApiClient.SendAsync(request, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
_logger.LogError("Failed to fetch playlist via GraphQL: {StatusCode}", response.StatusCode);
|
_logger.LogError("Failed to fetch playlist via GraphQL: {StatusCode}", response.StatusCode);
|
||||||
@@ -735,6 +746,18 @@ public class SpotifyApiClient : IDisposable
|
|||||||
public async Task<List<SpotifyPlaylist>> SearchUserPlaylistsAsync(
|
public async Task<List<SpotifyPlaylist>> SearchUserPlaylistsAsync(
|
||||||
string searchName,
|
string searchName,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return await GetUserPlaylistsAsync(searchName, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all playlists from the user's library, optionally filtered by name.
|
||||||
|
/// Uses GraphQL API which is less rate-limited than REST API.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="searchName">Optional name filter (case-insensitive). If null, returns all playlists.</param>
|
||||||
|
public async Task<List<SpotifyPlaylist>> GetUserPlaylistsAsync(
|
||||||
|
string? searchName = null,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var token = await GetWebAccessTokenAsync(cancellationToken);
|
var token = await GetWebAccessTokenAsync(cancellationToken);
|
||||||
if (string.IsNullOrEmpty(token))
|
if (string.IsNullOrEmpty(token))
|
||||||
@@ -744,61 +767,204 @@ public class SpotifyApiClient : IDisposable
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Use GraphQL endpoint instead of REST API to avoid rate limiting
|
||||||
|
// GraphQL is less aggressive with rate limits
|
||||||
var playlists = new List<SpotifyPlaylist>();
|
var playlists = new List<SpotifyPlaylist>();
|
||||||
var offset = 0;
|
var offset = 0;
|
||||||
const int limit = 50;
|
const int limit = 50;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var url = $"{OfficialApiBase}/me/playlists?offset={offset}&limit={limit}";
|
// GraphQL query to fetch user playlists - using libraryV3 operation
|
||||||
|
var queryParams = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "operationName", "libraryV3" },
|
||||||
|
{ "variables", $"{{\"filters\":[\"Playlists\",\"By Spotify\"],\"order\":null,\"textFilter\":\"\",\"features\":[\"LIKED_SONGS\",\"YOUR_EPISODES\"],\"offset\":{offset},\"limit\":{limit}}}" },
|
||||||
|
{ "extensions", "{\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"50650f72ea32a99b5b46240bee22fea83024eec302478a9a75cfd05a0814ba99\"}}" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var queryString = string.Join("&", queryParams.Select(kv => $"{Uri.EscapeDataString(kv.Key)}={Uri.EscapeDataString(kv.Value)}"));
|
||||||
|
var url = $"{WebApiBase}/query?{queryString}";
|
||||||
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||||
|
|
||||||
var response = await _httpClient.SendAsync(request, cancellationToken);
|
var response = await _webApiClient.SendAsync(request, cancellationToken);
|
||||||
if (!response.IsSuccessStatusCode) break;
|
|
||||||
|
// Handle 429 rate limiting with exponential backoff
|
||||||
|
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
|
||||||
|
{
|
||||||
|
var retryAfter = response.Headers.RetryAfter?.Delta ?? TimeSpan.FromSeconds(5);
|
||||||
|
_logger.LogWarning("Spotify rate limit hit (429) when fetching library playlists. Waiting {Seconds}s before retry...", retryAfter.TotalSeconds);
|
||||||
|
await Task.Delay(retryAfter, cancellationToken);
|
||||||
|
|
||||||
|
// Retry the request
|
||||||
|
response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("GraphQL user playlists request failed: {StatusCode}", response.StatusCode);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||||
using var doc = JsonDocument.Parse(json);
|
using var doc = JsonDocument.Parse(json);
|
||||||
var root = doc.RootElement;
|
var root = doc.RootElement;
|
||||||
|
|
||||||
if (!root.TryGetProperty("items", out var items) || items.GetArrayLength() == 0)
|
if (!root.TryGetProperty("data", out var data) ||
|
||||||
|
!data.TryGetProperty("me", out var me) ||
|
||||||
|
!me.TryGetProperty("libraryV3", out var library) ||
|
||||||
|
!library.TryGetProperty("items", out var items))
|
||||||
|
{
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get total count
|
||||||
|
if (library.TryGetProperty("totalCount", out var totalCount))
|
||||||
|
{
|
||||||
|
var total = totalCount.GetInt32();
|
||||||
|
if (total == 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var itemCount = 0;
|
||||||
foreach (var item in items.EnumerateArray())
|
foreach (var item in items.EnumerateArray())
|
||||||
{
|
{
|
||||||
var itemName = item.TryGetProperty("name", out var n) ? n.GetString() ?? "" : "";
|
itemCount++;
|
||||||
|
|
||||||
// Check if name matches (case-insensitive)
|
if (!item.TryGetProperty("item", out var playlistItem) ||
|
||||||
if (itemName.Contains(searchName, StringComparison.OrdinalIgnoreCase))
|
!playlistItem.TryGetProperty("data", out var playlist))
|
||||||
{
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check __typename to filter out folders and only include playlists
|
||||||
|
if (playlistItem.TryGetProperty("__typename", out var typename))
|
||||||
|
{
|
||||||
|
var typeStr = typename.GetString();
|
||||||
|
// Skip folders - only process Playlist types
|
||||||
|
if (typeStr != null && typeStr.Contains("Folder", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get playlist URI/ID
|
||||||
|
string? uri = null;
|
||||||
|
if (playlistItem.TryGetProperty("uri", out var uriProp))
|
||||||
|
{
|
||||||
|
uri = uriProp.GetString();
|
||||||
|
}
|
||||||
|
else if (playlistItem.TryGetProperty("_uri", out var uriProp2))
|
||||||
|
{
|
||||||
|
uri = uriProp2.GetString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(uri)) continue;
|
||||||
|
|
||||||
|
// Skip if not a playlist URI (e.g., folders have different URI format)
|
||||||
|
if (!uri.StartsWith("spotify:playlist:", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var spotifyId = uri.Replace("spotify:playlist:", "", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
var itemName = playlist.TryGetProperty("name", out var n) ? n.GetString() ?? "" : "";
|
||||||
|
|
||||||
|
// Check if name matches (case-insensitive) - if searchName is provided
|
||||||
|
if (!string.IsNullOrEmpty(searchName) &&
|
||||||
|
!itemName.Contains(searchName, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get track count if available - try multiple possible paths
|
||||||
|
var trackCount = 0;
|
||||||
|
if (playlist.TryGetProperty("content", out var content))
|
||||||
|
{
|
||||||
|
if (content.TryGetProperty("totalCount", out var totalTrackCount))
|
||||||
|
{
|
||||||
|
trackCount = totalTrackCount.GetInt32();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Fallback: try attributes.itemCount
|
||||||
|
else if (playlist.TryGetProperty("attributes", out var attributes) &&
|
||||||
|
attributes.TryGetProperty("itemCount", out var itemCountProp))
|
||||||
|
{
|
||||||
|
trackCount = itemCountProp.GetInt32();
|
||||||
|
}
|
||||||
|
// Fallback: try totalCount directly
|
||||||
|
else if (playlist.TryGetProperty("totalCount", out var directTotalCount))
|
||||||
|
{
|
||||||
|
trackCount = directTotalCount.GetInt32();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log if we couldn't find track count for debugging
|
||||||
|
if (trackCount == 0)
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Could not find track count for playlist {Name} (ID: {Id}). Response structure: {Json}",
|
||||||
|
itemName, spotifyId, playlist.GetRawText());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get owner name
|
||||||
|
string? ownerName = null;
|
||||||
|
if (playlist.TryGetProperty("ownerV2", out var ownerV2) &&
|
||||||
|
ownerV2.TryGetProperty("data", out var ownerData) &&
|
||||||
|
ownerData.TryGetProperty("username", out var ownerNameProp))
|
||||||
|
{
|
||||||
|
ownerName = ownerNameProp.GetString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get image URL
|
||||||
|
string? imageUrl = null;
|
||||||
|
if (playlist.TryGetProperty("images", out var images) &&
|
||||||
|
images.TryGetProperty("items", out var imageItems) &&
|
||||||
|
imageItems.GetArrayLength() > 0)
|
||||||
|
{
|
||||||
|
var firstImage = imageItems[0];
|
||||||
|
if (firstImage.TryGetProperty("sources", out var sources) &&
|
||||||
|
sources.GetArrayLength() > 0)
|
||||||
|
{
|
||||||
|
var firstSource = sources[0];
|
||||||
|
if (firstSource.TryGetProperty("url", out var urlProp))
|
||||||
|
{
|
||||||
|
imageUrl = urlProp.GetString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
playlists.Add(new SpotifyPlaylist
|
playlists.Add(new SpotifyPlaylist
|
||||||
{
|
{
|
||||||
SpotifyId = item.TryGetProperty("id", out var itemId) ? itemId.GetString() ?? "" : "",
|
SpotifyId = spotifyId,
|
||||||
Name = itemName,
|
Name = itemName,
|
||||||
Description = item.TryGetProperty("description", out var desc) ? desc.GetString() : null,
|
Description = playlist.TryGetProperty("description", out var desc) ? desc.GetString() : null,
|
||||||
TotalTracks = item.TryGetProperty("tracks", out var tracks) &&
|
TotalTracks = trackCount,
|
||||||
tracks.TryGetProperty("total", out var total)
|
OwnerName = ownerName,
|
||||||
? total.GetInt32() : 0,
|
ImageUrl = imageUrl,
|
||||||
SnapshotId = item.TryGetProperty("snapshot_id", out var snap) ? snap.GetString() : null
|
SnapshotId = null
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (items.GetArrayLength() < limit) break;
|
if (itemCount < limit) break;
|
||||||
offset += limit;
|
offset += limit;
|
||||||
|
|
||||||
if (_settings.RateLimitDelayMs > 0)
|
// Add delay between pages to avoid rate limiting
|
||||||
{
|
// Library fetching can be aggressive, so use a longer delay
|
||||||
await Task.Delay(_settings.RateLimitDelayMs, cancellationToken);
|
var delayMs = Math.Max(_settings.RateLimitDelayMs, 500); // Minimum 500ms between pages
|
||||||
}
|
_logger.LogDebug("Waiting {DelayMs}ms before fetching next page of library playlists...", delayMs);
|
||||||
|
await Task.Delay(delayMs, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Found {Count} playlists{Filter} via GraphQL",
|
||||||
|
playlists.Count,
|
||||||
|
string.IsNullOrEmpty(searchName) ? "" : $" matching '{searchName}'");
|
||||||
return playlists;
|
return playlists;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error searching user playlists for '{SearchName}'", searchName);
|
_logger.LogError(ex, "Error fetching user playlists{Filter} via GraphQL",
|
||||||
|
string.IsNullOrEmpty(searchName) ? "" : $" matching '{searchName}'");
|
||||||
return new List<SpotifyPlaylist>();
|
return new List<SpotifyPlaylist>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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, bypassSyncWindowCheck: true);
|
await FetchMissingTracksAsync(CancellationToken.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
@@ -84,19 +84,6 @@ 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);
|
||||||
|
|
||||||
// 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");
|
_logger.LogInformation("Background check interval: 5 minutes");
|
||||||
|
|
||||||
// Fetch playlist names from Jellyfin
|
// Fetch playlist names from Jellyfin
|
||||||
@@ -109,7 +96,7 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
|||||||
}
|
}
|
||||||
_logger.LogInformation("========================================");
|
_logger.LogInformation("========================================");
|
||||||
|
|
||||||
// Check if we should run on startup
|
// Run on startup if we don't have cache
|
||||||
if (!_hasRunOnce)
|
if (!_hasRunOnce)
|
||||||
{
|
{
|
||||||
var shouldRun = await ShouldRunOnStartupAsync();
|
var shouldRun = await ShouldRunOnStartupAsync();
|
||||||
@@ -118,7 +105,7 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
|||||||
_logger.LogInformation("Running initial fetch on startup");
|
_logger.LogInformation("Running initial fetch on startup");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await FetchMissingTracksAsync(stoppingToken, bypassSyncWindowCheck: true);
|
await FetchMissingTracksAsync(stoppingToken);
|
||||||
_hasRunOnce = true;
|
_hasRunOnce = true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -128,21 +115,20 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Skipping startup fetch - already have current files");
|
_logger.LogInformation("Skipping startup fetch - already have cached 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)
|
||||||
@@ -156,42 +142,29 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
|||||||
|
|
||||||
private async Task<bool> ShouldFetchNowAsync()
|
private async Task<bool> ShouldFetchNowAsync()
|
||||||
{
|
{
|
||||||
var settings = _spotifySettings.Value;
|
// Check if we have recent cache files (within last 24 hours)
|
||||||
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))
|
||||||
{
|
{
|
||||||
var fileTime = File.GetLastWriteTimeUtc(filePath);
|
// Missing cache file for this playlist
|
||||||
|
|
||||||
// If file is from today's sync or later, we already have it
|
|
||||||
if (fileTime >= todaySync)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Missing today's file for this playlist
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// All playlists have today's files
|
var fileTime = File.GetLastWriteTimeUtc(filePath);
|
||||||
|
if (fileTime < cacheThreshold)
|
||||||
|
{
|
||||||
|
// Cache file is older than 24 hours
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// All playlists have recent cache files
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,25 +183,6 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
|||||||
{
|
{
|
||||||
_logger.LogInformation("=== STARTUP CACHE CHECK ===");
|
_logger.LogInformation("=== STARTUP CACHE CHECK ===");
|
||||||
|
|
||||||
var settings = _spotifySettings.Value;
|
|
||||||
var now = DateTime.UtcNow;
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("We're before today's sync window end - checking if we have recent cache...");
|
|
||||||
|
|
||||||
var allPlaylistsHaveCache = true;
|
var allPlaylistsHaveCache = true;
|
||||||
|
|
||||||
foreach (var playlistName in _playlistIdToName.Values)
|
foreach (var playlistName in _playlistIdToName.Values)
|
||||||
@@ -265,66 +219,8 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
|||||||
if (allPlaylistsHaveCache)
|
if (allPlaylistsHaveCache)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("=== ALL PLAYLISTS HAVE CACHE - SKIPPING STARTUP FETCH ===");
|
_logger.LogInformation("=== ALL PLAYLISTS HAVE CACHE - SKIPPING STARTUP FETCH ===");
|
||||||
_logger.LogInformation("Will search again after {Time:yyyy-MM-dd HH:mm} UTC", todaySyncEnd);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// If we're after today's sync window end, check if we already have today's file
|
|
||||||
if (now >= todaySyncEnd)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("We're after today's sync window end - checking if we already fetched today's files...");
|
|
||||||
|
|
||||||
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 ===");
|
||||||
return true;
|
return true;
|
||||||
@@ -380,32 +276,9 @@ public class SpotifyMissingTracksFetcher : BackgroundService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task FetchMissingTracksAsync(CancellationToken cancellationToken, bool bypassSyncWindowCheck = false)
|
private async Task FetchMissingTracksAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var settings = _spotifySettings.Value;
|
_logger.LogInformation("=== FETCHING MISSING TRACKS ===");
|
||||||
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
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using allstarr.Models.Spotify;
|
|||||||
using allstarr.Services.Common;
|
using allstarr.Services.Common;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using Cronos;
|
||||||
|
|
||||||
namespace allstarr.Services.Spotify;
|
namespace allstarr.Services.Spotify;
|
||||||
|
|
||||||
@@ -14,6 +15,9 @@ namespace allstarr.Services.Spotify;
|
|||||||
/// - ISRC codes available for exact matching
|
/// - ISRC codes available for exact matching
|
||||||
/// - Real-time data without waiting for plugin sync schedules
|
/// - Real-time data without waiting for plugin sync schedules
|
||||||
/// - Full track metadata (duration, release date, etc.)
|
/// - Full track metadata (duration, release date, etc.)
|
||||||
|
///
|
||||||
|
/// CRON SCHEDULING: Playlists are fetched based on their cron schedules, not a global interval.
|
||||||
|
/// Cache persists until next cron run to prevent excess Spotify API calls.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SpotifyPlaylistFetcher : BackgroundService
|
public class SpotifyPlaylistFetcher : BackgroundService
|
||||||
{
|
{
|
||||||
@@ -45,6 +49,7 @@ public class SpotifyPlaylistFetcher : BackgroundService
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the Spotify playlist tracks in order, using cache if available.
|
/// Gets the Spotify playlist tracks in order, using cache if available.
|
||||||
|
/// Cache persists until next cron run to prevent excess API calls.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="playlistName">Playlist name (e.g., "Release Radar", "Discover Weekly")</param>
|
/// <param name="playlistName">Playlist name (e.g., "Release Radar", "Discover Weekly")</param>
|
||||||
/// <returns>List of tracks in playlist order, or empty list if not found</returns>
|
/// <returns>List of tracks in playlist order, or empty list if not found</returns>
|
||||||
@@ -57,7 +62,38 @@ public class SpotifyPlaylistFetcher : BackgroundService
|
|||||||
if (cached != null && cached.Tracks.Count > 0)
|
if (cached != null && cached.Tracks.Count > 0)
|
||||||
{
|
{
|
||||||
var age = DateTime.UtcNow - cached.FetchedAt;
|
var age = DateTime.UtcNow - cached.FetchedAt;
|
||||||
if (age.TotalMinutes < _spotifyApiSettings.CacheDurationMinutes)
|
|
||||||
|
// Calculate if cache should still be valid based on cron schedule
|
||||||
|
var playlistConfig = _spotifyImportSettings.GetPlaylistByName(playlistName);
|
||||||
|
var shouldRefresh = false;
|
||||||
|
|
||||||
|
if (playlistConfig != null && !string.IsNullOrEmpty(playlistConfig.SyncSchedule))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var cron = CronExpression.Parse(playlistConfig.SyncSchedule);
|
||||||
|
var nextRun = cron.GetNextOccurrence(cached.FetchedAt, TimeZoneInfo.Utc);
|
||||||
|
|
||||||
|
if (nextRun.HasValue && DateTime.UtcNow >= nextRun.Value)
|
||||||
|
{
|
||||||
|
shouldRefresh = true;
|
||||||
|
_logger.LogInformation("Cache expired for '{Name}' - next cron run was at {NextRun} UTC",
|
||||||
|
playlistName, nextRun.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Could not parse cron schedule for '{Name}', falling back to cache duration", playlistName);
|
||||||
|
shouldRefresh = age.TotalMinutes >= _spotifyApiSettings.CacheDurationMinutes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// No cron schedule, use cache duration from settings
|
||||||
|
shouldRefresh = age.TotalMinutes >= _spotifyApiSettings.CacheDurationMinutes;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!shouldRefresh)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Using cached playlist '{Name}' ({Count} tracks, age: {Age:F1}m)",
|
_logger.LogDebug("Using cached playlist '{Name}' ({Count} tracks, age: {Age:F1}m)",
|
||||||
playlistName, cached.Tracks.Count, age.TotalMinutes);
|
playlistName, cached.Tracks.Count, age.TotalMinutes);
|
||||||
@@ -94,11 +130,11 @@ public class SpotifyPlaylistFetcher : BackgroundService
|
|||||||
if (!_playlistNameToSpotifyId.TryGetValue(playlistName, out var spotifyId))
|
if (!_playlistNameToSpotifyId.TryGetValue(playlistName, out var spotifyId))
|
||||||
{
|
{
|
||||||
// Check if we have a configured Spotify ID for this playlist
|
// Check if we have a configured Spotify ID for this playlist
|
||||||
var playlistConfig = _spotifyImportSettings.GetPlaylistByName(playlistName);
|
var config = _spotifyImportSettings.GetPlaylistByName(playlistName);
|
||||||
if (playlistConfig != null && !string.IsNullOrEmpty(playlistConfig.Id))
|
if (config != null && !string.IsNullOrEmpty(config.Id))
|
||||||
{
|
{
|
||||||
// Use the configured Spotify playlist ID directly
|
// Use the configured Spotify playlist ID directly
|
||||||
spotifyId = playlistConfig.Id;
|
spotifyId = config.Id;
|
||||||
_playlistNameToSpotifyId[playlistName] = spotifyId;
|
_playlistNameToSpotifyId[playlistName] = spotifyId;
|
||||||
_logger.LogInformation("Using configured Spotify playlist ID for '{Name}': {Id}", playlistName, spotifyId);
|
_logger.LogInformation("Using configured Spotify playlist ID for '{Name}': {Id}", playlistName, spotifyId);
|
||||||
}
|
}
|
||||||
@@ -144,12 +180,39 @@ public class SpotifyPlaylistFetcher : BackgroundService
|
|||||||
return cached?.Tracks ?? new List<SpotifyPlaylistTrack>();
|
return cached?.Tracks ?? new List<SpotifyPlaylistTrack>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cache
|
// Calculate cache expiration based on cron schedule
|
||||||
await _cache.SetAsync(cacheKey, playlist, TimeSpan.FromMinutes(_spotifyApiSettings.CacheDurationMinutes * 2));
|
var playlistCfg = _spotifyImportSettings.GetPlaylistByName(playlistName);
|
||||||
|
var cacheExpiration = TimeSpan.FromMinutes(_spotifyApiSettings.CacheDurationMinutes * 2); // Default
|
||||||
|
|
||||||
|
if (playlistCfg != null && !string.IsNullOrEmpty(playlistCfg.SyncSchedule))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var cron = CronExpression.Parse(playlistCfg.SyncSchedule);
|
||||||
|
var nextRun = cron.GetNextOccurrence(DateTime.UtcNow, TimeZoneInfo.Utc);
|
||||||
|
|
||||||
|
if (nextRun.HasValue)
|
||||||
|
{
|
||||||
|
var timeUntilNextRun = nextRun.Value - DateTime.UtcNow;
|
||||||
|
// Add 5 minutes buffer
|
||||||
|
cacheExpiration = timeUntilNextRun + TimeSpan.FromMinutes(5);
|
||||||
|
|
||||||
|
_logger.LogInformation("Playlist '{Name}' cache will persist until next cron run: {NextRun} UTC (in {Hours:F1}h)",
|
||||||
|
playlistName, nextRun.Value, timeUntilNextRun.TotalHours);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Could not calculate next cron run for '{Name}', using default cache duration", playlistName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update cache with cron-based expiration
|
||||||
|
await _cache.SetAsync(cacheKey, playlist, cacheExpiration);
|
||||||
await SaveToFileCacheAsync(playlistName, playlist);
|
await SaveToFileCacheAsync(playlistName, playlist);
|
||||||
|
|
||||||
_logger.LogInformation("Fetched and cached playlist '{Name}' with {Count} tracks in order",
|
_logger.LogInformation("Fetched and cached playlist '{Name}' with {Count} tracks (expires in {Hours:F1}h)",
|
||||||
playlistName, playlist.Tracks.Count);
|
playlistName, playlist.Tracks.Count, cacheExpiration.TotalHours);
|
||||||
|
|
||||||
return playlist.Tracks;
|
return playlist.Tracks;
|
||||||
}
|
}
|
||||||
@@ -235,32 +298,102 @@ public class SpotifyPlaylistFetcher : BackgroundService
|
|||||||
|
|
||||||
_logger.LogInformation("Spotify API ENABLED");
|
_logger.LogInformation("Spotify API ENABLED");
|
||||||
_logger.LogInformation("Authenticated via sp_dc session cookie");
|
_logger.LogInformation("Authenticated via sp_dc session cookie");
|
||||||
_logger.LogInformation("Cache duration: {Minutes} minutes", _spotifyApiSettings.CacheDurationMinutes);
|
|
||||||
_logger.LogInformation("ISRC matching: {Enabled}", _spotifyApiSettings.PreferIsrcMatching ? "enabled" : "disabled");
|
_logger.LogInformation("ISRC matching: {Enabled}", _spotifyApiSettings.PreferIsrcMatching ? "enabled" : "disabled");
|
||||||
_logger.LogInformation("Configured Playlists: {Count}", _spotifyImportSettings.Playlists.Count);
|
_logger.LogInformation("Configured Playlists: {Count}", _spotifyImportSettings.Playlists.Count);
|
||||||
|
|
||||||
foreach (var playlist in _spotifyImportSettings.Playlists)
|
foreach (var playlist in _spotifyImportSettings.Playlists)
|
||||||
{
|
{
|
||||||
_logger.LogInformation(" - {Name}", playlist.Name);
|
var schedule = string.IsNullOrEmpty(playlist.SyncSchedule) ? "0 8 * * 1" : playlist.SyncSchedule;
|
||||||
|
_logger.LogInformation(" - {Name}: {Schedule}", playlist.Name, schedule);
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("========================================");
|
_logger.LogInformation("========================================");
|
||||||
|
|
||||||
// Initial fetch of all playlists
|
// Initial fetch of all playlists on startup
|
||||||
await FetchAllPlaylistsAsync(stoppingToken);
|
await FetchAllPlaylistsAsync(stoppingToken);
|
||||||
|
|
||||||
// Periodic refresh loop
|
// Cron-based refresh loop - only fetch when cron schedule triggers
|
||||||
|
// This prevents excess Spotify API calls
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromMinutes(_spotifyApiSettings.CacheDurationMinutes), stoppingToken);
|
try
|
||||||
|
{
|
||||||
|
// Check each playlist to see if it needs refreshing based on cron schedule
|
||||||
|
var now = DateTime.UtcNow;
|
||||||
|
var needsRefresh = new List<string>();
|
||||||
|
|
||||||
|
foreach (var config in _spotifyImportSettings.Playlists)
|
||||||
|
{
|
||||||
|
var schedule = string.IsNullOrEmpty(config.SyncSchedule) ? "0 8 * * 1" : config.SyncSchedule;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await FetchAllPlaylistsAsync(stoppingToken);
|
var cron = CronExpression.Parse(schedule);
|
||||||
|
|
||||||
|
// Check if we have cached data
|
||||||
|
var cacheKey = $"{CacheKeyPrefix}{config.Name}";
|
||||||
|
var cached = await _cache.GetAsync<SpotifyPlaylist>(cacheKey);
|
||||||
|
|
||||||
|
if (cached != null)
|
||||||
|
{
|
||||||
|
// Calculate when the next run should be after the last fetch
|
||||||
|
var nextRun = cron.GetNextOccurrence(cached.FetchedAt, TimeZoneInfo.Utc);
|
||||||
|
|
||||||
|
if (nextRun.HasValue && now >= nextRun.Value)
|
||||||
|
{
|
||||||
|
needsRefresh.Add(config.Name);
|
||||||
|
_logger.LogInformation("Playlist '{Name}' needs refresh - last fetched {Age:F1}h ago, next run was {NextRun}",
|
||||||
|
config.Name, (now - cached.FetchedAt).TotalHours, nextRun.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// No cache, fetch it
|
||||||
|
needsRefresh.Add(config.Name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error during periodic playlist refresh");
|
_logger.LogError(ex, "Invalid cron schedule for playlist {Name}: {Schedule}", config.Name, schedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch playlists that need refreshing
|
||||||
|
if (needsRefresh.Count > 0)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("=== CRON TRIGGER: Fetching {Count} playlists ===", needsRefresh.Count);
|
||||||
|
|
||||||
|
foreach (var playlistName in needsRefresh)
|
||||||
|
{
|
||||||
|
if (stoppingToken.IsCancellationRequested) break;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await GetPlaylistTracksAsync(playlistName);
|
||||||
|
|
||||||
|
// Rate limiting between playlists
|
||||||
|
if (playlistName != needsRefresh.Last())
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Waiting 3 seconds before next playlist to avoid rate limits...");
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(3), stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error fetching playlist '{Name}'", playlistName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("=== FINISHED FETCHING PLAYLISTS ===");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sleep for 1 hour before checking again
|
||||||
|
await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error in playlist fetcher loop");
|
||||||
|
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using allstarr.Services.Jellyfin;
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using Cronos;
|
||||||
|
|
||||||
namespace allstarr.Services.Spotify;
|
namespace allstarr.Services.Spotify;
|
||||||
|
|
||||||
@@ -17,6 +18,9 @@ namespace allstarr.Services.Spotify;
|
|||||||
/// 2. Direct API mode: Uses SpotifyPlaylistTrack (with ISRC and ordering)
|
/// 2. Direct API mode: Uses SpotifyPlaylistTrack (with ISRC and ordering)
|
||||||
///
|
///
|
||||||
/// When ISRC is available, exact matching is preferred. Falls back to fuzzy matching.
|
/// When ISRC is available, exact matching is preferred. Falls back to fuzzy matching.
|
||||||
|
///
|
||||||
|
/// CRON SCHEDULING: Each playlist has its own cron schedule. Matching only runs when the schedule triggers.
|
||||||
|
/// Manual refresh is always allowed. Cache persists until next cron run.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SpotifyTrackMatchingService : BackgroundService
|
public class SpotifyTrackMatchingService : BackgroundService
|
||||||
{
|
{
|
||||||
@@ -27,8 +31,10 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
private const int DelayBetweenSearchesMs = 150; // 150ms = ~6.6 searches/second to avoid rate limiting
|
private const int DelayBetweenSearchesMs = 150; // 150ms = ~6.6 searches/second to avoid rate limiting
|
||||||
private const int BatchSize = 11; // Number of parallel searches (matches SquidWTF provider count)
|
private const int BatchSize = 11; // Number of parallel searches (matches SquidWTF provider count)
|
||||||
private DateTime _lastMatchingRun = DateTime.MinValue;
|
|
||||||
private readonly TimeSpan _minimumMatchingInterval = TimeSpan.FromMinutes(5); // Don't run more than once per 5 minutes
|
// Track last run time per playlist to prevent duplicate runs
|
||||||
|
private readonly Dictionary<string, DateTime> _lastRunTimes = new();
|
||||||
|
private readonly TimeSpan _minimumRunInterval = TimeSpan.FromMinutes(5); // Cooldown between runs
|
||||||
|
|
||||||
public SpotifyTrackMatchingService(
|
public SpotifyTrackMatchingService(
|
||||||
IOptions<SpotifyImportSettings> spotifySettings,
|
IOptions<SpotifyImportSettings> spotifySettings,
|
||||||
@@ -57,17 +63,29 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("========================================");
|
||||||
_logger.LogInformation("SpotifyTrackMatchingService: Starting up...");
|
_logger.LogInformation("SpotifyTrackMatchingService: Starting up...");
|
||||||
|
|
||||||
if (!_spotifySettings.Enabled)
|
if (!_spotifySettings.Enabled)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Spotify playlist injection is DISABLED, matching service will not run");
|
_logger.LogInformation("Spotify playlist injection is DISABLED, matching service will not run");
|
||||||
|
_logger.LogInformation("========================================");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var matchMode = _spotifyApiSettings.Enabled && _spotifyApiSettings.PreferIsrcMatching
|
var matchMode = _spotifyApiSettings.Enabled && _spotifyApiSettings.PreferIsrcMatching
|
||||||
? "ISRC-preferred" : "fuzzy";
|
? "ISRC-preferred" : "fuzzy";
|
||||||
_logger.LogInformation("Matching mode: {Mode}", matchMode);
|
_logger.LogInformation("Matching mode: {Mode}", matchMode);
|
||||||
|
_logger.LogInformation("Cron-based scheduling: Each playlist has independent schedule");
|
||||||
|
|
||||||
|
// Log all playlist schedules
|
||||||
|
foreach (var playlist in _spotifySettings.Playlists)
|
||||||
|
{
|
||||||
|
var schedule = string.IsNullOrEmpty(playlist.SyncSchedule) ? "0 8 * * 1" : playlist.SyncSchedule;
|
||||||
|
_logger.LogInformation(" - {Name}: {Schedule}", playlist.Name, schedule);
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("========================================");
|
||||||
|
|
||||||
// Wait a bit for the fetcher to run first
|
// Wait a bit for the fetcher to run first
|
||||||
await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken);
|
await Task.Delay(TimeSpan.FromMinutes(2), stoppingToken);
|
||||||
@@ -75,7 +93,7 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
// Run once on startup to match any existing missing tracks
|
// Run once on startup to match any existing missing tracks
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Running initial track matching on startup");
|
_logger.LogInformation("Running initial track matching on startup (one-time)");
|
||||||
await MatchAllPlaylistsAsync(stoppingToken);
|
await MatchAllPlaylistsAsync(stoppingToken);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -83,46 +101,100 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
_logger.LogError(ex, "Error during startup track matching");
|
_logger.LogError(ex, "Error during startup track matching");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now start the periodic matching loop
|
// Now start the cron-based scheduling loop
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
// Wait for configured interval before next run (default 24 hours)
|
try
|
||||||
var intervalHours = _spotifySettings.MatchingIntervalHours;
|
|
||||||
if (intervalHours <= 0)
|
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Periodic matching disabled (MatchingIntervalHours = {Hours}), only startup run will execute", intervalHours);
|
// Calculate next run time for each playlist
|
||||||
break; // Exit loop - only run once on startup
|
var now = DateTime.UtcNow;
|
||||||
}
|
var nextRuns = new List<(string PlaylistName, DateTime NextRun, CronExpression Cron)>();
|
||||||
|
|
||||||
await Task.Delay(TimeSpan.FromHours(intervalHours), stoppingToken);
|
foreach (var playlist in _spotifySettings.Playlists)
|
||||||
|
{
|
||||||
|
var schedule = string.IsNullOrEmpty(playlist.SyncSchedule) ? "0 8 * * 1" : playlist.SyncSchedule;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await MatchAllPlaylistsAsync(stoppingToken);
|
var cron = CronExpression.Parse(schedule);
|
||||||
|
var nextRun = cron.GetNextOccurrence(now, TimeZoneInfo.Utc);
|
||||||
|
|
||||||
|
if (nextRun.HasValue)
|
||||||
|
{
|
||||||
|
nextRuns.Add((playlist.Name, nextRun.Value, cron));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Could not calculate next run for playlist {Name} with schedule {Schedule}",
|
||||||
|
playlist.Name, schedule);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error in track matching service");
|
_logger.LogError(ex, "Invalid cron schedule for playlist {Name}: {Schedule}",
|
||||||
|
playlist.Name, schedule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextRuns.Count == 0)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("No valid cron schedules found, sleeping for 1 hour");
|
||||||
|
await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the next playlist that needs to run
|
||||||
|
var nextPlaylist = nextRuns.OrderBy(x => x.NextRun).First();
|
||||||
|
var waitTime = nextPlaylist.NextRun - now;
|
||||||
|
|
||||||
|
if (waitTime.TotalSeconds > 0)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Next scheduled run: {Playlist} at {Time} UTC (in {Minutes:F1} minutes)",
|
||||||
|
nextPlaylist.PlaylistName, nextPlaylist.NextRun, waitTime.TotalMinutes);
|
||||||
|
|
||||||
|
// Wait until next run (or max 1 hour to re-check schedules)
|
||||||
|
var maxWait = TimeSpan.FromHours(1);
|
||||||
|
var actualWait = waitTime > maxWait ? maxWait : waitTime;
|
||||||
|
await Task.Delay(actualWait, stoppingToken);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time to run this playlist
|
||||||
|
_logger.LogInformation("=== CRON TRIGGER: Running scheduled match for {Playlist} ===", nextPlaylist.PlaylistName);
|
||||||
|
|
||||||
|
// Check cooldown to prevent duplicate runs
|
||||||
|
if (_lastRunTimes.TryGetValue(nextPlaylist.PlaylistName, out var lastRun))
|
||||||
|
{
|
||||||
|
var timeSinceLastRun = now - lastRun;
|
||||||
|
if (timeSinceLastRun < _minimumRunInterval)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Skipping {Playlist} - last run was {Seconds}s ago (cooldown: {Cooldown}s)",
|
||||||
|
nextPlaylist.PlaylistName, (int)timeSinceLastRun.TotalSeconds, (int)_minimumRunInterval.TotalSeconds);
|
||||||
|
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run matching for this playlist
|
||||||
|
await MatchSinglePlaylistAsync(nextPlaylist.PlaylistName, stoppingToken);
|
||||||
|
_lastRunTimes[nextPlaylist.PlaylistName] = DateTime.UtcNow;
|
||||||
|
|
||||||
|
_logger.LogInformation("=== FINISHED: {Playlist} - Next run at {NextRun} UTC ===",
|
||||||
|
nextPlaylist.PlaylistName, nextPlaylist.Cron.GetNextOccurrence(DateTime.UtcNow, TimeZoneInfo.Utc));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error in cron scheduling loop");
|
||||||
|
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Public method to trigger matching manually for all playlists (called from controller).
|
/// Matches tracks for a single playlist (called by cron scheduler or manual trigger).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task TriggerMatchingAsync()
|
private async Task MatchSinglePlaylistAsync(string playlistName, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Manual track matching triggered for all playlists");
|
|
||||||
await MatchAllPlaylistsAsync(CancellationToken.None);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Public method to trigger matching for a specific playlist (called from controller).
|
|
||||||
/// </summary>
|
|
||||||
public async Task TriggerMatchingForPlaylistAsync(string playlistName)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Manual track matching triggered for playlist: {Playlist}", playlistName);
|
|
||||||
|
|
||||||
var playlist = _spotifySettings.Playlists
|
var playlist = _spotifySettings.Playlists
|
||||||
.FirstOrDefault(p => p.Name.Equals(playlistName, StringComparison.OrdinalIgnoreCase));
|
.FirstOrDefault(p => p.Name.Equals(playlistName, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
@@ -142,63 +214,6 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
playlistFetcher = scope.ServiceProvider.GetService<SpotifyPlaylistFetcher>();
|
playlistFetcher = scope.ServiceProvider.GetService<SpotifyPlaylistFetcher>();
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (playlistFetcher != null)
|
|
||||||
{
|
|
||||||
// Use new direct API mode with ISRC support
|
|
||||||
await MatchPlaylistTracksWithIsrcAsync(
|
|
||||||
playlist.Name, playlistFetcher, metadataService, CancellationToken.None);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Fall back to legacy mode
|
|
||||||
await MatchPlaylistTracksLegacyAsync(
|
|
||||||
playlist.Name, metadataService, CancellationToken.None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError(ex, "Error matching tracks for playlist {Playlist}", playlist.Name);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task MatchAllPlaylistsAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
// Check if we've run too recently (cooldown period)
|
|
||||||
var timeSinceLastRun = DateTime.UtcNow - _lastMatchingRun;
|
|
||||||
if (timeSinceLastRun < _minimumMatchingInterval)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Skipping track matching - last run was {Seconds}s ago (minimum interval: {MinSeconds}s)",
|
|
||||||
(int)timeSinceLastRun.TotalSeconds, (int)_minimumMatchingInterval.TotalSeconds);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogInformation("=== STARTING TRACK MATCHING ===");
|
|
||||||
_lastMatchingRun = DateTime.UtcNow;
|
|
||||||
|
|
||||||
var playlists = _spotifySettings.Playlists;
|
|
||||||
if (playlists.Count == 0)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("No playlists configured for matching");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
using var scope = _serviceProvider.CreateScope();
|
|
||||||
var metadataService = scope.ServiceProvider.GetRequiredService<IMusicMetadataService>();
|
|
||||||
|
|
||||||
// Check if we should use the new SpotifyPlaylistFetcher
|
|
||||||
SpotifyPlaylistFetcher? playlistFetcher = null;
|
|
||||||
if (_spotifyApiSettings.Enabled)
|
|
||||||
{
|
|
||||||
playlistFetcher = scope.ServiceProvider.GetService<SpotifyPlaylistFetcher>();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var playlist in playlists)
|
|
||||||
{
|
|
||||||
if (cancellationToken.IsCancellationRequested) break;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (playlistFetcher != null)
|
if (playlistFetcher != null)
|
||||||
@@ -217,10 +232,70 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error matching tracks for playlist {Playlist}", playlist.Name);
|
_logger.LogError(ex, "Error matching tracks for playlist {Playlist}", playlist.Name);
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation("=== FINISHED TRACK MATCHING ===");
|
/// <summary>
|
||||||
|
/// Public method to trigger matching manually for all playlists (called from controller).
|
||||||
|
/// This bypasses cron schedules and runs immediately.
|
||||||
|
/// </summary>
|
||||||
|
public async Task TriggerMatchingAsync()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Manual track matching triggered for all playlists (bypassing cron schedules)");
|
||||||
|
await MatchAllPlaylistsAsync(CancellationToken.None);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Public method to trigger matching for a specific playlist (called from controller).
|
||||||
|
/// This bypasses cron schedules and runs immediately.
|
||||||
|
/// </summary>
|
||||||
|
public async Task TriggerMatchingForPlaylistAsync(string playlistName)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Manual track matching triggered for playlist: {Playlist} (bypassing cron schedule)", playlistName);
|
||||||
|
|
||||||
|
// Check cooldown to prevent abuse
|
||||||
|
if (_lastRunTimes.TryGetValue(playlistName, out var lastRun))
|
||||||
|
{
|
||||||
|
var timeSinceLastRun = DateTime.UtcNow - lastRun;
|
||||||
|
if (timeSinceLastRun < _minimumRunInterval)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Skipping manual refresh for {Playlist} - last run was {Seconds}s ago (cooldown: {Cooldown}s)",
|
||||||
|
playlistName, (int)timeSinceLastRun.TotalSeconds, (int)_minimumRunInterval.TotalSeconds);
|
||||||
|
throw new InvalidOperationException($"Please wait {(int)(_minimumRunInterval - timeSinceLastRun).TotalSeconds} more seconds before refreshing again");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await MatchSinglePlaylistAsync(playlistName, CancellationToken.None);
|
||||||
|
_lastRunTimes[playlistName] = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task MatchAllPlaylistsAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("=== STARTING TRACK MATCHING FOR ALL PLAYLISTS ===");
|
||||||
|
|
||||||
|
var playlists = _spotifySettings.Playlists;
|
||||||
|
if (playlists.Count == 0)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("No playlists configured for matching");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var playlist in playlists)
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested) break;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await MatchSinglePlaylistAsync(playlist.Name, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error matching tracks for playlist {Playlist}", playlist.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("=== FINISHED TRACK MATCHING FOR ALL PLAYLISTS ===");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -497,8 +572,37 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
|
|
||||||
if (matchedTracks.Count > 0)
|
if (matchedTracks.Count > 0)
|
||||||
{
|
{
|
||||||
// Cache matched tracks with position data
|
// Calculate cache expiration: until next cron run (not just cache duration from settings)
|
||||||
await _cache.SetAsync(matchedTracksKey, matchedTracks, TimeSpan.FromHours(1));
|
var playlist = _spotifySettings.Playlists
|
||||||
|
.FirstOrDefault(p => p.Name.Equals(playlistName, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
var cacheExpiration = TimeSpan.FromHours(24); // Default 24 hours
|
||||||
|
|
||||||
|
if (playlist != null && !string.IsNullOrEmpty(playlist.SyncSchedule))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var cron = CronExpression.Parse(playlist.SyncSchedule);
|
||||||
|
var nextRun = cron.GetNextOccurrence(DateTime.UtcNow, TimeZoneInfo.Utc);
|
||||||
|
|
||||||
|
if (nextRun.HasValue)
|
||||||
|
{
|
||||||
|
var timeUntilNextRun = nextRun.Value - DateTime.UtcNow;
|
||||||
|
// Add 5 minutes buffer to ensure cache doesn't expire before next run
|
||||||
|
cacheExpiration = timeUntilNextRun + TimeSpan.FromMinutes(5);
|
||||||
|
|
||||||
|
_logger.LogInformation("Cache will persist until next cron run: {NextRun} UTC (in {Hours:F1} hours)",
|
||||||
|
nextRun.Value, timeUntilNextRun.TotalHours);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogWarning(ex, "Could not calculate next cron run for {Playlist}, using default cache duration", playlistName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache matched tracks with position data until next cron run
|
||||||
|
await _cache.SetAsync(matchedTracksKey, matchedTracks, cacheExpiration);
|
||||||
|
|
||||||
// Save matched tracks to file for persistence across restarts
|
// Save matched tracks to file for persistence across restarts
|
||||||
await SaveMatchedTracksToFileAsync(playlistName, matchedTracks);
|
await SaveMatchedTracksToFileAsync(playlistName, matchedTracks);
|
||||||
@@ -506,15 +610,15 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
// Also update legacy cache for backward compatibility
|
// Also update legacy cache for backward compatibility
|
||||||
var legacyKey = $"spotify:matched:{playlistName}";
|
var legacyKey = $"spotify:matched:{playlistName}";
|
||||||
var legacySongs = matchedTracks.OrderBy(t => t.Position).Select(t => t.MatchedSong).ToList();
|
var legacySongs = matchedTracks.OrderBy(t => t.Position).Select(t => t.MatchedSong).ToList();
|
||||||
await _cache.SetAsync(legacyKey, legacySongs, TimeSpan.FromHours(1));
|
await _cache.SetAsync(legacyKey, legacySongs, cacheExpiration);
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
"✓ Cached {Matched}/{Total} tracks for {Playlist} via GREEDY ASSIGNMENT (ISRC: {Isrc}, Fuzzy: {Fuzzy}, No match: {NoMatch}) - manual mappings will be applied next",
|
"✓ Cached {Matched}/{Total} tracks for {Playlist} via GREEDY ASSIGNMENT (ISRC: {Isrc}, Fuzzy: {Fuzzy}, No match: {NoMatch}) - cache expires in {Hours:F1}h",
|
||||||
matchedTracks.Count, tracksToMatch.Count, playlistName, isrcMatches, fuzzyMatches, noMatch);
|
matchedTracks.Count, tracksToMatch.Count, playlistName, isrcMatches, fuzzyMatches, noMatch, cacheExpiration.TotalHours);
|
||||||
|
|
||||||
// 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
|
// 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, cacheExpiration, cancellationToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -849,6 +953,7 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
string? jellyfinPlaylistId,
|
string? jellyfinPlaylistId,
|
||||||
List<SpotifyPlaylistTrack> spotifyTracks,
|
List<SpotifyPlaylistTrack> spotifyTracks,
|
||||||
List<MatchedTrack> matchedTracks,
|
List<MatchedTrack> matchedTracks,
|
||||||
|
TimeSpan cacheExpiration,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -1196,9 +1301,9 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
|
|
||||||
if (finalItems.Count > 0)
|
if (finalItems.Count > 0)
|
||||||
{
|
{
|
||||||
// Save to Redis cache
|
// Save to Redis cache with same expiration as matched tracks (until next cron run)
|
||||||
var cacheKey = $"spotify:playlist:items:{playlistName}";
|
var cacheKey = $"spotify:playlist:items:{playlistName}";
|
||||||
await _cache.SetAsync(cacheKey, finalItems, TimeSpan.FromHours(24));
|
await _cache.SetAsync(cacheKey, finalItems, cacheExpiration);
|
||||||
|
|
||||||
// Save to file cache for persistence
|
// Save to file cache for persistence
|
||||||
await SavePlaylistItemsToFileAsync(playlistName, finalItems);
|
await SavePlaylistItemsToFileAsync(playlistName, finalItems);
|
||||||
@@ -1210,8 +1315,8 @@ public class SpotifyTrackMatchingService : BackgroundService
|
|||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogInformation(
|
_logger.LogInformation(
|
||||||
"✅ Pre-built playlist cache for {Playlist}: {Total} tracks ({Local} LOCAL + {External} EXTERNAL){ManualInfo}",
|
"✅ Pre-built playlist cache for {Playlist}: {Total} tracks ({Local} LOCAL + {External} EXTERNAL){ManualInfo} - expires in {Hours:F1}h",
|
||||||
playlistName, finalItems.Count, localUsedCount, externalUsedCount, manualMappingInfo);
|
playlistName, finalItems.Count, localUsedCount, externalUsedCount, manualMappingInfo, cacheExpiration.TotalHours);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ 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;
|
||||||
@@ -55,6 +56,7 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
private readonly SquidWTFSettings _squidwtfSettings;
|
private readonly SquidWTFSettings _squidwtfSettings;
|
||||||
private readonly OdesliService _odesliService;
|
private readonly OdesliService _odesliService;
|
||||||
private readonly RoundRobinFallbackHelper _fallbackHelper;
|
private readonly RoundRobinFallbackHelper _fallbackHelper;
|
||||||
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
protected override string ProviderName => "squidwtf";
|
protected override string ProviderName => "squidwtf";
|
||||||
|
|
||||||
@@ -75,6 +77,10 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
_squidwtfSettings = SquidWTFSettings.Value;
|
_squidwtfSettings = SquidWTFSettings.Value;
|
||||||
_odesliService = odesliService;
|
_odesliService = odesliService;
|
||||||
_fallbackHelper = new RoundRobinFallbackHelper(apiUrls, logger, "SquidWTF");
|
_fallbackHelper = new RoundRobinFallbackHelper(apiUrls, logger, "SquidWTF");
|
||||||
|
_serviceProvider = serviceProvider;
|
||||||
|
|
||||||
|
// Increase timeout for large downloads and slow endpoints
|
||||||
|
_httpClient.Timeout = TimeSpan.FromMinutes(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -108,9 +114,6 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
Logger.LogInformation("Track download URL obtained from hifi-api: {Url}", downloadInfo.DownloadUrl);
|
Logger.LogInformation("Track download URL obtained from hifi-api: {Url}", downloadInfo.DownloadUrl);
|
||||||
Logger.LogInformation("Using format: {Format} (Quality: {Quality})", downloadInfo.MimeType, downloadInfo.AudioQuality);
|
Logger.LogInformation("Using format: {Format} (Quality: {Quality})", downloadInfo.MimeType, downloadInfo.AudioQuality);
|
||||||
|
|
||||||
// Start Spotify ID conversion in parallel with download (don't await yet)
|
|
||||||
var spotifyIdTask = _odesliService.ConvertTidalToSpotifyIdAsync(trackId, cancellationToken);
|
|
||||||
|
|
||||||
// Determine extension from MIME type
|
// Determine extension from MIME type
|
||||||
var extension = downloadInfo.MimeType?.ToLower() switch
|
var extension = downloadInfo.MimeType?.ToLower() switch
|
||||||
{
|
{
|
||||||
@@ -122,10 +125,10 @@ 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 cache/Music folder (cleaned up after 24h), Permanent mode uses downloads folder
|
// Cache mode uses downloads/cache/ folder, Permanent mode uses downloads/permanent/
|
||||||
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
|
var basePath = SubsonicSettings.StorageMode == StorageMode.Cache
|
||||||
? Path.Combine("cache", "Music")
|
? Path.Combine("downloads", "cache")
|
||||||
: "downloads";
|
: 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
|
||||||
@@ -135,10 +138,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);
|
||||||
|
|
||||||
// Race all endpoints to download from the fastest one
|
// Use round-robin with fallback for downloads to reduce CPU usage
|
||||||
Logger.LogInformation("🏁 Racing {Count} endpoints for fastest download", _fallbackHelper.EndpointCount);
|
Logger.LogDebug("Using round-robin endpoint selection for download");
|
||||||
|
|
||||||
var response = await _fallbackHelper.RaceAllEndpointsAsync(async (baseUrl, ct) =>
|
var response = 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 per hifi-api spec
|
||||||
var quality = _squidwtfSettings.Quality?.ToUpperInvariant() switch
|
var quality = _squidwtfSettings.Quality?.ToUpperInvariant() switch
|
||||||
@@ -154,10 +157,10 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
var url = $"{baseUrl}/track/?id={trackId}&quality={quality}";
|
var url = $"{baseUrl}/track/?id={trackId}&quality={quality}";
|
||||||
|
|
||||||
// Get download info from this endpoint
|
// Get download info from this endpoint
|
||||||
var infoResponse = await _httpClient.GetAsync(url, ct);
|
var infoResponse = await _httpClient.GetAsync(url, cancellationToken);
|
||||||
infoResponse.EnsureSuccessStatusCode();
|
infoResponse.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
var json = await infoResponse.Content.ReadAsStringAsync(ct);
|
var json = await infoResponse.Content.ReadAsStringAsync(cancellationToken);
|
||||||
var doc = JsonDocument.Parse(json);
|
var doc = JsonDocument.Parse(json);
|
||||||
|
|
||||||
if (!doc.RootElement.TryGetProperty("data", out var data))
|
if (!doc.RootElement.TryGetProperty("data", out var data))
|
||||||
@@ -185,8 +188,8 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
request.Headers.Add("User-Agent", "Mozilla/5.0");
|
request.Headers.Add("User-Agent", "Mozilla/5.0");
|
||||||
request.Headers.Add("Accept", "*/*");
|
request.Headers.Add("Accept", "*/*");
|
||||||
|
|
||||||
return await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct);
|
return await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
||||||
}, cancellationToken);
|
});
|
||||||
|
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
@@ -199,14 +202,26 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
// Close file before writing metadata
|
// Close file before writing metadata
|
||||||
await outputFile.DisposeAsync();
|
await outputFile.DisposeAsync();
|
||||||
|
|
||||||
// Wait for Spotify ID conversion to complete and update song metadata
|
// Start Spotify ID conversion in background (for lyrics support)
|
||||||
var spotifyId = await spotifyIdTask;
|
// 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))
|
if (!string.IsNullOrEmpty(spotifyId))
|
||||||
{
|
{
|
||||||
song.SpotifyId = 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
|
// 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;
|
||||||
@@ -228,8 +243,8 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
{
|
{
|
||||||
return await QueueRequestAsync(async () =>
|
return await QueueRequestAsync(async () =>
|
||||||
{
|
{
|
||||||
// Race all endpoints for fastest download info retrieval
|
// Use round-robin with fallback instead of racing to reduce CPU usage
|
||||||
return await _fallbackHelper.RaceAllEndpointsAsync(async (baseUrl, ct) =>
|
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 per hifi-api spec
|
||||||
var quality = _squidwtfSettings.Quality?.ToUpperInvariant() switch
|
var quality = _squidwtfSettings.Quality?.ToUpperInvariant() switch
|
||||||
@@ -246,10 +261,10 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
|
|
||||||
Logger.LogDebug("Fetching track download info from: {Url}", url);
|
Logger.LogDebug("Fetching track download info from: {Url}", url);
|
||||||
|
|
||||||
var response = await _httpClient.GetAsync(url, ct);
|
var response = await _httpClient.GetAsync(url, cancellationToken);
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
var json = await response.Content.ReadAsStringAsync(ct);
|
var json = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||||
var doc = JsonDocument.Parse(json);
|
var doc = JsonDocument.Parse(json);
|
||||||
|
|
||||||
if (!doc.RootElement.TryGetProperty("data", out var data))
|
if (!doc.RootElement.TryGetProperty("data", out var data))
|
||||||
@@ -282,8 +297,7 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
? audioQualityEl.GetString()
|
? audioQualityEl.GetString()
|
||||||
: "LOSSLESS";
|
: "LOSSLESS";
|
||||||
|
|
||||||
Logger.LogDebug("Decoded manifest - URL: {Url}, MIME: {MimeType}, Quality: {Quality}",
|
Logger.LogInformation("Track download URL obtained from hifi-api: {Url}", downloadUrl);
|
||||||
downloadUrl, mimeType, audioQuality);
|
|
||||||
|
|
||||||
return new DownloadResult
|
return new DownloadResult
|
||||||
{
|
{
|
||||||
@@ -291,7 +305,7 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
MimeType = mimeType ?? "audio/flac",
|
MimeType = mimeType ?? "audio/flac",
|
||||||
AudioQuality = audioQuality ?? "LOSSLESS"
|
AudioQuality = audioQuality ?? "LOSSLESS"
|
||||||
};
|
};
|
||||||
}, cancellationToken);
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,6 +314,53 @@ public class SquidWTFDownloadService : BaseDownloadService
|
|||||||
|
|
||||||
#region Utility Methods
|
#region Utility Methods
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var spotifyId = await _odesliService.ConvertTidalToSpotifyIdAsync(externalId, CancellationToken.None);
|
||||||
|
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
|
||||||
|
{
|
||||||
|
using var scope = _serviceProvider.CreateScope();
|
||||||
|
var spotifyLyricsService = scope.ServiceProvider.GetService<SpotifyLyricsService>();
|
||||||
|
|
||||||
|
if (spotifyLyricsService != null)
|
||||||
|
{
|
||||||
|
var lyrics = await spotifyLyricsService.GetLyricsByTrackIdAsync(spotifyId);
|
||||||
|
if (lyrics != null && lyrics.Lines.Count > 0)
|
||||||
|
{
|
||||||
|
Logger.LogDebug("Background lyrics prefetched for Spotify/{SpotifyId}: {LineCount} lines",
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,9 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -592,6 +595,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
|
|
||||||
// Get all artists - Tidal provides both "artist" (singular) and "artists" (plural array)
|
// Get all artists - Tidal provides both "artist" (singular) and "artists" (plural array)
|
||||||
var allArtists = new List<string>();
|
var allArtists = new List<string>();
|
||||||
|
var allArtistIds = new List<string>();
|
||||||
string artistName = "";
|
string artistName = "";
|
||||||
string? artistId = null;
|
string? artistId = null;
|
||||||
|
|
||||||
@@ -601,9 +605,11 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
foreach (var artistEl in artists.EnumerateArray())
|
foreach (var artistEl in artists.EnumerateArray())
|
||||||
{
|
{
|
||||||
var name = artistEl.GetProperty("name").GetString();
|
var name = artistEl.GetProperty("name").GetString();
|
||||||
|
var id = artistEl.GetProperty("id").GetInt64();
|
||||||
if (!string.IsNullOrEmpty(name))
|
if (!string.IsNullOrEmpty(name))
|
||||||
{
|
{
|
||||||
allArtists.Add(name);
|
allArtists.Add(name);
|
||||||
|
allArtistIds.Add($"ext-squidwtf-artist-{id}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -611,7 +617,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
if (allArtists.Count > 0)
|
if (allArtists.Count > 0)
|
||||||
{
|
{
|
||||||
artistName = allArtists[0];
|
artistName = allArtists[0];
|
||||||
artistId = $"ext-squidwtf-artist-{artists[0].GetProperty("id").GetInt64()}";
|
artistId = allArtistIds[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Fallback to singular "artist" field
|
// Fallback to singular "artist" field
|
||||||
@@ -620,6 +626,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
artistName = artist.GetProperty("name").GetString() ?? "";
|
artistName = artist.GetProperty("name").GetString() ?? "";
|
||||||
artistId = $"ext-squidwtf-artist-{artist.GetProperty("id").GetInt64()}";
|
artistId = $"ext-squidwtf-artist-{artist.GetProperty("id").GetInt64()}";
|
||||||
allArtists.Add(artistName);
|
allArtists.Add(artistName);
|
||||||
|
allArtistIds.Add(artistId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get album info
|
// Get album info
|
||||||
@@ -646,6 +653,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
Artist = artistName,
|
Artist = artistName,
|
||||||
ArtistId = artistId,
|
ArtistId = artistId,
|
||||||
Artists = allArtists,
|
Artists = allArtists,
|
||||||
|
ArtistIds = allArtistIds,
|
||||||
Album = albumTitle,
|
Album = albumTitle,
|
||||||
AlbumId = albumId,
|
AlbumId = albumId,
|
||||||
Duration = track.TryGetProperty("duration", out var duration)
|
Duration = track.TryGetProperty("duration", out var duration)
|
||||||
@@ -708,6 +716,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
|
|
||||||
// Get all artists - prefer "artists" array for collaborations
|
// Get all artists - prefer "artists" array for collaborations
|
||||||
var allArtists = new List<string>();
|
var allArtists = new List<string>();
|
||||||
|
var allArtistIds = new List<string>();
|
||||||
string artistName = "";
|
string artistName = "";
|
||||||
long artistIdNum = 0;
|
long artistIdNum = 0;
|
||||||
|
|
||||||
@@ -716,9 +725,11 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
foreach (var artistEl in artists.EnumerateArray())
|
foreach (var artistEl in artists.EnumerateArray())
|
||||||
{
|
{
|
||||||
var name = artistEl.GetProperty("name").GetString();
|
var name = artistEl.GetProperty("name").GetString();
|
||||||
|
var id = artistEl.GetProperty("id").GetInt64();
|
||||||
if (!string.IsNullOrEmpty(name))
|
if (!string.IsNullOrEmpty(name))
|
||||||
{
|
{
|
||||||
allArtists.Add(name);
|
allArtists.Add(name);
|
||||||
|
allArtistIds.Add($"ext-squidwtf-artist-{id}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -733,6 +744,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
artistName = artist.GetProperty("name").GetString() ?? "";
|
artistName = artist.GetProperty("name").GetString() ?? "";
|
||||||
artistIdNum = artist.GetProperty("id").GetInt64();
|
artistIdNum = artist.GetProperty("id").GetInt64();
|
||||||
allArtists.Add(artistName);
|
allArtists.Add(artistName);
|
||||||
|
allArtistIds.Add($"ext-squidwtf-artist-{artistIdNum}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Album artist - same as main artist for Tidal tracks
|
// Album artist - same as main artist for Tidal tracks
|
||||||
@@ -768,6 +780,7 @@ public class SquidWTFMetadataService : IMusicMetadataService
|
|||||||
Artist = artistName,
|
Artist = artistName,
|
||||||
ArtistId = $"ext-squidwtf-artist-{artistIdNum}",
|
ArtistId = $"ext-squidwtf-artist-{artistIdNum}",
|
||||||
Artists = allArtists,
|
Artists = allArtists,
|
||||||
|
ArtistIds = allArtistIds,
|
||||||
Album = albumTitle,
|
Album = albumTitle,
|
||||||
AlbumId = $"ext-squidwtf-album-{albumIdNum}",
|
AlbumId = $"ext-squidwtf-album-{albumIdNum}",
|
||||||
AlbumArtist = albumArtist,
|
AlbumArtist = albumArtist,
|
||||||
|
|||||||
@@ -73,7 +73,11 @@ public class SquidWTFStartupValidator : BaseStartupValidator
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response = await _httpClient.GetAsync(endpoint, ct);
|
// 5 second timeout per ping - mark slow endpoints as failed
|
||||||
|
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||||
|
timeoutCts.CancelAfter(TimeSpan.FromSeconds(5));
|
||||||
|
|
||||||
|
var response = await _httpClient.GetAsync(endpoint, timeoutCts.Token);
|
||||||
return response.IsSuccessStatusCode;
|
return response.IsSuccessStatusCode;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.2" />
|
<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.2" />
|
||||||
|
<PackageReference Include="Cronos" Version="0.11.1" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
|
||||||
<PackageReference Include="Otp.NET" Version="1.4.1" />
|
<PackageReference Include="Otp.NET" Version="1.4.1" />
|
||||||
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
|
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
|
||||||
|
|||||||
@@ -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",
|
||||||
|
|||||||
@@ -537,8 +537,9 @@
|
|||||||
<div class="tabs">
|
<div class="tabs">
|
||||||
<div class="tab active" data-tab="dashboard">Dashboard</div>
|
<div class="tab active" data-tab="dashboard">Dashboard</div>
|
||||||
<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">Injected 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 -->
|
||||||
@@ -651,7 +652,7 @@
|
|||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<h2>
|
<h2>
|
||||||
Active Spotify Playlists
|
Injected 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()" title="Match tracks for all playlists against your local library and external providers. This may take several minutes.">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()" title="Fetch the latest playlist data from Spotify without re-matching tracks.">Refresh All</button>
|
||||||
@@ -659,13 +660,14 @@
|
|||||||
</div>
|
</div>
|
||||||
</h2>
|
</h2>
|
||||||
<p style="color: var(--text-secondary); margin-bottom: 12px;">
|
<p style="color: var(--text-secondary); margin-bottom: 12px;">
|
||||||
These are the Spotify playlists currently being monitored and filled with tracks from your music service.
|
These are the Spotify playlists currently being injected into Jellyfin with tracks from your music service.
|
||||||
</p>
|
</p>
|
||||||
<table class="playlist-table">
|
<table class="playlist-table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Spotify ID</th>
|
<th>Spotify ID</th>
|
||||||
|
<th>Sync Schedule</th>
|
||||||
<th>Tracks</th>
|
<th>Tracks</th>
|
||||||
<th>Completion</th>
|
<th>Completion</th>
|
||||||
<th>Cache Age</th>
|
<th>Cache Age</th>
|
||||||
@@ -674,7 +676,7 @@
|
|||||||
</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>
|
||||||
@@ -805,8 +807,62 @@
|
|||||||
|
|
||||||
<!-- Configuration Tab -->
|
<!-- Configuration Tab -->
|
||||||
<div class="tab-content" id="tab-config">
|
<div class="tab-content" id="tab-config">
|
||||||
|
<div class="card">
|
||||||
|
<h2>Core Settings</h2>
|
||||||
|
<div class="config-section">
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Backend Type <span style="color: var(--error);">*</span></span>
|
||||||
|
<span class="value" id="config-backend-type">-</span>
|
||||||
|
<button onclick="openEditSetting('BACKEND_TYPE', 'Backend Type', 'select', 'Choose your media server backend', ['Jellyfin', 'Subsonic'])">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Music Service <span style="color: var(--error);">*</span></span>
|
||||||
|
<span class="value" id="config-music-service">-</span>
|
||||||
|
<button onclick="openEditSetting('MUSIC_SERVICE', 'Music Service', 'select', 'Choose your music download provider', ['SquidWTF', 'Deezer', 'Qobuz'])">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Storage Mode</span>
|
||||||
|
<span class="value" id="config-storage-mode">-</span>
|
||||||
|
<button onclick="openEditSetting('STORAGE_MODE', 'Storage Mode', 'select', 'Permanent keeps files forever, Cache auto-deletes after duration', ['Permanent', 'Cache'])">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item" id="cache-duration-row" style="display: none;">
|
||||||
|
<span class="label">Cache Duration (hours)</span>
|
||||||
|
<span class="value" id="config-cache-duration-hours">-</span>
|
||||||
|
<button onclick="openEditSetting('CACHE_DURATION_HOURS', 'Cache Duration (hours)', 'number', 'How long to keep cached files before deletion')">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Download Mode</span>
|
||||||
|
<span class="value" id="config-download-mode">-</span>
|
||||||
|
<button onclick="openEditSetting('DOWNLOAD_MODE', 'Download Mode', 'select', 'Download individual tracks or full albums', ['Track', 'Album'])">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Explicit Filter</span>
|
||||||
|
<span class="value" id="config-explicit-filter">-</span>
|
||||||
|
<button onclick="openEditSetting('EXPLICIT_FILTER', 'Explicit Filter', 'select', 'Filter explicit content', ['All', 'Explicit', 'Clean'])">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Enable External Playlists</span>
|
||||||
|
<span class="value" id="config-enable-external-playlists">-</span>
|
||||||
|
<button onclick="openEditSetting('ENABLE_EXTERNAL_PLAYLISTS', 'Enable External Playlists', 'toggle')">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Playlists Directory</span>
|
||||||
|
<span class="value" id="config-playlists-directory">-</span>
|
||||||
|
<button onclick="openEditSetting('PLAYLISTS_DIRECTORY', 'Playlists Directory', 'text', 'Directory path for external playlists')">Edit</button>
|
||||||
|
</div>
|
||||||
|
<div class="config-item">
|
||||||
|
<span class="label">Redis Enabled</span>
|
||||||
|
<span class="value" id="config-redis-enabled">-</span>
|
||||||
|
<button onclick="openEditSetting('REDIS_ENABLED', 'Redis Enabled', 'toggle')">Edit</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<h2>Spotify API Settings</h2>
|
<h2>Spotify API Settings</h2>
|
||||||
|
<div style="background: rgba(248, 81, 73, 0.15); border: 1px solid var(--error); border-radius: 6px; padding: 12px; margin-bottom: 16px; color: var(--text-primary);">
|
||||||
|
⚠️ For active playlists and link functionality to work, sp_dc session cookie must be set!
|
||||||
|
</div>
|
||||||
<div class="config-section">
|
<div class="config-section">
|
||||||
<div class="config-item">
|
<div class="config-item">
|
||||||
<span class="label">API Enabled</span>
|
<span class="label">API Enabled</span>
|
||||||
@@ -814,7 +870,7 @@
|
|||||||
<button onclick="openEditSetting('SPOTIFY_API_ENABLED', 'Spotify API Enabled', 'toggle')">Edit</button>
|
<button onclick="openEditSetting('SPOTIFY_API_ENABLED', 'Spotify API Enabled', 'toggle')">Edit</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="config-item">
|
<div class="config-item">
|
||||||
<span class="label">Session Cookie (sp_dc)</span>
|
<span class="label">Session Cookie (sp_dc) <span style="color: var(--error);">*</span></span>
|
||||||
<span class="value" id="config-spotify-cookie">-</span>
|
<span class="value" id="config-spotify-cookie">-</span>
|
||||||
<button onclick="openEditSetting('SPOTIFY_API_SESSION_COOKIE', 'Spotify Session Cookie', 'password', 'Get from browser dev tools while logged into Spotify. Cookie typically lasts ~1 year.')">Update</button>
|
<button onclick="openEditSetting('SPOTIFY_API_SESSION_COOKIE', 'Spotify Session Cookie', 'password', 'Get from browser dev tools while logged into Spotify. Cookie typically lasts ~1 year.')">Update</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -903,17 +959,17 @@
|
|||||||
<h2>Jellyfin Settings</h2>
|
<h2>Jellyfin Settings</h2>
|
||||||
<div class="config-section">
|
<div class="config-section">
|
||||||
<div class="config-item">
|
<div class="config-item">
|
||||||
<span class="label">URL</span>
|
<span class="label">URL <span style="color: var(--error);">*</span></span>
|
||||||
<span class="value" id="config-jellyfin-url">-</span>
|
<span class="value" id="config-jellyfin-url">-</span>
|
||||||
<button onclick="openEditSetting('JELLYFIN_URL', 'Jellyfin URL', 'text')">Edit</button>
|
<button onclick="openEditSetting('JELLYFIN_URL', 'Jellyfin URL', 'text')">Edit</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="config-item">
|
<div class="config-item">
|
||||||
<span class="label">API Key</span>
|
<span class="label">API Key <span style="color: var(--error);">*</span></span>
|
||||||
<span class="value" id="config-jellyfin-api-key">-</span>
|
<span class="value" id="config-jellyfin-api-key">-</span>
|
||||||
<button onclick="openEditSetting('JELLYFIN_API_KEY', 'Jellyfin API Key', 'password')">Update</button>
|
<button onclick="openEditSetting('JELLYFIN_API_KEY', 'Jellyfin API Key', 'password')">Update</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="config-item">
|
<div class="config-item">
|
||||||
<span class="label">User ID</span>
|
<span class="label">User ID <span style="color: var(--error);">*</span></span>
|
||||||
<span class="value" id="config-jellyfin-user-id">-</span>
|
<span class="value" id="config-jellyfin-user-id">-</span>
|
||||||
<button onclick="openEditSetting('JELLYFIN_USER_ID', 'Jellyfin User ID', 'text', 'Required for playlist operations. Get from Jellyfin user profile URL: userId=...')">Edit</button>
|
<button onclick="openEditSetting('JELLYFIN_USER_ID', 'Jellyfin User ID', 'text', 'Required for playlist operations. Get from Jellyfin user profile URL: userId=...')">Edit</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -942,17 +998,17 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<h2>Sync Schedule</h2>
|
<h2>Spotify Import Settings</h2>
|
||||||
<div class="config-section">
|
<div class="config-section">
|
||||||
<div class="config-item">
|
<div class="config-item">
|
||||||
<span class="label">Sync Start Time</span>
|
<span class="label">Spotify Import Enabled</span>
|
||||||
<span class="value" id="config-sync-time">-</span>
|
<span class="value" id="config-spotify-import-enabled">-</span>
|
||||||
<button onclick="openEditSetting('SPOTIFY_IMPORT_SYNC_START_HOUR', 'Sync Start Hour (0-23)', 'number')">Edit</button>
|
<button onclick="openEditSetting('SPOTIFY_IMPORT_ENABLED', 'Spotify Import Enabled', 'toggle')">Edit</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="config-item">
|
<div class="config-item">
|
||||||
<span class="label">Sync Window</span>
|
<span class="label">Matching Interval (hours)</span>
|
||||||
<span class="value" id="config-sync-window">-</span>
|
<span class="value" id="config-matching-interval">-</span>
|
||||||
<button onclick="openEditSetting('SPOTIFY_IMPORT_SYNC_WINDOW_HOURS', 'Sync Window (hours)', 'number')">Edit</button>
|
<button onclick="openEditSetting('SPOTIFY_IMPORT_MATCHING_INTERVAL_HOURS', 'Matching Interval (hours)', 'number', 'How often to check for playlist updates')">Edit</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -980,6 +1036,85 @@
|
|||||||
</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 -->
|
||||||
@@ -1039,7 +1174,7 @@
|
|||||||
<div class="modal-content" style="max-width: 600px;">
|
<div class="modal-content" style="max-width: 600px;">
|
||||||
<h3>Map Track to External Provider</h3>
|
<h3>Map Track to External Provider</h3>
|
||||||
<p style="color: var(--text-secondary); margin-bottom: 16px;">
|
<p style="color: var(--text-secondary); margin-bottom: 16px;">
|
||||||
Map this track to an external provider (SquidWTF, Deezer, or Qobuz). For local Jellyfin tracks, use the Spotify Import plugin instead.
|
Map this track to an external provider (SquidWTF, Deezer, or Qobuz). For local Jellyfin tracks, use the Jellyfin mapping modal instead.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<!-- Track Info -->
|
<!-- Track Info -->
|
||||||
@@ -1081,25 +1216,94 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Local Jellyfin Track Mapping Modal -->
|
||||||
|
<div class="modal" id="local-map-modal">
|
||||||
|
<div class="modal-content" style="max-width: 700px;">
|
||||||
|
<h3>Map Track to Local Jellyfin Track</h3>
|
||||||
|
<p style="color: var(--text-secondary); margin-bottom: 16px;">
|
||||||
|
Search your Jellyfin library and select a local track to map to this Spotify track.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<!-- Track Info -->
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Spotify Track (Position <span id="local-map-position"></span>)</label>
|
||||||
|
<div style="background: var(--bg-primary); padding: 12px; border-radius: 8px; margin-bottom: 16px;">
|
||||||
|
<strong id="local-map-spotify-title"></strong><br>
|
||||||
|
<span style="color: var(--text-secondary);" id="local-map-spotify-artist"></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Search Section -->
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Search Jellyfin Library</label>
|
||||||
|
<input type="text" id="local-map-search" placeholder="Search for track name or artist...">
|
||||||
|
<button onclick="searchJellyfinTracks()" style="margin-top: 8px; width: 100%;">🔍 Search</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Search Results -->
|
||||||
|
<div id="local-map-results" style="max-height: 300px; overflow-y: auto; margin-top: 16px;"></div>
|
||||||
|
|
||||||
|
<input type="hidden" id="local-map-playlist-name">
|
||||||
|
<input type="hidden" id="local-map-spotify-id">
|
||||||
|
<input type="hidden" id="local-map-jellyfin-id">
|
||||||
|
<div class="modal-actions">
|
||||||
|
<button onclick="closeModal('local-map-modal')">Cancel</button>
|
||||||
|
<button class="primary" onclick="saveLocalMapping()" id="local-map-save-btn" disabled>Save Mapping</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Link Playlist Modal -->
|
<!-- Link Playlist Modal -->
|
||||||
<div class="modal" id="link-playlist-modal">
|
<div class="modal" id="link-playlist-modal">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<h3>Link to Spotify Playlist</h3>
|
<h3>Link to Spotify Playlist</h3>
|
||||||
<p style="color: var(--text-secondary); margin-bottom: 16px;">
|
<p style="color: var(--text-secondary); margin-bottom: 16px;">
|
||||||
Enter the Spotify playlist ID or URL. Allstarr will automatically download missing tracks from your configured music service.
|
Select a playlist from your Spotify library or enter a playlist ID/URL manually. Allstarr will automatically download missing tracks from your configured music service.
|
||||||
</p>
|
</p>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>Jellyfin Playlist</label>
|
<label>Jellyfin Playlist</label>
|
||||||
<input type="text" id="link-jellyfin-name" readonly style="background: var(--bg-primary);">
|
<input type="text" id="link-jellyfin-name" readonly style="background: var(--bg-primary);">
|
||||||
<input type="hidden" id="link-jellyfin-id">
|
<input type="hidden" id="link-jellyfin-id">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
|
||||||
|
<!-- Toggle between select and manual input -->
|
||||||
|
<div style="display: flex; gap: 8px; margin-bottom: 16px;">
|
||||||
|
<button type="button" id="select-mode-btn" class="primary" onclick="switchLinkMode('select')" style="flex: 1;">Select from My Playlists</button>
|
||||||
|
<button type="button" id="manual-mode-btn" onclick="switchLinkMode('manual')" style="flex: 1;">Enter Manually</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Select from user playlists -->
|
||||||
|
<div class="form-group" id="link-select-group">
|
||||||
|
<label>Your Spotify Playlists</label>
|
||||||
|
<select id="link-spotify-select" style="width: 100%;">
|
||||||
|
<option value="">Loading playlists...</option>
|
||||||
|
</select>
|
||||||
|
<small style="color: var(--text-secondary); display: block; margin-top: 4px;">
|
||||||
|
Select a playlist from your Spotify library
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Manual input -->
|
||||||
|
<div class="form-group" id="link-manual-group" style="display: none;">
|
||||||
<label>Spotify Playlist ID or URL</label>
|
<label>Spotify Playlist ID or URL</label>
|
||||||
<input type="text" id="link-spotify-id" placeholder="37i9dQZF1DXcBWIGoYBM5M or spotify:playlist:... or full URL">
|
<input type="text" id="link-spotify-id" placeholder="37i9dQZF1DXcBWIGoYBM5M or spotify:playlist:... or full URL">
|
||||||
<small style="color: var(--text-secondary); display: block; margin-top: 4px;">
|
<small style="color: var(--text-secondary); display: block; margin-top: 4px;">
|
||||||
Accepts: <code>37i9dQZF1DXcBWIGoYBM5M</code>, <code>spotify:playlist:37i9dQZF1DXcBWIGoYBM5M</code>, or full Spotify URL
|
Accepts: <code>37i9dQZF1DXcBWIGoYBM5M</code>, <code>spotify:playlist:37i9dQZF1DXcBWIGoYBM5M</code>, or full Spotify URL
|
||||||
</small>
|
</small>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Sync Schedule -->
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Sync Schedule (Cron)</label>
|
||||||
|
<input type="text" id="link-sync-schedule" placeholder="0 8 * * 1" value="0 8 * * 1" style="font-family: monospace;">
|
||||||
|
<small style="color: var(--text-secondary); display: block; margin-top: 4px;">
|
||||||
|
Cron format: <code>minute hour day month dayofweek</code><br>
|
||||||
|
Default: <code>0 8 * * 1</code> = 8 AM every Monday<br>
|
||||||
|
Examples: <code>0 6 * * *</code> = daily at 6 AM, <code>0 20 * * 5</code> = Fridays at 8 PM<br>
|
||||||
|
<a href="https://crontab.guru/" target="_blank" style="color: var(--primary);">Use crontab.guru to build your schedule</a>
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="modal-actions">
|
<div class="modal-actions">
|
||||||
<button onclick="closeModal('link-playlist-modal')">Cancel</button>
|
<button onclick="closeModal('link-playlist-modal')">Cancel</button>
|
||||||
<button class="primary" onclick="linkPlaylist()">Link Playlist</button>
|
<button class="primary" onclick="linkPlaylist()">Link Playlist</button>
|
||||||
@@ -1380,7 +1584,7 @@
|
|||||||
|
|
||||||
if (data.playlists.length === 0) {
|
if (data.playlists.length === 0) {
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
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>';
|
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>';
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1434,10 +1638,16 @@
|
|||||||
// Debug logging
|
// Debug logging
|
||||||
console.log(`Progress bar for ${p.name}: local=${localPct}%, external=${externalPct}%, missing=${missingPct}%, total=${completionPct}%`);
|
console.log(`Progress bar for ${p.name}: local=${localPct}%, external=${externalPct}%, missing=${missingPct}%, total=${completionPct}%`);
|
||||||
|
|
||||||
|
const syncSchedule = p.syncSchedule || '0 8 * * 1';
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<tr>
|
<tr>
|
||||||
<td><strong>${escapeHtml(p.name)}</strong></td>
|
<td><strong>${escapeHtml(p.name)}</strong></td>
|
||||||
<td style="font-family:monospace;font-size:0.85rem;color:var(--text-secondary);">${p.id || '-'}</td>
|
<td style="font-family:monospace;font-size:0.85rem;color:var(--text-secondary);">${p.id || '-'}</td>
|
||||||
|
<td style="font-family:monospace;font-size:0.85rem;">
|
||||||
|
${escapeHtml(syncSchedule)}
|
||||||
|
<button onclick="editPlaylistSchedule('${escapeJs(p.name)}', '${escapeJs(syncSchedule)}')" style="margin-left:4px;font-size:0.75rem;padding:2px 6px;">Edit</button>
|
||||||
|
</td>
|
||||||
<td>${statsHtml}${breakdown}</td>
|
<td>${statsHtml}${breakdown}</td>
|
||||||
<td>
|
<td>
|
||||||
<div style="display:flex;align-items:center;gap:8px;">
|
<div style="display:flex;align-items:center;gap:8px;">
|
||||||
@@ -1696,6 +1906,23 @@
|
|||||||
const res = await fetch('/api/admin/config');
|
const res = await fetch('/api/admin/config');
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
|
// Core settings
|
||||||
|
document.getElementById('config-backend-type').textContent = data.backendType || 'Jellyfin';
|
||||||
|
document.getElementById('config-music-service').textContent = data.musicService || 'SquidWTF';
|
||||||
|
document.getElementById('config-storage-mode').textContent = data.library?.storageMode || 'Cache';
|
||||||
|
document.getElementById('config-cache-duration-hours').textContent = data.library?.cacheDurationHours || '24';
|
||||||
|
document.getElementById('config-download-mode').textContent = data.library?.downloadMode || 'Track';
|
||||||
|
document.getElementById('config-explicit-filter').textContent = data.explicitFilter || 'All';
|
||||||
|
document.getElementById('config-enable-external-playlists').textContent = data.enableExternalPlaylists ? 'Yes' : 'No';
|
||||||
|
document.getElementById('config-playlists-directory').textContent = data.playlistsDirectory || '(not set)';
|
||||||
|
document.getElementById('config-redis-enabled').textContent = data.redisEnabled ? 'Yes' : 'No';
|
||||||
|
|
||||||
|
// Show/hide cache duration based on storage mode
|
||||||
|
const cacheDurationRow = document.getElementById('cache-duration-row');
|
||||||
|
if (cacheDurationRow) {
|
||||||
|
cacheDurationRow.style.display = data.library?.storageMode === 'Cache' ? 'grid' : 'none';
|
||||||
|
}
|
||||||
|
|
||||||
// Spotify API settings
|
// Spotify API settings
|
||||||
document.getElementById('config-spotify-enabled').textContent = data.spotifyApi.enabled ? 'Yes' : 'No';
|
document.getElementById('config-spotify-enabled').textContent = data.spotifyApi.enabled ? 'Yes' : 'No';
|
||||||
document.getElementById('config-spotify-cookie').textContent = data.spotifyApi.sessionCookie;
|
document.getElementById('config-spotify-cookie').textContent = data.spotifyApi.sessionCookie;
|
||||||
@@ -1737,10 +1964,8 @@
|
|||||||
document.getElementById('config-kept-path').textContent = data.library?.keptPath || '/app/kept';
|
document.getElementById('config-kept-path').textContent = data.library?.keptPath || '/app/kept';
|
||||||
|
|
||||||
// Sync settings
|
// Sync settings
|
||||||
const syncHour = data.spotifyImport.syncStartHour;
|
document.getElementById('config-spotify-import-enabled').textContent = data.spotifyImport?.enabled ? 'Yes' : 'No';
|
||||||
const syncMin = data.spotifyImport.syncStartMinute;
|
document.getElementById('config-matching-interval').textContent = (data.spotifyImport?.matchingIntervalHours || 24) + ' hours';
|
||||||
document.getElementById('config-sync-time').textContent = `${String(syncHour).padStart(2, '0')}:${String(syncMin).padStart(2, '0')}`;
|
|
||||||
document.getElementById('config-sync-window').textContent = data.spotifyImport.syncWindowHours + ' hours';
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to fetch config:', error);
|
console.error('Failed to fetch config:', error);
|
||||||
}
|
}
|
||||||
@@ -1816,22 +2041,137 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function openLinkPlaylist(jellyfinId, name) {
|
let currentLinkMode = 'select'; // 'select' or 'manual'
|
||||||
|
let spotifyUserPlaylists = []; // Cache of user playlists
|
||||||
|
|
||||||
|
function switchLinkMode(mode) {
|
||||||
|
currentLinkMode = mode;
|
||||||
|
|
||||||
|
const selectGroup = document.getElementById('link-select-group');
|
||||||
|
const manualGroup = document.getElementById('link-manual-group');
|
||||||
|
const selectBtn = document.getElementById('select-mode-btn');
|
||||||
|
const manualBtn = document.getElementById('manual-mode-btn');
|
||||||
|
|
||||||
|
if (mode === 'select') {
|
||||||
|
selectGroup.style.display = 'block';
|
||||||
|
manualGroup.style.display = 'none';
|
||||||
|
selectBtn.classList.add('primary');
|
||||||
|
manualBtn.classList.remove('primary');
|
||||||
|
} else {
|
||||||
|
selectGroup.style.display = 'none';
|
||||||
|
manualGroup.style.display = 'block';
|
||||||
|
selectBtn.classList.remove('primary');
|
||||||
|
manualBtn.classList.add('primary');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchSpotifyUserPlaylists() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/spotify/user-playlists');
|
||||||
|
if (!res.ok) {
|
||||||
|
const error = await res.json();
|
||||||
|
console.error('Failed to fetch Spotify playlists:', res.status, error);
|
||||||
|
|
||||||
|
// Show user-friendly error message
|
||||||
|
if (res.status === 429) {
|
||||||
|
showToast('Spotify rate limit reached. Please wait a moment and try again.', 'warning', 5000);
|
||||||
|
} else if (res.status === 401) {
|
||||||
|
showToast('Spotify authentication failed. Check your sp_dc cookie.', 'error', 5000);
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
return data.playlists || [];
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch Spotify playlists:', error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openLinkPlaylist(jellyfinId, name) {
|
||||||
document.getElementById('link-jellyfin-id').value = jellyfinId;
|
document.getElementById('link-jellyfin-id').value = jellyfinId;
|
||||||
document.getElementById('link-jellyfin-name').value = name;
|
document.getElementById('link-jellyfin-name').value = name;
|
||||||
document.getElementById('link-spotify-id').value = '';
|
document.getElementById('link-spotify-id').value = '';
|
||||||
|
|
||||||
|
// Reset to select mode
|
||||||
|
switchLinkMode('select');
|
||||||
|
|
||||||
|
// Fetch user playlists if not already cached
|
||||||
|
if (spotifyUserPlaylists.length === 0) {
|
||||||
|
const select = document.getElementById('link-spotify-select');
|
||||||
|
select.innerHTML = '<option value="">Loading playlists...</option>';
|
||||||
|
|
||||||
|
spotifyUserPlaylists = await fetchSpotifyUserPlaylists();
|
||||||
|
|
||||||
|
// Filter out already-linked playlists
|
||||||
|
const availablePlaylists = spotifyUserPlaylists.filter(p => !p.isLinked);
|
||||||
|
|
||||||
|
if (availablePlaylists.length === 0) {
|
||||||
|
if (spotifyUserPlaylists.length > 0) {
|
||||||
|
select.innerHTML = '<option value="">All your playlists are already linked</option>';
|
||||||
|
} else {
|
||||||
|
select.innerHTML = '<option value="">No playlists found or Spotify not configured</option>';
|
||||||
|
}
|
||||||
|
// Switch to manual mode if no available playlists
|
||||||
|
switchLinkMode('manual');
|
||||||
|
} else {
|
||||||
|
// Populate dropdown with only unlinked playlists
|
||||||
|
select.innerHTML = '<option value="">-- Select a playlist --</option>' +
|
||||||
|
availablePlaylists.map(p =>
|
||||||
|
`<option value="${escapeHtml(p.id)}">${escapeHtml(p.name)} (${p.trackCount} tracks)</option>`
|
||||||
|
).join('');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Re-filter in case playlists were linked since last fetch
|
||||||
|
const select = document.getElementById('link-spotify-select');
|
||||||
|
const availablePlaylists = spotifyUserPlaylists.filter(p => !p.isLinked);
|
||||||
|
|
||||||
|
if (availablePlaylists.length === 0) {
|
||||||
|
select.innerHTML = '<option value="">All your playlists are already linked</option>';
|
||||||
|
switchLinkMode('manual');
|
||||||
|
} else {
|
||||||
|
select.innerHTML = '<option value="">-- Select a playlist --</option>' +
|
||||||
|
availablePlaylists.map(p =>
|
||||||
|
`<option value="${escapeHtml(p.id)}">${escapeHtml(p.name)} (${p.trackCount} tracks)</option>`
|
||||||
|
).join('');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
openModal('link-playlist-modal');
|
openModal('link-playlist-modal');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function linkPlaylist() {
|
async function linkPlaylist() {
|
||||||
const jellyfinId = document.getElementById('link-jellyfin-id').value;
|
const jellyfinId = document.getElementById('link-jellyfin-id').value;
|
||||||
const name = document.getElementById('link-jellyfin-name').value;
|
const name = document.getElementById('link-jellyfin-name').value;
|
||||||
const spotifyId = document.getElementById('link-spotify-id').value.trim();
|
const syncSchedule = document.getElementById('link-sync-schedule').value.trim();
|
||||||
|
|
||||||
|
// Validate sync schedule (basic cron format check)
|
||||||
|
if (!syncSchedule) {
|
||||||
|
showToast('Sync schedule is required', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cronParts = syncSchedule.split(/\s+/);
|
||||||
|
if (cronParts.length !== 5) {
|
||||||
|
showToast('Invalid cron format. Expected: minute hour day month dayofweek', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Spotify ID based on current mode
|
||||||
|
let spotifyId = '';
|
||||||
|
if (currentLinkMode === 'select') {
|
||||||
|
spotifyId = document.getElementById('link-spotify-select').value;
|
||||||
|
if (!spotifyId) {
|
||||||
|
showToast('Please select a Spotify playlist', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
spotifyId = document.getElementById('link-spotify-id').value.trim();
|
||||||
if (!spotifyId) {
|
if (!spotifyId) {
|
||||||
showToast('Spotify Playlist ID is required', 'error');
|
showToast('Spotify Playlist ID is required', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Extract ID from various Spotify formats:
|
// Extract ID from various Spotify formats:
|
||||||
// - spotify:playlist:37i9dQZF1DXcBWIGoYBM5M
|
// - spotify:playlist:37i9dQZF1DXcBWIGoYBM5M
|
||||||
@@ -1855,7 +2195,11 @@
|
|||||||
const res = await fetch(`/api/admin/jellyfin/playlists/${encodeURIComponent(jellyfinId)}/link`, {
|
const res = await fetch(`/api/admin/jellyfin/playlists/${encodeURIComponent(jellyfinId)}/link`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ name, spotifyPlaylistId: cleanSpotifyId })
|
body: JSON.stringify({
|
||||||
|
name,
|
||||||
|
spotifyPlaylistId: cleanSpotifyId,
|
||||||
|
syncSchedule: syncSchedule
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
@@ -1865,6 +2209,9 @@
|
|||||||
showRestartBanner();
|
showRestartBanner();
|
||||||
closeModal('link-playlist-modal');
|
closeModal('link-playlist-modal');
|
||||||
|
|
||||||
|
// Clear the Spotify playlists cache so it refreshes next time
|
||||||
|
spotifyUserPlaylists = [];
|
||||||
|
|
||||||
// Update UI state without refetching all playlists
|
// Update UI state without refetching all playlists
|
||||||
const playlistsTable = document.getElementById('jellyfinPlaylistsTable');
|
const playlistsTable = document.getElementById('jellyfinPlaylistsTable');
|
||||||
if (playlistsTable) {
|
if (playlistsTable) {
|
||||||
@@ -1902,6 +2249,9 @@
|
|||||||
showToast('Playlist unlinked.', 'success');
|
showToast('Playlist unlinked.', 'success');
|
||||||
showRestartBanner();
|
showRestartBanner();
|
||||||
|
|
||||||
|
// Clear the Spotify playlists cache so it refreshes next time
|
||||||
|
spotifyUserPlaylists = [];
|
||||||
|
|
||||||
// Update UI state without refetching all playlists
|
// Update UI state without refetching all playlists
|
||||||
const playlistsTable = document.getElementById('jellyfinPlaylistsTable');
|
const playlistsTable = document.getElementById('jellyfinPlaylistsTable');
|
||||||
if (playlistsTable) {
|
if (playlistsTable) {
|
||||||
@@ -2265,6 +2615,39 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function editPlaylistSchedule(playlistName, currentSchedule) {
|
||||||
|
const newSchedule = prompt(`Edit sync schedule for "${playlistName}"\n\nCron format: minute hour day month dayofweek\nExamples:\n• 0 8 * * 1 = Monday 8 AM\n• 0 6 * * * = Daily 6 AM\n• 0 20 * * 5 = Friday 8 PM\n\nUse https://crontab.guru/ to build your schedule`, currentSchedule);
|
||||||
|
|
||||||
|
if (!newSchedule || newSchedule === currentSchedule) return;
|
||||||
|
|
||||||
|
// Validate cron format
|
||||||
|
const cronParts = newSchedule.trim().split(/\s+/);
|
||||||
|
if (cronParts.length !== 5) {
|
||||||
|
showToast('Invalid cron format. Expected: minute hour day month dayofweek', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/admin/playlists/${encodeURIComponent(playlistName)}/schedule`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ syncSchedule: newSchedule.trim() })
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
showToast('Sync schedule updated!', 'success');
|
||||||
|
showRestartBanner();
|
||||||
|
fetchPlaylists();
|
||||||
|
} else {
|
||||||
|
const error = await res.json();
|
||||||
|
showToast(error.error || 'Failed to update schedule', 'error');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to update schedule:', error);
|
||||||
|
showToast('Failed to update schedule', 'error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function removePlaylist(name) {
|
async function removePlaylist(name) {
|
||||||
if (!confirm(`Remove playlist "${name}"?`)) return;
|
if (!confirm(`Remove playlist "${name}"?`)) return;
|
||||||
|
|
||||||
@@ -2294,8 +2677,23 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/admin/playlists/' + encodeURIComponent(name) + '/tracks');
|
const res = await fetch('/api/admin/playlists/' + encodeURIComponent(name) + '/tracks');
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
console.error('Failed to fetch tracks:', res.status, res.statusText);
|
||||||
|
document.getElementById('tracks-list').innerHTML = '<p style="text-align:center;color:var(--error);padding:40px;">Failed to load tracks: ' + res.status + ' ' + res.statusText + '</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
|
console.log('Tracks data received:', data);
|
||||||
|
|
||||||
|
if (!data || !data.tracks) {
|
||||||
|
console.error('Invalid data structure:', data);
|
||||||
|
document.getElementById('tracks-list').innerHTML = '<p style="text-align:center;color:var(--error);padding:40px;">Invalid data received from server</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (data.tracks.length === 0) {
|
if (data.tracks.length === 0) {
|
||||||
document.getElementById('tracks-list').innerHTML = '<p style="text-align:center;color:var(--text-secondary);padding:40px;">No tracks found</p>';
|
document.getElementById('tracks-list').innerHTML = '<p style="text-align:center;color:var(--text-secondary);padding:40px;">No tracks found</p>';
|
||||||
return;
|
return;
|
||||||
@@ -2410,7 +2808,8 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
document.getElementById('tracks-list').innerHTML = '<p style="text-align:center;color:var(--error);padding:40px;">Failed to load tracks</p>';
|
console.error('Error in viewTracks:', error);
|
||||||
|
document.getElementById('tracks-list').innerHTML = '<p style="text-align:center;color:var(--error);padding:40px;">Failed to load tracks: ' + error.message + '</p>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2635,8 +3034,27 @@
|
|||||||
saveBtn.disabled = !externalId;
|
saveBtn.disabled = !externalId;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open manual mapping modal (external only)
|
// Open local Jellyfin mapping modal
|
||||||
function openManualMap(playlistName, position, title, artist, spotifyId) {
|
function openManualMap(playlistName, position, title, artist, spotifyId) {
|
||||||
|
document.getElementById('local-map-playlist-name').value = playlistName;
|
||||||
|
document.getElementById('local-map-position').textContent = position + 1;
|
||||||
|
document.getElementById('local-map-spotify-title').textContent = title;
|
||||||
|
document.getElementById('local-map-spotify-artist').textContent = artist;
|
||||||
|
document.getElementById('local-map-spotify-id').value = spotifyId;
|
||||||
|
|
||||||
|
// Pre-fill search with track info
|
||||||
|
document.getElementById('local-map-search').value = `${title} ${artist}`;
|
||||||
|
|
||||||
|
// Reset fields
|
||||||
|
document.getElementById('local-map-results').innerHTML = '';
|
||||||
|
document.getElementById('local-map-jellyfin-id').value = '';
|
||||||
|
document.getElementById('local-map-save-btn').disabled = true;
|
||||||
|
|
||||||
|
openModal('local-map-modal');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open external mapping modal
|
||||||
|
function openExternalMap(playlistName, position, title, artist, spotifyId) {
|
||||||
document.getElementById('map-playlist-name').value = playlistName;
|
document.getElementById('map-playlist-name').value = playlistName;
|
||||||
document.getElementById('map-position').textContent = position + 1;
|
document.getElementById('map-position').textContent = position + 1;
|
||||||
document.getElementById('map-spotify-title').textContent = title;
|
document.getElementById('map-spotify-title').textContent = title;
|
||||||
@@ -2651,12 +3069,123 @@
|
|||||||
openModal('manual-map-modal');
|
openModal('manual-map-modal');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alias for backward compatibility
|
// Search Jellyfin tracks for local mapping
|
||||||
function openExternalMap(playlistName, position, title, artist, spotifyId) {
|
async function searchJellyfinTracks() {
|
||||||
openManualMap(playlistName, position, title, artist, spotifyId);
|
const query = document.getElementById('local-map-search').value.trim();
|
||||||
|
if (!query) {
|
||||||
|
showToast('Please enter a search query', 'error');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save manual mapping (external only)
|
const resultsDiv = document.getElementById('local-map-results');
|
||||||
|
resultsDiv.innerHTML = '<p style="text-align:center;padding:20px;">Searching...</p>';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/admin/jellyfin/search?query=' + encodeURIComponent(query));
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
resultsDiv.innerHTML = '<p style="text-align:center;color:var(--error);padding:20px;">Search failed</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data.tracks || data.tracks.length === 0) {
|
||||||
|
resultsDiv.innerHTML = '<p style="text-align:center;color:var(--text-secondary);padding:20px;">No tracks found</p>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resultsDiv.innerHTML = data.tracks.map(track => `
|
||||||
|
<div style="padding: 12px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; cursor: pointer; transition: background 0.2s;"
|
||||||
|
onclick="selectJellyfinTrack('${escapeJs(track.id)}', '${escapeJs(track.name)}', '${escapeJs(track.artist)}')"
|
||||||
|
onmouseover="this.style.background='var(--bg-primary)'"
|
||||||
|
onmouseout="this.style.background='transparent'">
|
||||||
|
<strong>${escapeHtml(track.name)}</strong><br>
|
||||||
|
<span style="color: var(--text-secondary); font-size: 0.9em;">${escapeHtml(track.artist)}</span>
|
||||||
|
${track.album ? '<br><span style="color: var(--text-secondary); font-size: 0.85em;">' + escapeHtml(track.album) + '</span>' : ''}
|
||||||
|
</div>
|
||||||
|
`).join('');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Search error:', error);
|
||||||
|
resultsDiv.innerHTML = '<p style="text-align:center;color:var(--error);padding:20px;">Search failed</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select a Jellyfin track for mapping
|
||||||
|
function selectJellyfinTrack(jellyfinId, name, artist) {
|
||||||
|
document.getElementById('local-map-jellyfin-id').value = jellyfinId;
|
||||||
|
document.getElementById('local-map-save-btn').disabled = false;
|
||||||
|
|
||||||
|
// Highlight selected track
|
||||||
|
document.querySelectorAll('#local-map-results > div').forEach(div => {
|
||||||
|
div.style.background = 'transparent';
|
||||||
|
div.style.border = '1px solid var(--border)';
|
||||||
|
});
|
||||||
|
event.target.closest('div').style.background = 'var(--primary)';
|
||||||
|
event.target.closest('div').style.border = '1px solid var(--primary)';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save local Jellyfin mapping
|
||||||
|
async function saveLocalMapping() {
|
||||||
|
const playlistName = document.getElementById('local-map-playlist-name').value;
|
||||||
|
const spotifyId = document.getElementById('local-map-spotify-id').value;
|
||||||
|
const jellyfinId = document.getElementById('local-map-jellyfin-id').value;
|
||||||
|
|
||||||
|
if (!jellyfinId) {
|
||||||
|
showToast('Please select a Jellyfin track', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestBody = {
|
||||||
|
spotifyId,
|
||||||
|
jellyfinId
|
||||||
|
};
|
||||||
|
|
||||||
|
// Show loading state
|
||||||
|
const saveBtn = document.getElementById('local-map-save-btn');
|
||||||
|
const originalText = saveBtn.textContent;
|
||||||
|
saveBtn.textContent = 'Saving...';
|
||||||
|
saveBtn.disabled = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), 30000);
|
||||||
|
|
||||||
|
const res = await fetch(`/api/admin/playlists/${encodeURIComponent(playlistName)}/map`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(requestBody),
|
||||||
|
signal: controller.signal
|
||||||
|
});
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
showToast('Track mapped successfully!', 'success');
|
||||||
|
closeModal('local-map-modal');
|
||||||
|
|
||||||
|
// Refresh the tracks view if it's open
|
||||||
|
const tracksModal = document.getElementById('tracks-modal');
|
||||||
|
if (tracksModal.style.display === 'flex') {
|
||||||
|
await viewTracks(playlistName);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const data = await res.json();
|
||||||
|
showToast(data.error || 'Failed to save mapping', 'error');
|
||||||
|
saveBtn.textContent = originalText;
|
||||||
|
saveBtn.disabled = false;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error.name === 'AbortError') {
|
||||||
|
showToast('Request timed out. The mapping may still be processing.', 'warning');
|
||||||
|
} else {
|
||||||
|
showToast('Failed to save mapping', 'error');
|
||||||
|
}
|
||||||
|
saveBtn.textContent = originalText;
|
||||||
|
saveBtn.disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save manual mapping (external only) - kept for backward compatibility
|
||||||
async function saveManualMapping() {
|
async function saveManualMapping() {
|
||||||
const playlistName = document.getElementById('map-playlist-name').value;
|
const playlistName = document.getElementById('map-playlist-name').value;
|
||||||
const spotifyId = document.getElementById('map-spotify-id').value;
|
const spotifyId = document.getElementById('map-spotify-id').value;
|
||||||
@@ -2864,6 +3393,7 @@
|
|||||||
fetchJellyfinUsers();
|
fetchJellyfinUsers();
|
||||||
fetchJellyfinPlaylists();
|
fetchJellyfinPlaylists();
|
||||||
fetchConfig();
|
fetchConfig();
|
||||||
|
fetchEndpointUsage();
|
||||||
|
|
||||||
// Auto-refresh every 30 seconds
|
// Auto-refresh every 30 seconds
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
@@ -2872,7 +3402,102 @@
|
|||||||
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>
|
||||||
|
|||||||
@@ -17,8 +17,11 @@ services:
|
|||||||
networks:
|
networks:
|
||||||
- allstarr-network
|
- allstarr-network
|
||||||
|
|
||||||
|
# Spotify Lyrics API sidecar service
|
||||||
|
# Note: This image only supports AMD64. On ARM64 systems, Docker will use emulation.
|
||||||
spotify-lyrics:
|
spotify-lyrics:
|
||||||
image: akashrchandran/spotify-lyrics-api:latest
|
image: akashrchandran/spotify-lyrics-api:latest
|
||||||
|
platform: linux/amd64
|
||||||
container_name: allstarr-spotify-lyrics
|
container_name: allstarr-spotify-lyrics
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
Reference in New Issue
Block a user