This commit is contained in:
afkarxyz
2026-04-14 07:36:41 +07:00
parent 59a057b14a
commit 7346730be9
336 changed files with 13800 additions and 1142 deletions
+29 -15
View File
@@ -204,7 +204,7 @@ func (a *AmazonDownloader) DownloadFromService(amazonURL, outputDir, quality str
return a.DownloadFromAfkarXYZ(amazonURL, outputDir, quality)
}
func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filenameFormat, playlistName, playlistOwner string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL string, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks int, embedMaxQualityCover bool, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyURL string, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filenameFormat, playlistName, playlistOwner string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL string, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks int, embedMaxQualityCover bool, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL string, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
if outputDir != "." {
if err := os.MkdirAll(outputDir, 0755); err != nil {
@@ -219,12 +219,14 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename
filenameArtist = GetFirstArtist(spotifyArtistName)
filenameAlbumArtist = GetFirstArtist(spotifyAlbumArtist)
}
expectedFilename := BuildExpectedFilename(spotifyTrackName, filenameArtist, spotifyAlbumName, filenameAlbumArtist, spotifyReleaseDate, filenameFormat, playlistName, playlistOwner, includeTrackNumber, position, spotifyDiscNumber, false)
expectedFilename := BuildExpectedFilename(spotifyTrackName, filenameArtist, spotifyAlbumName, filenameAlbumArtist, spotifyReleaseDate, filenameFormat, playlistName, playlistOwner, includeTrackNumber, position, spotifyDiscNumber, false, isrcOverride)
expectedPath := filepath.Join(outputDir, expectedFilename)
if fileInfo, err := os.Stat(expectedPath); err == nil && fileInfo.Size() > 0 {
fmt.Printf("File already exists: %s (%.2f MB)\n", expectedPath, float64(fileInfo.Size())/(1024*1024))
return "EXISTS:" + expectedPath, nil
if !GetRedownloadWithSuffixSetting() {
if fileInfo, err := os.Stat(expectedPath); err == nil && fileInfo.Size() > 0 {
fmt.Printf("File already exists: %s (%.2f MB)\n", expectedPath, float64(fileInfo.Size())/(1024*1024))
return "EXISTS:" + expectedPath, nil
}
}
}
@@ -250,12 +252,16 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename
}
res.ISRC = isrc
if isrc != "" {
fmt.Println("Fetching MusicBrainz metadata...")
if fetchedMeta, err := FetchMusicBrainzMetadata(isrc, spotifyTrackName, spotifyArtistName, spotifyAlbumName, useSingleGenre, embedGenre); err == nil {
res.Metadata = fetchedMeta
fmt.Println("✓ MusicBrainz metadata fetched")
if ShouldSkipMusicBrainzMetadataFetch() {
fmt.Println("Skipping MusicBrainz metadata fetch because status check is offline.")
} else {
fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err)
fmt.Println("Fetching MusicBrainz metadata...")
if fetchedMeta, err := FetchMusicBrainzMetadata(isrc, spotifyTrackName, spotifyArtistName, spotifyAlbumName, 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
@@ -271,11 +277,13 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename
return "", err
}
var isrc string
isrc := strings.TrimSpace(isrcOverride)
var mbMeta Metadata
if spotifyURL != "" {
result := <-metaChan
isrc = result.ISRC
if isrc == "" {
isrc = result.ISRC
}
mbMeta = result.Metadata
}
@@ -309,6 +317,7 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename
newFilename = strings.ReplaceAll(newFilename, "{album_artist}", safeAlbumArtist)
newFilename = strings.ReplaceAll(newFilename, "{year}", year)
newFilename = strings.ReplaceAll(newFilename, "{date}", SanitizeFilename(spotifyReleaseDate))
newFilename = strings.ReplaceAll(newFilename, "{isrc}", SanitizeOptionalFilename(isrc))
if spotifyDiscNumber > 0 {
newFilename = strings.ReplaceAll(newFilename, "{disc}", fmt.Sprintf("%d", spotifyDiscNumber))
@@ -346,6 +355,9 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename
}
newFilename = newFilename + ext
newFilePath := filepath.Join(outputDir, newFilename)
if GetRedownloadWithSuffixSetting() {
newFilePath, _ = ResolveOutputPathForDownload(newFilePath, true)
}
if err := os.Rename(filePath, newFilePath); err != nil {
fmt.Printf("Warning: Failed to rename file: %v\n", err)
@@ -390,7 +402,9 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename
Comment: spotifyURL,
Copyright: spotifyCopyright,
Publisher: spotifyPublisher,
Description: "https://github.com/afkarxyz/SpotiFLAC",
Composer: spotifyComposer,
Separator: metadataSeparator,
Description: "https://github.com/spotbye/SpotiFLAC",
ISRC: isrc,
Genre: mbMeta.Genre,
}
@@ -418,7 +432,7 @@ func (a *AmazonDownloader) DownloadByURL(amazonURL, outputDir, quality, filename
return filePath, nil
}
func (a *AmazonDownloader) DownloadBySpotifyID(spotifyTrackID, outputDir, quality, filenameFormat, playlistName, playlistOwner string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL string, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks int, embedMaxQualityCover bool, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyURL string,
func (a *AmazonDownloader) DownloadBySpotifyID(spotifyTrackID, outputDir, quality, filenameFormat, playlistName, playlistOwner string, includeTrackNumber bool, position int, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL string, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks int, embedMaxQualityCover bool, spotifyTotalDiscs int, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL string,
useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool,
) (string, error) {
@@ -427,5 +441,5 @@ func (a *AmazonDownloader) DownloadBySpotifyID(spotifyTrackID, outputDir, qualit
return "", err
}
return a.DownloadByURL(amazonURL, outputDir, quality, filenameFormat, playlistName, playlistOwner, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, embedMaxQualityCover, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyURL, useFirstArtistOnly, useSingleGenre, embedGenre)
return a.DownloadByURL(amazonURL, outputDir, quality, filenameFormat, playlistName, playlistOwner, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, spotifyCoverURL, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, embedMaxQualityCover, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, isrcOverride, spotifyURL, useFirstArtistOnly, useSingleGenre, embedGenre)
}
+90
View File
@@ -0,0 +1,90 @@
package backend
import "strings"
func normalizeArtistSeparator(separator string) string {
separator = strings.TrimSpace(separator)
if separator == "," || separator == ";" {
return separator
}
return ""
}
func splitArtistSegment(segment string, separator string) []string {
segment = strings.TrimSpace(segment)
if segment == "" {
return nil
}
if strings.Contains(segment, "|||SEP|||") {
return strings.Split(segment, "|||SEP|||")
}
parts := []string{segment}
if separator = normalizeArtistSeparator(separator); separator != "" {
var separated []string
for _, part := range parts {
for _, item := range strings.Split(part, separator) {
separated = append(separated, item)
}
}
parts = separated
} else if strings.Contains(segment, ";") {
var separated []string
for _, part := range parts {
for _, item := range strings.Split(part, ";") {
separated = append(separated, item)
}
}
parts = separated
}
return parts
}
func SplitArtistCredits(artistStr, separator string) []string {
rawParts := splitArtistSegment(artistStr, separator)
if len(rawParts) == 0 {
return nil
}
seen := make(map[string]struct{}, len(rawParts))
result := make([]string, 0, len(rawParts))
for _, part := range rawParts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
if _, exists := seen[part]; exists {
continue
}
seen[part] = struct{}{}
result = append(result, part)
}
return result
}
func SplitMetadataValues(value, separator string) []string {
rawParts := splitArtistSegment(value, separator)
if len(rawParts) == 0 {
return nil
}
seen := make(map[string]struct{}, len(rawParts))
result := make([]string, 0, len(rawParts))
for _, part := range rawParts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
if _, exists := seen[part]; exists {
continue
}
seen[part] = struct{}{}
result = append(result, part)
}
return result
}
+4 -13
View File
@@ -50,23 +50,14 @@ func LoadConfigSettings() (map[string]interface{}, error) {
return settings, nil
}
func GetSpotFetchAPISettings() (bool, string) {
func GetRedownloadWithSuffixSetting() bool {
settings, err := LoadConfigSettings()
if err != nil || settings == nil {
return false, ""
return false
}
useAPI, _ := settings["useSpotFetchAPI"].(bool)
if !useAPI {
return false, ""
}
apiURL, _ := settings["spotFetchAPIUrl"].(string)
if apiURL == "" {
apiURL = "https://sp.afkarxyz.qzz.io/api"
}
return true, apiURL
enabled, _ := settings["redownloadWithSuffix"].(bool)
return enabled
}
func GetLinkResolverSetting() string {
+96 -103
View File
@@ -83,6 +83,37 @@ func GetFFmpegDir() (string, error) {
return EnsureAppDir()
}
func resolveSystemExecutable(executableName string) string {
if runtime.GOOS == "darwin" {
candidates := []string{
"/opt/homebrew/bin/" + executableName,
"/usr/local/bin/" + executableName,
}
for _, candidate := range candidates {
if _, err := os.Stat(candidate); err == nil {
return candidate
}
}
}
if runtime.GOOS != "windows" {
path, err := exec.Command("which", executableName).Output()
if err == nil {
trimmed := strings.TrimSpace(string(path))
if trimmed != "" {
return trimmed
}
}
}
path, err := exec.LookPath(executableName)
if err == nil {
return path
}
return ""
}
func GetFFmpegPath() (string, error) {
ffmpegDir, err := GetFFmpegDir()
if err != nil {
@@ -94,38 +125,15 @@ func GetFFmpegPath() (string, error) {
ffmpegName = "ffmpeg.exe"
}
if path := resolveSystemExecutable(ffmpegName); path != "" {
return path, nil
}
localPath := filepath.Join(ffmpegDir, ffmpegName)
if _, err := os.Stat(localPath); err == nil {
return localPath, nil
}
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
homebrewPath := "/opt/homebrew/bin/" + ffmpegName
if _, err := os.Stat(homebrewPath); err == nil {
return homebrewPath, nil
}
} else if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" {
homebrewPath := "/usr/local/bin/" + ffmpegName
if _, err := os.Stat(homebrewPath); err == nil {
return homebrewPath, nil
}
}
if runtime.GOOS != "windows" {
path, err := exec.Command("which", ffmpegName).Output()
if err == nil {
trimmed := strings.TrimSpace(string(path))
if trimmed != "" {
return trimmed, nil
}
}
}
path, err := exec.LookPath(ffmpegName)
if err == nil {
return path, nil
}
return localPath, nil
}
@@ -140,38 +148,15 @@ func GetFFprobePath() (string, error) {
ffprobeName = "ffprobe.exe"
}
if path := resolveSystemExecutable(ffprobeName); path != "" {
return path, nil
}
localPath := filepath.Join(ffmpegDir, ffprobeName)
if _, err := os.Stat(localPath); err == nil {
return localPath, nil
}
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
homebrewPath := "/opt/homebrew/bin/" + ffprobeName
if _, err := os.Stat(homebrewPath); err == nil {
return homebrewPath, nil
}
} else if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" {
homebrewPath := "/usr/local/bin/" + ffprobeName
if _, err := os.Stat(homebrewPath); err == nil {
return homebrewPath, nil
}
}
if runtime.GOOS != "windows" {
path, err := exec.Command("which", ffprobeName).Output()
if err == nil {
trimmed := strings.TrimSpace(string(path))
if trimmed != "" {
return trimmed, nil
}
}
}
path, err := exec.LookPath(ffprobeName)
if err == nil {
return path, nil
}
return localPath, fmt.Errorf("ffprobe not found in app directory or system path")
}
@@ -205,7 +190,11 @@ func IsFFmpegInstalled() (bool, error) {
setHideWindow(cmd)
err = cmd.Run()
return err == nil, nil
if err != nil {
return false, nil
}
return IsFFprobeInstalled()
}
func GetBrewPath() string {
@@ -255,10 +244,38 @@ func InstallFFmpegWithBrew(progressCallback func(int, string)) error {
return nil
}
const (
ffmpegWindowsURL = "https://github.com/afkarxyz/ffmpeg-binaries/releases/download/v8.0/ffmpeg-windows-amd64.zip"
ffmpegLinuxURL = "https://github.com/afkarxyz/ffmpeg-binaries/releases/download/v8.0/ffmpeg-linux-amd64.tar.xz"
)
const ffmpegReleaseBaseURL = "https://github.com/afkarxyz/ffmpeg-binaries/releases/download/v8.1"
func buildFFmpegReleaseURL(assetName string) string {
return ffmpegReleaseBaseURL + "/" + assetName
}
func getFFmpegDownloadURLs() ([]string, []string, error) {
switch runtime.GOOS {
case "windows":
return []string{buildFFmpegReleaseURL("ffmpeg-windows.zip")}, []string{buildFFmpegReleaseURL("ffprobe-windows.zip")}, nil
case "linux":
switch runtime.GOARCH {
case "amd64":
return []string{buildFFmpegReleaseURL("ffmpeg-linux-amd64.zip")}, []string{buildFFmpegReleaseURL("ffprobe-linux-amd64.zip")}, nil
case "arm64":
return []string{buildFFmpegReleaseURL("ffmpeg-linux-arm64v8.zip")}, []string{buildFFmpegReleaseURL("ffprobe-linux-arm64v8.zip")}, nil
default:
return nil, nil, fmt.Errorf("unsupported Linux architecture: %s", runtime.GOARCH)
}
case "darwin":
switch runtime.GOARCH {
case "amd64":
return []string{buildFFmpegReleaseURL("ffmpeg-macos-amd64.zip")}, []string{buildFFmpegReleaseURL("ffprobe-macos-amd64.zip")}, nil
case "arm64":
return []string{buildFFmpegReleaseURL("ffmpeg-macos-arm64.zip")}, []string{buildFFmpegReleaseURL("ffprobe-macos-arm64.zip")}, nil
default:
return nil, nil, fmt.Errorf("unsupported macOS architecture: %s", runtime.GOARCH)
}
default:
return nil, nil, fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
}
func DownloadFFmpeg(progressCallback func(int)) error {
@@ -276,57 +293,30 @@ func DownloadFFmpeg(progressCallback func(int)) error {
return fmt.Errorf("failed to create ffmpeg directory: %w", err)
}
if runtime.GOOS == "darwin" {
ffmpegInstalled, _ := IsFFmpegInstalled()
ffprobeInstalled, _ := IsFFprobeInstalled()
ffmpegInstalled, _ := IsFFmpegInstalled()
ffprobeInstalled, _ := IsFFprobeInstalled()
isARM := runtime.GOARCH == "arm64"
ffmpegURLs, ffprobeURLs, err := getFFmpegDownloadURLs()
if err != nil {
return err
}
var macFFmpegURLs []string
var macFFprobeURLs []string
if isARM {
macFFmpegURLs = []string{"https://github.com/afkarxyz/ffmpeg-binaries/releases/download/v8.0/ffmpeg-macos-arm64.zip"}
macFFprobeURLs = []string{"https://github.com/afkarxyz/ffmpeg-binaries/releases/download/v8.0/ffprobe-macos-arm64.zip"}
} else {
macFFmpegURLs = []string{"https://github.com/afkarxyz/ffmpeg-binaries/releases/download/v8.0/ffmpeg-macos-intel.zip"}
macFFprobeURLs = []string{"https://github.com/afkarxyz/ffmpeg-binaries/releases/download/v8.0/ffprobe-macos-intel.zip"}
if !ffmpegInstalled && !ffprobeInstalled {
if err := downloadWithFallback(ffmpegURLs, ffmpegDir, progressCallback, 0, 50); err != nil {
return err
}
if !ffmpegInstalled && !ffprobeInstalled {
if err := downloadWithFallback(macFFmpegURLs, ffmpegDir, progressCallback, 0, 50); err != nil {
return err
}
if err := downloadWithFallback(macFFprobeURLs, ffmpegDir, progressCallback, 50, 100); err != nil {
return err
}
} else if !ffmpegInstalled {
if err := downloadWithFallback(macFFmpegURLs, ffmpegDir, progressCallback, 0, 100); err != nil {
return err
}
} else if !ffprobeInstalled {
if err := downloadWithFallback(macFFprobeURLs, ffmpegDir, progressCallback, 0, 100); err != nil {
return err
}
if err := downloadWithFallback(ffprobeURLs, ffmpegDir, progressCallback, 50, 100); err != nil {
return err
}
return nil
}
var url string
switch runtime.GOOS {
case "windows":
url = ffmpegWindowsURL
case "linux":
url = ffmpegLinuxURL
default:
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
if !ffmpegInstalled {
return downloadWithFallback(ffmpegURLs, ffmpegDir, progressCallback, 0, 100)
}
fmt.Printf("[FFmpeg] Downloading from: %s\n", url)
if err := downloadAndExtract(url, ffmpegDir, progressCallback, 0, 100); err != nil {
return err
if !ffprobeInstalled {
return downloadWithFallback(ffprobeURLs, ffmpegDir, progressCallback, 0, 100)
}
return nil
@@ -452,10 +442,13 @@ func downloadAndExtract(url, destDir string, progressCallback func(int), progres
}
fmt.Printf("[FFmpeg] Extracting...\n")
if strings.HasSuffix(url, ".tar.xz") || runtime.GOOS == "linux" {
if strings.HasSuffix(url, ".tar.xz") {
return extractTarXz(tmpFile.Name(), destDir)
}
return extractZip(tmpFile.Name(), destDir)
if strings.HasSuffix(url, ".zip") {
return extractZip(tmpFile.Name(), destDir)
}
return fmt.Errorf("unsupported archive format for %s", url)
}
func extractZip(zipPath, destDir string) error {
+35
View File
@@ -30,6 +30,8 @@ type AudioMetadata struct {
TrackNumber int `json:"track_number"`
DiscNumber int `json:"disc_number"`
Year string `json:"year"`
ISRC string `json:"isrc"`
UPC string `json:"upc"`
}
type RenamePreview struct {
@@ -175,6 +177,12 @@ func readFlacMetadata(filePath string) (*AudioMetadata, error) {
}
case "DATE", "YEAR":
metadata.Year = value
case "ISRC", "TSRC":
metadata.ISRC = value
case "UPC":
assignPreferredUPC(&metadata.UPC, value, true)
case "BARCODE":
assignPreferredUPC(&metadata.UPC, value, false)
}
}
}
@@ -221,6 +229,28 @@ func readMp3Metadata(filePath string) (*AudioMetadata, error) {
}
}
if frames := tag.GetFrames("TSRC"); len(frames) > 0 {
if textFrame, ok := frames[0].(id3v2.TextFrame); ok {
metadata.ISRC = textFrame.Text
}
}
if frames := tag.GetFrames("TXXX"); len(frames) > 0 {
for _, frame := range frames {
userTextFrame, ok := frame.(id3v2.UserDefinedTextFrame)
if !ok {
continue
}
matched, preferred := classifyUPCDescription(userTextFrame.Description)
if !matched {
continue
}
assignPreferredUPC(&metadata.UPC, userTextFrame.Value, preferred)
if preferred && strings.TrimSpace(metadata.UPC) != "" {
break
}
}
}
return metadata, nil
}
@@ -301,9 +331,13 @@ func readMetadataWithFFprobe(filePath string) (*AudioMetadata, error) {
if metadata.Year == "" || len(value) > len(metadata.Year) {
metadata.Year = value
}
case "isrc", "tsrc":
metadata.ISRC = value
}
}
metadata.UPC = firstPreferredFFprobeUPCValue(allTags)
return metadata, nil
}
@@ -333,6 +367,7 @@ func GenerateFilename(metadata *AudioMetadata, format string, ext string) string
result = strings.ReplaceAll(result, "{album_artist}", sanitizeFilenameForRename(metadata.AlbumArtist))
result = strings.ReplaceAll(result, "{year}", sanitizeFilenameForRename(year))
result = strings.ReplaceAll(result, "{date}", sanitizeFilenameForRename(metadata.Year))
result = strings.ReplaceAll(result, "{isrc}", sanitizeFilenameForRename(metadata.ISRC))
if metadata.TrackNumber > 0 {
result = strings.ReplaceAll(result, "{track}", fmt.Sprintf("%02d", metadata.TrackNumber))
+52 -3
View File
@@ -2,6 +2,7 @@ package backend
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
@@ -9,12 +10,12 @@ import (
"unicode/utf8"
)
func BuildExpectedFilename(trackName, artistName, albumName, albumArtist, releaseDate, filenameFormat, playlistName, playlistOwner string, includeTrackNumber bool, position, discNumber int, useAlbumTrackNumber bool) string {
func buildFormattedFilenameBase(trackName, artistName, albumName, albumArtist, releaseDate, filenameFormat, playlistName, playlistOwner, isrc string, includeTrackNumber bool, position, discNumber int, useAlbumTrackNumber bool) string {
safeTitle := SanitizeFilename(trackName)
safeArtist := SanitizeFilename(artistName)
safeAlbum := SanitizeFilename(albumName)
safeAlbumArtist := SanitizeFilename(albumArtist)
safeISRC := SanitizeOptionalFilename(isrc)
safePlaylist := SanitizeFilename(playlistName)
safeCreator := SanitizeFilename(playlistOwner)
@@ -36,6 +37,7 @@ func BuildExpectedFilename(trackName, artistName, albumName, albumArtist, releas
filename = strings.ReplaceAll(filename, "{date}", SanitizeFilename(releaseDate))
filename = strings.ReplaceAll(filename, "{playlist}", safePlaylist)
filename = strings.ReplaceAll(filename, "{creator}", safeCreator)
filename = strings.ReplaceAll(filename, "{isrc}", safeISRC)
if discNumber > 0 {
filename = strings.ReplaceAll(filename, "{disc}", fmt.Sprintf("%d", discNumber))
@@ -67,7 +69,47 @@ func BuildExpectedFilename(trackName, artistName, albumName, albumArtist, releas
}
}
return filename + ".flac"
return filename
}
func BuildExpectedFilename(trackName, artistName, albumName, albumArtist, releaseDate, filenameFormat, playlistName, playlistOwner string, includeTrackNumber bool, position, discNumber int, useAlbumTrackNumber bool, extra ...string) string {
isrc := ""
if len(extra) > 0 {
isrc = extra[0]
}
return buildFormattedFilenameBase(trackName, artistName, albumName, albumArtist, releaseDate, filenameFormat, playlistName, playlistOwner, isrc, includeTrackNumber, position, discNumber, useAlbumTrackNumber) + ".flac"
}
func ResolveOutputPathForDownload(path string, redownloadWithSuffix bool) (string, bool) {
if !redownloadWithSuffix {
if info, err := os.Stat(path); err == nil && info.Size() > 0 {
return path, true
}
return path, false
}
if info, err := os.Stat(path); err != nil || info.Size() == 0 {
return path, false
}
ext := filepath.Ext(path)
base := strings.TrimSuffix(path, ext)
for i := 1; ; i++ {
candidate := fmt.Sprintf("%s_%02d%s", base, i, ext)
if info, err := os.Stat(candidate); err != nil || info.Size() == 0 {
return candidate, false
}
}
}
func mustFileSize(path string) int64 {
info, err := os.Stat(path)
if err != nil {
return 0
}
return info.Size()
}
func SanitizeFilename(name string) string {
@@ -188,3 +230,10 @@ func sanitizeFolderName(name string) string { return SanitizeFilename(name) }
func sanitizeFilename(name string) string {
return SanitizeFilename(name)
}
func SanitizeOptionalFilename(name string) string {
if strings.TrimSpace(name) == "" {
return ""
}
return SanitizeFilename(name)
}
+158 -266
View File
@@ -1,9 +1,6 @@
package backend
import (
"crypto/hmac"
"crypto/sha1"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
@@ -20,16 +17,10 @@ import (
)
const (
spotifyServerTimeURL = "https://open.spotify.com/api/server-time"
spotifySessionTokenURL = "https://open.spotify.com/api/token"
spotifyTOTPSecretsURL = "https://git.gay/thereallo/totp-secrets/raw/branch/main/secrets/secretDict.json"
spotifyGIDMetadataURL = "https://spclient.wg.spotify.com/metadata/4/%s/%s?market=from_token"
spotifyTOTPPeriod = 30
spotifyTOTPDigits = 6
spotifyBase62Alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
spotifyTokenCacheFile = ".isrc-finder-token.json"
spotifySecretsCacheFile = "spotify-secret-dict-cache.json"
spotifySecretsCacheTTL = 24 * time.Hour
spotifySessionTokenURL = "https://open.spotify.com/api/token"
spotifyGIDMetadataURL = "https://spclient.wg.spotify.com/metadata/4/%s/%s?market=from_token"
spotifyBase62Alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
spotifyTokenCacheFile = ".isrc-finder-token.json"
)
var spotifyAnonymousTokenMu sync.Mutex
@@ -39,91 +30,104 @@ type spotifyAnonymousToken struct {
AccessTokenExpirationTimestampMs int64 `json:"accessTokenExpirationTimestampMs"`
}
type spotifyServerTimeResponse struct {
ServerTime int64 `json:"serverTime"`
}
type spotifySecretsCache struct {
FetchedAtUnix int64 `json:"fetched_at_unix"`
Secrets map[string][]int `json:"secrets"`
}
type spotifyTrackRawData struct {
Album struct {
GID string `json:"gid"`
} `json:"album"`
ExternalID []struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"external_id"`
}
type spotFetchISRCResponse struct {
Input string `json:"input"`
TrackID string `json:"track_id"`
GID string `json:"gid"`
CanonicalURI string `json:"canonical_uri"`
Name string `json:"name"`
Artists []string `json:"artists"`
AlbumName string `json:"album_name"`
ReleaseDate string `json:"release_date"`
Label string `json:"label"`
ISRC string `json:"isrc"`
type spotifyAlbumRawData struct {
ExternalID []struct {
Type string `json:"type"`
ID string `json:"id"`
} `json:"external_id"`
}
func (s *SongLinkClient) lookupSpotifyISRC(spotifyTrackID string) (string, error) {
type SpotifyTrackIdentifiers struct {
ISRC string `json:"isrc,omitempty"`
UPC string `json:"upc,omitempty"`
}
func GetSpotifyTrackIdentifiersDirect(spotifyTrackID string) (SpotifyTrackIdentifiers, error) {
normalizedTrackID, err := extractSpotifyTrackID(spotifyTrackID)
if err != nil {
return "", err
return SpotifyTrackIdentifiers{}, err
}
identifiers := SpotifyTrackIdentifiers{}
cachedISRC, err := GetCachedISRC(normalizedTrackID)
if err != nil {
fmt.Printf("Warning: failed to read ISRC cache: %v\n", err)
} else if cachedISRC != "" {
fmt.Printf("Found ISRC in cache: %s\n", cachedISRC)
return cachedISRC, nil
identifiers.ISRC = cachedISRC
}
useSpotFetchAPI, spotFetchAPIURL := GetSpotFetchAPISettings()
if useSpotFetchAPI {
isrc, resolvedTrackID, err := s.lookupSpotifyISRCViaSpotFetchAPI(normalizedTrackID, spotFetchAPIURL)
if err == nil && isrc != "" {
fmt.Printf("Found ISRC via SpotFetch API: %s\n", isrc)
cacheResolvedSpotifyTrackISRC(normalizedTrackID, resolvedTrackID, isrc)
return isrc, nil
}
if err != nil {
fmt.Printf("Warning: SpotFetch ISRC lookup failed, falling back to Spotify metadata: %v\n", err)
}
}
httpClient := &http.Client{Timeout: 30 * time.Second}
payload, metadataErr := fetchSpotifyTrackRawData(s.client, normalizedTrackID)
payload, metadataErr := fetchSpotifyTrackRawData(httpClient, normalizedTrackID)
if metadataErr == nil {
isrc, extractErr := extractSpotifyTrackISRC(payload)
metadataIdentifiers, extractErr := extractSpotifyTrackIdentifiers(httpClient, payload)
if extractErr == nil {
fmt.Printf("Found ISRC via Spotify metadata: %s\n", isrc)
cacheResolvedSpotifyTrackISRC(normalizedTrackID, "", isrc)
return isrc, nil
mergeSpotifyTrackIdentifiers(&identifiers, metadataIdentifiers)
if identifiers.ISRC != "" {
fmt.Printf("Found identifiers via Spotify metadata: isrc=%s upc=%s\n", identifiers.ISRC, identifiers.UPC)
cacheResolvedSpotifyTrackISRC(normalizedTrackID, "", identifiers.ISRC)
}
if identifiers.ISRC != "" && identifiers.UPC != "" {
return identifiers, nil
}
}
metadataErr = extractErr
}
if metadataErr != nil {
fmt.Printf("Warning: Spotify metadata ISRC lookup failed, falling back to Soundplate: %v\n", metadataErr)
fmt.Printf("Warning: Spotify metadata identifier lookup failed, falling back to Soundplate: %v\n", metadataErr)
}
isrc, resolvedTrackID, soundplateErr := s.lookupSpotifyISRCViaSoundplate(normalizedTrackID)
if soundplateErr == nil && isrc != "" {
fmt.Printf("Found ISRC via Soundplate: %s\n", isrc)
cacheResolvedSpotifyTrackISRC(normalizedTrackID, resolvedTrackID, isrc)
return isrc, nil
if identifiers.ISRC == "" {
client := NewSongLinkClient()
isrc, resolvedTrackID, soundplateErr := client.lookupSpotifyISRCViaSoundplate(normalizedTrackID)
if soundplateErr == nil && isrc != "" {
identifiers.ISRC = isrc
fmt.Printf("Found ISRC via Soundplate: %s\n", isrc)
cacheResolvedSpotifyTrackISRC(normalizedTrackID, resolvedTrackID, isrc)
return identifiers, nil
}
if metadataErr != nil && soundplateErr != nil {
return identifiers, fmt.Errorf("spotify metadata lookup failed: %v | soundplate lookup failed: %w", metadataErr, soundplateErr)
}
if soundplateErr != nil && identifiers.UPC == "" {
return identifiers, soundplateErr
}
}
if metadataErr != nil && soundplateErr != nil {
return "", fmt.Errorf("spotify metadata lookup failed: %v | soundplate lookup failed: %w", metadataErr, soundplateErr)
if identifiers.ISRC != "" || identifiers.UPC != "" {
return identifiers, nil
}
if soundplateErr != nil {
return "", soundplateErr
if metadataErr != nil {
return identifiers, metadataErr
}
return "", metadataErr
return identifiers, fmt.Errorf("no Spotify identifiers found for track %s", normalizedTrackID)
}
func (s *SongLinkClient) lookupSpotifyISRC(spotifyTrackID string) (string, error) {
identifiers, err := GetSpotifyTrackIdentifiersDirect(spotifyTrackID)
if err != nil {
return "", err
}
if identifiers.ISRC == "" {
return "", fmt.Errorf("no Spotify ISRC found for track %s", strings.TrimSpace(spotifyTrackID))
}
return identifiers.ISRC, nil
}
func cacheResolvedSpotifyTrackISRC(trackID string, resolvedTrackID string, isrc string) {
@@ -137,47 +141,28 @@ func cacheResolvedSpotifyTrackISRC(trackID string, resolvedTrackID string, isrc
}
}
func (s *SongLinkClient) lookupSpotifyISRCViaSpotFetchAPI(spotifyTrackID string, apiBaseURL string) (string, string, error) {
normalizedTrackID := strings.TrimSpace(spotifyTrackID)
baseURL := strings.TrimRight(strings.TrimSpace(apiBaseURL), "/")
if normalizedTrackID == "" {
return "", "", fmt.Errorf("spotify track ID is required")
func mergeSpotifyTrackIdentifiers(target *SpotifyTrackIdentifiers, incoming SpotifyTrackIdentifiers) {
if incoming.ISRC != "" {
target.ISRC = strings.TrimSpace(incoming.ISRC)
}
if baseURL == "" {
return "", "", fmt.Errorf("spotfetch api url is required")
if incoming.UPC != "" {
target.UPC = strings.TrimSpace(incoming.UPC)
}
}
func lookupSpotifyAlbumUPC(albumID string) (string, error) {
normalizedAlbumID := strings.TrimSpace(albumID)
if normalizedAlbumID == "" {
return "", fmt.Errorf("spotify album ID is required")
}
requestURL := fmt.Sprintf("%s/isrc/%s", baseURL, url.PathEscape(normalizedTrackID))
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
httpClient := &http.Client{Timeout: 30 * time.Second}
payload, err := fetchSpotifyAlbumRawData(httpClient, normalizedAlbumID)
if err != nil {
return "", "", fmt.Errorf("failed to create SpotFetch ISRC request: %w", err)
}
req.Header.Set("User-Agent", songLinkUserAgent)
req.Header.Set("Accept", "application/json")
client := &http.Client{Timeout: 15 * time.Second}
resp, err := client.Do(req)
if err != nil {
return "", "", fmt.Errorf("SpotFetch ISRC request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyPreview, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
return "", "", fmt.Errorf("SpotFetch ISRC returned status %d (%s)", resp.StatusCode, strings.TrimSpace(string(bodyPreview)))
return "", err
}
var payload spotFetchISRCResponse
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
return "", "", fmt.Errorf("failed to decode SpotFetch ISRC response: %w", err)
}
isrc := firstISRCMatch(payload.ISRC)
if isrc == "" {
return "", "", fmt.Errorf("ISRC missing in SpotFetch response")
}
return isrc, strings.TrimSpace(payload.TrackID), nil
return extractSpotifyAlbumUPC(payload)
}
func requestSpotifyBytes(client *http.Client, targetURL string, headers map[string]string) ([]byte, error) {
@@ -269,50 +254,6 @@ func saveSpotifyCachedToken(token *spotifyAnonymousToken) error {
return nil
}
func loadSpotifyCachedSecrets() (*spotifySecretsCache, error) {
cachePath, err := spotifySecretsCachePath()
if err != nil {
return nil, err
}
body, err := os.ReadFile(cachePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, fmt.Errorf("failed to read secrets cache: %w", err)
}
var cache spotifySecretsCache
if err := json.Unmarshal(body, &cache); err != nil {
return nil, fmt.Errorf("failed to parse secrets cache: %w", err)
}
return &cache, nil
}
func saveSpotifyCachedSecrets(cache *spotifySecretsCache) error {
cachePath, err := spotifySecretsCachePath()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
return fmt.Errorf("failed to create secrets cache directory: %w", err)
}
body, err := json.MarshalIndent(cache, "", " ")
if err != nil {
return err
}
if err := os.WriteFile(cachePath, body, 0o644); err != nil {
return fmt.Errorf("failed to write secrets cache: %w", err)
}
return nil
}
func spotifyTokenCachePath() (string, error) {
appDir, err := EnsureAppDir()
if err != nil {
@@ -322,15 +263,6 @@ func spotifyTokenCachePath() (string, error) {
return filepath.Join(appDir, spotifyTokenCacheFile), nil
}
func spotifySecretsCachePath() (string, error) {
appDir, err := EnsureAppDir()
if err != nil {
return "", err
}
return filepath.Join(appDir, spotifySecretsCacheFile), nil
}
func spotifyTokenIsValid(token *spotifyAnonymousToken) bool {
if token == nil || token.AccessToken == "" || token.AccessTokenExpirationTimestampMs == 0 {
return false
@@ -339,47 +271,6 @@ func spotifyTokenIsValid(token *spotifyAnonymousToken) bool {
return time.Now().UnixMilli() < token.AccessTokenExpirationTimestampMs-30_000
}
func spotifySecretsCacheIsValid(cache *spotifySecretsCache) bool {
if cache == nil || cache.FetchedAtUnix == 0 || len(cache.Secrets) == 0 {
return false
}
return time.Since(time.Unix(cache.FetchedAtUnix, 0)) < spotifySecretsCacheTTL
}
func deriveSpotifyTOTPSecret(ciphertext []int) []byte {
var builder strings.Builder
for index, value := range ciphertext {
builder.WriteString(strconv.Itoa(value ^ ((index % 33) + 9)))
}
return []byte(builder.String())
}
func generateSpotifyTOTP(secret []byte, timestampMs int64) string {
counter := timestampMs / 1000 / spotifyTOTPPeriod
counterBytes := make([]byte, 8)
binary.BigEndian.PutUint64(counterBytes, uint64(counter))
mac := hmac.New(sha1.New, secret)
mac.Write(counterBytes)
digest := mac.Sum(nil)
offset := digest[len(digest)-1] & 0x0f
binaryCode := (int(digest[offset])&0x7f)<<24 |
(int(digest[offset+1])&0xff)<<16 |
(int(digest[offset+2])&0xff)<<8 |
(int(digest[offset+3]) & 0xff)
modulo := 1
for i := 0; i < spotifyTOTPDigits; i++ {
modulo *= 10
}
return fmt.Sprintf("%0*d", spotifyTOTPDigits, binaryCode%modulo)
}
func requestSpotifyAnonymousAccessToken(client *http.Client) (string, error) {
spotifyAnonymousTokenMu.Lock()
defer spotifyAnonymousTokenMu.Unlock()
@@ -393,52 +284,17 @@ func requestSpotifyAnonymousAccessToken(client *http.Client) (string, error) {
return cachedToken.AccessToken, nil
}
var serverTime spotifyServerTimeResponse
if err := requestSpotifyJSON(client, spotifyServerTimeURL, nil, &serverTime); err != nil {
return "", err
}
var secrets map[string][]int
cachedSecrets, err := loadSpotifyCachedSecrets()
generatedTOTP, version, err := generateSpotifyTOTP(time.Now())
if err != nil {
fmt.Printf("Warning: failed to read Spotify secrets cache: %v\n", err)
return "", fmt.Errorf("failed to generate Spotify TOTP: %w", err)
}
if spotifySecretsCacheIsValid(cachedSecrets) {
secrets = cachedSecrets.Secrets
} else {
if err := requestSpotifyJSON(client, spotifyTOTPSecretsURL, nil, &secrets); err != nil {
if cachedSecrets != nil && len(cachedSecrets.Secrets) > 0 {
fmt.Printf("Warning: failed to refresh Spotify secrets cache, using stale cache: %v\n", err)
secrets = cachedSecrets.Secrets
} else {
return "", err
}
} else {
cache := &spotifySecretsCache{
FetchedAtUnix: time.Now().Unix(),
Secrets: secrets,
}
if err := saveSpotifyCachedSecrets(cache); err != nil {
fmt.Printf("Warning: failed to write Spotify secrets cache: %v\n", err)
}
}
}
version, err := latestSpotifySecretVersion(secrets)
if err != nil {
return "", err
}
secret := deriveSpotifyTOTPSecret(secrets[version])
generatedTOTP := generateSpotifyTOTP(secret, serverTime.ServerTime*1000)
query := url.Values{
"reason": {"init"},
"productType": {"web-player"},
"totp": {generatedTOTP},
"totpServer": {generatedTOTP},
"totpVer": {version},
"totpVer": {strconv.Itoa(version)},
}
var token spotifyAnonymousToken
@@ -453,30 +309,6 @@ func requestSpotifyAnonymousAccessToken(client *http.Client) (string, error) {
return token.AccessToken, nil
}
func latestSpotifySecretVersion(secrets map[string][]int) (string, error) {
var (
bestVersion string
bestNumber int
)
for version := range secrets {
number, err := strconv.Atoi(version)
if err != nil {
return "", fmt.Errorf("invalid secret version %q: %w", version, err)
}
if bestVersion == "" || number > bestNumber {
bestVersion = version
bestNumber = number
}
}
if bestVersion == "" {
return "", errors.New("no TOTP secret versions available")
}
return bestVersion, nil
}
func extractSpotifyTrackID(value string) (string, error) {
value = strings.TrimSpace(value)
if value == "" {
@@ -504,14 +336,18 @@ func extractSpotifyTrackID(value string) (string, error) {
}
func spotifyTrackIDToGID(trackID string) (string, error) {
if trackID == "" {
return "", errors.New("track ID is empty")
return spotifyEntityIDToGID(trackID)
}
func spotifyEntityIDToGID(entityID string) (string, error) {
if entityID == "" {
return "", errors.New("entity ID is empty")
}
value := big.NewInt(0)
base := big.NewInt(62)
for _, char := range trackID {
for _, char := range entityID {
index := strings.IndexRune(spotifyBase62Alphabet, char)
if index < 0 {
return "", fmt.Errorf("invalid base62 character: %q", string(char))
@@ -530,43 +366,99 @@ func spotifyTrackIDToGID(trackID string) (string, error) {
}
func fetchSpotifyTrackRawData(client *http.Client, trackID string) ([]byte, error) {
accessToken, err := requestSpotifyAnonymousAccessToken(client)
gid, err := spotifyTrackIDToGID(trackID)
if err != nil {
return nil, err
}
gid, err := spotifyTrackIDToGID(trackID)
return fetchSpotifyRawMetadataByGID(client, "track", gid)
}
func fetchSpotifyAlbumRawData(client *http.Client, albumID string) ([]byte, error) {
gid, err := spotifyEntityIDToGID(albumID)
if err != nil {
return nil, err
}
return fetchSpotifyRawMetadataByGID(client, "album", gid)
}
func fetchSpotifyRawMetadataByGID(client *http.Client, entityType string, gid string) ([]byte, error) {
accessToken, err := requestSpotifyAnonymousAccessToken(client)
if err != nil {
return nil, err
}
return requestSpotifyBytes(
client,
fmt.Sprintf(spotifyGIDMetadataURL, "track", gid),
fmt.Sprintf(spotifyGIDMetadataURL, entityType, gid),
map[string]string{
"authorization": "Bearer " + accessToken,
"accept": "application/json",
"user-agent": songLinkUserAgent,
},
)
}
func extractSpotifyTrackISRC(payload []byte) (string, error) {
func extractSpotifyTrackIdentifiers(client *http.Client, payload []byte) (SpotifyTrackIdentifiers, error) {
var track spotifyTrackRawData
if err := json.Unmarshal(payload, &track); err != nil {
return "", fmt.Errorf("failed to decode Spotify track metadata: %w", err)
return SpotifyTrackIdentifiers{}, fmt.Errorf("failed to decode Spotify track metadata: %w", err)
}
identifiers := SpotifyTrackIdentifiers{}
for _, externalID := range track.ExternalID {
if strings.EqualFold(strings.TrimSpace(externalID.Type), "isrc") {
if isrc := firstISRCMatch(externalID.ID); isrc != "" {
return isrc, nil
identifiers.ISRC = isrc
break
}
}
}
if fallbackISRC := firstISRCMatch(string(payload)); fallbackISRC != "" {
return fallbackISRC, nil
if identifiers.ISRC == "" {
identifiers.ISRC = firstISRCMatch(string(payload))
}
albumGID := strings.TrimSpace(track.Album.GID)
if client != nil && albumGID != "" {
albumPayload, err := fetchSpotifyRawMetadataByGID(client, "album", albumGID)
if err == nil {
if upc, upcErr := extractSpotifyAlbumUPC(albumPayload); upcErr == nil {
identifiers.UPC = upc
}
}
}
return identifiers, nil
}
func extractSpotifyTrackISRC(payload []byte) (string, error) {
identifiers, err := extractSpotifyTrackIdentifiers(nil, payload)
if err != nil {
return "", err
}
if identifiers.ISRC != "" {
return identifiers.ISRC, nil
}
return "", fmt.Errorf("ISRC not found in Spotify track metadata")
}
func extractSpotifyAlbumUPC(payload []byte) (string, error) {
var album spotifyAlbumRawData
if err := json.Unmarshal(payload, &album); err != nil {
return "", fmt.Errorf("failed to decode Spotify album metadata: %w", err)
}
for _, externalID := range album.ExternalID {
if strings.EqualFold(strings.TrimSpace(externalID.Type), "upc") {
upc := strings.TrimSpace(externalID.ID)
if upc != "" {
return upc, nil
}
}
}
return "", fmt.Errorf("UPC not found in Spotify album metadata")
}
+22
View File
@@ -0,0 +1,22 @@
package backend
import "strings"
func ResolveTrackISRC(spotifyTrackID string) string {
spotifyTrackID = strings.TrimSpace(spotifyTrackID)
if spotifyTrackID == "" {
return ""
}
if cachedISRC, err := GetCachedISRC(spotifyTrackID); err == nil && cachedISRC != "" {
return strings.ToUpper(strings.TrimSpace(cachedISRC))
}
client := NewSongLinkClient()
isrc, err := client.GetISRCDirect(spotifyTrackID)
if err != nil {
return ""
}
return strings.ToUpper(strings.TrimSpace(isrc))
}
+11 -3
View File
@@ -44,6 +44,7 @@ type LyricsDownloadRequest struct {
AlbumName string `json:"album_name"`
AlbumArtist string `json:"album_artist"`
ReleaseDate string `json:"release_date"`
ISRC string `json:"isrc"`
OutputDir string `json:"output_dir"`
FilenameFormat string `json:"filename_format"`
TrackNumber bool `json:"track_number"`
@@ -363,11 +364,12 @@ func msToLRCTimestamp(msStr string) string {
return fmt.Sprintf("[%02d:%02d.%02d]", minutes, seconds, centiseconds)
}
func buildLyricsFilename(trackName, artistName, albumName, albumArtist, releaseDate, filenameFormat string, includeTrackNumber bool, position, discNumber int) string {
func buildLyricsFilename(trackName, artistName, albumName, albumArtist, releaseDate, filenameFormat, isrc string, includeTrackNumber bool, position, discNumber int) string {
safeTitle := sanitizeFilename(trackName)
safeArtist := sanitizeFilename(artistName)
safeAlbum := sanitizeFilename(albumName)
safeAlbumArtist := sanitizeFilename(albumArtist)
safeISRC := SanitizeOptionalFilename(isrc)
year := ""
if len(releaseDate) >= 4 {
@@ -384,6 +386,7 @@ func buildLyricsFilename(trackName, artistName, albumName, albumArtist, releaseD
filename = strings.ReplaceAll(filename, "{album_artist}", safeAlbumArtist)
filename = strings.ReplaceAll(filename, "{year}", year)
filename = strings.ReplaceAll(filename, "{date}", sanitizeFilename(releaseDate))
filename = strings.ReplaceAll(filename, "{isrc}", safeISRC)
if discNumber > 0 {
filename = strings.ReplaceAll(filename, "{disc}", fmt.Sprintf("%d", discNumber))
@@ -485,10 +488,15 @@ func (c *LyricsClient) DownloadLyrics(req LyricsDownloadRequest) (*LyricsDownloa
if filenameFormat == "" {
filenameFormat = "title-artist"
}
filename := buildLyricsFilename(req.TrackName, req.ArtistName, req.AlbumName, req.AlbumArtist, req.ReleaseDate, filenameFormat, req.TrackNumber, req.Position, req.DiscNumber)
resolvedISRC := strings.TrimSpace(req.ISRC)
if resolvedISRC == "" && strings.Contains(filenameFormat, "{isrc}") {
resolvedISRC = ResolveTrackISRC(req.SpotifyID)
}
filename := buildLyricsFilename(req.TrackName, req.ArtistName, req.AlbumName, req.AlbumArtist, req.ReleaseDate, filenameFormat, resolvedISRC, req.TrackNumber, req.Position, req.DiscNumber)
filePath := filepath.Join(outputDir, filename)
if fileInfo, err := os.Stat(filePath); err == nil && fileInfo.Size() > 0 {
filePath, alreadyExists := ResolveOutputPathForDownload(filePath, GetRedownloadWithSuffixSetting())
if alreadyExists {
return &LyricsDownloadResponse{
Success: true,
Message: "Lyrics file already exists",
+151 -19
View File
@@ -21,6 +21,7 @@ type Metadata struct {
Artist string
Album string
AlbumArtist string
Separator string
Date string
ReleaseDate string
TrackNumber int
@@ -31,12 +32,73 @@ type Metadata struct {
Comment string
Copyright string
Publisher string
Composer string
Lyrics string
Description string
ISRC string
UPC string
Genre string
}
func resolveMetadataSeparator(separator string) string {
if normalized := normalizeArtistSeparator(separator); normalized != "" {
return normalized
}
return normalizeArtistSeparator(GetSeparator())
}
func displayMetadataSeparator(separator string) string {
if resolved := resolveMetadataSeparator(separator); resolved != "" {
return resolved + " "
}
return "; "
}
func addVorbisTagValues(cmt *flacvorbis.MetaDataBlockVorbisComment, key string, values []string) {
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" {
continue
}
_ = cmt.Add(key, value)
}
}
func addMP3TextFrame(tag *id3v2.Tag, frameID string, value string) {
tag.DeleteFrames(frameID)
value = strings.TrimSpace(value)
if value == "" {
return
}
tag.AddTextFrame(frameID, id3v2.EncodingUTF8, value)
}
func joinMultiValueText(values []string, separator string, nullSeparated bool) string {
cleaned := make([]string, 0, len(values))
for _, value := range values {
value = strings.TrimSpace(value)
if value != "" {
cleaned = append(cleaned, value)
}
}
if len(cleaned) == 0 {
return ""
}
if len(cleaned) == 1 {
return cleaned[0]
}
if nullSeparated {
return strings.Join(cleaned, "\x00")
}
return strings.Join(cleaned, displayMetadataSeparator(separator))
}
func EmbedMetadata(filepath string, metadata Metadata, coverPath string) error {
f, err := flac.ParseFile(filepath)
if err != nil {
@@ -52,17 +114,22 @@ func EmbedMetadata(filepath string, metadata Metadata, coverPath string) error {
}
cmt := flacvorbis.New()
separator := resolveMetadataSeparator(metadata.Separator)
if metadata.Title != "" {
_ = cmt.Add(flacvorbis.FIELD_TITLE, metadata.Title)
}
if metadata.Artist != "" {
if artistValues := SplitArtistCredits(metadata.Artist, separator); len(artistValues) > 0 {
addVorbisTagValues(cmt, flacvorbis.FIELD_ARTIST, artistValues)
} else if metadata.Artist != "" {
_ = cmt.Add(flacvorbis.FIELD_ARTIST, metadata.Artist)
}
if metadata.Album != "" {
_ = cmt.Add(flacvorbis.FIELD_ALBUM, metadata.Album)
}
if metadata.AlbumArtist != "" {
if albumArtistValues := SplitArtistCredits(metadata.AlbumArtist, separator); len(albumArtistValues) > 0 {
addVorbisTagValues(cmt, "ALBUMARTIST", albumArtistValues)
} else if metadata.AlbumArtist != "" {
_ = cmt.Add("ALBUMARTIST", metadata.AlbumArtist)
}
if metadata.Date != "" {
@@ -86,6 +153,11 @@ func EmbedMetadata(filepath string, metadata Metadata, coverPath string) error {
if metadata.Publisher != "" {
_ = cmt.Add("PUBLISHER", metadata.Publisher)
}
if composerValues := SplitArtistCredits(metadata.Composer, separator); len(composerValues) > 0 {
addVorbisTagValues(cmt, "COMPOSER", composerValues)
} else if metadata.Composer != "" {
_ = cmt.Add("COMPOSER", metadata.Composer)
}
if metadata.Description != "" {
_ = cmt.Add("DESCRIPTION", metadata.Description)
}
@@ -96,8 +168,13 @@ func EmbedMetadata(filepath string, metadata Metadata, coverPath string) error {
if metadata.ISRC != "" {
_ = cmt.Add("ISRC", metadata.ISRC)
}
if metadata.UPC != "" {
_ = cmt.Add(preferredUPCTagKey, metadata.UPC)
}
if metadata.Genre != "" {
if genreValues := SplitMetadataValues(metadata.Genre, separator); len(genreValues) > 0 {
addVorbisTagValues(cmt, "GENRE", genreValues)
} else if metadata.Genre != "" {
_ = cmt.Add("GENRE", metadata.Genre)
}
@@ -901,8 +978,14 @@ func ExtractFullMetadataFromFile(filePath string) (Metadata, error) {
metadata.Copyright = value
case "publisher", "tpub", "label":
metadata.Publisher = value
case "composer", "writer", "wm/composer", "©wrt":
metadata.Composer = value
case "genre", "tcon":
metadata.Genre = value
case "url":
metadata.URL = value
case "isrc", "tsrc":
metadata.ISRC = value
case "comment", "comments":
if metadata.Comment == "" {
metadata.Comment = value
@@ -914,6 +997,8 @@ func ExtractFullMetadataFromFile(filePath string) (Metadata, error) {
}
}
metadata.UPC = firstPreferredFFprobeUPCValue(allTags)
return metadata, nil
}
@@ -940,15 +1025,13 @@ func embedMetadataToMP3(filePath string, metadata Metadata, coverPath string) er
return fmt.Errorf("failed to open MP3 file: %w", err)
}
defer tag.Close()
separator := resolveMetadataSeparator(metadata.Separator)
tag.DeleteFrames("TXXX")
if metadata.Title != "" {
tag.SetTitle(metadata.Title)
}
if metadata.Artist != "" {
tag.SetArtist(metadata.Artist)
}
if metadata.Album != "" {
tag.SetAlbum(metadata.Album)
}
@@ -960,10 +1043,17 @@ func embedMetadataToMP3(filePath string, metadata Metadata, coverPath string) er
tag.SetYear(year)
}
if metadata.AlbumArtist != "" {
tag.DeleteFrames("TPE2")
tag.AddTextFrame("TPE2", id3v2.EncodingUTF8, metadata.AlbumArtist)
artistText := joinMultiValueText(SplitArtistCredits(metadata.Artist, separator), separator, true)
if artistText == "" {
artistText = strings.TrimSpace(metadata.Artist)
}
addMP3TextFrame(tag, "TPE1", artistText)
albumArtistText := joinMultiValueText(SplitArtistCredits(metadata.AlbumArtist, separator), separator, true)
if albumArtistText == "" {
albumArtistText = strings.TrimSpace(metadata.AlbumArtist)
}
addMP3TextFrame(tag, "TPE2", albumArtistText)
if metadata.TrackNumber > 0 {
tag.DeleteFrames(tag.CommonID("Track number/Position in set"))
@@ -984,18 +1074,28 @@ func embedMetadataToMP3(filePath string, metadata Metadata, coverPath string) er
}
if metadata.Copyright != "" {
tag.DeleteFrames("TCOP")
tag.AddTextFrame("TCOP", id3v2.EncodingUTF8, metadata.Copyright)
addMP3TextFrame(tag, "TCOP", metadata.Copyright)
}
if metadata.Publisher != "" {
tag.DeleteFrames("TPUB")
tag.AddTextFrame("TPUB", id3v2.EncodingUTF8, metadata.Publisher)
addMP3TextFrame(tag, "TPUB", metadata.Publisher)
}
composerText := joinMultiValueText(SplitArtistCredits(metadata.Composer, separator), separator, true)
if composerText == "" {
composerText = strings.TrimSpace(metadata.Composer)
}
addMP3TextFrame(tag, "TCOM", composerText)
if metadata.ISRC != "" {
tag.DeleteFrames("TSRC")
tag.AddTextFrame("TSRC", id3v2.EncodingUTF8, metadata.ISRC)
addMP3TextFrame(tag, "TSRC", metadata.ISRC)
}
if metadata.UPC != "" {
tag.AddUserDefinedTextFrame(id3v2.UserDefinedTextFrame{
Encoding: id3v2.EncodingUTF8,
Description: "UPC",
Value: metadata.UPC,
})
}
if comment := resolveMetadataComment(metadata); comment != "" {
@@ -1027,6 +1127,12 @@ func embedMetadataToMP3(filePath string, metadata Metadata, coverPath string) er
}
}
genreText := joinMultiValueText(SplitMetadataValues(metadata.Genre, separator), separator, true)
if genreText == "" {
genreText = strings.TrimSpace(metadata.Genre)
}
addMP3TextFrame(tag, "TCON", genreText)
if err := tag.Save(); err != nil {
return fmt.Errorf("failed to save MP3 tags: %w", err)
}
@@ -1048,6 +1154,7 @@ func embedMetadataToM4A(filePath string, metadata Metadata, coverPath string) er
"-i", filePath,
"-y",
}
separator := resolveMetadataSeparator(metadata.Separator)
if coverPath != "" && fileExists(coverPath) {
args = append(args, "-i", coverPath)
@@ -1059,14 +1166,22 @@ func embedMetadataToM4A(filePath string, metadata Metadata, coverPath string) er
if metadata.Title != "" {
args = append(args, "-metadata", "title="+metadata.Title)
}
if metadata.Artist != "" {
args = append(args, "-metadata", "artist="+metadata.Artist)
artistText := joinMultiValueText(SplitArtistCredits(metadata.Artist, separator), separator, false)
if artistText == "" {
artistText = strings.TrimSpace(metadata.Artist)
}
if artistText != "" {
args = append(args, "-metadata", "artist="+artistText)
}
if metadata.Album != "" {
args = append(args, "-metadata", "album="+metadata.Album)
}
if metadata.AlbumArtist != "" {
args = append(args, "-metadata", "album_artist="+metadata.AlbumArtist)
albumArtistText := joinMultiValueText(SplitArtistCredits(metadata.AlbumArtist, separator), separator, false)
if albumArtistText == "" {
albumArtistText = strings.TrimSpace(metadata.AlbumArtist)
}
if albumArtistText != "" {
args = append(args, "-metadata", "album_artist="+albumArtistText)
}
if metadata.Date != "" {
args = append(args, "-metadata", "date="+metadata.Date)
@@ -1091,9 +1206,26 @@ func embedMetadataToM4A(filePath string, metadata Metadata, coverPath string) er
if metadata.Publisher != "" {
args = append(args, "-metadata", "publisher="+metadata.Publisher)
}
composerText := joinMultiValueText(SplitArtistCredits(metadata.Composer, separator), separator, false)
if composerText == "" {
composerText = strings.TrimSpace(metadata.Composer)
}
if composerText != "" {
args = append(args, "-metadata", "composer="+composerText)
}
if metadata.ISRC != "" {
args = append(args, "-metadata", "isrc="+metadata.ISRC)
}
if metadata.UPC != "" {
args = append(args, "-metadata", "upc="+metadata.UPC)
}
genreText := joinMultiValueText(SplitMetadataValues(metadata.Genre, separator), separator, false)
if genreText == "" {
genreText = strings.TrimSpace(metadata.Genre)
}
if genreText != "" {
args = append(args, "-metadata", "genre="+genreText)
}
if comment := resolveMetadataComment(metadata); comment != "" {
args = append(args, "-metadata", "comment="+comment)
}
+220 -43
View File
@@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
"golang.org/x/text/cases"
@@ -14,7 +15,66 @@ import (
var AppVersion = "Unknown"
const musicBrainzAPIBase = "https://musicbrainz.org/ws/2"
const (
musicBrainzAPIBase = "https://musicbrainz.org/ws/2"
musicBrainzRequestTimeout = 10 * time.Second
musicBrainzRequestRetries = 3
musicBrainzRequestRetryWait = 3 * time.Second
musicBrainzMinRequestInterval = 1100 * time.Millisecond
musicBrainzThrottleCooldownOn503 = 5 * time.Second
musicBrainzStatusCheckSkipWindow = 5 * time.Minute
)
type musicBrainzStatusError struct {
StatusCode int
}
func (e *musicBrainzStatusError) Error() string {
return fmt.Sprintf("MusicBrainz API returned status: %d", e.StatusCode)
}
type musicBrainzInflightCall struct {
done chan struct{}
result Metadata
err error
}
var (
musicBrainzCache sync.Map
musicBrainzInflightMu sync.Mutex
musicBrainzInflight = make(map[string]*musicBrainzInflightCall)
musicBrainzThrottleMu sync.Mutex
musicBrainzNextRequest time.Time
musicBrainzBlockedTill time.Time
musicBrainzStatusMu sync.RWMutex
musicBrainzLastCheckedAt time.Time
musicBrainzLastCheckedOnline bool
)
func SetMusicBrainzStatusCheckResult(online bool) {
musicBrainzStatusMu.Lock()
defer musicBrainzStatusMu.Unlock()
musicBrainzLastCheckedAt = time.Now()
musicBrainzLastCheckedOnline = online
}
func ShouldSkipMusicBrainzMetadataFetch() bool {
musicBrainzStatusMu.RLock()
defer musicBrainzStatusMu.RUnlock()
if musicBrainzLastCheckedAt.IsZero() {
return false
}
if musicBrainzLastCheckedOnline {
return false
}
return time.Since(musicBrainzLastCheckedAt) <= musicBrainzStatusCheckSkipWindow
}
type MusicBrainzRecordingResponse struct {
Recordings []struct {
@@ -54,66 +114,176 @@ type MusicBrainzRecordingResponse struct {
} `json:"recordings"`
}
func musicBrainzCacheKey(isrc string, useSingleGenre bool) string {
separator := strings.TrimSpace(GetSeparator())
if separator == "" {
separator = ";"
}
return strings.ToUpper(strings.TrimSpace(isrc)) + "|" + fmt.Sprintf("%t", useSingleGenre) + "|" + separator
}
func waitForMusicBrainzRequestSlot() {
musicBrainzThrottleMu.Lock()
readyAt := musicBrainzNextRequest
if musicBrainzBlockedTill.After(readyAt) {
readyAt = musicBrainzBlockedTill
}
now := time.Now()
if readyAt.Before(now) {
readyAt = now
}
musicBrainzNextRequest = readyAt.Add(musicBrainzMinRequestInterval)
waitDuration := time.Until(readyAt)
musicBrainzThrottleMu.Unlock()
if waitDuration > 0 {
time.Sleep(waitDuration)
}
}
func noteMusicBrainzThrottle() {
musicBrainzThrottleMu.Lock()
defer musicBrainzThrottleMu.Unlock()
cooldownUntil := time.Now().Add(musicBrainzThrottleCooldownOn503)
if cooldownUntil.After(musicBrainzBlockedTill) {
musicBrainzBlockedTill = cooldownUntil
}
if musicBrainzNextRequest.Before(musicBrainzBlockedTill) {
musicBrainzNextRequest = musicBrainzBlockedTill
}
}
func shouldRetryMusicBrainzRequest(err error) bool {
if err == nil {
return false
}
statusErr, ok := err.(*musicBrainzStatusError)
if !ok {
return true
}
return statusErr.StatusCode == http.StatusServiceUnavailable || statusErr.StatusCode >= http.StatusInternalServerError
}
func queryMusicBrainzRecordings(client *http.Client, query string) (*MusicBrainzRecordingResponse, error) {
reqURL := fmt.Sprintf("%s/recording?query=%s&fmt=json&inc=releases+artist-credits+tags+media+release-groups+labels", musicBrainzAPIBase, url.QueryEscape(query))
req, err := http.NewRequest(http.MethodGet, reqURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("SpotiFLAC/%s ( hi@afkarxyz.qzz.io )", AppVersion))
req.Header.Set("Accept", "application/json")
var lastErr error
for attempt := 0; attempt < musicBrainzRequestRetries; attempt++ {
waitForMusicBrainzRequestSlot()
resp, err := client.Do(req)
if err == nil && resp != nil && resp.StatusCode == http.StatusOK {
defer resp.Body.Close()
var mbResp MusicBrainzRecordingResponse
if decodeErr := json.NewDecoder(resp.Body).Decode(&mbResp); decodeErr != nil {
return nil, decodeErr
}
return &mbResp, nil
}
if err != nil {
lastErr = err
} else if resp == nil {
lastErr = fmt.Errorf("empty response from MusicBrainz")
} else {
if resp.StatusCode == http.StatusServiceUnavailable {
noteMusicBrainzThrottle()
}
lastErr = &musicBrainzStatusError{StatusCode: resp.StatusCode}
resp.Body.Close()
}
if attempt < musicBrainzRequestRetries-1 && shouldRetryMusicBrainzRequest(lastErr) {
time.Sleep(musicBrainzRequestRetryWait)
continue
}
break
}
if lastErr == nil {
lastErr = fmt.Errorf("empty response from MusicBrainz")
}
return nil, lastErr
}
func FetchMusicBrainzMetadata(isrc, title, artist, album string, useSingleGenre bool, embedGenre bool) (Metadata, error) {
var meta Metadata
var resultErr error
if !embedGenre {
return meta, nil
}
if isrc == "" {
return meta, fmt.Errorf("no ISRC provided")
resultErr = fmt.Errorf("no ISRC provided")
return meta, resultErr
}
cacheKey := musicBrainzCacheKey(isrc, useSingleGenre)
if cached, ok := musicBrainzCache.Load(cacheKey); ok {
return cached.(Metadata), nil
}
if ShouldSkipMusicBrainzMetadataFetch() {
resultErr = fmt.Errorf("skipping MusicBrainz lookup because the latest status check reported offline")
return meta, resultErr
}
musicBrainzInflightMu.Lock()
if call, ok := musicBrainzInflight[cacheKey]; ok {
musicBrainzInflightMu.Unlock()
<-call.done
return call.result, call.err
}
call := &musicBrainzInflightCall{done: make(chan struct{})}
musicBrainzInflight[cacheKey] = call
musicBrainzInflightMu.Unlock()
defer func() {
call.result = meta
call.err = resultErr
musicBrainzInflightMu.Lock()
delete(musicBrainzInflight, cacheKey)
close(call.done)
musicBrainzInflightMu.Unlock()
}()
client := &http.Client{
Timeout: 10 * time.Second,
Timeout: musicBrainzRequestTimeout,
}
query := fmt.Sprintf("isrc:%s", isrc)
reqURL := fmt.Sprintf("%s/recording?query=%s&fmt=json&inc=releases+artist-credits+tags+media+release-groups+labels", musicBrainzAPIBase, url.QueryEscape(query))
req, err := http.NewRequest("GET", reqURL, nil)
mbResp, err := queryMusicBrainzRecordings(client, query)
if err != nil {
return meta, err
}
req.Header.Set("User-Agent", fmt.Sprintf("SpotiFLAC/%s ( hi@afkarxyz.qzz.io )", AppVersion))
var resp *http.Response
var lastErr error
for i := 0; i < 3; i++ {
resp, lastErr = client.Do(req)
if lastErr == nil && resp.StatusCode == http.StatusOK {
break
}
if resp != nil {
resp.Body.Close()
}
if i < 2 {
time.Sleep(2 * time.Second)
}
}
if lastErr != nil {
return meta, lastErr
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return meta, fmt.Errorf("MusicBrainz API returned status: %d", resp.StatusCode)
}
defer resp.Body.Close()
var mbResp MusicBrainzRecordingResponse
if err := json.NewDecoder(resp.Body).Decode(&mbResp); err != nil {
return meta, err
resultErr = err
return meta, resultErr
}
if len(mbResp.Recordings) == 0 {
return meta, fmt.Errorf("no recordings found for ISRC: %s", isrc)
resultErr = fmt.Errorf("no recordings found for ISRC: %s", isrc)
return meta, resultErr
}
recording := mbResp.Recordings[0]
@@ -150,5 +320,12 @@ func FetchMusicBrainzMetadata(isrc, title, artist, album string, useSingleGenre
}
}
if meta.Genre == "" {
resultErr = fmt.Errorf("no genre tags found in MusicBrainz")
return meta, resultErr
}
musicBrainzCache.Store(cacheKey, meta)
return meta, nil
}
+62 -20
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
@@ -72,21 +73,41 @@ func NewQobuzDownloader() *QobuzDownloader {
client: &http.Client{
Timeout: 60 * time.Second,
},
appID: "798273057",
appID: qobuzDefaultAPIAppID,
}
}
func (q *QobuzDownloader) searchByISRC(isrc string) (*QobuzTrack, error) {
apiBase := "https://www.qobuz.com/api.json/0.2/track/search?query="
url := fmt.Sprintf("%s%s&limit=1&app_id=%s", apiBase, isrc, q.appID)
if strings.HasPrefix(isrc, "qobuz_") {
trackID := strings.TrimPrefix(isrc, "qobuz_")
resp, err := doQobuzSignedRequest(http.MethodGet, "track/get", url.Values{"track_id": {trackID}}, q.client)
if err != nil {
return nil, fmt.Errorf("failed to fetch track: %w", err)
}
defer resp.Body.Close()
resp, err := q.client.Get(url)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
var trackResp QobuzTrack
if err := json.NewDecoder(resp.Body).Decode(&trackResp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &trackResp, nil
}
resp, err := doQobuzSignedRequest(http.MethodGet, "track/search", url.Values{
"query": {isrc},
"limit": {"1"},
}, q.client)
if err != nil {
return nil, fmt.Errorf("failed to search track: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned status %d", resp.StatusCode)
}
@@ -305,8 +326,12 @@ func (q *QobuzDownloader) DownloadCoverArt(coverURL, filepath string) error {
return err
}
func buildQobuzFilename(title, artist, album, albumArtist, releaseDate string, trackNumber, discNumber int, format string, includeTrackNumber bool, position int, useAlbumTrackNumber bool) string {
func buildQobuzFilename(title, artist, album, albumArtist, releaseDate string, trackNumber, discNumber int, format string, includeTrackNumber bool, position int, useAlbumTrackNumber bool, extra ...string) string {
var filename string
isrc := ""
if len(extra) > 0 {
isrc = SanitizeOptionalFilename(extra[0])
}
numberToUse := position
if useAlbumTrackNumber && trackNumber > 0 {
@@ -326,6 +351,7 @@ func buildQobuzFilename(title, artist, album, albumArtist, releaseDate string, t
filename = strings.ReplaceAll(filename, "{album_artist}", albumArtist)
filename = strings.ReplaceAll(filename, "{year}", year)
filename = strings.ReplaceAll(filename, "{date}", SanitizeFilename(releaseDate))
filename = strings.ReplaceAll(filename, "{isrc}", isrc)
if discNumber > 0 {
filename = strings.ReplaceAll(filename, "{disc}", fmt.Sprintf("%d", discNumber))
@@ -360,7 +386,7 @@ func buildQobuzFilename(title, artist, album, albumArtist, releaseDate string, t
return filename + ".flac"
}
func (q *QobuzDownloader) DownloadTrack(spotifyID, 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, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
func (q *QobuzDownloader) DownloadTrack(spotifyID, 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, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
var isrc string
if spotifyID != "" {
linkClient := NewSongLinkClient()
@@ -373,22 +399,27 @@ func (q *QobuzDownloader) DownloadTrack(spotifyID, outputDir, quality, filenameF
return "", fmt.Errorf("spotify ID is required for Qobuz download")
}
return q.DownloadTrackWithISRC(isrc, outputDir, quality, filenameFormat, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, useAlbumTrackNumber, spotifyCoverURL, embedMaxQualityCover, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyURL, allowFallback, useFirstArtistOnly, useSingleGenre, embedGenre)
return q.DownloadTrackWithISRC(isrc, outputDir, quality, filenameFormat, includeTrackNumber, position, spotifyTrackName, spotifyArtistName, spotifyAlbumName, spotifyAlbumArtist, spotifyReleaseDate, useAlbumTrackNumber, spotifyCoverURL, embedMaxQualityCover, spotifyTrackNumber, spotifyDiscNumber, spotifyTotalTracks, spotifyTotalDiscs, spotifyCopyright, spotifyPublisher, spotifyComposer, metadataSeparator, spotifyURL, allowFallback, useFirstArtistOnly, useSingleGenre, embedGenre)
}
func (q *QobuzDownloader) DownloadTrackWithISRC(isrc, 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, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
func (q *QobuzDownloader) DownloadTrackWithISRC(isrc, 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, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
fmt.Printf("Fetching track info for ISRC: %s\n", isrc)
metaChan := make(chan Metadata, 1)
if embedGenre && isrc != "" {
go func() {
fmt.Println("Fetching MusicBrainz metadata...")
if fetchedMeta, err := FetchMusicBrainzMetadata(isrc, spotifyTrackName, spotifyArtistName, spotifyAlbumName, useSingleGenre, embedGenre); err == nil {
fmt.Println("✓ MusicBrainz metadata fetched")
metaChan <- fetchedMeta
} else {
fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err)
if ShouldSkipMusicBrainzMetadataFetch() {
fmt.Println("Skipping MusicBrainz metadata fetch because status check is offline.")
metaChan <- Metadata{}
} else {
fmt.Println("Fetching MusicBrainz metadata...")
if fetchedMeta, err := FetchMusicBrainzMetadata(isrc, spotifyTrackName, spotifyArtistName, spotifyAlbumName, useSingleGenre, embedGenre); err == nil {
fmt.Println("✓ MusicBrainz metadata fetched")
metaChan <- fetchedMeta
} else {
fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err)
metaChan <- Metadata{}
}
}
}()
} else {
@@ -446,11 +477,11 @@ func (q *QobuzDownloader) DownloadTrackWithISRC(isrc, outputDir, quality, filena
safeTitle := sanitizeFilename(trackTitle)
safeAlbum := sanitizeFilename(albumTitle)
filename := buildQobuzFilename(safeTitle, safeArtist, safeAlbum, safeAlbumArtist, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber)
filename := buildQobuzFilename(safeTitle, safeArtist, safeAlbum, safeAlbumArtist, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber, isrc)
filepath := filepath.Join(outputDir, filename)
if fileInfo, err := os.Stat(filepath); err == nil && fileInfo.Size() > 0 {
fmt.Printf("File already exists: %s (%.2f MB)\n", filepath, float64(fileInfo.Size())/(1024*1024))
filepath, alreadyExists := ResolveOutputPathForDownload(filepath, GetRedownloadWithSuffixSetting())
if alreadyExists {
fmt.Printf("File already exists: %s (%.2f MB)\n", filepath, float64(mustFileSize(filepath))/(1024*1024))
return "EXISTS:" + filepath, nil
}
@@ -487,6 +518,14 @@ func (q *QobuzDownloader) DownloadTrackWithISRC(isrc, outputDir, quality, filena
trackNumberToEmbed = 1
}
upc := ""
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)
}
metadata := Metadata{
Title: trackTitle,
Artist: artists,
@@ -501,8 +540,11 @@ func (q *QobuzDownloader) DownloadTrackWithISRC(isrc, outputDir, quality, filena
Comment: spotifyURL,
Copyright: spotifyCopyright,
Publisher: spotifyPublisher,
Description: "https://github.com/afkarxyz/SpotiFLAC",
Composer: spotifyComposer,
Separator: metadataSeparator,
Description: "https://github.com/spotbye/SpotiFLAC",
ISRC: isrc,
UPC: upc,
Genre: mbMeta.Genre,
}
+407
View File
@@ -0,0 +1,407 @@
package backend
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"time"
)
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"
qobuzCredentialsCacheFile = "qobuz-api-credentials.json"
qobuzCredentialsCacheTTL = 24 * time.Hour
qobuzCredentialsProbeTrackISRC = "USUM71703861"
qobuzOpenTrackProbeURL = "https://open.qobuz.com/track/1"
)
var (
qobuzCredentialsMu sync.Mutex
qobuzCachedCredentials *qobuzAPICredentials
qobuzOpenBundleScriptPattern = regexp.MustCompile(`<script[^>]+src="([^"]+/js/main\.js|/resources/[^"]+/js/main\.js)"`)
qobuzOpenAPIConfigPattern = regexp.MustCompile(`app_id:"(?P<app_id>\d{9})",app_secret:"(?P<app_secret>[a-f0-9]{32})"`)
)
type qobuzAPICredentials struct {
AppID string `json:"app_id"`
AppSecret string `json:"app_secret"`
Source string `json:"source,omitempty"`
FetchedAtUnix int64 `json:"fetched_at_unix"`
}
type qobuzCredentialProbeResponse struct {
Tracks struct {
Total int `json:"total"`
} `json:"tracks"`
}
func defaultQobuzAPICredentials() *qobuzAPICredentials {
return &qobuzAPICredentials{
AppID: qobuzDefaultAPIAppID,
AppSecret: qobuzDefaultAPIAppSecret,
Source: "embedded-default",
FetchedAtUnix: time.Now().Unix(),
}
}
func qobuzCredentialsCachePath() (string, error) {
appDir, err := GetFFmpegDir()
if err != nil {
return "", err
}
return filepath.Join(appDir, qobuzCredentialsCacheFile), nil
}
func loadQobuzCachedCredentials() (*qobuzAPICredentials, error) {
cachePath, err := qobuzCredentialsCachePath()
if err != nil {
return nil, err
}
body, err := os.ReadFile(cachePath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("failed to read qobuz credentials cache: %w", err)
}
var creds qobuzAPICredentials
if err := json.Unmarshal(body, &creds); err != nil {
return nil, fmt.Errorf("failed to parse qobuz credentials cache: %w", err)
}
if strings.TrimSpace(creds.AppID) == "" || strings.TrimSpace(creds.AppSecret) == "" {
return nil, fmt.Errorf("qobuz credentials cache is incomplete")
}
return &creds, nil
}
func saveQobuzCachedCredentials(creds *qobuzAPICredentials) error {
if creds == nil {
return fmt.Errorf("qobuz credentials are required")
}
cachePath, err := qobuzCredentialsCachePath()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(cachePath), 0o755); err != nil {
return fmt.Errorf("failed to create qobuz credentials cache directory: %w", err)
}
body, err := json.MarshalIndent(creds, "", " ")
if err != nil {
return err
}
if err := os.WriteFile(cachePath, body, 0o644); err != nil {
return fmt.Errorf("failed to write qobuz credentials cache: %w", err)
}
return nil
}
func qobuzCredentialsCacheIsFresh(creds *qobuzAPICredentials) bool {
if creds == nil || creds.FetchedAtUnix == 0 || strings.TrimSpace(creds.AppID) == "" || strings.TrimSpace(creds.AppSecret) == "" {
return false
}
return time.Since(time.Unix(creds.FetchedAtUnix, 0)) < qobuzCredentialsCacheTTL
}
func scrapeQobuzOpenCredentials(client *http.Client) (*qobuzAPICredentials, error) {
req, err := http.NewRequest(http.MethodGet, qobuzOpenTrackProbeURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", qobuzDefaultUA)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch open.qobuz.com shell: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
preview, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
return nil, fmt.Errorf("open.qobuz.com returned status %d: %s", resp.StatusCode, strings.TrimSpace(string(preview)))
}
htmlBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read open.qobuz.com shell: %w", err)
}
scriptMatch := qobuzOpenBundleScriptPattern.FindStringSubmatch(string(htmlBody))
if len(scriptMatch) < 2 {
return nil, fmt.Errorf("qobuz open bundle URL not found")
}
bundleURL := strings.TrimSpace(scriptMatch[1])
if strings.HasPrefix(bundleURL, "/") {
bundleURL = "https://open.qobuz.com" + bundleURL
}
if bundleURL == "" {
return nil, fmt.Errorf("qobuz open bundle URL is empty")
}
bundleReq, err := http.NewRequest(http.MethodGet, bundleURL, nil)
if err != nil {
return nil, err
}
bundleReq.Header.Set("User-Agent", qobuzDefaultUA)
bundleResp, err := client.Do(bundleReq)
if err != nil {
return nil, fmt.Errorf("failed to fetch qobuz open bundle: %w", err)
}
defer bundleResp.Body.Close()
if bundleResp.StatusCode != http.StatusOK {
preview, _ := io.ReadAll(io.LimitReader(bundleResp.Body, 512))
return nil, fmt.Errorf("qobuz open bundle returned status %d: %s", bundleResp.StatusCode, strings.TrimSpace(string(preview)))
}
bundleBody, err := io.ReadAll(bundleResp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read qobuz open bundle: %w", err)
}
configMatch := qobuzOpenAPIConfigPattern.FindStringSubmatch(string(bundleBody))
if len(configMatch) < 3 {
return nil, fmt.Errorf("qobuz api app_id/app_secret pair not found in open bundle")
}
return &qobuzAPICredentials{
AppID: strings.TrimSpace(configMatch[1]),
AppSecret: strings.TrimSpace(configMatch[2]),
Source: bundleURL,
FetchedAtUnix: time.Now().Unix(),
}, nil
}
func qobuzNormalizedPath(path string) string {
return strings.Trim(strings.TrimSpace(path), "/")
}
func qobuzSignaturePayload(path string, params url.Values, timestamp string, secret string) string {
normalizedPath := strings.ReplaceAll(qobuzNormalizedPath(path), "/", "")
keys := make([]string, 0, len(params))
for key := range params {
switch key {
case "app_id", "request_ts", "request_sig":
continue
}
keys = append(keys, key)
}
sort.Strings(keys)
var builder strings.Builder
builder.WriteString(normalizedPath)
for _, key := range keys {
values := params[key]
if len(values) == 0 {
builder.WriteString(key)
continue
}
for _, value := range values {
builder.WriteString(key)
builder.WriteString(value)
}
}
builder.WriteString(timestamp)
builder.WriteString(secret)
return builder.String()
}
func qobuzRequestSignature(path string, params url.Values, timestamp string, secret string) string {
sum := md5.Sum([]byte(qobuzSignaturePayload(path, params, timestamp, secret)))
return hex.EncodeToString(sum[:])
}
func newQobuzSignedRequestWithCredentials(method string, path string, params url.Values, creds *qobuzAPICredentials) (*http.Request, error) {
normalizedPath := qobuzNormalizedPath(path)
if normalizedPath == "" {
return nil, fmt.Errorf("qobuz request path is empty")
}
if creds == nil || strings.TrimSpace(creds.AppID) == "" || strings.TrimSpace(creds.AppSecret) == "" {
return nil, fmt.Errorf("qobuz credentials are incomplete")
}
clonedParams := url.Values{}
for key, values := range params {
for _, value := range values {
clonedParams.Add(key, value)
}
}
timestamp := fmt.Sprintf("%d", time.Now().Unix())
clonedParams.Set("app_id", creds.AppID)
clonedParams.Set("request_ts", timestamp)
clonedParams.Set("request_sig", qobuzRequestSignature(normalizedPath, params, timestamp, creds.AppSecret))
reqURL := fmt.Sprintf("%s/%s?%s", qobuzAPIBaseURL, normalizedPath, clonedParams.Encode())
req, err := http.NewRequest(method, reqURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", qobuzDefaultUA)
req.Header.Set("Accept", "application/json")
req.Header.Set("X-App-Id", creds.AppID)
return req, nil
}
func qobuzCredentialsSupportSignedMetadata(client *http.Client, creds *qobuzAPICredentials) bool {
if creds == nil {
return false
}
req, err := newQobuzSignedRequestWithCredentials(http.MethodGet, "track/search", url.Values{
"query": {qobuzCredentialsProbeTrackISRC},
"limit": {"1"},
}, creds)
if err != nil {
return false
}
resp, err := client.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return false
}
var payload qobuzCredentialProbeResponse
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
return false
}
return payload.Tracks.Total > 0
}
func getQobuzAPICredentials(forceRefresh bool) (*qobuzAPICredentials, error) {
qobuzCredentialsMu.Lock()
defer qobuzCredentialsMu.Unlock()
if !forceRefresh && qobuzCredentialsCacheIsFresh(qobuzCachedCredentials) {
return qobuzCachedCredentials, nil
}
cachedFromDisk, diskErr := loadQobuzCachedCredentials()
if diskErr != nil {
fmt.Printf("Warning: failed to read Qobuz credentials cache: %v\n", diskErr)
}
if !forceRefresh && qobuzCredentialsCacheIsFresh(cachedFromDisk) {
qobuzCachedCredentials = cachedFromDisk
return qobuzCachedCredentials, nil
}
client := &http.Client{Timeout: 30 * time.Second}
scrapedCreds, scrapeErr := scrapeQobuzOpenCredentials(client)
if scrapeErr == nil {
if qobuzCredentialsSupportSignedMetadata(client, scrapedCreds) {
qobuzCachedCredentials = scrapedCreds
if err := saveQobuzCachedCredentials(scrapedCreds); err != nil {
fmt.Printf("Warning: failed to write Qobuz credentials cache: %v\n", err)
}
fmt.Printf("Loaded fresh Qobuz credentials from %s (app_id=%s)\n", scrapedCreds.Source, scrapedCreds.AppID)
return qobuzCachedCredentials, nil
}
scrapeErr = fmt.Errorf("scraped qobuz credentials did not pass validation")
}
if cachedFromDisk != nil {
qobuzCachedCredentials = cachedFromDisk
fmt.Printf("Warning: failed to refresh Qobuz credentials, using cached credentials: %v\n", scrapeErr)
return qobuzCachedCredentials, nil
}
if qobuzCachedCredentials != nil {
fmt.Printf("Warning: failed to refresh Qobuz credentials, using in-memory credentials: %v\n", scrapeErr)
return qobuzCachedCredentials, nil
}
fallback := defaultQobuzAPICredentials()
qobuzCachedCredentials = fallback
if scrapeErr != nil {
fmt.Printf("Warning: failed to refresh Qobuz credentials, using embedded fallback: %v\n", scrapeErr)
}
return qobuzCachedCredentials, nil
}
func qobuzShouldRefreshCredentials(statusCode int) bool {
return statusCode == http.StatusBadRequest || statusCode == http.StatusUnauthorized
}
func newQobuzSignedRequest(method string, path string, params url.Values) (*http.Request, error) {
creds, err := getQobuzAPICredentials(false)
if err != nil {
return nil, err
}
return newQobuzSignedRequestWithCredentials(method, path, params, creds)
}
func doQobuzSignedRequest(method string, path string, params url.Values, client *http.Client) (*http.Response, error) {
if client == nil {
client = &http.Client{Timeout: 20 * time.Second}
}
call := func(forceRefresh bool) (*http.Response, error) {
creds, err := getQobuzAPICredentials(forceRefresh)
if err != nil {
return nil, err
}
req, err := newQobuzSignedRequestWithCredentials(method, path, params, creds)
if err != nil {
return nil, err
}
return client.Do(req)
}
resp, err := call(false)
if err != nil {
return nil, err
}
if qobuzShouldRefreshCredentials(resp.StatusCode) {
resp.Body.Close()
return call(true)
}
return resp, nil
}
func doQobuzSignedJSONRequest(path string, params url.Values, target interface{}) error {
resp, err := doQobuzSignedRequest(http.MethodGet, path, params, &http.Client{Timeout: 20 * time.Second})
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
snippet, _ := io.ReadAll(io.LimitReader(resp.Body, 2048))
return fmt.Errorf("qobuz request failed: HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(snippet)))
}
return json.NewDecoder(resp.Body).Decode(target)
}
+91
View File
@@ -0,0 +1,91 @@
package backend
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
)
const recentFetchesFileName = "recent_fetches.json"
type RecentFetchItem struct {
ID string `json:"id"`
URL string `json:"url"`
Type string `json:"type"`
Name string `json:"name"`
Artist string `json:"artist"`
Image string `json:"image"`
Timestamp int64 `json:"timestamp"`
}
var (
recentFetchesMu sync.Mutex
recentFetchesDirResolver = GetFFmpegDir
)
func recentFetchesFilePath() (string, error) {
baseDir, err := recentFetchesDirResolver()
if err != nil {
return "", err
}
if err := os.MkdirAll(baseDir, 0o755); err != nil {
return "", err
}
return filepath.Join(baseDir, recentFetchesFileName), nil
}
func LoadRecentFetches() ([]RecentFetchItem, error) {
recentFetchesMu.Lock()
defer recentFetchesMu.Unlock()
filePath, err := recentFetchesFilePath()
if err != nil {
return nil, err
}
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
return []RecentFetchItem{}, nil
}
return nil, err
}
if strings.TrimSpace(string(data)) == "" {
return []RecentFetchItem{}, nil
}
var items []RecentFetchItem
if err := json.Unmarshal(data, &items); err != nil {
return nil, err
}
if items == nil {
return []RecentFetchItem{}, nil
}
return items, nil
}
func SaveRecentFetches(items []RecentFetchItem) error {
recentFetchesMu.Lock()
defer recentFetchesMu.Unlock()
filePath, err := recentFetchesFilePath()
if err != nil {
return err
}
if items == nil {
items = []RecentFetchItem{}
}
data, err := json.MarshalIndent(items, "", " ")
if err != nil {
return err
}
return os.WriteFile(filePath, data, 0o644)
}
+85 -21
View File
@@ -47,6 +47,16 @@ type songLinkAPIResponse struct {
} `json:"linksByPlatform"`
}
type qobuzAvailabilityTrack struct {
ID int64 `json:"id"`
Album struct {
ID string `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
RelativeURL string `json:"relative_url"`
} `json:"album"`
}
func NewSongLinkClient() *SongLinkClient {
return &SongLinkClient{
client: &http.Client{
@@ -114,7 +124,7 @@ func (s *SongLinkClient) CheckTrackAvailability(spotifyTrackID string) (*TrackAv
}
if isrc != "" {
availability.Qobuz = checkQobuzAvailability(isrc)
availability.Qobuz, availability.QobuzURL = checkQobuzAvailability(isrc)
}
if availability.Tidal || availability.Amazon || availability.Deezer || availability.Qobuz {
@@ -128,36 +138,90 @@ func (s *SongLinkClient) CheckTrackAvailability(spotifyTrackID string) (*TrackAv
return availability, fmt.Errorf("no platforms found")
}
func checkQobuzAvailability(isrc string) bool {
client := &http.Client{Timeout: 10 * time.Second}
appID := "798273057"
searchURL := fmt.Sprintf(
"https://www.qobuz.com/api.json/0.2/track/search?query=%s&limit=1&app_id=%s",
url.QueryEscape(strings.TrimSpace(isrc)),
appID,
)
resp, err := client.Get(searchURL)
if err != nil {
return false
func qobuzNormalizeRelativeURL(rawURL string) string {
rawURL = strings.TrimSpace(rawURL)
if rawURL == "" {
return ""
}
defer resp.Body.Close()
if strings.HasPrefix(rawURL, "http://") || strings.HasPrefix(rawURL, "https://") {
return rawURL
}
if strings.HasPrefix(rawURL, "/") {
return "https://www.qobuz.com" + rawURL
}
return "https://www.qobuz.com/" + rawURL
}
if resp.StatusCode != http.StatusOK {
return false
func qobuzSlugifySegment(value string) string {
value = strings.ToLower(strings.TrimSpace(value))
if value == "" {
return ""
}
var builder strings.Builder
lastDash := false
for _, r := range value {
switch {
case r >= 'a' && r <= 'z', r >= '0' && r <= '9':
builder.WriteRune(r)
lastDash = false
default:
if !lastDash {
builder.WriteByte('-')
lastDash = true
}
}
}
return strings.Trim(builder.String(), "-")
}
func qobuzAlbumSlugURL(albumTitle string, albumID string) string {
albumID = strings.TrimSpace(albumID)
if albumID == "" {
return ""
}
slug := qobuzSlugifySegment(albumTitle)
if slug == "" {
return fmt.Sprintf("https://www.qobuz.com/album/%s", albumID)
}
return fmt.Sprintf("https://www.qobuz.com/album/%s/%s", slug, albumID)
}
func checkQobuzAvailability(isrc string) (bool, string) {
var searchResp struct {
Tracks struct {
Total int `json:"total"`
Total int `json:"total"`
Items []qobuzAvailabilityTrack `json:"items"`
} `json:"tracks"`
}
if err := json.NewDecoder(resp.Body).Decode(&searchResp); err != nil {
return false
if err := doQobuzSignedJSONRequest("track/search", url.Values{
"query": {strings.TrimSpace(isrc)},
"limit": {"1"},
}, &searchResp); err != nil {
return false, ""
}
return searchResp.Tracks.Total > 0
if searchResp.Tracks.Total == 0 || len(searchResp.Tracks.Items) == 0 {
return false, ""
}
item := searchResp.Tracks.Items[0]
qobuzURL := strings.TrimSpace(item.Album.URL)
if qobuzURL == "" {
qobuzURL = qobuzNormalizeRelativeURL(item.Album.RelativeURL)
}
if qobuzURL == "" {
qobuzURL = qobuzAlbumSlugURL(item.Album.Title, item.Album.ID)
}
if qobuzURL == "" && item.ID > 0 {
qobuzURL = fmt.Sprintf("https://www.qobuz.com/us-en/track/%d", item.ID)
}
return true, qobuzURL
}
func (s *SongLinkClient) GetDeezerURLFromSpotify(spotifyTrackID string) (string, error) {
+1 -18
View File
@@ -15,9 +15,6 @@ import (
"time"
"sort"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
var SpotifyError = errors.New("spotify error")
@@ -40,21 +37,7 @@ func NewSpotifyClient() *SpotifyClient {
}
func (c *SpotifyClient) generateTOTP() (string, int, error) {
secret := "GM3TMMJTGYZTQNZVGM4DINJZHA4TGOBYGMZTCMRTGEYDSMJRHE4TEOBUG4YTCMRUGQ4DQOJUGQYTAMRRGA2TCMJSHE3TCMBY"
version := 61
key, err := otp.NewKeyFromURL(fmt.Sprintf("otpauth://totp/secret?secret=%s", secret))
if err != nil {
return "", 0, err
}
totpCode, err := totp.GenerateCode(key.Secret(), time.Now())
if err != nil {
return "", 0, err
}
return totpCode, version, nil
return generateSpotifyTOTP(time.Now())
}
func (c *SpotifyClient) getAccessToken() error {
-185
View File
@@ -1,185 +0,0 @@
package backend
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"
)
func streamTrackListChunks(ctx context.Context, tracks []AlbumTrackMetadata, callback MetadataCallback) error {
if callback == nil || len(tracks) == 0 {
return nil
}
const chunkSize = 25
for start := 0; start < len(tracks); start += chunkSize {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
end := start + chunkSize
if end > len(tracks) {
end = len(tracks)
}
callback(tracks[start:end])
if end < len(tracks) {
time.Sleep(15 * time.Millisecond)
}
}
return nil
}
func GetSpotifyDataWithAPI(ctx context.Context, spotifyURL string, useAPI bool, apiBaseURL string, batch bool, delay time.Duration, separator string, callback MetadataCallback) (interface{}, error) {
if !useAPI || apiBaseURL == "" {
return GetFilteredSpotifyData(ctx, spotifyURL, batch, delay, separator, callback)
}
spotifyType, id := parseSpotifyURLToTypeAndID(spotifyURL)
if spotifyType == "" || id == "" {
return nil, fmt.Errorf("invalid Spotify URL: %s", spotifyURL)
}
if spotifyType == "artist" {
return GetFilteredSpotifyData(ctx, spotifyURL, batch, delay, separator, callback)
}
apiURL := fmt.Sprintf("%s/%s/%s", strings.TrimSuffix(apiBaseURL, "/"), spotifyType, id)
req, err := http.NewRequestWithContext(ctx, "GET", apiURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create API request: %w", err)
}
client := &http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("SpotFetch API request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("SpotFetch API error: HTTP %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read API response: %w", err)
}
var data interface{}
switch spotifyType {
case "track":
var trackResp TrackResponse
if err := json.Unmarshal(bodyBytes, &trackResp); err != nil {
return nil, fmt.Errorf("failed to decode track response: %w", err)
}
data = trackResp
case "album":
var albumResp AlbumResponsePayload
if err := json.Unmarshal(bodyBytes, &albumResp); err != nil {
return nil, fmt.Errorf("failed to decode album response: %w", err)
}
data = &albumResp
if callback != nil {
callback(&AlbumResponsePayload{
AlbumInfo: albumResp.AlbumInfo,
TrackList: []AlbumTrackMetadata{},
})
if err := streamTrackListChunks(ctx, albumResp.TrackList, callback); err != nil {
return nil, err
}
}
case "playlist":
var playlistResp PlaylistResponsePayload
if err := json.Unmarshal(bodyBytes, &playlistResp); err != nil {
return nil, fmt.Errorf("failed to decode playlist response: %w", err)
}
data = playlistResp
if callback != nil {
callback(PlaylistResponsePayload{
PlaylistInfo: playlistResp.PlaylistInfo,
TrackList: []AlbumTrackMetadata{},
})
if err := streamTrackListChunks(ctx, playlistResp.TrackList, callback); err != nil {
return nil, err
}
}
case "artist":
var artistResp ArtistDiscographyPayload
if err := json.Unmarshal(bodyBytes, &artistResp); err != nil {
return nil, fmt.Errorf("failed to decode artist response: %w", err)
}
data = &artistResp
if callback != nil {
callback(&ArtistDiscographyPayload{
ArtistInfo: artistResp.ArtistInfo,
AlbumList: artistResp.AlbumList,
TrackList: []AlbumTrackMetadata{},
})
if err := streamTrackListChunks(ctx, artistResp.TrackList, callback); err != nil {
return nil, err
}
}
default:
return nil, fmt.Errorf("unsupported Spotify type: %s", spotifyType)
}
if callback != nil {
switch payload := data.(type) {
case TrackResponse:
t := payload.Track
callback([]AlbumTrackMetadata{{
SpotifyID: t.SpotifyID,
Artists: t.Artists,
Name: t.Name,
AlbumName: t.AlbumName,
AlbumArtist: t.AlbumArtist,
DurationMS: t.DurationMS,
Images: t.Images,
ReleaseDate: t.ReleaseDate,
TrackNumber: t.TrackNumber,
TotalTracks: t.TotalTracks,
DiscNumber: t.DiscNumber,
TotalDiscs: t.TotalDiscs,
ExternalURL: t.ExternalURL,
Plays: t.Plays,
PreviewURL: t.PreviewURL,
IsExplicit: t.IsExplicit,
}})
}
}
return data, nil
}
func parseSpotifyURLToTypeAndID(url string) (string, string) {
if strings.HasPrefix(url, "spotify:") {
parts := strings.Split(url, ":")
if len(parts) >= 3 {
return parts[1], parts[2]
}
}
re := regexp.MustCompile(`spotify\.com/(track|album|playlist|artist)/([a-zA-Z0-9]+)`)
matches := re.FindStringSubmatch(url)
if len(matches) == 3 {
return matches[1], matches[2]
}
return "", ""
}
+215 -27
View File
@@ -33,24 +33,31 @@ func NewSpotifyMetadataClient() *SpotifyMetadataClient {
}
type TrackMetadata struct {
SpotifyID string `json:"spotify_id,omitempty"`
Artists string `json:"artists"`
Name string `json:"name"`
AlbumName string `json:"album_name"`
AlbumArtist string `json:"album_artist,omitempty"`
DurationMS int `json:"duration_ms"`
Images string `json:"images"`
ReleaseDate string `json:"release_date"`
TrackNumber int `json:"track_number"`
TotalTracks int `json:"total_tracks,omitempty"`
DiscNumber int `json:"disc_number,omitempty"`
TotalDiscs int `json:"total_discs,omitempty"`
ExternalURL string `json:"external_urls"`
Copyright string `json:"copyright,omitempty"`
Publisher string `json:"publisher,omitempty"`
Plays string `json:"plays,omitempty"`
PreviewURL string `json:"preview_url,omitempty"`
IsExplicit bool `json:"is_explicit,omitempty"`
SpotifyID string `json:"spotify_id,omitempty"`
Artists string `json:"artists"`
Name string `json:"name"`
AlbumName string `json:"album_name"`
AlbumArtist string `json:"album_artist,omitempty"`
DurationMS int `json:"duration_ms"`
Images string `json:"images"`
ReleaseDate string `json:"release_date"`
TrackNumber int `json:"track_number"`
TotalTracks int `json:"total_tracks,omitempty"`
DiscNumber int `json:"disc_number,omitempty"`
TotalDiscs int `json:"total_discs,omitempty"`
ExternalURL string `json:"external_urls"`
AlbumID string `json:"album_id,omitempty"`
AlbumURL string `json:"album_url,omitempty"`
ArtistID string `json:"artist_id,omitempty"`
ArtistURL string `json:"artist_url,omitempty"`
ArtistsData []ArtistSimple `json:"artists_data,omitempty"`
UPC string `json:"upc,omitempty"`
Copyright string `json:"copyright,omitempty"`
Publisher string `json:"publisher,omitempty"`
Composer string `json:"composer,omitempty"`
Plays string `json:"plays,omitempty"`
PreviewURL string `json:"preview_url,omitempty"`
IsExplicit bool `json:"is_explicit,omitempty"`
}
type ArtistSimple struct {
@@ -79,6 +86,7 @@ type AlbumTrackMetadata struct {
ArtistID string `json:"artist_id,omitempty"`
ArtistURL string `json:"artist_url,omitempty"`
ArtistsData []ArtistSimple `json:"artists_data,omitempty"`
UPC string `json:"upc,omitempty"`
Plays string `json:"plays,omitempty"`
Status string `json:"status,omitempty"`
PreviewURL string `json:"preview_url,omitempty"`
@@ -95,6 +103,7 @@ type AlbumInfoMetadata struct {
ReleaseDate string `json:"release_date"`
Artists string `json:"artists"`
Images string `json:"images"`
UPC string `json:"upc,omitempty"`
Batch string `json:"batch,omitempty"`
ArtistID string `json:"artist_id,omitempty"`
ArtistURL string `json:"artist_url,omitempty"`
@@ -179,15 +188,18 @@ type spotifyURI struct {
}
type apiTrackResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Artists string `json:"artists"`
Duration string `json:"duration"`
Track int `json:"track"`
Disc int `json:"disc"`
Discs int `json:"discs"`
Copyright string `json:"copyright"`
Plays string `json:"plays"`
ID string `json:"id"`
Name string `json:"name"`
Artists string `json:"artists"`
ArtistIds []string `json:"artistIds,omitempty"`
UPC string `json:"upc,omitempty"`
Duration string `json:"duration"`
Track int `json:"track"`
Disc int `json:"disc"`
Discs int `json:"discs"`
Copyright string `json:"copyright"`
Composer string `json:"composer,omitempty"`
Plays string `json:"plays"`
Album struct {
ID string `json:"id"`
Name string `json:"name"`
@@ -211,6 +223,7 @@ type apiAlbumResponse struct {
Artists string `json:"artists"`
Cover string `json:"cover"`
ReleaseDate string `json:"releaseDate"`
UPC string `json:"upc,omitempty"`
Count int `json:"count"`
Label string `json:"label"`
Discs struct {
@@ -223,6 +236,7 @@ type apiAlbumResponse struct {
ArtistIds []string `json:"artistIds"`
Duration string `json:"duration"`
Plays string `json:"plays"`
UPC string `json:"upc,omitempty"`
IsExplicit bool `json:"is_explicit"`
DiscNumber int `json:"disc_number"`
} `json:"tracks"`
@@ -250,6 +264,7 @@ type apiPlaylistResponse struct {
Album string `json:"album"`
AlbumArtist string `json:"albumArtist"`
AlbumID string `json:"albumId"`
UPC string `json:"upc,omitempty"`
Duration string `json:"duration"`
IsExplicit bool `json:"is_explicit"`
DiscNumber int `json:"disc_number"`
@@ -490,6 +505,10 @@ func (c *SpotifyMetadataClient) fetchTrack(ctx context.Context, trackID string)
}
filteredData := FilterTrack(data, c.Separator, albumFetchData)
composer, composerErr := c.fetchTrackComposerWithClient(ctx, client, trackID)
if composerErr == nil && composer != "" {
filteredData["composer"] = composer
}
jsonData, err := json.Marshal(filteredData)
if err != nil {
@@ -501,9 +520,100 @@ func (c *SpotifyMetadataClient) fetchTrack(ctx context.Context, trackID string)
return nil, fmt.Errorf("failed to unmarshal to apiTrackResponse: %w", err)
}
if result.ID != "" {
if identifiers, err := GetSpotifyTrackIdentifiersDirect(result.ID); err == nil || identifiers.UPC != "" {
if identifiers.UPC != "" {
result.UPC = identifiers.UPC
}
}
}
return &result, nil
}
func collectTrackCreditNamesByRole(items []interface{}, role string) []string {
role = strings.TrimSpace(role)
if role == "" || len(items) == 0 {
return nil
}
seen := make(map[string]struct{}, len(items))
names := make([]string, 0, len(items))
for _, item := range items {
itemMap, ok := item.(map[string]interface{})
if !ok {
continue
}
if !strings.EqualFold(strings.TrimSpace(getString(itemMap, "role")), role) {
continue
}
name := strings.TrimSpace(getString(itemMap, "name"))
if name == "" {
continue
}
if _, exists := seen[name]; exists {
continue
}
seen[name] = struct{}{}
names = append(names, name)
}
return names
}
func (c *SpotifyMetadataClient) fetchTrackComposerWithClient(ctx context.Context, client *SpotifyClient, trackID string) (string, error) {
_ = ctx
payload := map[string]interface{}{
"variables": map[string]interface{}{
"trackUri": fmt.Sprintf("spotify:track:%s", trackID),
"contributorsLimit": 100,
"contributorsOffset": 0,
},
"operationName": "queryTrackCreditsModal",
"extensions": map[string]interface{}{
"persistedQuery": map[string]interface{}{
"version": 1,
"sha256Hash": "e2ca40d46cf1fde36562261ccec754f23fb31b561877252e9fe0d6834aabb84b",
},
},
}
data, err := client.Query(payload)
if err != nil {
return "", fmt.Errorf("failed to query track credits: %w", err)
}
creditItems := getSlice(
getMap(
getMap(
getMap(
getMap(data, "data"),
"trackUnion",
),
"creditsTrait",
),
"contributors",
),
"items",
)
composerNames := collectTrackCreditNamesByRole(creditItems, "Composer")
if len(composerNames) == 0 {
return "", nil
}
separator := strings.TrimSpace(c.Separator)
if separator == "" {
separator = ", "
}
return strings.Join(composerNames, separator), nil
}
func (c *SpotifyMetadataClient) fetchAlbum(ctx context.Context, albumID string, callback MetadataCallback) (*apiAlbumResponse, error) {
client := NewSpotifyClient()
if err := client.Initialize(); err != nil {
@@ -607,6 +717,17 @@ func (c *SpotifyMetadataClient) fetchAlbumWithClient(ctx context.Context, client
return nil, fmt.Errorf("failed to unmarshal to apiAlbumResponse: %w", err)
}
if result.ID != "" {
if upc, err := lookupSpotifyAlbumUPC(result.ID); err == nil && strings.TrimSpace(upc) != "" {
result.UPC = upc
for i := range result.Tracks {
if strings.TrimSpace(result.Tracks[i].UPC) == "" {
result.Tracks[i].UPC = upc
}
}
}
}
return &result, nil
}
@@ -895,6 +1016,34 @@ func (c *SpotifyMetadataClient) formatTrackData(raw *apiTrackResponse) TrackResp
durationMS := parseDuration(raw.Duration)
externalURL := fmt.Sprintf("https://open.spotify.com/track/%s", raw.ID)
albumID := strings.TrimSpace(raw.Album.ID)
albumURL := ""
if albumID != "" {
albumURL = fmt.Sprintf("https://open.spotify.com/album/%s", albumID)
}
artistID := ""
artistURL := ""
artistsData := make([]ArtistSimple, 0, len(raw.ArtistIds))
for index, id := range raw.ArtistIds {
trimmedID := strings.TrimSpace(id)
if trimmedID == "" {
continue
}
if artistID == "" {
artistID = trimmedID
artistURL = fmt.Sprintf("https://open.spotify.com/artist/%s", trimmedID)
}
artistName := ""
artistNames := splitAndCleanArtists(raw.Artists)
if index < len(artistNames) {
artistName = artistNames[index]
}
artistsData = append(artistsData, ArtistSimple{
ID: trimmedID,
Name: artistName,
ExternalURL: fmt.Sprintf("https://open.spotify.com/artist/%s", trimmedID),
})
}
coverURL := raw.Cover.Small
if coverURL == "" {
@@ -922,8 +1071,15 @@ func (c *SpotifyMetadataClient) formatTrackData(raw *apiTrackResponse) TrackResp
DiscNumber: raw.Disc,
TotalDiscs: raw.Discs,
ExternalURL: externalURL,
AlbumID: albumID,
AlbumURL: albumURL,
ArtistID: artistID,
ArtistURL: artistURL,
ArtistsData: artistsData,
UPC: raw.UPC,
Copyright: raw.Copyright,
Publisher: raw.Album.Label,
Composer: raw.Composer,
Plays: raw.Plays,
IsExplicit: raw.IsExplicit,
}
@@ -935,6 +1091,18 @@ func (c *SpotifyMetadataClient) formatTrackData(raw *apiTrackResponse) TrackResp
func (c *SpotifyMetadataClient) formatAlbumData(raw *apiAlbumResponse, callback MetadataCallback) (*AlbumResponsePayload, error) {
var artistID, artistURL string
for _, item := range raw.Tracks {
if len(item.ArtistIds) == 0 {
continue
}
candidate := strings.TrimSpace(item.ArtistIds[0])
if candidate == "" {
continue
}
artistID = candidate
artistURL = fmt.Sprintf("https://open.spotify.com/artist/%s", candidate)
break
}
info := AlbumInfoMetadata{
TotalTracks: raw.Count,
@@ -942,6 +1110,7 @@ func (c *SpotifyMetadataClient) formatAlbumData(raw *apiAlbumResponse, callback
ReleaseDate: raw.ReleaseDate,
Artists: raw.Artists,
Images: raw.Cover,
UPC: raw.UPC,
ArtistID: artistID,
ArtistURL: artistURL,
}
@@ -957,6 +1126,10 @@ func (c *SpotifyMetadataClient) formatAlbumData(raw *apiAlbumResponse, callback
for idx, item := range raw.Tracks {
durationMS := parseDuration(item.Duration)
trackNumber := idx + 1
trackUPC := strings.TrimSpace(item.UPC)
if trackUPC == "" {
trackUPC = strings.TrimSpace(raw.UPC)
}
var artistID, artistURL string
if len(item.ArtistIds) > 0 {
@@ -992,6 +1165,7 @@ func (c *SpotifyMetadataClient) formatAlbumData(raw *apiAlbumResponse, callback
ArtistID: artistID,
ArtistURL: artistURL,
ArtistsData: artistsData,
UPC: trackUPC,
Plays: item.Plays,
IsExplicit: item.IsExplicit,
})
@@ -1062,6 +1236,7 @@ func (c *SpotifyMetadataClient) formatPlaylistData(raw *apiPlaylistResponse, cal
ArtistID: artistID,
ArtistURL: artistURL,
ArtistsData: artistsData,
UPC: item.UPC,
Plays: item.Plays,
Status: item.Status,
IsExplicit: item.IsExplicit,
@@ -1188,6 +1363,7 @@ func (c *SpotifyMetadataClient) formatArtistDiscographyData(ctx context.Context,
TrackNumber: trackNumber,
TotalTracks: albumData.Count,
DiscNumber: tr.DiscNumber,
UPC: tr.UPC,
ExternalURL: fmt.Sprintf("https://open.spotify.com/track/%s", tr.ID),
AlbumID: albumID,
AlbumURL: fmt.Sprintf("https://open.spotify.com/album/%s", albumID),
@@ -1321,6 +1497,18 @@ func parseArtistIDsFromString(artists string) []string {
return []string{}
}
func splitAndCleanArtists(artists string) []string {
raw := regexp.MustCompile(`\s*[;,]\s*`).Split(strings.TrimSpace(artists), -1)
parts := make([]string, 0, len(raw))
for _, part := range raw {
part = strings.TrimSpace(part)
if part != "" {
parts = append(parts, part)
}
}
return parts
}
func (c *SpotifyMetadataClient) Search(ctx context.Context, query string, limit int) (*SearchResponse, error) {
if query == "" {
return nil, errors.New("search query cannot be empty")
+28
View File
@@ -0,0 +1,28 @@
package backend
import (
"fmt"
"time"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
const (
spotifyTOTPSecret = "GM3TMMJTGYZTQNZVGM4DINJZHA4TGOBYGMZTCMRTGEYDSMJRHE4TEOBUG4YTCMRUGQ4DQOJUGQYTAMRRGA2TCMJSHE3TCMBY"
spotifyTOTPVersion = 61
)
func generateSpotifyTOTP(now time.Time) (string, int, error) {
key, err := otp.NewKeyFromURL(fmt.Sprintf("otpauth://totp/secret?secret=%s", spotifyTOTPSecret))
if err != nil {
return "", 0, err
}
code, err := totp.GenerateCode(key.Secret(), now)
if err != nil {
return "", 0, err
}
return code, spotifyTOTPVersion, nil
}
+50 -27
View File
@@ -416,7 +416,7 @@ func (t *TidalDownloader) DownloadFromManifest(manifestB64, outputPath string) e
return nil
}
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, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
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)
@@ -449,11 +449,12 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo
trackTitleForFile := sanitizeFilename(trackTitle)
albumTitleForFile := sanitizeFilename(albumTitle)
filename := buildTidalFilename(trackTitleForFile, artistNameForFile, albumTitleForFile, albumArtistForFile, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber)
filename := buildTidalFilename(trackTitleForFile, artistNameForFile, albumTitleForFile, albumArtistForFile, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber, isrcOverride)
outputFilename := filepath.Join(outputDir, filename)
if fileInfo, err := os.Stat(outputFilename); err == nil && fileInfo.Size() > 0 {
fmt.Printf("File already exists: %s (%.2f MB)\n", outputFilename, float64(fileInfo.Size())/(1024*1024))
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
}
@@ -492,12 +493,16 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo
}
res.ISRC = isrc
if isrc != "" {
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")
if ShouldSkipMusicBrainzMetadataFetch() {
fmt.Println("Skipping MusicBrainz metadata fetch because status check is offline.")
} else {
fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err)
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
@@ -511,11 +516,13 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo
return "", err
}
var isrc string
isrc := strings.TrimSpace(isrcOverride)
var mbMeta Metadata
if spotifyURL != "" {
result := <-metaChan
isrc = result.ISRC
if isrc == "" {
isrc = result.ISRC
}
mbMeta = result.Metadata
}
@@ -554,7 +561,9 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo
Comment: spotifyURL,
Copyright: spotifyCopyright,
Publisher: spotifyPublisher,
Description: "https://github.com/afkarxyz/SpotiFLAC",
Composer: spotifyComposer,
Separator: metadataSeparator,
Description: "https://github.com/spotbye/SpotiFLAC",
ISRC: isrc,
Genre: mbMeta.Genre,
}
@@ -570,7 +579,7 @@ func (t *TidalDownloader) DownloadByURL(tidalURL, outputDir, quality, filenameFo
return outputFilename, nil
}
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, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
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)
@@ -608,11 +617,12 @@ func (t *TidalDownloader) DownloadByURLWithFallback(tidalURL, outputDir, quality
trackTitleForFile := sanitizeFilename(trackTitle)
albumTitleForFile := sanitizeFilename(albumTitle)
filename := buildTidalFilename(trackTitleForFile, artistNameForFile, albumTitleForFile, albumArtistForFile, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber)
filename := buildTidalFilename(trackTitleForFile, artistNameForFile, albumTitleForFile, albumArtistForFile, spotifyReleaseDate, spotifyTrackNumber, spotifyDiscNumber, filenameFormat, includeTrackNumber, position, useAlbumTrackNumber, isrcOverride)
outputFilename := filepath.Join(outputDir, filename)
if fileInfo, err := os.Stat(outputFilename); err == nil && fileInfo.Size() > 0 {
fmt.Printf("File already exists: %s (%.2f MB)\n", outputFilename, float64(fileInfo.Size())/(1024*1024))
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
}
@@ -651,12 +661,16 @@ func (t *TidalDownloader) DownloadByURLWithFallback(tidalURL, outputDir, quality
}
res.ISRC = isrc
if isrc != "" {
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")
if ShouldSkipMusicBrainzMetadataFetch() {
fmt.Println("Skipping MusicBrainz metadata fetch because status check is offline.")
} else {
fmt.Printf("Warning: Failed to fetch MusicBrainz metadata: %v\n", err)
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
@@ -671,11 +685,13 @@ func (t *TidalDownloader) DownloadByURLWithFallback(tidalURL, outputDir, quality
return "", err
}
var isrc string
isrc := strings.TrimSpace(isrcOverride)
var mbMeta Metadata
if spotifyURL != "" {
result := <-metaChan
isrc = result.ISRC
if isrc == "" {
isrc = result.ISRC
}
mbMeta = result.Metadata
}
@@ -714,7 +730,9 @@ func (t *TidalDownloader) DownloadByURLWithFallback(tidalURL, outputDir, quality
Comment: spotifyURL,
Copyright: spotifyCopyright,
Publisher: spotifyPublisher,
Description: "https://github.com/afkarxyz/SpotiFLAC",
Composer: spotifyComposer,
Separator: metadataSeparator,
Description: "https://github.com/spotbye/SpotiFLAC",
ISRC: isrc,
Genre: mbMeta.Genre,
}
@@ -730,14 +748,14 @@ func (t *TidalDownloader) DownloadByURLWithFallback(tidalURL, outputDir, quality
return outputFilename, nil
}
func (t *TidalDownloader) Download(spotifyTrackID, 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, spotifyURL string, allowFallback bool, useFirstArtistOnly bool, useSingleGenre bool, embedGenre bool) (string, error) {
func (t *TidalDownloader) Download(spotifyTrackID, 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) {
tidalURL, err := t.GetTidalURLFromSpotify(spotifyTrackID)
if err != nil {
return "", fmt.Errorf("songlink 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, spotifyURL, allowFallback, useFirstArtistOnly, useSingleGenre, embedGenre)
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)
}
type SegmentTemplate struct {
@@ -977,8 +995,12 @@ func getDownloadURLRotated(apis []string, trackID int64, quality string) (string
return "", "", fmt.Errorf("all %d APIs failed. Last error: %v", len(apis), lastError)
}
func buildTidalFilename(title, artist, album, albumArtist, releaseDate string, trackNumber, discNumber int, format string, includeTrackNumber bool, position int, useAlbumTrackNumber bool) string {
func buildTidalFilename(title, artist, album, albumArtist, releaseDate string, trackNumber, discNumber int, format string, includeTrackNumber bool, position int, useAlbumTrackNumber bool, extra ...string) string {
var filename string
isrc := ""
if len(extra) > 0 {
isrc = SanitizeOptionalFilename(extra[0])
}
numberToUse := position
if useAlbumTrackNumber && trackNumber > 0 {
@@ -998,6 +1020,7 @@ func buildTidalFilename(title, artist, album, albumArtist, releaseDate string, t
filename = strings.ReplaceAll(filename, "{album_artist}", albumArtist)
filename = strings.ReplaceAll(filename, "{year}", year)
filename = strings.ReplaceAll(filename, "{date}", SanitizeFilename(releaseDate))
filename = strings.ReplaceAll(filename, "{isrc}", isrc)
if discNumber > 0 {
filename = strings.ReplaceAll(filename, "{disc}", fmt.Sprintf("%d", discNumber))
+50
View File
@@ -0,0 +1,50 @@
package backend
import "strings"
const preferredUPCTagKey = "UPC"
var ffprobeUPCTagKeys = []string{
"upc",
"barcode",
"wm/upc",
"txxx:upc",
"txxx:barcode",
"txxx/upc",
"txxx/barcode",
"----:com.apple.itunes:upc",
"----:com.apple.itunes:barcode",
}
func assignPreferredUPC(current *string, incoming string, preferred bool) {
incoming = strings.TrimSpace(incoming)
if incoming == "" {
return
}
if preferred || strings.TrimSpace(*current) == "" {
*current = incoming
}
}
func classifyUPCDescription(description string) (matched bool, preferred bool) {
switch strings.ToUpper(strings.TrimSpace(description)) {
case preferredUPCTagKey:
return true, true
case "BARCODE":
return true, false
default:
return false, false
}
}
func firstPreferredFFprobeUPCValue(tags map[string]string) string {
for _, key := range ffprobeUPCTagKeys {
value := strings.TrimSpace(tags[key])
if value != "" {
return value
}
}
return ""
}