feat(subsonic): also read unsynced lyrics from .txt files and tag metadata
closes #593
This commit is contained in:
No files matched your search
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -512,30 +513,37 @@ func (c *Controller) ServeGetLyrics(r *http.Request) *spec.Response {
|
|||||||
|
|
||||||
sub := spec.NewResponse()
|
sub := spec.NewResponse()
|
||||||
|
|
||||||
if track.TagLyrics != "" {
|
|
||||||
sub.Lyrics = &spec.Lyrics{
|
|
||||||
Value: track.TagLyrics,
|
|
||||||
Artist: track.TagTrackArtist,
|
|
||||||
Title: track.TagTitle,
|
|
||||||
}
|
|
||||||
return sub
|
|
||||||
}
|
|
||||||
|
|
||||||
_, text, err := lyricsFile(track)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return sub
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return spec.NewError(0, "lyricsFile: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
contents := strings.Join(text, "\n")
|
|
||||||
|
|
||||||
sub.Lyrics = &spec.Lyrics{
|
sub.Lyrics = &spec.Lyrics{
|
||||||
Value: contents,
|
|
||||||
Artist: track.TagTrackArtist,
|
Artist: track.TagTrackArtist,
|
||||||
Title: track.TagTitle,
|
Title: track.TagTitle,
|
||||||
|
Value: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lyrics, err := lyricsAll(track)
|
||||||
|
if err != nil {
|
||||||
|
return spec.NewError(0, "get lyrics: %v", err)
|
||||||
|
}
|
||||||
|
if len(lyrics) == 0 {
|
||||||
|
return sub
|
||||||
|
}
|
||||||
|
|
||||||
|
// prefer unsynced for this endpoint
|
||||||
|
slices.SortFunc(lyrics, func(a, b *spec.StructuredLyrics) int {
|
||||||
|
if a.Synced {
|
||||||
|
return +1
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
})
|
||||||
|
|
||||||
|
lyric := lyrics[0]
|
||||||
|
|
||||||
|
var lines []string
|
||||||
|
for _, l := range lyric.Lines {
|
||||||
|
lines = append(lines, l.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
sub.Lyrics.Value = strings.Join(lines, "\n")
|
||||||
|
|
||||||
return sub
|
return sub
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -562,81 +570,165 @@ func (c *Controller) ServeGetLyricsBySongID(r *http.Request) *spec.Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub := spec.NewResponse()
|
sub := spec.NewResponse()
|
||||||
|
sub.LyricsList = &spec.LyricsList{
|
||||||
|
StructuredLyrics: []*spec.StructuredLyrics{},
|
||||||
|
}
|
||||||
|
|
||||||
if track.TagLyrics != "" {
|
structuredLyrics, err := lyricsAll(track)
|
||||||
times, lrc, err := lrc.ParseString(track.TagLyrics)
|
if err != nil {
|
||||||
if err != nil {
|
return spec.NewError(0, "get lyrics: %v", err)
|
||||||
return spec.NewError(0, "lyricsFile: %v", err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
sub.LyricsList.StructuredLyrics = structuredLyrics
|
||||||
|
|
||||||
|
return sub
|
||||||
|
}
|
||||||
|
|
||||||
|
func lyricsAll(track db.Track) ([]*spec.StructuredLyrics, error) {
|
||||||
|
var r []*spec.StructuredLyrics
|
||||||
|
|
||||||
|
fromTags, err := lyricsFromTags(track)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("from tags: %w", err)
|
||||||
|
}
|
||||||
|
if fromTags != nil {
|
||||||
|
r = append(r, fromTags)
|
||||||
|
}
|
||||||
|
|
||||||
|
fromFile, err := lyricsFromFile(track)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("from file: %w", err)
|
||||||
|
}
|
||||||
|
if fromFile != nil {
|
||||||
|
r = append(r, fromFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
fromFileUnsynced, err := lyricsFromFileUnsynced(track)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("from file unsynced: %w", err)
|
||||||
|
}
|
||||||
|
if fromFileUnsynced != nil {
|
||||||
|
r = append(r, fromFileUnsynced)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func lyricsFromTags(track db.Track) (*spec.StructuredLyrics, error) {
|
||||||
|
if track.TagLyrics == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r := spec.StructuredLyrics{
|
||||||
|
Lang: "xxx",
|
||||||
|
DisplayArtist: track.TagTrackArtist,
|
||||||
|
DisplayTitle: track.TagTitle,
|
||||||
|
}
|
||||||
|
|
||||||
|
times, lrc, err := lrc.ParseString(track.TagLyrics)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(times) > 0 {
|
||||||
lines := make([]spec.Lyric, len(times))
|
lines := make([]spec.Lyric, len(times))
|
||||||
for i, time := range times {
|
for i, time := range times {
|
||||||
|
start := time.Milliseconds()
|
||||||
lines[i] = spec.Lyric{
|
lines[i] = spec.Lyric{
|
||||||
Start: time.Milliseconds(),
|
Start: &start,
|
||||||
Value: strings.TrimSpace(lrc[i]),
|
Value: strings.TrimSpace(lrc[i]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
structured := spec.StructuredLyrics{
|
r.Synced = true
|
||||||
Lang: "xxx",
|
r.Lines = lines
|
||||||
Synced: true,
|
|
||||||
Lines: lines,
|
|
||||||
DisplayArtist: track.TagTrackArtist,
|
|
||||||
DisplayTitle: track.TagTitle,
|
|
||||||
Offset: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
sub.LyricsList = &spec.LyricsList{
|
return &r, nil
|
||||||
StructuredLyrics: []spec.StructuredLyrics{structured},
|
|
||||||
}
|
|
||||||
return sub
|
|
||||||
}
|
}
|
||||||
|
|
||||||
times, lrc, err := lyricsFile(track)
|
rawLines := strings.Split(track.TagLyrics, "\n")
|
||||||
|
|
||||||
|
lines := make([]spec.Lyric, 0, len(rawLines))
|
||||||
|
for _, line := range rawLines {
|
||||||
|
lines = append(lines, spec.Lyric{
|
||||||
|
Value: strings.TrimSpace(line),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Lines = lines
|
||||||
|
|
||||||
|
return &r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func lyricsFromFile(track db.Track) (*spec.StructuredLyrics, error) {
|
||||||
|
filePath := track.AbsPath()
|
||||||
|
fileDir := filepath.Dir(filePath)
|
||||||
|
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
|
||||||
|
|
||||||
|
lrcContent, err := os.ReadFile(filepath.Join(fileDir, fileName+".lrc"))
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
return nil, err
|
||||||
sub.LyricsList = &spec.LyricsList{
|
}
|
||||||
StructuredLyrics: []spec.StructuredLyrics{},
|
|
||||||
}
|
times, lrc, err := lrc.Parse(lrcContent)
|
||||||
return sub
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
return spec.NewError(0, "lyricsFile: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := make([]spec.Lyric, len(times))
|
lines := make([]spec.Lyric, len(times))
|
||||||
for i, time := range times {
|
for i, time := range times {
|
||||||
|
start := time.Milliseconds()
|
||||||
lines[i] = spec.Lyric{
|
lines[i] = spec.Lyric{
|
||||||
Start: time.Milliseconds(),
|
Start: &start,
|
||||||
Value: strings.TrimSpace(lrc[i]),
|
Value: strings.TrimSpace(lrc[i]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
structured := spec.StructuredLyrics{
|
r := spec.StructuredLyrics{
|
||||||
Lang: "xxx",
|
Lang: "xxx",
|
||||||
Synced: true,
|
Synced: true,
|
||||||
Lines: lines,
|
Lines: lines,
|
||||||
DisplayArtist: track.TagTrackArtist,
|
DisplayArtist: track.TagTrackArtist,
|
||||||
DisplayTitle: track.TagTitle,
|
DisplayTitle: track.TagTitle,
|
||||||
Offset: 0,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub.LyricsList = &spec.LyricsList{
|
return &r, nil
|
||||||
StructuredLyrics: []spec.StructuredLyrics{structured},
|
|
||||||
}
|
|
||||||
return sub
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func lyricsFile(file db.Track) ([]lrc.Duration, []lrc.Text, error) {
|
func lyricsFromFileUnsynced(track db.Track) (*spec.StructuredLyrics, error) {
|
||||||
filePath := file.AbsPath()
|
filePath := track.AbsPath()
|
||||||
fileDir := filepath.Dir(filePath)
|
fileDir := filepath.Dir(filePath)
|
||||||
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
|
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))
|
||||||
|
|
||||||
lrcContent, err := os.ReadFile(filepath.Join(fileDir, fileName+".lrc"))
|
text, err := os.ReadFile(filepath.Join(fileDir, fileName+".txt"))
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []lrc.Duration{}, []lrc.Text{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return lrc.Parse(lrcContent)
|
r := spec.StructuredLyrics{
|
||||||
|
Lang: "xxx",
|
||||||
|
DisplayArtist: track.TagTrackArtist,
|
||||||
|
DisplayTitle: track.TagTitle,
|
||||||
|
}
|
||||||
|
|
||||||
|
rawLines := strings.Split(string(text), "\n")
|
||||||
|
|
||||||
|
lines := make([]spec.Lyric, 0, len(rawLines))
|
||||||
|
for _, line := range rawLines {
|
||||||
|
lines = append(lines, spec.Lyric{
|
||||||
|
Value: strings.TrimSpace(line),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Lines = lines
|
||||||
|
|
||||||
|
return &r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func scrobbleStatsUpdateTrack(dbc *db.DB, track *db.Track, userID int, playTime time.Time) error {
|
func scrobbleStatsUpdateTrack(dbc *db.DB, track *db.Track, userID int, playTime time.Time) error {
|
||||||
|
|||||||
@@ -477,12 +477,12 @@ type Lyrics struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Lyric struct {
|
type Lyric struct {
|
||||||
Start int64 `xml:"start,attr" json:"start"`
|
Start *int64 `xml:"start,attr,omitempty" json:"start,omitempty"`
|
||||||
Value string `xml:",chardata" json:"value"`
|
Value string `xml:",chardata" json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LyricsList struct {
|
type LyricsList struct {
|
||||||
StructuredLyrics []StructuredLyrics `xml:"structuredLyrics" json:"structuredLyrics"`
|
StructuredLyrics []*StructuredLyrics `xml:"structuredLyrics" json:"structuredLyrics"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type StructuredLyrics struct {
|
type StructuredLyrics struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user