diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 66ca432..46bee43 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -81,13 +81,13 @@ jobs: - name: Prepare artifacts run: | mkdir -p dist - Compress-Archive -Path "build\bin\SpotiFLAC.exe" -DestinationPath "dist\spotiflac-windows.zip" -Force + Copy-Item -Path "build\bin\SpotiFLAC.exe" -Destination "dist\SpotiFLAC.exe" - name: Upload artifacts uses: actions/upload-artifact@v4 with: - name: windows-bundle - path: dist/spotiflac-windows.zip + name: windows-portable + path: dist/SpotiFLAC.exe retention-days: 7 build-macos: @@ -147,33 +147,56 @@ jobs: - name: Build application run: wails build -platform darwin/universal - - name: Create macOS bundle + - name: Create DMG run: | mkdir -p dist - ditto -c -k --sequesterRsrc --keepParent "build/bin/SpotiFLAC.app" "dist/spotiflac-macos-bundle.zip" + # Install create-dmg if not available + brew install create-dmg || true + + # Create DMG + create-dmg \ + --volname "SpotiFLAC" \ + --window-pos 200 120 \ + --window-size 600 400 \ + --icon-size 100 \ + --icon "SpotiFLAC.app" 175 120 \ + --hide-extension "SpotiFLAC.app" \ + --app-drop-link 425 120 \ + "dist/SpotiFLAC.dmg" \ + "build/bin/SpotiFLAC.app" || \ + # Fallback to hdiutil if create-dmg fails + hdiutil create -volname SpotiFLAC -srcfolder build/bin/SpotiFLAC.app -ov -format UDZO dist/SpotiFLAC.dmg - name: Upload artifacts uses: actions/upload-artifact@v4 with: - name: macos-bundle - path: dist/spotiflac-macos-bundle.zip + name: macos-portable + path: dist/SpotiFLAC.dmg retention-days: 7 build-linux: - name: Build Linux (${{ matrix.arch }}) + name: Build Linux (${{ matrix.display_name }}) runs-on: ${{ matrix.runner }} strategy: fail-fast: false matrix: include: - - arch: amd64 - goarch: amd64 + - display_name: amd64 runner: ubuntu-24.04 + wails_platform: linux/amd64 + artifact_name: linux-portable + output_name: SpotiFLAC.AppImage appimage_arch: x86_64 - - arch: arm64 - goarch: arm64 + appimagetool_arch: x86_64 + pkgconfig_dir: /usr/lib/x86_64-linux-gnu/pkgconfig + - display_name: arm64 runner: ubuntu-24.04-arm + wails_platform: linux/arm64 + artifact_name: linux-portable-arm + output_name: SpotiFLAC-ARM.AppImage appimage_arch: aarch64 + appimagetool_arch: aarch64 + pkgconfig_dir: /usr/lib/aarch64-linux-gnu/pkgconfig steps: - name: Checkout code uses: actions/checkout@v4 @@ -219,15 +242,10 @@ jobs: - name: Install dependencies run: | sudo apt-get update - PACKAGES="libgtk-3-dev libwebkit2gtk-4.1-dev libfuse2 imagemagick" - if [ "${{ matrix.goarch }}" = "amd64" ]; then - PACKAGES="$PACKAGES upx-ucl" - fi - sudo apt-get install -y $PACKAGES + sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev libfuse2 imagemagick upx-ucl # Create symlink for webkit2gtk-4.0 -> webkit2gtk-4.1 (Ubuntu 24.04 compatibility) - MULTIARCH="$(dpkg-architecture -qDEB_HOST_MULTIARCH)" - sudo ln -sf "/usr/lib/${MULTIARCH}/pkgconfig/webkit2gtk-4.1.pc" "/usr/lib/${MULTIARCH}/pkgconfig/webkit2gtk-4.0.pc" + sudo ln -sf "${{ matrix.pkgconfig_dir }}/webkit2gtk-4.1.pc" "${{ matrix.pkgconfig_dir }}/webkit2gtk-4.0.pc" - name: Install Wails CLI run: go install github.com/wailsapp/wails/v2/cmd/wails@latest @@ -239,10 +257,9 @@ jobs: pnpm run generate-icon - name: Build application - run: wails build -platform linux/${{ matrix.goarch }} + run: wails build -platform ${{ matrix.wails_platform }} - name: Compress with UPX - if: matrix.goarch == 'amd64' run: | upx --best --lzma build/bin/SpotiFLAC @@ -251,13 +268,13 @@ jobs: uses: actions/cache@v4 with: path: appimagetool - key: appimagetool-${{ matrix.appimage_arch }}-v1 + key: appimagetool-${{ matrix.appimagetool_arch }}-v2 - name: Download appimagetool if: steps.cache-appimagetool.outputs.cache-hit != 'true' run: | - wget --timeout=30 --tries=5 --retry-connrefused --waitretry=5 -O appimagetool https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-${{ matrix.appimage_arch }}.AppImage || \ - wget --timeout=30 --tries=5 --retry-connrefused --waitretry=5 -O appimagetool https://github.com/AppImage/appimagetool/releases/download/1.9.1/appimagetool-${{ matrix.appimage_arch }}.AppImage + wget --timeout=30 --tries=5 --retry-connrefused --waitretry=5 -O appimagetool "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-${{ matrix.appimagetool_arch }}.AppImage" || \ + wget --timeout=30 --tries=5 --retry-connrefused --waitretry=5 -O appimagetool "https://github.com/AppImage/appimagetool/releases/download/1.9.1/appimagetool-${{ matrix.appimagetool_arch }}.AppImage" - name: Make appimagetool executable run: chmod +x appimagetool @@ -312,18 +329,13 @@ jobs: # Create AppImage mkdir -p dist - if [ "${{ matrix.goarch }}" = "arm64" ]; then - RELEASE_ARCH="arm64v8" - else - RELEASE_ARCH="amd64" - fi - ARCH=${{ matrix.appimage_arch }} ./appimagetool --no-appstream AppDir "dist/SpotiFLAC-${RELEASE_ARCH}.AppImage" + ARCH=${{ matrix.appimage_arch }} ./appimagetool --no-appstream AppDir "dist/${{ matrix.output_name }}" - name: Upload artifacts uses: actions/upload-artifact@v4 with: - name: linux-appimage-${{ matrix.arch }} - path: dist/*.AppImage + name: ${{ matrix.artifact_name }} + path: dist/${{ matrix.output_name }} retention-days: 7 create-release: @@ -351,13 +363,6 @@ jobs: - name: Display structure of downloaded files run: ls -R artifacts - - name: Create Linux bundle - run: | - mkdir -p release/SpotiFLAC-linux-bundle - cp artifacts/linux-appimage-amd64/*.AppImage release/SpotiFLAC-linux-bundle/ - cp artifacts/linux-appimage-arm64/*.AppImage release/SpotiFLAC-linux-bundle/ - tar -czf release/spotiflac-linux-bundle.tar.gz -C release SpotiFLAC-linux-bundle - - name: Create Release uses: softprops/action-gh-release@v2 with: @@ -369,20 +374,16 @@ jobs: ## Downloads - - `spotiflac-windows.zip` - amd64 - - `spotiflac-macos-bundle.zip` - amd64 + arm64 - - `spotiflac-linux-bundle.tar.gz` - amd64 + arm64v8 + - `SpotiFLAC.exe` - Windows + - `SpotiFLAC.dmg` - macOS + - `SpotiFLAC.AppImage` - Linux AMD64 + - `SpotiFLAC-ARM.AppImage` - Linux ARM64
Linux Requirements The AppImage requires `webkit2gtk-4.1` to be installed on your system: - Choose the correct AppImage after extracting the bundle: - - - `SpotiFLAC-amd64.AppImage` - amd64 - - `SpotiFLAC-arm64v8.AppImage` - arm64v8 - **Ubuntu/Debian:** ```bash sudo apt install libwebkit2gtk-4.1-0 @@ -400,14 +401,21 @@ jobs: After installing the dependency, make the AppImage executable: ```bash - tar -xzf spotiflac-linux-bundle.tar.gz - chmod +x SpotiFLAC-*.AppImage + chmod +x SpotiFLAC.AppImage + ./SpotiFLAC.AppImage + ``` + + For ARM64: + ```bash + chmod +x SpotiFLAC-ARM.AppImage + ./SpotiFLAC-ARM.AppImage ```
files: | - artifacts/windows-bundle/*.zip - artifacts/macos-bundle/*.zip - release/spotiflac-linux-bundle.tar.gz + artifacts/windows-portable/SpotiFLAC.exe + artifacts/macos-portable/SpotiFLAC.dmg + artifacts/linux-portable/SpotiFLAC.AppImage + artifacts/linux-portable-arm/SpotiFLAC-ARM.AppImage env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index d3604ab..1d940fc 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,6 @@ Get Spotify tracks in true FLAC from Tidal, Qobuz & Amazon Music — no account Get Spotify tracks in true Lossless from Tidal, Qobuz, Amazon Music, Deezer & Apple Music — no account required. -### [SpotiDownloader](https://github.com/spotbye/SpotiDownloader) - -Get Spotify tracks, albums, playlists and discography in MP3 and FLAC. - ### [SpotubeDL.com](https://spotubedl.com) Download Spotify Tracks, Albums, Playlists & Discography as MP3/OGG/Opus. diff --git a/app.go b/app.go index 998b7c5..4dc9be4 100644 --- a/app.go +++ b/app.go @@ -34,14 +34,6 @@ type CurrentIPInfo struct { } const checkOperationTimeout = 10 * time.Second -const unifiedStatusAPIURL = "https://api-status.afkarxyz.qzz.io/api/status/spotiflac/" -const unifiedStatusCacheTTL = 5 * time.Second - -var ( - unifiedStatusCacheMu sync.Mutex - unifiedStatusCacheBody string - unifiedStatusCacheExpiry time.Time -) func NewApp() *App { return &App{} @@ -152,60 +144,6 @@ func previewResponseBody(body []byte, maxLen int) string { return preview } -func fetchUnifiedStatusPayload(forceRefresh bool, endpoint string) (string, error) { - unifiedStatusCacheMu.Lock() - defer unifiedStatusCacheMu.Unlock() - - if !forceRefresh && unifiedStatusCacheBody != "" && time.Now().Before(unifiedStatusCacheExpiry) { - return unifiedStatusCacheBody, nil - } - - client := &http.Client{Timeout: 10 * time.Second} - maxRetries := 3 - var lastErr error - - for i := 0; i < maxRetries; i++ { - req, err := http.NewRequest(http.MethodGet, endpoint, nil) - if err != nil { - return "", fmt.Errorf("failed to create unified status request: %w", err) - } - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36") - req.Header.Set("Accept", "application/json") - - resp, err := client.Do(req) - if err == nil { - body, readErr := io.ReadAll(resp.Body) - resp.Body.Close() - if readErr != nil { - lastErr = fmt.Errorf("attempt %d: failed reading response: %w", i+1, readErr) - } else if resp.StatusCode != http.StatusOK { - lastErr = fmt.Errorf("attempt %d: returned status %d (%s)", i+1, resp.StatusCode, previewResponseBody(body, 200)) - } else { - payload := strings.TrimSpace(string(body)) - if payload == "" { - lastErr = fmt.Errorf("attempt %d: empty response body", i+1) - } else { - unifiedStatusCacheBody = payload - unifiedStatusCacheExpiry = time.Now().Add(unifiedStatusCacheTTL) - return payload, nil - } - } - } else { - lastErr = fmt.Errorf("attempt %d: connection failed: %w", i+1, err) - } - - if i < maxRetries-1 { - time.Sleep(1 * time.Second) - } - } - - if lastErr == nil { - lastErr = fmt.Errorf("unknown error") - } - - return "", fmt.Errorf("unified status API failed after %d retries: %w", maxRetries, lastErr) -} - func fetchCurrentIPInfo() (CurrentIPInfo, error) { type ipwhoisResponse struct { Success bool `json:"success"` @@ -313,10 +251,6 @@ func (a *App) GetCurrentIPInfo() (string, error) { return string(payload), nil } -func (a *App) FetchUnifiedAPIStatus(forceRefresh bool) (string, error) { - return fetchUnifiedStatusPayload(forceRefresh, unifiedStatusAPIURL) -} - func (a *App) getFirstArtist(artistString string) string { if artistString == "" { return "" @@ -342,6 +276,11 @@ func (a *App) startup(ctx context.Context) { if err := backend.InitProviderPriorityDB(); err != nil { fmt.Printf("Failed to init provider priority DB: %v\n", err) } + go func() { + if err := backend.PrimeTidalAPIList(); err != nil { + fmt.Printf("Failed to prime Tidal API list: %v\n", err) + } + }() } func (a *App) shutdown(ctx context.Context) { @@ -368,6 +307,7 @@ type DownloadRequest struct { ReleaseDate string `json:"release_date,omitempty"` CoverURL string `json:"cover_url,omitempty"` TidalAPIURL string `json:"tidal_api_url,omitempty"` + TidalVariant string `json:"tidal_variant,omitempty"` OutputDir string `json:"output_dir,omitempty"` AudioFormat string `json:"audio_format,omitempty"` FilenameFormat string `json:"filename_format,omitempty"` @@ -722,7 +662,11 @@ func (a *App) DownloadTrack(req DownloadRequest) (DownloadResponse, error) { } case "tidal": - if req.TidalAPIURL == "" || req.TidalAPIURL == "auto" { + tidalVariant := strings.ToLower(strings.TrimSpace(req.TidalVariant)) + if tidalVariant == "alt" { + downloader := backend.NewTidalDownloader("") + filename, err = downloader.DownloadAlt(req.SpotifyID, req.OutputDir, req.FilenameFormat, req.TrackNumber, req.Position, req.TrackName, req.ArtistName, req.AlbumName, req.AlbumArtist, req.ReleaseDate, req.UseAlbumTrackNumber, req.CoverURL, req.EmbedMaxQualityCover, req.SpotifyTrackNumber, req.SpotifyDiscNumber, req.SpotifyTotalTracks, req.SpotifyTotalDiscs, req.Copyright, req.Publisher, req.Composer, metadataSeparator, req.ISRC, spotifyURL, req.UseFirstArtistOnly, req.UseSingleGenre, req.EmbedGenre) + } else if req.TidalAPIURL == "" || req.TidalAPIURL == "auto" { downloader := backend.NewTidalDownloader("") if req.ServiceURL != "" { filename, err = downloader.DownloadByURLWithFallback(req.ServiceURL, req.OutputDir, req.AudioFormat, req.FilenameFormat, req.TrackNumber, req.Position, req.TrackName, req.ArtistName, req.AlbumName, req.AlbumArtist, req.ReleaseDate, req.UseAlbumTrackNumber, req.CoverURL, req.EmbedMaxQualityCover, req.SpotifyTrackNumber, req.SpotifyDiscNumber, req.SpotifyTotalTracks, req.SpotifyTotalDiscs, req.Copyright, req.Publisher, req.Composer, metadataSeparator, req.ISRC, spotifyURL, req.AllowFallback, req.UseFirstArtistOnly, req.UseSingleGenre, req.EmbedGenre) @@ -850,6 +794,11 @@ func (a *App) DownloadTrack(req DownloadRequest) (DownloadResponse, error) { backend.CompleteDownloadItem(itemID, filename, 0) } + historySource := req.Service + if req.Service == "tidal" && strings.EqualFold(strings.TrimSpace(req.TidalVariant), "alt") { + historySource = "tidal alt" + } + go func(fPath, track, artist, album, sID, cover, format, source string) { time.Sleep(2 * time.Second) @@ -895,7 +844,7 @@ func (a *App) DownloadTrack(req DownloadRequest) (DownloadResponse, error) { } backend.AddHistoryItem(item, "SpotiFLAC") - }(filename, req.TrackName, req.ArtistName, req.AlbumName, req.SpotifyID, req.CoverURL, req.AudioFormat, req.Service) + }(filename, req.TrackName, req.ArtistName, req.AlbumName, req.SpotifyID, req.CoverURL, req.AudioFormat, historySource) } return DownloadResponse{ @@ -1042,70 +991,28 @@ func (a *App) ExportFailedDownloads() (string, error) { func (a *App) CheckAPIStatus(apiType string, apiURL string) bool { isOnline, err := runWithTimeout(checkOperationTimeout, func() (bool, error) { - var checkURL string - if apiType == "tidal" { - checkURL = fmt.Sprintf("%s/track/?id=441821360&quality=HI_RES_LOSSLESS", apiURL) - } else if apiType == "qobuz" { - checkURL = fmt.Sprintf("%s/api/stream?trackId=360735657&quality=27", apiURL) - } else if apiType == "qbz" { - checkURL = fmt.Sprintf("%s/api/track/360735657?quality=27", apiURL) - } else if apiType == "amazon" { - checkURL = fmt.Sprintf("%s/status", apiURL) - } else if apiType == "lrclib" { - checkURL = fmt.Sprintf("%s/api/search?artist_name=Adele&track_name=Hello", strings.TrimRight(apiURL, "/")) - } else if apiType == "musicbrainz" { - checkURL = fmt.Sprintf("%s/ws/2/recording?query=%s&fmt=json&limit=1", strings.TrimRight(apiURL, "/"), url.QueryEscape(`recording:"Hello" AND artist:"Adele"`)) - } else { - checkURL = apiURL - } - - client := &http.Client{Timeout: 10 * time.Second} - req, err := http.NewRequest("GET", checkURL, nil) - if err != nil { - return false, err - } - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36") - req.Header.Set("Accept", "application/json") - - maxRetries := 3 - for i := 0; i < maxRetries; i++ { - resp, err := client.Do(req) - if err == nil { - statusCode := resp.StatusCode - body, readErr := io.ReadAll(resp.Body) - resp.Body.Close() - if readErr != nil { - if i < maxRetries-1 { - time.Sleep(1 * time.Second) - } - continue - } - - if apiType == "amazon" && statusCode == 200 && strings.Contains(string(body), `"amazonMusic":"up"`) { - return true, nil - } - - if (apiType == "qobuz" || apiType == "qbz") && statusCode == 200 && containsStreamingURL(body) { - return true, nil - } - - if apiType == "lrclib" && statusCode == 200 && containsLRCLIBResults(body) { - return true, nil - } - - if apiType == "musicbrainz" && statusCode == 200 && containsMusicBrainzResults(body) { - return true, nil - } - - if apiType != "amazon" && apiType != "qobuz" && apiType != "qbz" && apiType != "lrclib" && apiType != "musicbrainz" && statusCode == 200 { + switch apiType { + case "tidal": + if checkGroupedAPIStatus("tidal", buildTidalStatusCheckURLs(apiURL)) { + return true, nil + } + if strings.TrimSpace(apiURL) == "" { + if _, refreshErr := backend.RefreshTidalAPIList(true); refreshErr == nil && checkGroupedAPIStatus("tidal", buildTidalStatusCheckURLs("")) { return true, nil } } - if i < maxRetries-1 { - time.Sleep(1 * time.Second) - } + return false, nil + case "qobuz", "qbz": + return checkGroupedAPIStatus("qobuz", buildQobuzStatusCheckURLs(apiURL)), nil + case "amazon": + return checkGroupedAPIStatus("amazon", buildAmazonStatusCheckURLs(apiURL)), nil + case "lrclib": + return checkGroupedAPIStatus("lrclib", buildLRCLIBStatusCheckURLs(apiURL)), nil + case "musicbrainz": + return checkGroupedAPIStatus("musicbrainz", buildMusicBrainzStatusCheckURLs(apiURL)), nil + default: + return checkGroupedAPIStatus(apiType, []string{strings.TrimSpace(apiURL)}), nil } - return false, nil }) if err != nil { if apiType == "musicbrainz" { @@ -1122,6 +1029,146 @@ func (a *App) CheckAPIStatus(apiType string, apiURL string) bool { return isOnline } +func buildTidalStatusCheckURLs(apiURL string) []string { + apiURL = strings.TrimRight(strings.TrimSpace(apiURL), "/") + if apiURL != "" { + return []string{fmt.Sprintf("%s/track/?id=441821360&quality=HI_RES_LOSSLESS", apiURL)} + } + + apis, err := backend.GetRotatedTidalAPIList() + if err != nil { + fmt.Printf("Warning: failed to load rotated Tidal API list for status check: %v\n", err) + } + + urls := make([]string, 0, len(apis)) + for _, baseURL := range apis { + baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/") + if baseURL == "" { + continue + } + urls = append(urls, fmt.Sprintf("%s/track/?id=441821360&quality=HI_RES_LOSSLESS", baseURL)) + } + + return urls +} + +func buildQobuzStatusCheckURLs(apiURL string) []string { + if trimmed := strings.TrimSpace(apiURL); trimmed != "" { + return []string{buildQobuzStatusCheckURL(trimmed)} + } + + bases := backend.GetQobuzStreamAPIBaseURLs() + urls := make([]string, 0, len(bases)) + for _, baseURL := range bases { + urls = append(urls, buildQobuzStatusCheckURL(baseURL)) + } + return urls +} + +func buildQobuzStatusCheckURL(apiBase string) string { + apiBase = strings.TrimSpace(apiBase) + if strings.Contains(apiBase, "qobuz.spotbye.qzz.io") { + return fmt.Sprintf("%s360735657?quality=27", apiBase) + } + return fmt.Sprintf("%s360735657&quality=27", apiBase) +} + +func buildAmazonStatusCheckURLs(apiURL string) []string { + baseURL := strings.TrimRight(strings.TrimSpace(apiURL), "/") + if baseURL == "" { + baseURL = backend.GetAmazonMusicAPIBaseURL() + } + return []string{fmt.Sprintf("%s/status", baseURL)} +} + +func buildLRCLIBStatusCheckURLs(apiURL string) []string { + baseURL := strings.TrimRight(strings.TrimSpace(apiURL), "/") + if baseURL == "" { + baseURL = "https://lrclib.net" + } + return []string{fmt.Sprintf("%s/api/search?artist_name=Adele&track_name=Hello", baseURL)} +} + +func buildMusicBrainzStatusCheckURLs(apiURL string) []string { + baseURL := strings.TrimRight(strings.TrimSpace(apiURL), "/") + if baseURL == "" { + baseURL = "https://musicbrainz.org" + } + return []string{fmt.Sprintf("%s/ws/2/recording?query=%s&fmt=json&limit=1", baseURL, url.QueryEscape(`recording:"Hello" AND artist:"Adele"`))} +} + +func checkGroupedAPIStatus(apiType string, checkURLs []string) bool { + filtered := make([]string, 0, len(checkURLs)) + for _, rawURL := range checkURLs { + url := strings.TrimSpace(rawURL) + if url == "" { + continue + } + filtered = append(filtered, url) + } + + if len(filtered) == 0 { + return false + } + + results := make(chan bool, len(filtered)) + var wg sync.WaitGroup + + for _, checkURL := range filtered { + wg.Add(1) + go func(target string) { + defer wg.Done() + results <- checkSingleAPIStatus(apiType, target) + }(checkURL) + } + + go func() { + wg.Wait() + close(results) + }() + + for online := range results { + if online { + return true + } + } + + return false +} + +func checkSingleAPIStatus(apiType string, checkURL string) bool { + client := &http.Client{Timeout: 4 * time.Second} + req, err := backend.NewRequestWithDefaultHeaders(http.MethodGet, checkURL, nil) + if err != nil { + return false + } + + resp, err := client.Do(req) + if err != nil { + return false + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return false + } + + statusCode := resp.StatusCode + switch apiType { + case "amazon": + return statusCode == http.StatusOK && strings.Contains(string(body), `"amazonMusic":"up"`) + case "qobuz", "qbz": + return statusCode == http.StatusOK && containsStreamingURL(body) + case "lrclib": + return statusCode == http.StatusOK && containsLRCLIBResults(body) + case "musicbrainz": + return statusCode == http.StatusOK && containsMusicBrainzResults(body) + default: + return statusCode == http.StatusOK + } +} + func (a *App) Quit() { panic("quit") diff --git a/backend/amazon.go b/backend/amazon.go index 679f16b..51ecd33 100644 --- a/backend/amazon.go +++ b/backend/amazon.go @@ -56,12 +56,11 @@ func (a *AmazonDownloader) DownloadFromAfkarXYZ(amazonURL, outputDir, quality st return "", fmt.Errorf("failed to extract ASIN from URL: %s", amazonURL) } - apiURL := fmt.Sprintf("https://amzn.afkarxyz.qzz.io/api/track/%s", asin) - req, err := http.NewRequest("GET", apiURL, nil) + apiURL := fmt.Sprintf("%s/api/track/%s", amazonMusicAPIBaseURL, asin) + req, err := NewRequestWithDefaultHeaders(http.MethodGet, apiURL, nil) if err != nil { return "", err } - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36") fmt.Printf("Fetching from Amazon API (ASIN: %s)...\n", asin) resp, err := a.client.Do(req) @@ -98,8 +97,10 @@ func (a *AmazonDownloader) DownloadFromAfkarXYZ(amazonURL, outputDir, quality st } defer out.Close() - dlReq, _ := http.NewRequest("GET", downloadURL, nil) - dlReq.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36") + dlReq, err := NewRequestWithDefaultHeaders(http.MethodGet, downloadURL, nil) + if err != nil { + return "", err + } dlResp, err := a.client.Do(dlReq) if err != nil { @@ -287,6 +288,16 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename mbMeta = result.Metadata } + upc := "" + if spotifyURL != "" { + if identifiers, err := GetSpotifyTrackIdentifiersDirect(spotifyURL); err == nil || identifiers.ISRC != "" || identifiers.UPC != "" { + if strings.TrimSpace(isrc) == "" && strings.TrimSpace(identifiers.ISRC) != "" { + isrc = strings.TrimSpace(identifiers.ISRC) + } + upc = strings.TrimSpace(identifiers.UPC) + } + } + originalFileDir := filepath.Dir(filePath) originalFileBase := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath)) @@ -406,6 +417,7 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename Separator: metadataSeparator, Description: "https://github.com/spotbye/SpotiFLAC", ISRC: isrc, + UPC: upc, Genre: mbMeta.Genre, } diff --git a/backend/http_headers.go b/backend/http_headers.go new file mode 100644 index 0000000..4d6a042 --- /dev/null +++ b/backend/http_headers.go @@ -0,0 +1,20 @@ +package backend + +import ( + "io" + "net/http" +) + +const DefaultDownloaderUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36" + +func NewRequestWithDefaultHeaders(method string, rawURL string, body io.Reader) (*http.Request, error) { + req, err := http.NewRequest(method, rawURL, body) + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", DefaultDownloaderUserAgent) + req.Header.Set("Accept", "application/json, text/plain, */*") + + return req, nil +} diff --git a/backend/musicbrainz.go b/backend/musicbrainz.go index 81cb2f9..f7fdf21 100644 --- a/backend/musicbrainz.go +++ b/backend/musicbrainz.go @@ -180,7 +180,7 @@ func queryMusicBrainzRecordings(client *http.Client, query string) (*MusicBrainz return nil, err } - req.Header.Set("User-Agent", fmt.Sprintf("SpotiFLAC/%s ( hi@afkarxyz.qzz.io )", AppVersion)) + req.Header.Set("User-Agent", fmt.Sprintf("SpotiFLAC/%s ( support@spotbye.qzz.io )", AppVersion)) req.Header.Set("Accept", "application/json") var lastErr error diff --git a/backend/provider_endpoints.go b/backend/provider_endpoints.go new file mode 100644 index 0000000..bc04208 --- /dev/null +++ b/backend/provider_endpoints.go @@ -0,0 +1,17 @@ +package backend + +const amazonMusicAPIBaseURL = "https://amazon.spotbye.qzz.io" + +var defaultQobuzStreamAPIBaseURLs = []string{ + "https://dab.yeet.su/api/stream?trackId=", + "https://dabmusic.xyz/api/stream?trackId=", + "https://qobuz.spotbye.qzz.io/api/track/", +} + +func GetQobuzStreamAPIBaseURLs() []string { + return append([]string(nil), defaultQobuzStreamAPIBaseURLs...) +} + +func GetAmazonMusicAPIBaseURL() string { + return amazonMusicAPIBaseURL +} diff --git a/backend/qobuz.go b/backend/qobuz.go index ba1d36b..989e6f7 100644 --- a/backend/qobuz.go +++ b/backend/qobuz.go @@ -139,7 +139,7 @@ func (q *QobuzDownloader) searchByISRC(isrc string) (*QobuzTrack, error) { } func buildQobuzAPIURL(apiBase string, trackID int64, quality string) string { - if strings.Contains(apiBase, "qbz.afkarxyz.qzz.io") { + if strings.Contains(apiBase, "qobuz.spotbye.qzz.io") { return fmt.Sprintf("%s%d?quality=%s", apiBase, trackID, quality) } return fmt.Sprintf("%s%d&quality=%s", apiBase, trackID, quality) @@ -147,7 +147,12 @@ func buildQobuzAPIURL(apiBase string, trackID int64, quality string) string { func (q *QobuzDownloader) DownloadFromStandard(apiBase string, trackID int64, quality string) (string, error) { apiURL := buildQobuzAPIURL(apiBase, trackID, quality) - resp, err := q.client.Get(apiURL) + req, err := NewRequestWithDefaultHeaders(http.MethodGet, apiURL, nil) + if err != nil { + return "", err + } + + resp, err := q.client.Do(req) if err != nil { return "", err } @@ -191,11 +196,7 @@ func (q *QobuzDownloader) GetDownloadURL(trackID int64, quality string, allowFal fmt.Printf("Getting download URL for track ID: %d with requested quality: %s\n", trackID, qualityCode) - standardAPIs := prioritizeProviders("qobuz", []string{ - "https://dab.yeet.su/api/stream?trackId=", - "https://dabmusic.xyz/api/stream?trackId=", - "https://qbz.afkarxyz.qzz.io/api/track/", - }) + standardAPIs := prioritizeProviders("qobuz", GetQobuzStreamAPIBaseURLs()) downloadFunc := func(qual string) (string, error) { type Provider struct { @@ -272,7 +273,12 @@ func (q *QobuzDownloader) DownloadFile(url, filepath string) error { Timeout: 5 * time.Minute, } - resp, err := downloadClient.Get(url) + req, err := NewRequestWithDefaultHeaders(http.MethodGet, url, nil) + if err != nil { + return fmt.Errorf("failed to create download request: %w", err) + } + + resp, err := downloadClient.Do(req) if err != nil { return fmt.Errorf("failed to download file: %w", err) } @@ -306,7 +312,12 @@ func (q *QobuzDownloader) DownloadCoverArt(coverURL, filepath string) error { return fmt.Errorf("no cover URL provided") } - resp, err := q.client.Get(coverURL) + req, err := NewRequestWithDefaultHeaders(http.MethodGet, coverURL, nil) + if err != nil { + return fmt.Errorf("failed to create cover request: %w", err) + } + + resp, err := q.client.Do(req) if err != nil { return fmt.Errorf("failed to download cover: %w", err) } diff --git a/backend/qobuz_api.go b/backend/qobuz_api.go index de5bfb9..7815b8a 100644 --- a/backend/qobuz_api.go +++ b/backend/qobuz_api.go @@ -21,7 +21,7 @@ const ( qobuzAPIBaseURL = "https://www.qobuz.com/api.json/0.2" qobuzDefaultAPIAppID = "712109809" qobuzDefaultAPIAppSecret = "589be88e4538daea11f509d29e4a23b1" - qobuzDefaultUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36" + qobuzDefaultUA = DefaultDownloaderUserAgent qobuzCredentialsCacheFile = "qobuz-api-credentials.json" qobuzCredentialsCacheTTL = 24 * time.Hour qobuzCredentialsProbeTrackISRC = "USUM71703861" diff --git a/backend/tidal.go b/backend/tidal.go index ee16973..a482058 100644 --- a/backend/tidal.go +++ b/backend/tidal.go @@ -9,7 +9,6 @@ import ( "net/http" "os" "os/exec" - "path/filepath" "regexp" "strings" "time" @@ -49,17 +48,9 @@ type TidalBTSManifest struct { } func NewTidalDownloader(apiURL string) *TidalDownloader { + apiURL = strings.TrimRight(strings.TrimSpace(apiURL), "/") if apiURL == "" { - downloader := &TidalDownloader{ - client: &http.Client{ - Timeout: 5 * time.Second, - }, - timeout: 5 * time.Second, - maxRetries: 3, - apiURL: "", - } - - apis, err := downloader.GetAvailableAPIs() + apis, err := GetRotatedTidalAPIList() if err == nil && len(apis) > 0 { apiURL = apis[0] } @@ -76,16 +67,12 @@ func NewTidalDownloader(apiURL string) *TidalDownloader { } func (t *TidalDownloader) GetAvailableAPIs() ([]string, error) { - apis := []string{ - "https://hifi-one.spotisaver.net", - "https://hifi-two.spotisaver.net", - "https://eu-central.monochrome.tf", - "https://us-west.monochrome.tf", - "https://api.monochrome.tf", - "https://monochrome-api.samidy.com", - "https://tidal.kinoplus.online", + apis, err := GetRotatedTidalAPIList() + if err == nil && len(apis) > 0 { + return apis, nil } - return prioritizeProviders("tidal", apis), nil + + return nil, err } func (t *TidalDownloader) GetTidalURLFromSpotify(spotifyTrackID string) (string, error) { @@ -129,14 +116,12 @@ func (t *TidalDownloader) GetDownloadURL(trackID int64, quality string) (string, url := fmt.Sprintf("%s/track/?id=%d&quality=%s", t.apiURL, trackID, quality) fmt.Printf("Tidal API URL: %s\n", url) - req, err := http.NewRequest("GET", url, nil) + req, err := NewRequestWithDefaultHeaders(http.MethodGet, url, nil) if err != nil { fmt.Printf("✗ failed to create request: %v\n", err) return "", fmt.Errorf("failed to create request: %w", err) } - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36") - resp, err := t.client.Do(req) if err != nil { fmt.Printf("✗ Tidal API request failed: %v\n", err) @@ -194,13 +179,11 @@ func (t *TidalDownloader) DownloadFile(url, filepath string) error { return t.DownloadFromManifest(strings.TrimPrefix(url, "MANIFEST:"), filepath) } - req, err := http.NewRequest("GET", url, nil) + req, err := NewRequestWithDefaultHeaders(http.MethodGet, url, nil) if err != nil { return fmt.Errorf("failed to create request: %w", err) } - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36") - resp, err := t.client.Do(req) if err != nil { @@ -241,11 +224,10 @@ func (t *TidalDownloader) DownloadFromManifest(manifestB64, outputPath string) e } doRequest := func(url string) (*http.Response, error) { - req, err := http.NewRequest("GET", url, nil) + req, err := NewRequestWithDefaultHeaders(http.MethodGet, url, nil) if err != nil { return nil, err } - req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36") return client.Do(req) } @@ -417,12 +399,6 @@ func (t *TidalDownloader) DownloadFromManifest(manifestB64, outputPath string) e } func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFormat string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate string, useAlbumTrackNumber bool, spotifyCoverURL string, embedMaxQualityCover bool, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks int, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) { - if outputDir != "." { - if err := os.MkdirAll(outputDir, 0755); err != nil { - return "", fmt.Errorf("directory error: %w", err) - } - } - fmt.Printf("Using Tidal URL: %s\n", tidalURL) trackID, err := t.GetTrackIDFromURL(tidalURL) @@ -434,25 +410,10 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo return "", fmt.Errorf("no track ID found") } - artistName := spotifyArtistName - trackTitle := spotifyTrackName - albumTitle := spotifyAlbumName - - artistNameForFile := sanitizeFilename(artistName) - albumArtistForFile := sanitizeFilename(spotifyAlbumArtist) - - if useFirstArtistOnly { - artistNameForFile = sanitizeFilename(GetFirstArtist(artistName)) - albumArtistForFile = sanitizeFilename(GetFirstArtist(spotifyAlbumArtist)) + outputFilename, alreadyExists, err := buildTidalOutputPath(outputDir, filenameFormat, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, useAlbumTrackNumber, spotifyTrackNumber, spotifyDiscNumber, isrcOverride, useFirstArtistOnly) + if err != nil { + return "", err } - - trackTitleForFile := sanitizeFilename(trackTitle) - albumTitleForFile := sanitizeFilename(albumTitle) - - filename := buildTidalFilename(trackTitleForFile, artistNameForFile, albumTitleForFile, albumArtistForFile, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber, isrcOverride) - outputFilename := filepath.Join(outputDir, filename) - - outputFilename, alreadyExists := ResolveOutputPathForDownload(outputFilename, GetRedownloadWithSuffixSetting()) if alreadyExists { fmt.Printf("File already exists: %s (%.2f MB)\n", outputFilename, float64(mustFileSize(outputFilename))/(1024*1024)) return "EXISTS:" + outputFilename, nil @@ -460,119 +421,29 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo downloadURL, err := t.GetDownloadURL(trackID, quality) if err != nil { - if quality == "HI_RES" && allowFallback { + if isTidalHiResQuality(quality) && allowFallback { fmt.Println("⚠ HI_RES unavailable/failed, falling back to LOSSLESS...") downloadURL, err = t.GetDownloadURL(trackID, "LOSSLESS") if err != nil { - return "", fmt.Errorf("failed to get download URL (HI_RES & LOSSLESS both failed): %w", err) + return outputFilename, fmt.Errorf("failed to get download URL (HI_RES & LOSSLESS both failed): %w", err) } } else { - return "", err + return outputFilename, err } } - type mbResult struct { - ISRC string - Metadata Metadata - } - - metaChan := make(chan mbResult, 1) - if embedGenre && spotifyURL != "" { - go func() { - res := mbResult{} - var isrc string - parts := strings.Split(spotifyURL, "/") - if len(parts) > 0 { - sID := strings.Split(parts[len(parts)-1], "?")[0] - if sID != "" { - client := NewSongLinkClient() - if val, err := client.GetISRC(sID); err == nil { - isrc = val - } - } - } - res.ISRC = isrc - if isrc != "" { - if ShouldSkipMusicBrainzMetadataFetch() { - fmt.Println("Skipping MusicBrainz metadata fetch because status check is offline.") - } else { - fmt.Println("Fetching MusicBrainz metadata...") - if fetchedMeta, err := FetchMusicBrainzMetadata(isrc, trackTitle, artistName, albumTitle, useSingleGenre, embedGenre); err == nil { - res.Metadata = fetchedMeta - fmt.Println("✓ MusicBrainz metadata fetched") - } else { - fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err) - } - } - } - metaChan <- res - }() - } else { - close(metaChan) - } - fmt.Printf("Downloading to: %s\n", outputFilename) if err := t.DownloadFile(downloadURL, outputFilename); err != nil { - return "", err + cleanupTidalDownloadArtifacts(outputFilename) + return outputFilename, err } - - isrc := strings.TrimSpace(isrcOverride) - var mbMeta Metadata - if spotifyURL != "" { - result := <-metaChan - if isrc == "" { - isrc = result.ISRC - } - mbMeta = result.Metadata - } - - fmt.Println("Adding metadata...") - - coverPath := "" - - if spotifyCoverURL != "" { - coverPath = outputFilename + ".cover.jpg" - coverClient := NewCoverClient() - if err := coverClient.DownloadCoverToPath(spotifyCoverURL, coverPath, embedMaxQualityCover); err != nil { - fmt.Printf("Warning: Failed to download Spotify cover: %v\n", err) - coverPath = "" - } else { - defer os.Remove(coverPath) - fmt.Println("Spotify cover downloaded") + if t.apiURL != "" { + if err := RememberTidalAPIUsage(t.apiURL); err != nil { + fmt.Printf("Warning: failed to persist last used Tidal API: %v\n", err) } } - trackNumberToEmbed := spotifyTrackNumber - if trackNumberToEmbed == 0 { - trackNumberToEmbed = 1 - } - - metadata := Metadata{ - Title: trackTitle, - Artist: artistName, - Album: albumTitle, - AlbumArtist: spotifyAlbumArtist, - Date: spotifyReleaseDate, - TrackNumber: trackNumberToEmbed, - TotalTracks: spotifyTotalTracks, - DiscNumber: spotifyDiscNumber, - TotalDiscs: spotifyTotalDiscs, - URL: spotifyURL, - Comment: spotifyURL, - Copyright: spotifyCopyright, - Publisher: spotifyPublisher, - Composer: spotifyComposer, - Separator: metadataSeparator, - Description: "https://github.com/spotbye/SpotiFLAC", - ISRC: isrc, - Genre: mbMeta.Genre, - } - - if err := EmbedMetadata(outputFilename, metadata, coverPath); err != nil { - fmt.Printf("Tagging failed: %v\n", err) - } else { - fmt.Println("Metadata saved") - } + finalizeTidalDownload(outputFilename, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL, embedMaxQualityCover, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL, useSingleGenre, embedGenre) fmt.Println("Done") fmt.Println("✓ Downloaded successfully from Tidal") @@ -580,17 +451,6 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo } func (t *TidalDownloader) DownloadByURLWithFallback(tidalURL, outputDir, quality, filenameFormat string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate string, useAlbumTrackNumber bool, spotifyCoverURL string, embedMaxQualityCover bool, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks int, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) { - apis, err := t.GetAvailableAPIs() - if err != nil { - return "", fmt.Errorf("no APIs available for fallback: %w", err) - } - - if outputDir != "." { - if err := os.MkdirAll(outputDir, 0755); err != nil { - return "", fmt.Errorf("directory error: %w", err) - } - } - fmt.Printf("Using Tidal URL: %s\n", tidalURL) trackID, err := t.GetTrackIDFromURL(tidalURL) @@ -602,146 +462,24 @@ func (t *TidalDownloader) DownloadByURLWithFallback(tidalURL, outputDir, quality return "", fmt.Errorf("no track ID found") } - artistName := spotifyArtistName - trackTitle := spotifyTrackName - albumTitle := spotifyAlbumName - - artistNameForFile := sanitizeFilename(artistName) - albumArtistForFile := sanitizeFilename(spotifyAlbumArtist) - - if useFirstArtistOnly { - artistNameForFile = sanitizeFilename(GetFirstArtist(artistName)) - albumArtistForFile = sanitizeFilename(GetFirstArtist(spotifyAlbumArtist)) + outputFilename, alreadyExists, err := buildTidalOutputPath(outputDir, filenameFormat, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, useAlbumTrackNumber, spotifyTrackNumber, spotifyDiscNumber, isrcOverride, useFirstArtistOnly) + if err != nil { + return "", err } - - trackTitleForFile := sanitizeFilename(trackTitle) - albumTitleForFile := sanitizeFilename(albumTitle) - - filename := buildTidalFilename(trackTitleForFile, artistNameForFile, albumTitleForFile, albumArtistForFile, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber, isrcOverride) - outputFilename := filepath.Join(outputDir, filename) - - outputFilename, alreadyExists := ResolveOutputPathForDownload(outputFilename, GetRedownloadWithSuffixSetting()) if alreadyExists { fmt.Printf("File already exists: %s (%.2f MB)\n", outputFilename, float64(mustFileSize(outputFilename))/(1024*1024)) return "EXISTS:" + outputFilename, nil } - successAPI, downloadURL, err := getDownloadURLRotated(apis, trackID, quality) - if err != nil { - if quality == "HI_RES" && allowFallback { - fmt.Println("⚠ HI_RES unavailable/failed on all APIs, falling back to LOSSLESS...") - successAPI, downloadURL, err = getDownloadURLRotated(apis, trackID, "LOSSLESS") - if err != nil { - return "", fmt.Errorf("failed to get download URL (HI_RES & LOSSLESS both failed): %w", err) - } - } else { - return "", err - } - } - - type mbResultFallback struct { - ISRC string - Metadata Metadata - } - - metaChan := make(chan mbResultFallback, 1) - if embedGenre && spotifyURL != "" { - go func() { - res := mbResultFallback{} - var isrc string - parts := strings.Split(spotifyURL, "/") - if len(parts) > 0 { - sID := strings.Split(parts[len(parts)-1], "?")[0] - if sID != "" { - client := NewSongLinkClient() - if val, err := client.GetISRC(sID); err == nil { - isrc = val - } - } - } - res.ISRC = isrc - if isrc != "" { - if ShouldSkipMusicBrainzMetadataFetch() { - fmt.Println("Skipping MusicBrainz metadata fetch because status check is offline.") - } else { - fmt.Println("Fetching MusicBrainz metadata...") - if fetchedMeta, err := FetchMusicBrainzMetadata(isrc, trackTitle, artistName, albumTitle, useSingleGenre, embedGenre); err == nil { - res.Metadata = fetchedMeta - fmt.Println("✓ MusicBrainz metadata fetched") - } else { - fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err) - } - } - } - metaChan <- res - }() - } else { - close(metaChan) - } - fmt.Printf("Downloading to: %s\n", outputFilename) - downloader := NewTidalDownloader(successAPI) - if err := downloader.DownloadFile(downloadURL, outputFilename); err != nil { - return "", err + successAPI, err := t.downloadWithRotatingAPIs(trackID, outputFilename, quality, allowFallback) + if err != nil { + cleanupTidalDownloadArtifacts(outputFilename) + return outputFilename, err } + fmt.Printf("✓ Downloaded using API: %s\n", successAPI) - isrc := strings.TrimSpace(isrcOverride) - var mbMeta Metadata - if spotifyURL != "" { - result := <-metaChan - if isrc == "" { - isrc = result.ISRC - } - mbMeta = result.Metadata - } - - fmt.Println("Adding metadata...") - - coverPath := "" - - if spotifyCoverURL != "" { - coverPath = outputFilename + ".cover.jpg" - coverClient := NewCoverClient() - if err := coverClient.DownloadCoverToPath(spotifyCoverURL, coverPath, embedMaxQualityCover); err != nil { - fmt.Printf("Warning: Failed to download Spotify cover: %v\n", err) - coverPath = "" - } else { - defer os.Remove(coverPath) - fmt.Println("Spotify cover downloaded") - } - } - - trackNumberToEmbed := spotifyTrackNumber - if trackNumberToEmbed == 0 { - trackNumberToEmbed = 1 - } - - metadata := Metadata{ - Title: trackTitle, - Artist: artistName, - Album: albumTitle, - AlbumArtist: spotifyAlbumArtist, - Date: spotifyReleaseDate, - TrackNumber: trackNumberToEmbed, - TotalTracks: spotifyTotalTracks, - DiscNumber: spotifyDiscNumber, - TotalDiscs: spotifyTotalDiscs, - URL: spotifyURL, - Comment: spotifyURL, - Copyright: spotifyCopyright, - Publisher: spotifyPublisher, - Composer: spotifyComposer, - Separator: metadataSeparator, - Description: "https://github.com/spotbye/SpotiFLAC", - ISRC: isrc, - Genre: mbMeta.Genre, - } - - if err := EmbedMetadata(outputFilename, metadata, coverPath); err != nil { - fmt.Printf("Tagging failed: %v\n", err) - } else { - fmt.Println("Metadata saved") - } + finalizeTidalDownload(outputFilename, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL, embedMaxQualityCover, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL, useSingleGenre, embedGenre) fmt.Println("Done") fmt.Println("✓ Downloaded successfully from Tidal") @@ -752,7 +490,7 @@ func (t *TidalDownloader) Download(spotifyTrackID, outputDir, quality, filenameF tidalURL, err := t.GetTidalURLFromSpotify(spotifyTrackID) if err != nil { - return "", fmt.Errorf("songlink couldn't find Tidal URL: %w", err) + return "", fmt.Errorf("songlink/songstats couldn't find Tidal URL: %w", err) } return t.DownloadByURLWithFallback(tidalURL, outputDir, quality, filenameFormat, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, useAlbumTrackNumber, spotifyCoverURL, embedMaxQualityCover, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL, allowFallback, useFirstArtistOnly, useSingleGenre, embedGenre) @@ -920,79 +658,101 @@ func parseManifest(manifestB64 string) (directURL string, initURL string, mediaU return "", initURL, mediaURLs, "", nil } -func getDownloadURLRotated(apis []string, trackID int64, quality string) (string, string, error) { - if len(apis) == 0 { - return "", "", fmt.Errorf("no APIs available") +func (t *TidalDownloader) downloadWithRotatingAPIs(trackID int64, outputFilename string, quality string, allowFallback bool) (string, error) { + qualities := []string{quality} + if isTidalHiResQuality(quality) && allowFallback { + qualities = append(qualities, "LOSSLESS") } - orderedAPIs := prioritizeProviders("tidal", apis) - fmt.Printf("Trying %d prioritized APIs...\n", len(orderedAPIs)) - - var lastError error - var errors []string - - for _, apiURL := range orderedAPIs { - fmt.Printf("Trying API: %s\n", apiURL) - - client := &http.Client{ - Timeout: 15 * time.Second, + var lastErr error + for idx, candidateQuality := range qualities { + if idx > 0 { + fmt.Printf("⚠ %s unavailable/failed on all APIs, falling back to %s...\n", quality, candidateQuality) } - url := fmt.Sprintf("%s/track/?id=%d&quality=%s", apiURL, trackID, quality) - resp, err := client.Get(url) + apiURL, err := t.tryDownloadAcrossTidalAPIs(trackID, outputFilename, candidateQuality, false) + if err == nil { + return apiURL, nil + } + lastErr = err + } + + if lastErr == nil { + lastErr = fmt.Errorf("no tidal api succeeded") + } + return "", lastErr +} + +func (t *TidalDownloader) tryDownloadAcrossTidalAPIs(trackID int64, outputFilename string, quality string, refreshed bool) (string, error) { + apis, err := GetRotatedTidalAPIList() + if err != nil && len(apis) == 0 { + return "", fmt.Errorf("failed to load tidal api list: %w", err) + } + if len(apis) == 0 { + return "", fmt.Errorf("no tidal apis available") + } + + var lastErr error + errors := make([]string, 0, len(apis)) + + for _, apiURL := range apis { + fmt.Printf("Trying Tidal API: %s\n", apiURL) + + downloader := NewTidalDownloader(apiURL) + downloadURL, err := downloader.GetDownloadURL(trackID, quality) if err != nil { - lastError = err - recordProviderFailure("tidal", apiURL) + lastErr = err errors = append(errors, fmt.Sprintf("%s: %v", apiURL, err)) continue } - if resp.StatusCode != 200 { - resp.Body.Close() - lastError = fmt.Errorf("HTTP %d", resp.StatusCode) - recordProviderFailure("tidal", apiURL) - errors = append(errors, fmt.Sprintf("%s: %v", apiURL, lastError)) + if err := downloader.DownloadFile(downloadURL, outputFilename); err != nil { + lastErr = err + cleanupTidalDownloadArtifacts(outputFilename) + errors = append(errors, fmt.Sprintf("%s: %v", apiURL, err)) continue } - body, err := io.ReadAll(resp.Body) - resp.Body.Close() - if err != nil { - lastError = err - recordProviderFailure("tidal", apiURL) - errors = append(errors, fmt.Sprintf("%s: read body failed", apiURL)) - continue + if err := RememberTidalAPIUsage(apiURL); err != nil { + fmt.Printf("Warning: failed to persist last used Tidal API: %v\n", err) } - var v2Response TidalAPIResponseV2 - if err := json.Unmarshal(body, &v2Response); err == nil && v2Response.Data.Manifest != "" { - fmt.Printf("✓ Success with: %s\n", apiURL) - recordProviderSuccess("tidal", apiURL) - return apiURL, "MANIFEST:" + v2Response.Data.Manifest, nil - } - - var v1Responses []TidalAPIResponse - if err := json.Unmarshal(body, &v1Responses); err == nil { - for _, item := range v1Responses { - if item.OriginalTrackURL != "" { - fmt.Printf("✓ Success with: %s\n", apiURL) - recordProviderSuccess("tidal", apiURL) - return apiURL, item.OriginalTrackURL, nil - } - } - } - - lastError = fmt.Errorf("no download URL or manifest in response") - recordProviderFailure("tidal", apiURL) - errors = append(errors, fmt.Sprintf("%s: %v", apiURL, lastError)) + return apiURL, nil } - fmt.Println("All APIs failed:") - for _, e := range errors { - fmt.Printf(" ✗ %s\n", e) + if !refreshed { + if _, refreshErr := RefreshTidalAPIList(true); refreshErr != nil { + errors = append(errors, fmt.Sprintf("gist refresh failed: %v", refreshErr)) + } else { + fmt.Println("All cached Tidal APIs failed, refreshed gist list and retrying...") + return t.tryDownloadAcrossTidalAPIs(trackID, outputFilename, quality, true) + } } - return "", "", fmt.Errorf("all %d APIs failed. Last error: %v", len(apis), lastError) + if lastErr == nil { + lastErr = fmt.Errorf("all tidal apis failed") + } + + fmt.Println("All Tidal APIs failed:") + for _, item := range errors { + fmt.Printf(" ✗ %s\n", item) + } + + return "", fmt.Errorf("all tidal apis failed for quality %s: %w", quality, lastErr) +} + +func cleanupTidalDownloadArtifacts(outputPath string) { + if outputPath == "" { + return + } + + _ = os.Remove(outputPath) + _ = os.Remove(outputPath + ".m4a.tmp") +} + +func isTidalHiResQuality(quality string) bool { + normalized := strings.TrimSpace(strings.ToUpper(quality)) + return normalized == "HI_RES" || normalized == "HI_RES_LOSSLESS" } func buildTidalFilename(title, artist, album, albumArtist, releaseDate string, trackNumber, discNumber int, format string, includeTrackNumber bool, position int, useAlbumTrackNumber bool, extra ...string) string { diff --git a/backend/tidal_alt.go b/backend/tidal_alt.go new file mode 100644 index 0000000..6ad4bb9 --- /dev/null +++ b/backend/tidal_alt.go @@ -0,0 +1,238 @@ +package backend + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "strings" +) + +const tidalAltDownloadAPIBaseURL = "https://tidal.spotbye.qzz.io/get" + +type TidalAltAPIResponse struct { + Title string `json:"title"` + Link string `json:"link"` +} + +func buildTidalOutputPath(outputDir, filenameFormat string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate string, useAlbumTrackNumber bool, spotifyTrackNumber, spotifyDiscNumber int, isrcOverride string, useFirstArtistOnly bool) (string, bool, error) { + if outputDir != "." { + if err := os.MkdirAll(outputDir, 0755); err != nil { + return "", false, fmt.Errorf("directory error: %w", err) + } + } + + artistNameForFile := sanitizeFilename(spotifyArtistName) + albumArtistForFile := sanitizeFilename(spotifyAlbumArtist) + if useFirstArtistOnly { + artistNameForFile = sanitizeFilename(GetFirstArtist(spotifyArtistName)) + albumArtistForFile = sanitizeFilename(GetFirstArtist(spotifyAlbumArtist)) + } + + trackTitleForFile := sanitizeFilename(spotifyTrackName) + albumTitleForFile := sanitizeFilename(spotifyAlbumName) + + filename := buildTidalFilename(trackTitleForFile, artistNameForFile, albumTitleForFile, albumArtistForFile, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber, isrcOverride) + outputFilename := filepath.Join(outputDir, filename) + + outputFilename, alreadyExists := ResolveOutputPathForDownload(outputFilename, GetRedownloadWithSuffixSetting()) + return outputFilename, alreadyExists, nil +} + +func finalizeTidalDownload(outputFilename, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate string, spotifyCoverURL string, embedMaxQualityCover bool, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL string, useSingleGenre bool, embedGenre bool) { + trackTitle := spotifyTrackName + artistName := spotifyArtistName + albumTitle := spotifyAlbumName + + type mbResult struct { + ISRC string + Metadata Metadata + } + + metaChan := make(chan mbResult, 1) + if embedGenre && spotifyURL != "" { + go func() { + res := mbResult{} + var isrc string + parts := strings.Split(spotifyURL, "/") + if len(parts) > 0 { + sID := strings.Split(parts[len(parts)-1], "?")[0] + if sID != "" { + client := NewSongLinkClient() + if val, err := client.GetISRC(sID); err == nil { + isrc = val + } + } + } + res.ISRC = isrc + if isrc != "" { + if ShouldSkipMusicBrainzMetadataFetch() { + fmt.Println("Skipping MusicBrainz metadata fetch because status check is offline.") + } else { + fmt.Println("Fetching MusicBrainz metadata...") + if fetchedMeta, err := FetchMusicBrainzMetadata(isrc, trackTitle, artistName, albumTitle, useSingleGenre, embedGenre); err == nil { + res.Metadata = fetchedMeta + fmt.Println("✓ MusicBrainz metadata fetched") + } else { + fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err) + } + } + } + metaChan <- res + }() + } else { + close(metaChan) + } + + isrc := strings.TrimSpace(isrcOverride) + var mbMeta Metadata + if spotifyURL != "" { + result := <-metaChan + if isrc == "" { + isrc = result.ISRC + } + mbMeta = result.Metadata + } + + upc := "" + if spotifyURL != "" { + if identifiers, err := GetSpotifyTrackIdentifiersDirect(spotifyURL); err == nil || identifiers.ISRC != "" || identifiers.UPC != "" { + if strings.TrimSpace(isrc) == "" && strings.TrimSpace(identifiers.ISRC) != "" { + isrc = strings.TrimSpace(identifiers.ISRC) + } + upc = strings.TrimSpace(identifiers.UPC) + } + } + + fmt.Println("Adding metadata...") + + coverPath := "" + if spotifyCoverURL != "" { + coverPath = outputFilename + ".cover.jpg" + coverClient := NewCoverClient() + if err := coverClient.DownloadCoverToPath(spotifyCoverURL, coverPath, embedMaxQualityCover); err != nil { + fmt.Printf("Warning: Failed to download Spotify cover: %v\n", err) + coverPath = "" + } else { + defer os.Remove(coverPath) + fmt.Println("Spotify cover downloaded") + } + } + + trackNumberToEmbed := spotifyTrackNumber + if trackNumberToEmbed == 0 { + trackNumberToEmbed = 1 + } + + metadata := Metadata{ + Title: trackTitle, + Artist: artistName, + Album: albumTitle, + AlbumArtist: spotifyAlbumArtist, + Date: spotifyReleaseDate, + TrackNumber: trackNumberToEmbed, + TotalTracks: spotifyTotalTracks, + DiscNumber: spotifyDiscNumber, + TotalDiscs: spotifyTotalDiscs, + URL: spotifyURL, + Comment: spotifyURL, + Copyright: spotifyCopyright, + Publisher: spotifyPublisher, + Composer: spotifyComposer, + Separator: metadataSeparator, + Description: "https://github.com/spotbye/SpotiFLAC", + ISRC: isrc, + UPC: upc, + Genre: mbMeta.Genre, + } + + if err := EmbedMetadata(outputFilename, metadata, coverPath); err != nil { + fmt.Printf("Tagging failed: %v\n", err) + } else { + fmt.Println("Metadata saved") + } +} + +func (t *TidalDownloader) GetAltDownloadURLFromSpotify(spotifyTrackID string) (string, error) { + spotifyTrackID = strings.TrimSpace(spotifyTrackID) + if spotifyTrackID == "" { + return "", fmt.Errorf("spotify track ID is required") + } + + apiURL := fmt.Sprintf("%s/%s", tidalAltDownloadAPIBaseURL, spotifyTrackID) + fmt.Printf("Tidal Alt. API URL: %s\n", apiURL) + + req, err := NewRequestWithDefaultHeaders(http.MethodGet, apiURL, nil) + if err != nil { + return "", fmt.Errorf("failed to create Tidal Alt. request: %w", err) + } + + resp, err := t.client.Do(req) + if err != nil { + return "", fmt.Errorf("failed to get Tidal Alt. download URL: %w", err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", fmt.Errorf("failed to read Tidal Alt. response: %w", err) + } + + if resp.StatusCode != http.StatusOK { + preview := strings.TrimSpace(string(body)) + if len(preview) > 200 { + preview = preview[:200] + "..." + } + return "", fmt.Errorf("Tidal Alt. returned status %d: %s", resp.StatusCode, preview) + } + + var payload TidalAltAPIResponse + if err := json.Unmarshal(body, &payload); err != nil { + return "", fmt.Errorf("failed to decode Tidal Alt. response: %w", err) + } + + downloadURL := strings.TrimSpace(payload.Link) + if downloadURL == "" { + return "", fmt.Errorf("Tidal Alt. response did not include a download link") + } + + fmt.Println("✓ Tidal Alt. download URL found") + return downloadURL, nil +} + +func (t *TidalDownloader) DownloadAlt(spotifyTrackID, outputDir, filenameFormat string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate string, useAlbumTrackNumber bool, spotifyCoverURL string, embedMaxQualityCover bool, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks int, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL string, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) { + spotifyTrackID = strings.TrimSpace(spotifyTrackID) + if spotifyTrackID == "" { + return "", fmt.Errorf("spotify track ID is required for Tidal Alt.") + } + + outputFilename, alreadyExists, err := buildTidalOutputPath(outputDir, filenameFormat, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, useAlbumTrackNumber, spotifyTrackNumber, spotifyDiscNumber, isrcOverride, useFirstArtistOnly) + if err != nil { + return "", err + } + if alreadyExists { + fmt.Printf("File already exists: %s (%.2f MB)\n", outputFilename, float64(mustFileSize(outputFilename))/(1024*1024)) + return "EXISTS:" + outputFilename, nil + } + + fmt.Printf("Using Tidal Alt. for Spotify track: %s\n", spotifyTrackID) + + downloadURL, err := t.GetAltDownloadURLFromSpotify(spotifyTrackID) + if err != nil { + return outputFilename, err + } + + fmt.Printf("Downloading to: %s\n", outputFilename) + if err := t.DownloadFile(downloadURL, outputFilename); err != nil { + cleanupTidalDownloadArtifacts(outputFilename) + return outputFilename, err + } + + finalizeTidalDownload(outputFilename, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL, embedMaxQualityCover, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL, useSingleGenre, embedGenre) + + fmt.Println("Done") + fmt.Println("✓ Downloaded successfully from Tidal Alt.") + return outputFilename, nil +} diff --git a/backend/tidal_api_list.go b/backend/tidal_api_list.go new file mode 100644 index 0000000..fd40804 --- /dev/null +++ b/backend/tidal_api_list.go @@ -0,0 +1,296 @@ +package backend + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "sync" + "time" +) + +const ( + tidalAPIListGistURL = "https://gist.githubusercontent.com/afkarxyz/2ce772b943321b9448b454f39403ce25/raw" + tidalAPIListCacheFile = "tidal-api-urls.json" +) + +type tidalAPIListCache struct { + URLs []string `json:"urls"` + LastUsedURL string `json:"last_used_url,omitempty"` + UpdatedAt int64 `json:"updated_at_unix"` + Source string `json:"source,omitempty"` +} + +var ( + tidalAPIListMu sync.Mutex + tidalAPIListState *tidalAPIListCache +) + +func loadTidalAPIListStateLocked() (*tidalAPIListCache, error) { + if tidalAPIListState != nil { + return cloneTidalAPIListState(tidalAPIListState), nil + } + + appDir, err := EnsureAppDir() + if err != nil { + return nil, err + } + + cachePath := filepath.Join(appDir, tidalAPIListCacheFile) + data, err := os.ReadFile(cachePath) + if err != nil { + if os.IsNotExist(err) { + state := &tidalAPIListCache{} + tidalAPIListState = cloneTidalAPIListState(state) + return cloneTidalAPIListState(state), nil + } + return nil, fmt.Errorf("failed to read tidal api cache: %w", err) + } + + var state tidalAPIListCache + if err := json.Unmarshal(data, &state); err != nil { + return nil, fmt.Errorf("failed to parse tidal api cache: %w", err) + } + + state.URLs = normalizeTidalAPIURLs(state.URLs) + + tidalAPIListState = cloneTidalAPIListState(&state) + return cloneTidalAPIListState(&state), nil +} + +func saveTidalAPIListStateLocked(state *tidalAPIListCache) error { + appDir, err := EnsureAppDir() + if err != nil { + return err + } + + cachePath := filepath.Join(appDir, tidalAPIListCacheFile) + payload, err := json.MarshalIndent(state, "", " ") + if err != nil { + return fmt.Errorf("failed to encode tidal api cache: %w", err) + } + + if err := os.WriteFile(cachePath, payload, 0o644); err != nil { + return fmt.Errorf("failed to write tidal api cache: %w", err) + } + + tidalAPIListState = cloneTidalAPIListState(state) + return nil +} + +func cloneTidalAPIListState(state *tidalAPIListCache) *tidalAPIListCache { + if state == nil { + return nil + } + + return &tidalAPIListCache{ + URLs: append([]string(nil), state.URLs...), + LastUsedURL: state.LastUsedURL, + UpdatedAt: state.UpdatedAt, + Source: state.Source, + } +} + +func normalizeTidalAPIURLs(urls []string) []string { + seen := make(map[string]struct{}, len(urls)) + normalized := make([]string, 0, len(urls)) + + for _, rawURL := range urls { + url := strings.TrimRight(strings.TrimSpace(rawURL), "/") + if url == "" { + continue + } + if _, exists := seen[url]; exists { + continue + } + seen[url] = struct{}{} + normalized = append(normalized, url) + } + + return normalized +} + +func fetchTidalAPIURLsFromGist() ([]string, error) { + client := &http.Client{Timeout: 12 * time.Second} + req, err := NewRequestWithDefaultHeaders(http.MethodGet, tidalAPIListGistURL, nil) + if err != nil { + return nil, fmt.Errorf("failed to create tidal api gist request: %w", err) + } + + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to fetch tidal api gist: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + preview, _ := io.ReadAll(io.LimitReader(resp.Body, 200)) + return nil, fmt.Errorf("tidal api gist returned status %d: %s", resp.StatusCode, strings.TrimSpace(string(preview))) + } + + var urls []string + if err := json.NewDecoder(resp.Body).Decode(&urls); err != nil { + return nil, fmt.Errorf("failed to decode tidal api gist: %w", err) + } + + urls = normalizeTidalAPIURLs(urls) + if len(urls) == 0 { + return nil, fmt.Errorf("tidal api gist returned no valid urls") + } + + return urls, nil +} + +func PrimeTidalAPIList() error { + _, err := RefreshTidalAPIList(true) + if err != nil { + fmt.Printf("Warning: failed to refresh Tidal API list from gist: %v\n", err) + } + + tidalAPIListMu.Lock() + defer tidalAPIListMu.Unlock() + + state, loadErr := loadTidalAPIListStateLocked() + if loadErr != nil { + return loadErr + } + + if len(state.URLs) == 0 { + return fmt.Errorf("tidal api cache is empty") + } + + if state.UpdatedAt == 0 { + state.UpdatedAt = time.Now().Unix() + return saveTidalAPIListStateLocked(state) + } + + return nil +} + +func RefreshTidalAPIList(force bool) ([]string, error) { + tidalAPIListMu.Lock() + defer tidalAPIListMu.Unlock() + + state, err := loadTidalAPIListStateLocked() + if err != nil { + state = &tidalAPIListCache{} + } + + if !force && len(state.URLs) > 0 { + return append([]string(nil), state.URLs...), nil + } + + urls, fetchErr := fetchTidalAPIURLsFromGist() + if fetchErr != nil { + if len(state.URLs) > 0 { + return append([]string(nil), state.URLs...), fetchErr + } + return nil, fetchErr + } + + state.URLs = urls + state.UpdatedAt = time.Now().Unix() + state.Source = "gist" + + if !containsString(state.URLs, state.LastUsedURL) { + state.LastUsedURL = "" + } + + if err := saveTidalAPIListStateLocked(state); err != nil { + return append([]string(nil), state.URLs...), err + } + + return append([]string(nil), state.URLs...), nil +} + +func GetTidalAPIList() ([]string, error) { + tidalAPIListMu.Lock() + defer tidalAPIListMu.Unlock() + + state, err := loadTidalAPIListStateLocked() + if err != nil { + return nil, err + } + + if len(state.URLs) == 0 { + return nil, fmt.Errorf("no cached tidal api urls") + } + + return append([]string(nil), state.URLs...), nil +} + +func GetRotatedTidalAPIList() ([]string, error) { + tidalAPIListMu.Lock() + defer tidalAPIListMu.Unlock() + + state, err := loadTidalAPIListStateLocked() + if err != nil { + return nil, err + } + + urls := state.URLs + if len(urls) == 0 { + return nil, fmt.Errorf("no cached tidal api urls") + } + + return rotateTidalAPIURLs(urls, state.LastUsedURL), nil +} + +func RememberTidalAPIUsage(apiURL string) error { + tidalAPIListMu.Lock() + defer tidalAPIListMu.Unlock() + + state, err := loadTidalAPIListStateLocked() + if err != nil { + return err + } + + state.LastUsedURL = strings.TrimRight(strings.TrimSpace(apiURL), "/") + if state.UpdatedAt == 0 { + state.UpdatedAt = time.Now().Unix() + } + + return saveTidalAPIListStateLocked(state) +} + +func rotateTidalAPIURLs(urls []string, lastUsedURL string) []string { + normalized := normalizeTidalAPIURLs(urls) + if len(normalized) < 2 { + return normalized + } + + lastUsedURL = strings.TrimRight(strings.TrimSpace(lastUsedURL), "/") + if lastUsedURL == "" { + return normalized + } + + lastIndex := -1 + for idx, candidate := range normalized { + if candidate == lastUsedURL { + lastIndex = idx + break + } + } + + if lastIndex == -1 { + return normalized + } + + rotated := make([]string, 0, len(normalized)) + rotated = append(rotated, normalized[lastIndex+1:]...) + rotated = append(rotated, normalized[:lastIndex+1]...) + return rotated +} + +func containsString(values []string, target string) bool { + target = strings.TrimRight(strings.TrimSpace(target), "/") + for _, value := range values { + if strings.TrimRight(strings.TrimSpace(value), "/") == target { + return true + } + } + return false +} diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index bb1ae62..e5288d0 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useCallback, useLayoutEffect } from "react"; +import { useState, useEffect, useCallback, useLayoutEffect, useRef } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Search, X, ArrowUp } from "lucide-react"; @@ -32,7 +32,7 @@ import { useMetadata } from "@/hooks/useMetadata"; import { useLyrics } from "@/hooks/useLyrics"; import { useCover } from "@/hooks/useCover"; import { useAvailability } from "@/hooks/useAvailability"; -import { ensureApiStatusCheckStarted } from "@/lib/api-status"; +import { ensureSpotiFLACNextStatusCheckStarted } from "@/lib/api-status"; import { useDownloadQueueDialog } from "@/hooks/useDownloadQueueDialog"; import { useDownloadProgress } from "@/hooks/useDownloadProgress"; import { buildPlaylistFolderName } from "@/lib/playlist"; @@ -125,6 +125,7 @@ function parseStoredHistory(value: string | null): HistoryItem[] { } function App() { const [currentPage, setCurrentPage] = useState("main"); + const contentScrollRef = useRef(null); const [spotifyUrl, setSpotifyUrl] = useState(""); const [selectedTracks, setSelectedTracks] = useState([]); const [searchQuery, setSearchQuery] = useState(""); @@ -197,20 +198,33 @@ function App() { }; mediaQuery.addEventListener("change", handleChange); checkForUpdates(); - ensureApiStatusCheckStarted(); + ensureSpotiFLACNextStatusCheckStarted(); void loadHistory(); - const handleScroll = () => { - setShowScrollTop(window.scrollY > 300); - }; - window.addEventListener("scroll", handleScroll); return () => { mediaQuery.removeEventListener("change", handleChange); - window.removeEventListener("scroll", handleScroll); + }; + }, []); + useEffect(() => { + const contentElement = contentScrollRef.current; + if (!contentElement) { + return; + } + const handleScroll = () => { + setShowScrollTop(contentElement.scrollTop > 300); + }; + handleScroll(); + contentElement.addEventListener("scroll", handleScroll, { passive: true }); + return () => { + contentElement.removeEventListener("scroll", handleScroll); }; }, []); const scrollToTop = useCallback(() => { - window.scrollTo({ top: 0, behavior: "smooth" }); + contentScrollRef.current?.scrollTo({ top: 0, behavior: "smooth" }); }, []); + useEffect(() => { + contentScrollRef.current?.scrollTo({ top: 0, behavior: "auto" }); + setShowScrollTop(false); + }, [currentPage]); useEffect(() => { setSelectedTracks([]); setSearchQuery(""); @@ -584,14 +598,16 @@ function App() { } }; return ( -
+
-
-
- {renderPage()} +
+
+
+ {renderPage()} +
diff --git a/frontend/src/assets/icons/am.png b/frontend/src/assets/icons/am.png new file mode 100644 index 0000000..5040e0e Binary files /dev/null and b/frontend/src/assets/icons/am.png differ diff --git a/frontend/src/assets/icons/dzr.png b/frontend/src/assets/icons/dzr.png new file mode 100644 index 0000000..c31e8be Binary files /dev/null and b/frontend/src/assets/icons/dzr.png differ diff --git a/frontend/src/assets/icons/spotidownloader.svg b/frontend/src/assets/icons/spotidownloader.svg deleted file mode 100644 index a1da83b..0000000 --- a/frontend/src/assets/icons/spotidownloader.svg +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/frontend/src/components/AboutPage.tsx b/frontend/src/components/AboutPage.tsx index 2b1fd56..cf41f75 100644 --- a/frontend/src/components/AboutPage.tsx +++ b/frontend/src/components/AboutPage.tsx @@ -8,7 +8,6 @@ import ChatGPTTTSIcon from "@/assets/chatgpt-tts.webp"; import XIcon from "@/assets/x.webp"; import XProIcon from "@/assets/x-pro.webp"; import SpotubeDLIcon from "@/assets/icons/spotubedl.svg"; -import SpotiDownloaderIcon from "@/assets/icons/spotidownloader.svg"; import XBatchDLIcon from "@/assets/icons/xbatchdl.svg"; import SpotiFLACNextIcon from "@/assets/icons/next.svg"; import KofiLogo from "@/assets/ko-fi.gif"; @@ -21,7 +20,12 @@ const browserExtensionItems = [ { icon: XIcon, label: "Twitter/X Media Batch Downloader", alt: "Twitter/X Media Batch Downloader" }, { icon: XProIcon, label: "Twitter/X Media Batch Downloader Pro", alt: "Twitter/X Media Batch Downloader Pro" }, ]; -const projectCardClass = "cursor-pointer transition-colors hover:bg-muted/50 dark:hover:bg-accent/50"; +const projectCardClass = "cursor-pointer gap-3 py-5 transition-colors hover:bg-muted/50 dark:hover:bg-accent/50"; +const projectCardHeaderClass = "px-5 gap-1.5"; +const projectCardContentClass = "px-5"; +const projectBodyClass = "text-[13px] leading-snug"; +const releaseMetaClass = "text-xs text-muted-foreground whitespace-nowrap"; +const releaseVersionClass = "text-xs bg-primary text-primary-foreground px-1.5 py-0.5 rounded-sm font-mono font-semibold whitespace-nowrap"; export function AboutPage() { const [activeTab, setActiveTab] = useState<"projects" | "support">("projects"); const [repoStats, setRepoStats] = useState>({}); @@ -44,8 +48,7 @@ export function AboutPage() { } } const repos = [ - { name: "SpotiDownloader", owner: "afkarxyz" }, - { name: "SpotiFLAC-Next", owner: "spotiverse" }, + { name: "SpotiFLAC-Next", owner: "spotbye" }, { name: "Twitter-X-Media-Batch-Downloader", owner: "afkarxyz" }, ]; const stats: Record = {}; @@ -176,7 +179,7 @@ export function AboutPage() { const getRepoDescription = (repoName: string): string => { return repoStats[repoName]?.description || ""; }; - return (
+ return (

About

@@ -195,17 +198,17 @@ export function AboutPage() {
- {activeTab === "projects" && (
-
- openExternal("https://github.com/spotbye/SpotiFLAC-Next")}> - + {activeTab === "projects" && (
+
+ openExternal("https://github.com/spotbye/SpotiFLAC-Next")}> +
SpotiFLAC Next -
- {repoStats["SpotiFLAC-Next"]?.latestReleaseAt && ( +
+ {repoStats["SpotiFLAC-Next"]?.latestReleaseAt && ( {formatReleaseTimeAgo(repoStats["SpotiFLAC-Next"].latestReleaseAt)} )} - {repoStats["SpotiFLAC-Next"]?.latestVersion && ( + {repoStats["SpotiFLAC-Next"]?.latestVersion && ( {repoStats["SpotiFLAC-Next"].latestVersion} )}
@@ -213,11 +216,11 @@ export function AboutPage() { SpotiFLAC Next - + {getRepoDescription("SpotiFLAC-Next")} - {repoStats["SpotiFLAC-Next"] && ( + {repoStats["SpotiFLAC-Next"] && ( {repoStats["SpotiFLAC-Next"].languages?.length > 0 && (
{repoStats["SpotiFLAC-Next"].languages.map((lang: string) => ( Note
-

+

This project was created as a thank-you to everyone who has supported SpotiFLAC on Ko-fi.

)} - openExternal("https://github.com/spotbye/SpotiDownloader")}> - -
- SpotiDownloader -
- {repoStats["SpotiDownloader"]?.latestReleaseAt && ( - {formatReleaseTimeAgo(repoStats["SpotiDownloader"].latestReleaseAt)} - )} - {repoStats["SpotiDownloader"]?.latestVersion && ( - {repoStats["SpotiDownloader"].latestVersion} - )} -
-
- - SpotiDownloader - - - {getRepoDescription("SpotiDownloader")} - -
- {repoStats["SpotiDownloader"] && ( -
- {repoStats["SpotiDownloader"].languages?.map((lang: string) => ( - {lang} - ))} -
-
- - {" "} - {formatNumber(repoStats["SpotiDownloader"].stars)} - - - {" "} - {repoStats["SpotiDownloader"].forks} - - - {" "} - {formatTimeAgo(repoStats["SpotiDownloader"].createdAt)} - -
-
- - TOTAL:{" "} - {formatNumber(repoStats["SpotiDownloader"].totalDownloads)} - - - LATEST:{" "} - {formatNumber(repoStats["SpotiDownloader"].latestDownloads)} - -
-
)} -
openExternal("https://github.com/afkarxyz/Twitter-X-Media-Batch-Downloader")}> - +
Twitter/X Media Batch Downloader -
- {repoStats["Twitter-X-Media-Batch-Downloader"]?.latestReleaseAt && ( +
+ {repoStats["Twitter-X-Media-Batch-Downloader"]?.latestReleaseAt && ( {formatReleaseTimeAgo(repoStats["Twitter-X-Media-Batch-Downloader"].latestReleaseAt)} )} - {repoStats["Twitter-X-Media-Batch-Downloader"]?.latestVersion && ( + {repoStats["Twitter-X-Media-Batch-Downloader"]?.latestVersion && ( {repoStats["Twitter-X-Media-Batch-Downloader"].latestVersion} )}
@@ -322,11 +270,11 @@ export function AboutPage() { Twitter/X Media Batch Downloader - + {getRepoDescription("Twitter-X-Media-Batch-Downloader")} - {repoStats["Twitter-X-Media-Batch-Downloader"] && ( + {repoStats["Twitter-X-Media-Batch-Downloader"] && (
{repoStats["Twitter-X-Media-Batch-Downloader"].languages?.map((lang: string) => ( )} -
+
openExternal("https://exyezed.qzz.io/")}> - - Browser Extensions & Scripts - - {browserExtensionItems.map((item) => (
+ + Browser Extensions & Scripts + + {browserExtensionItems.map((item) => (
{item.alt}/ - + {item.label}
))} @@ -379,12 +327,12 @@ export function AboutPage() {
openExternal("https://spotubedl.com/")}> - - + + SpotubeDL{" "} SpotubeDL.com - + Download Spotify Tracks, Albums, Playlists & Discography as MP3/OGG/Opus. diff --git a/frontend/src/components/ApiStatusTab.tsx b/frontend/src/components/ApiStatusTab.tsx index 674ccd4..3314b28 100644 --- a/frontend/src/components/ApiStatusTab.tsx +++ b/frontend/src/components/ApiStatusTab.tsx @@ -1,34 +1,79 @@ import { Button } from "@/components/ui/button"; -import { RefreshCw, CheckCircle2, XCircle, Loader2 } from "lucide-react"; -import { TidalIcon, QobuzIcon, AmazonIcon, LrclibIcon, MusicBrainzIcon } from "./PlatformIcons"; +import { SearchCheck, CheckCircle2, XCircle, Loader2 } from "lucide-react"; +import { TidalIcon, QobuzIcon, AmazonIcon, MusicBrainzIcon, AppleMusicIcon, DeezerIcon } from "./PlatformIcons"; import { useApiStatus } from "@/hooks/useApiStatus"; +import { SPOTIFLAC_NEXT_SOURCES } from "@/lib/api-status"; +function renderStatusIcon(status: "checking" | "online" | "offline" | "idle") { + if (status === "online") { + return ; + } + if (status === "offline") { + return ; + } + return null; +} +function renderPlatformIcon(type: string) { + if (type === "tidal") { + return ; + } + if (type === "amazon") { + return ; + } + if (type === "musicbrainz") { + return ; + } + if (type === "deezer") { + return ; + } + if (type === "apple") { + return ; + } + return ; +} export function ApiStatusTab() { - const { sources, statuses, isCheckingAll, refreshAll } = useApiStatus(); + const { sources, statuses, nextStatuses, checkingSources, checkOne } = useApiStatus(); return (
-
- +
+

SpotiFLAC Services

+ +
+ {sources.map((source) => { + const status = statuses[source.id] || "idle"; + const isChecking = checkingSources[source.id] === true; + return (
+
+
+ {renderPlatformIcon(source.type)} +

{source.name}

+
+
{renderStatusIcon(status)}
+
+ +
); + })} +
-
- {sources.map((source) => { - const status = statuses[source.id] || "idle"; +
+ +
+

SpotiFLAC Next Services

+ +
+ {SPOTIFLAC_NEXT_SOURCES.map((source) => { + const status = nextStatuses[source.id] || "idle"; return (
- {source.type === "tidal" ? : source.type === "amazon" ? : source.type === "lrclib" ? : source.type === "musicbrainz" ? : } + {renderPlatformIcon(source.id)}

{source.name}

- -
- {status === "checking" && } - {status === "online" && } - {status === "offline" && } - {status === "idle" &&
} -
+
{renderStatusIcon(status)}
); })} +
); } diff --git a/frontend/src/components/PlatformIcons.tsx b/frontend/src/components/PlatformIcons.tsx index e944837..388e1c6 100644 --- a/frontend/src/components/PlatformIcons.tsx +++ b/frontend/src/components/PlatformIcons.tsx @@ -1,4 +1,6 @@ import amazonMusicIcon from "../assets/icons/amzn.png"; +import appleMusicIcon from "../assets/icons/am.png"; +import deezerIcon from "../assets/icons/dzr.png"; import lrclibIcon from "../assets/icons/lrclib.png"; import musicBrainzDarkIcon from "../assets/icons/musicbrainz_d.png"; import musicBrainzLightIcon from "../assets/icons/musicbrainz_l.png"; @@ -81,6 +83,12 @@ export function QobuzIcon({ className = "w-4 h-4" }: PlatformIconProps) { export function AmazonIcon({ className = "w-4 h-4" }: PlatformIconProps) { return ; } +export function AppleMusicIcon({ className = "w-4 h-4" }: PlatformIconProps) { + return ; +} +export function DeezerIcon({ className = "w-4 h-4" }: PlatformIconProps) { + return ; +} export function LrclibIcon({ className = "w-4 h-4" }: PlatformIconProps) { return ; } diff --git a/frontend/src/components/SettingsPage.tsx b/frontend/src/components/SettingsPage.tsx index 04e01e7..30e539a 100644 --- a/frontend/src/components/SettingsPage.tsx +++ b/frontend/src/components/SettingsPage.tsx @@ -102,6 +102,9 @@ export function SettingsPage({ onUnsavedChangesChange, onResetRequest, }: Settin const handleTidalQualityChange = async (value: "LOSSLESS" | "HI_RES_LOSSLESS") => { setTempSettings((prev) => ({ ...prev, tidalQuality: value })); }; + const handleTidalVariantChange = (value: "tidal" | "alt") => { + setTempSettings((prev) => ({ ...prev, tidalVariant: value })); + }; const handleQobuzQualityChange = (value: "6" | "7" | "27") => { setTempSettings((prev) => ({ ...prev, qobuzQuality: value })); }; @@ -424,17 +427,19 @@ export function SettingsPage({ onUnsavedChangesChange, onResetRequest, }: Settin )} - {tempSettings.downloader === "tidal" && ()} + {tempSettings.downloader === "tidal" && (tempSettings.tidalVariant === "alt" ? (
+ 16-bit/44.1kHz +
) : ())} {tempSettings.downloader === "qobuz" && ( + + + + + Tidal + Tidal Alt. + + +
)} + {((tempSettings.downloader === "tidal" && + tempSettings.tidalVariant !== "alt" && tempSettings.tidalQuality === "HI_RES_LOSSLESS") || (tempSettings.downloader === "qobuz" && tempSettings.qobuzQuality === "27") || diff --git a/frontend/src/hooks/useApiStatus.ts b/frontend/src/hooks/useApiStatus.ts index 24a8b8b..b311a45 100644 --- a/frontend/src/hooks/useApiStatus.ts +++ b/frontend/src/hooks/useApiStatus.ts @@ -1,9 +1,8 @@ import { useEffect, useState } from "react"; -import { API_SOURCES, checkAllApiStatuses, ensureApiStatusCheckStarted, getApiStatusState, subscribeApiStatus, } from "@/lib/api-status"; +import { API_SOURCES, checkApiStatus, getApiStatusState, subscribeApiStatus, } from "@/lib/api-status"; export function useApiStatus() { const [state, setState] = useState(getApiStatusState); useEffect(() => { - ensureApiStatusCheckStarted(); return subscribeApiStatus(() => { setState(getApiStatusState()); }); @@ -11,6 +10,6 @@ export function useApiStatus() { return { ...state, sources: API_SOURCES, - refreshAll: () => checkAllApiStatuses(true), + checkOne: (sourceId: string) => checkApiStatus(sourceId), }; } diff --git a/frontend/src/hooks/useDownload.ts b/frontend/src/hooks/useDownload.ts index ea773c6..4e91dfc 100644 --- a/frontend/src/hooks/useDownload.ts +++ b/frontend/src/hooks/useDownload.ts @@ -52,6 +52,24 @@ async function resolveTemplateISRC(settings: { return ""; } } +function getTidalVariant(settings: any): "tidal" | "alt" { + return settings?.tidalVariant === "alt" ? "alt" : "tidal"; +} +function isTidalAltVariant(settings: any): boolean { + return getTidalVariant(settings) === "alt"; +} +function getTidalAudioFormat(settings: any, mode: "single" | "auto"): "LOSSLESS" | "HI_RES_LOSSLESS" { + if (isTidalAltVariant(settings)) { + return "LOSSLESS"; + } + if (mode === "auto") { + return (settings.autoQuality || "24") === "24" ? "HI_RES_LOSSLESS" : "LOSSLESS"; + } + return settings.tidalQuality || "LOSSLESS"; +} +function shouldFetchStreamingURLs(order: string[], settings: any): boolean { + return order.includes("amazon") || (order.includes("tidal") && !isTidalAltVariant(settings)); +} export function useDownload(region: string) { const [downloadProgress, setDownloadProgress] = useState(0); const [isDownloading, setIsDownloading] = useState(false); @@ -170,8 +188,11 @@ export function useDownload(region: string) { itemID = await AddToDownloadQueue(id, trackName || "", displayArtist || "", albumName || ""); } if (service === "auto") { + const order = (settings.autoOrder || "tidal-amazon-qobuz").split("-"); + const tidalVariant = getTidalVariant(settings); + const tidalLabel = tidalVariant === "alt" ? "Tidal Alt." : "Tidal"; let streamingURLs: any = null; - if (spotifyId) { + if (spotifyId && shouldFetchStreamingURLs(order, settings)) { try { const { GetStreamingURLs } = await import("../../wailsjs/go/main/App"); const urlsJson = await GetStreamingURLs(spotifyId, region); @@ -182,16 +203,15 @@ export function useDownload(region: string) { } } const durationSeconds = durationMs ? Math.round(durationMs / 1000) : undefined; - const order = (settings.autoOrder || "tidal-amazon-qobuz").split("-"); let lastResponse: any = { success: false, error: "No matching services found" }; const fallbackErrors: string[] = []; + const tidalQuality = getTidalAudioFormat(settings, "auto"); const is24Bit = (settings.autoQuality || "24") === "24"; - const tidalQuality = is24Bit ? "HI_RES_LOSSLESS" : "LOSSLESS"; const qobuzQuality = is24Bit ? "27" : "6"; for (const s of order) { - if (s === "tidal" && streamingURLs?.tidal_url) { + if (s === "tidal" && ((tidalVariant === "alt" && spotifyId) || streamingURLs?.tidal_url)) { try { - logger.debug(`trying tidal for: ${trackName} - ${artistName}`); + logger.debug(`trying ${tidalLabel} for: ${trackName} - ${artistName}`); const response = await downloadTrack({ service: "tidal", query, @@ -209,7 +229,8 @@ export function useDownload(region: string) { spotify_id: spotifyId, embed_lyrics: settings.embedLyrics, embed_max_quality_cover: settings.embedMaxQualityCover, - service_url: streamingURLs.tidal_url, + service_url: tidalVariant === "alt" ? undefined : streamingURLs?.tidal_url, + tidal_variant: tidalVariant, duration: durationSeconds, item_id: itemID, audio_format: tidalQuality, @@ -225,17 +246,17 @@ export function useDownload(region: string) { embed_genre: settings.embedGenre, }); if (response.success) { - logger.success(`tidal: ${trackName} - ${artistName}`); + logger.success(`${tidalLabel}: ${trackName} - ${artistName}`); return response; } const errMsg = response.error || response.message || "Failed"; - fallbackErrors.push(`[Tidal] ${errMsg}`); + fallbackErrors.push(`[${tidalLabel}] ${errMsg}`); lastResponse = response; - logger.warning(`tidal failed, trying next...`); + logger.warning(`${tidalLabel} failed, trying next...`); } catch (err) { - logger.error(`tidal error: ${err}`); - fallbackErrors.push(`[Tidal] ${String(err)}`); + logger.error(`${tidalLabel} error: ${err}`); + fallbackErrors.push(`[${tidalLabel}] ${String(err)}`); lastResponse = { success: false, error: String(err) }; } } @@ -344,7 +365,7 @@ export function useDownload(region: string) { const durationSecondsForFallback = durationMs ? Math.round(durationMs / 1000) : undefined; let audioFormat: string | undefined; if (service === "tidal") { - audioFormat = settings.tidalQuality || "LOSSLESS"; + audioFormat = getTidalAudioFormat(settings, "single"); } else if (service === "qobuz") { audioFormat = settings.qobuzQuality || "6"; @@ -373,6 +394,7 @@ export function useDownload(region: string) { duration: durationSecondsForFallback, item_id: itemID, audio_format: audioFormat, + tidal_variant: service === "tidal" ? getTidalVariant(settings) : undefined, spotify_track_number: spotifyTrackNumber, spotify_disc_number: spotifyDiscNumber, spotify_total_tracks: spotifyTotalTracks, @@ -380,6 +402,7 @@ export function useDownload(region: string) { isrc: resolvedTemplateISRC || undefined, copyright: copyright, publisher: publisher, + use_first_artist_only: settings.useFirstArtistOnly, use_single_genre: settings.useSingleGenre, embed_genre: settings.embedGenre, }); @@ -451,8 +474,11 @@ export function useDownload(region: string) { } } if (service === "auto") { + const order = (settings.autoOrder || "tidal-amazon-qobuz").split("-"); + const tidalVariant = getTidalVariant(settings); + const tidalLabel = tidalVariant === "alt" ? "Tidal Alt." : "Tidal"; let streamingURLs: any = null; - if (spotifyId) { + if (spotifyId && shouldFetchStreamingURLs(order, settings)) { try { const { GetStreamingURLs } = await import("../../wailsjs/go/main/App"); const urlsJson = await GetStreamingURLs(spotifyId, region); @@ -463,16 +489,15 @@ export function useDownload(region: string) { } } const durationSeconds = durationMs ? Math.round(durationMs / 1000) : undefined; - const order = (settings.autoOrder || "tidal-amazon-qobuz").split("-"); let lastResponse: any = { success: false, error: "No matching services found" }; const fallbackErrors: string[] = []; + const tidalQuality = getTidalAudioFormat(settings, "auto"); const is24Bit = (settings.autoQuality || "24") === "24"; - const tidalQuality = is24Bit ? "HI_RES_LOSSLESS" : "LOSSLESS"; const qobuzQuality = is24Bit ? "27" : "6"; for (const s of order) { - if (s === "tidal" && streamingURLs?.tidal_url) { + if (s === "tidal" && ((tidalVariant === "alt" && spotifyId) || streamingURLs?.tidal_url)) { try { - logger.debug(`trying tidal for: ${trackName} - ${artistName}`); + logger.debug(`trying ${tidalLabel} for: ${trackName} - ${artistName}`); const response = await downloadTrack({ service: "tidal", query, @@ -490,7 +515,8 @@ export function useDownload(region: string) { spotify_id: spotifyId, embed_lyrics: settings.embedLyrics, embed_max_quality_cover: settings.embedMaxQualityCover, - service_url: streamingURLs.tidal_url, + service_url: tidalVariant === "alt" ? undefined : streamingURLs?.tidal_url, + tidal_variant: tidalVariant, duration: durationSeconds, item_id: itemID, audio_format: tidalQuality, @@ -506,17 +532,17 @@ export function useDownload(region: string) { embed_genre: settings.embedGenre, }); if (response.success) { - logger.success(`tidal: ${trackName} - ${artistName}`); + logger.success(`${tidalLabel}: ${trackName} - ${artistName}`); return response; } const errMsg = response.error || response.message || "Failed"; - fallbackErrors.push(`[Tidal] ${errMsg}`); + fallbackErrors.push(`[${tidalLabel}] ${errMsg}`); lastResponse = response; - logger.warning(`tidal failed, trying next...`); + logger.warning(`${tidalLabel} failed, trying next...`); } catch (err) { - logger.error(`tidal error: ${err}`); - fallbackErrors.push(`[Tidal] ${String(err)}`); + logger.error(`${tidalLabel} error: ${err}`); + fallbackErrors.push(`[${tidalLabel}] ${String(err)}`); lastResponse = { success: false, error: String(err) }; } } @@ -628,7 +654,7 @@ export function useDownload(region: string) { const durationSecondsForFallback = durationMs ? Math.round(durationMs / 1000) : undefined; let audioFormat: string | undefined; if (service === "tidal") { - audioFormat = settings.tidalQuality || "LOSSLESS"; + audioFormat = getTidalAudioFormat(settings, "single"); } else if (service === "qobuz") { audioFormat = settings.qobuzQuality || "6"; @@ -653,6 +679,7 @@ export function useDownload(region: string) { duration: durationSecondsForFallback, item_id: itemID, audio_format: audioFormat, + tidal_variant: service === "tidal" ? getTidalVariant(settings) : undefined, spotify_track_number: spotifyTrackNumber, spotify_disc_number: spotifyDiscNumber, spotify_total_tracks: spotifyTotalTracks, diff --git a/frontend/src/index.css b/frontend/src/index.css index 4698284..dbb2340 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -73,6 +73,13 @@ } @layer base { + html, + body, + #root { + height: 100%; + overflow: hidden; + } + * { @apply border-border outline-ring/50; } @@ -265,4 +272,4 @@ .custom-scrollbar::-webkit-scrollbar-thumb:hover { filter: brightness(1.2); -} \ No newline at end of file +} diff --git a/frontend/src/lib/api-status.ts b/frontend/src/lib/api-status.ts index dfe1126..ad6b915 100644 --- a/frontend/src/lib/api-status.ts +++ b/frontend/src/lib/api-status.ts @@ -1,4 +1,4 @@ -import { CheckAPIStatus, FetchUnifiedAPIStatus } from "../../wailsjs/go/main/App"; +import { CheckAPIStatus } from "../../wailsjs/go/main/App"; import { CHECK_TIMEOUT_MS, withTimeout } from "@/lib/async-timeout"; export type ApiCheckStatus = "checking" | "online" | "offline" | "idle"; export interface ApiSource { @@ -7,39 +7,51 @@ export interface ApiSource { name: string; url: string; } -export const API_SOURCES: ApiSource[] = [ - { id: "tidal1", type: "tidal", name: "Tidal A", url: "https://hifi-one.spotisaver.net" }, - { id: "tidal2", type: "tidal", name: "Tidal B", url: "https://hifi-two.spotisaver.net" }, - { id: "tidal3", type: "tidal", name: "Tidal C", url: "https://eu-central.monochrome.tf" }, - { id: "tidal4", type: "tidal", name: "Tidal D", url: "https://us-west.monochrome.tf" }, - { id: "tidal5", type: "tidal", name: "Tidal E", url: "https://api.monochrome.tf" }, - { id: "tidal6", type: "tidal", name: "Tidal F", url: "https://monochrome-api.samidy.com" }, - { id: "tidal7", type: "tidal", name: "Tidal G", url: "https://tidal.kinoplus.online" }, - { id: "qobuz1", type: "qobuz", name: "Qobuz A", url: "https://dab.yeet.su" }, - { id: "qobuz2", type: "qobuz", name: "Qobuz B", url: "https://dabmusic.xyz" }, - { id: "qobuz3", type: "qbz", name: "Qobuz C", url: "https://qbz.afkarxyz.qzz.io" }, - { id: "amazon1", type: "amazon", name: "Amazon Music", url: "https://amzn.afkarxyz.qzz.io" }, - { id: "lrclib", type: "lrclib", name: "LRCLIB", url: "https://lrclib.net" }, - { id: "musicbrainz", type: "musicbrainz", name: "MusicBrainz", url: "https://musicbrainz.org" }, -]; -type ApiStatusState = { - isCheckingAll: boolean; - statuses: Record; -}; -let apiStatusState: ApiStatusState = { - isCheckingAll: false, - statuses: {}, -}; -let activeCheckAll: Promise | null = null; -const listeners = new Set<() => void>(); -type SpotiFLACUnifiedStatusResponse = { +interface SpotiFLACNextSource { + id: string; + name: string; +} +type SpotiFLACNextStatusResponse = { tidal?: string; qobuz_a?: string; qobuz_b?: string; qobuz_c?: string; - amazon?: string; - lrclib?: string; + deezer_a?: string; + deezer_b?: string; + amazon_a?: string; + amazon_b?: string; + amazon_c?: string; + apple?: string; }; +export const API_SOURCES: ApiSource[] = [ + { id: "tidal", type: "tidal", name: "Tidal", url: "" }, + { id: "qobuz", type: "qobuz", name: "Qobuz", url: "" }, + { id: "amazon", type: "amazon", name: "Amazon Music", url: "" }, + { id: "musicbrainz", type: "musicbrainz", name: "MusicBrainz", url: "https://musicbrainz.org" }, +]; +export const SPOTIFLAC_NEXT_SOURCES: SpotiFLACNextSource[] = [ + { id: "tidal", name: "Tidal" }, + { id: "qobuz", name: "Qobuz" }, + { id: "amazon", name: "Amazon Music" }, + { id: "deezer", name: "Deezer" }, + { id: "apple", name: "Apple Music" }, +]; +const SPOTIFLAC_NEXT_STATUS_URL = "https://status.spotbye.qzz.io/status"; +const SPOTIFLAC_NEXT_MAX_ATTEMPTS = 3; +const SPOTIFLAC_NEXT_RETRY_DELAY_MS = 1200; +type ApiStatusState = { + checkingSources: Record; + statuses: Record; + nextStatuses: Record; +}; +let apiStatusState: ApiStatusState = { + checkingSources: {}, + statuses: {}, + nextStatuses: {}, +}; +let activeCheckNextOnly: Promise | null = null; +const activeSourceChecks = new Map>(); +const listeners = new Set<() => void>(); function emitApiStatusChange() { for (const listener of listeners) { listener(); @@ -49,39 +61,66 @@ function setApiStatusState(updater: (current: ApiStatusState) => ApiStatusState) apiStatusState = updater(apiStatusState); emitApiStatusChange(); } -function statusFromUnifiedValue(value: string | undefined): ApiCheckStatus { - return value === "up" ? "online" : "offline"; -} -async function fetchUnifiedStatuses(forceRefresh: boolean): Promise> { - const response = await FetchUnifiedAPIStatus(forceRefresh); - const payload = JSON.parse(response) as SpotiFLACUnifiedStatusResponse; - const tidalStatus = statusFromUnifiedValue(payload.tidal); - return { - statuses: { - tidal1: tidalStatus, - tidal2: tidalStatus, - tidal3: tidalStatus, - tidal4: tidalStatus, - tidal5: tidalStatus, - tidal6: tidalStatus, - tidal7: tidalStatus, - qobuz1: statusFromUnifiedValue(payload.qobuz_a), - qobuz2: statusFromUnifiedValue(payload.qobuz_b), - qobuz3: statusFromUnifiedValue(payload.qobuz_c), - amazon1: statusFromUnifiedValue(payload.amazon), - lrclib: statusFromUnifiedValue(payload.lrclib), - }, - }; -} -async function checkMusicBrainzStatus(): Promise { +async function checkSourceStatus(source: ApiSource): Promise { try { - const isOnline = await withTimeout(CheckAPIStatus("musicbrainz", "https://musicbrainz.org"), CHECK_TIMEOUT_MS, "API status check timed out after 10 seconds for MusicBrainz"); + const isOnline = await withTimeout(CheckAPIStatus(source.type, source.url), CHECK_TIMEOUT_MS, `API status check timed out after 10 seconds for ${source.name}`); return isOnline ? "online" : "offline"; } catch { return "offline"; } } +function statusFromNextValue(value: string | undefined): ApiCheckStatus { + return value === "up" ? "online" : "offline"; +} +function anyNextVariantUp(values: Array): ApiCheckStatus { + return values.some((value) => value === "up") ? "online" : "offline"; +} +function delay(ms: number): Promise { + return new Promise((resolve) => window.setTimeout(resolve, ms)); +} +function getSafeNextStatusesFallback(currentStatuses: Record): Record { + return SPOTIFLAC_NEXT_SOURCES.reduce>((acc, source) => { + const current = currentStatuses[source.id]; + acc[source.id] = current === "online" || current === "offline" ? current : "idle"; + return acc; + }, {}); +} +async function fetchSpotiFLACNextStatusesOnce(): Promise> { + const response = await withTimeout(fetch(SPOTIFLAC_NEXT_STATUS_URL, { + method: "GET", + cache: "no-store", + headers: { + Accept: "application/json", + }, + }), CHECK_TIMEOUT_MS, "SpotiFLAC Next status check timed out after 10 seconds"); + if (!response.ok) { + throw new Error(`SpotiFLAC Next status returned ${response.status}`); + } + const payload = (await response.json()) as SpotiFLACNextStatusResponse; + return { + tidal: statusFromNextValue(payload.tidal), + qobuz: anyNextVariantUp([payload.qobuz_a, payload.qobuz_b, payload.qobuz_c]), + deezer: anyNextVariantUp([payload.deezer_a, payload.deezer_b]), + amazon: anyNextVariantUp([payload.amazon_a, payload.amazon_b, payload.amazon_c]), + apple: statusFromNextValue(payload.apple), + }; +} +async function checkSpotiFLACNextStatuses(): Promise> { + let lastError: unknown = null; + for (let attempt = 1; attempt <= SPOTIFLAC_NEXT_MAX_ATTEMPTS; attempt++) { + try { + return await fetchSpotiFLACNextStatusesOnce(); + } + catch (error) { + lastError = error; + if (attempt < SPOTIFLAC_NEXT_MAX_ATTEMPTS) { + await delay(SPOTIFLAC_NEXT_RETRY_DELAY_MS * attempt); + } + } + } + throw lastError instanceof Error ? lastError : new Error("SpotiFLAC Next status check failed"); +} export function getApiStatusState(): ApiStatusState { return apiStatusState; } @@ -91,70 +130,98 @@ export function subscribeApiStatus(listener: () => void): () => void { listeners.delete(listener); }; } -export function hasApiStatusResults(): boolean { - return API_SOURCES.some((source) => { - const status = apiStatusState.statuses[source.id]; +function hasSpotiFLACNextResults(): boolean { + return SPOTIFLAC_NEXT_SOURCES.some((source) => { + const status = apiStatusState.nextStatuses[source.id]; return status === "online" || status === "offline"; }); } -export function ensureApiStatusCheckStarted(): void { - if (!activeCheckAll && !hasApiStatusResults()) { - void checkAllApiStatuses(false); +export async function checkSpotiFLACNextStatusesOnly(): Promise { + if (activeCheckNextOnly) { + return activeCheckNextOnly; } -} -export async function checkAllApiStatuses(forceRefresh: boolean = false): Promise { - if (activeCheckAll) { - return activeCheckAll; - } - activeCheckAll = (async () => { - const checkingStatuses = Object.fromEntries(API_SOURCES.map((source) => [source.id, "checking" as ApiCheckStatus])); + activeCheckNextOnly = (async () => { + const checkingNextStatuses = Object.fromEntries(SPOTIFLAC_NEXT_SOURCES.map((source) => [source.id, "checking" as ApiCheckStatus])); setApiStatusState((current) => ({ ...current, - isCheckingAll: true, - statuses: { - ...current.statuses, - ...checkingStatuses, + nextStatuses: { + ...current.nextStatuses, + ...checkingNextStatuses, }, })); try { - const [unifiedResult, musicBrainzStatus] = await Promise.allSettled([ - withTimeout(fetchUnifiedStatuses(forceRefresh), CHECK_TIMEOUT_MS, "Unified SpotiFLAC status check timed out after 10 seconds"), - checkMusicBrainzStatus(), - ]); - setApiStatusState((current) => { - const nextStatuses = { ...current.statuses }; - if (unifiedResult.status === "fulfilled") { - Object.assign(nextStatuses, unifiedResult.value.statuses); - } - else { - nextStatuses.tidal1 = "offline"; - nextStatuses.tidal2 = "offline"; - nextStatuses.tidal3 = "offline"; - nextStatuses.tidal4 = "offline"; - nextStatuses.tidal5 = "offline"; - nextStatuses.tidal6 = "offline"; - nextStatuses.tidal7 = "offline"; - nextStatuses.qobuz1 = "offline"; - nextStatuses.qobuz2 = "offline"; - nextStatuses.qobuz3 = "offline"; - nextStatuses.amazon1 = "offline"; - nextStatuses.lrclib = "offline"; - } - nextStatuses.musicbrainz = - musicBrainzStatus.status === "fulfilled" ? musicBrainzStatus.value : "offline"; - return { - ...current, - statuses: nextStatuses, - }; - }); + setApiStatusState((current) => ({ + ...current, + nextStatuses: { ...current.nextStatuses }, + })); + const nextStatuses = await checkSpotiFLACNextStatuses(); + setApiStatusState((current) => ({ + ...current, + nextStatuses: { + ...current.nextStatuses, + ...nextStatuses, + }, + })); + } + catch { + setApiStatusState((current) => ({ + ...current, + nextStatuses: getSafeNextStatusesFallback(current.nextStatuses), + })); + } + finally { + activeCheckNextOnly = null; + } + })(); + return activeCheckNextOnly; +} +export function ensureSpotiFLACNextStatusCheckStarted(): void { + if (!activeCheckNextOnly && !hasSpotiFLACNextResults()) { + void checkSpotiFLACNextStatusesOnly(); + } +} +export async function checkApiStatus(sourceId: string): Promise { + const source = API_SOURCES.find((item) => item.id === sourceId); + if (!source) { + return; + } + const activeCheck = activeSourceChecks.get(sourceId); + if (activeCheck) { + return activeCheck; + } + const task = (async () => { + setApiStatusState((current) => ({ + ...current, + checkingSources: { + ...current.checkingSources, + [sourceId]: true, + }, + statuses: { + ...current.statuses, + [sourceId]: "checking", + }, + })); + try { + const status = await checkSourceStatus(source); + setApiStatusState((current) => ({ + ...current, + statuses: { + ...current.statuses, + [sourceId]: status, + }, + })); } finally { setApiStatusState((current) => ({ ...current, - isCheckingAll: false, + checkingSources: { + ...current.checkingSources, + [sourceId]: false, + }, })); - activeCheckAll = null; + activeSourceChecks.delete(sourceId); } })(); - return activeCheckAll; + activeSourceChecks.set(sourceId, task); + return task; } diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index d2160f1..ef9c56a 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -13,6 +13,9 @@ export async function fetchSpotifyMetadata(url: string, batch: boolean = true, d } export async function downloadTrack(request: DownloadRequest): Promise { const req = new main.DownloadRequest(request); + if (request.tidal_variant !== undefined) { + (req as any).tidal_variant = request.tidal_variant; + } if (request.use_single_genre !== undefined) { (req as any).use_single_genre = request.use_single_genre; } diff --git a/frontend/src/lib/settings.ts b/frontend/src/lib/settings.ts index 291e6ec..a4b3d59 100644 --- a/frontend/src/lib/settings.ts +++ b/frontend/src/lib/settings.ts @@ -22,6 +22,7 @@ export interface Settings { embedLyrics: boolean; embedMaxQualityCover: boolean; operatingSystem: "Windows" | "linux/MacOS"; + tidalVariant: "tidal" | "alt"; tidalQuality: "LOSSLESS" | "HI_RES_LOSSLESS"; qobuzQuality: "6" | "7" | "27"; amazonQuality: "original"; @@ -110,6 +111,7 @@ export const DEFAULT_SETTINGS: Settings = { embedLyrics: false, embedMaxQualityCover: false, operatingSystem: detectOS(), + tidalVariant: "tidal", tidalQuality: "LOSSLESS", qobuzQuality: "6", amazonQuality: "original", @@ -215,6 +217,9 @@ function getSettingsFromLocalStorage(): Settings { if (!('tidalQuality' in parsed)) { parsed.tidalQuality = "LOSSLESS"; } + if (!('tidalVariant' in parsed)) { + parsed.tidalVariant = "tidal"; + } if (!('qobuzQuality' in parsed)) { parsed.qobuzQuality = "6"; } @@ -306,6 +311,9 @@ export async function loadSettings(): Promise { if (!('tidalQuality' in parsed)) { parsed.tidalQuality = "LOSSLESS"; } + if (!('tidalVariant' in parsed)) { + parsed.tidalVariant = "tidal"; + } if (!('qobuzQuality' in parsed)) { parsed.qobuzQuality = "6"; } diff --git a/frontend/src/types/api.ts b/frontend/src/types/api.ts index 48dc659..6e0e007 100644 --- a/frontend/src/types/api.ts +++ b/frontend/src/types/api.ts @@ -120,6 +120,7 @@ export interface DownloadRequest { release_date?: string; cover_url?: string; tidal_api_url?: string; + tidal_variant?: "tidal" | "alt"; output_dir?: string; audio_format?: string; folder_name?: string; diff --git a/wails.json b/wails.json index ff85543..891084c 100644 --- a/wails.json +++ b/wails.json @@ -12,7 +12,7 @@ }, "info": { "productName": "SpotiFLAC", - "productVersion": "7.1.4", + "productVersion": "7.1.5", "copyright": "© 2026 afkarxyz" }, "wailsjsdir": "./frontend",