98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
package ripsort
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.kapelle.org/niklas/ripsort/internal/clients/musicbrainz"
|
|
"git.kapelle.org/niklas/ripsort/internal/metadata"
|
|
)
|
|
|
|
func Scan(filePath string) error {
|
|
info, err := metadata.ReadAudioTags(filePath)
|
|
|
|
if err != nil {
|
|
slog.Error("Reading metadata", "file", filePath, "err", err)
|
|
return err
|
|
}
|
|
|
|
mbc := musicbrainz.NewMusicBrainzClient()
|
|
|
|
genre, err := metadata.SearchForGenre(mbc, info)
|
|
if err != nil {
|
|
slog.Error("Failed to search for genere", "err", err)
|
|
return err
|
|
}
|
|
|
|
if info.Title != nil {
|
|
fmt.Printf("Title: %s\n", *info.Title)
|
|
}
|
|
fmt.Printf("Artist: %s\n", info.Artist)
|
|
if info.Album != nil {
|
|
fmt.Printf("Album: %s\n", *info.Album)
|
|
}
|
|
fmt.Printf("Album Artist: %s\n", info.AlbumArtist)
|
|
fmt.Printf("Track: %d/%d\n", info.Track, info.TotalTracks)
|
|
if info.ISRC != nil {
|
|
fmt.Printf("ISRC: %s\n", *info.ISRC)
|
|
}
|
|
if info.Comment != nil {
|
|
fmt.Printf("Comment: %s\n", *info.Comment)
|
|
}
|
|
fmt.Printf("Genre: %s\n", genre)
|
|
|
|
sortPath := pathForFile(filePath, *info)
|
|
fmt.Printf("Sort path: %s\n", sortPath)
|
|
|
|
return nil
|
|
}
|
|
|
|
func Sort(dst, path string) error {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
slog.Error("Failed to stat path", "file", path, "err", err)
|
|
return err
|
|
}
|
|
|
|
// Handle single file
|
|
if !info.IsDir() {
|
|
if !fileSupported(path) {
|
|
slog.Error("Unsupported file format", "file", path)
|
|
return err
|
|
}
|
|
if err := sortSong(path, dst, true); err != nil {
|
|
slog.Error("Failed to sort single file", "file", path, "err", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Handle directory
|
|
err = filepath.WalkDir(path, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
slog.Error("Failed to walk path", "path", path, "file", p, "err", err)
|
|
return err
|
|
}
|
|
|
|
if !d.IsDir() {
|
|
if !fileSupported(p) {
|
|
return nil
|
|
}
|
|
if err := sortSong(p, dst, true); err != nil {
|
|
slog.Error("Failed to sort file", "file", p, "err", err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|