Skip to content

Commit

Permalink
fix: only adds lyrics support if the server has the OpenSubsonic lyri…
Browse files Browse the repository at this point in the history
…cs extension
  • Loading branch information
xxxserxxx committed Dec 25, 2024
1 parent c804ff1 commit b9ec938
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 33 deletions.
14 changes: 9 additions & 5 deletions event_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func (ui *Ui) guiEventLoop() {
ui.logger.Print("mpvEvent: stopped")
ui.app.QueueUpdateDraw(func() {
ui.startStopStatus.SetText("[red::b]Stopped[::-]")
ui.queuePage.lyrics.SetText("")
if ui.queuePage.lyrics != nil {
ui.queuePage.lyrics.SetText("")
}
ui.queuePage.updateQueue()
})

Expand Down Expand Up @@ -157,10 +159,12 @@ func (ui *Ui) guiEventLoop() {
ui.app.QueueUpdateDraw(func() {
ui.startStopStatus.SetText(statusText)
ui.queuePage.updateQueue()
if len(ui.queuePage.currentLyrics.Lines) == 0 {
ui.queuePage.lyrics.SetText("\n[::i]No lyrics[-:-:-]")
} else {
ui.queuePage.lyrics.SetText("")
if ui.queuePage.lyrics != nil {
if len(ui.queuePage.currentLyrics.Lines) == 0 {
ui.queuePage.lyrics.SetText("\n[::i]No lyrics[-:-:-]")
} else {
ui.queuePage.lyrics.SetText("")
}
}
})

Expand Down
29 changes: 17 additions & 12 deletions page_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,30 @@ func (ui *Ui) createQueuePage() *QueuePage {
return action, nil
})

queuePage.lyrics = tview.NewTextView()
queuePage.lyrics.SetBorder(true)
queuePage.lyrics.SetTitle(" lyrics ")
queuePage.lyrics.SetTitleAlign(tview.AlignCenter)
queuePage.lyrics.SetDynamicColors(true).SetScrollable(true)
queuePage.lyrics.SetWrap(true)
queuePage.lyrics.SetWordWrap(true)
queuePage.lyrics.SetTextAlign(tview.AlignCenter)
queuePage.lyrics.SetBorderPadding(1, 1, 1, 1)
serverHasLyrics := ui.connection.HasOpenSubsonicExtension("songLyrics")
if serverHasLyrics {
queuePage.lyrics = tview.NewTextView()
queuePage.lyrics.SetBorder(true)
queuePage.lyrics.SetTitle(" lyrics ")
queuePage.lyrics.SetTitleAlign(tview.AlignCenter)
queuePage.lyrics.SetDynamicColors(true).SetScrollable(true)
queuePage.lyrics.SetWrap(true)
queuePage.lyrics.SetWordWrap(true)
queuePage.lyrics.SetTextAlign(tview.AlignCenter)
queuePage.lyrics.SetBorderPadding(1, 1, 1, 1)
}

queuePage.queueList.SetSelectionChangedFunc(queuePage.changeSelection)

queuePage.coverArt = tview.NewImage()
queuePage.coverArt.SetImage(STMPS_LOGO)

queuePage.infoFlex = tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(queuePage.songInfo, 0, 1, false).
AddItem(queuePage.lyrics, 0, 1, false).
AddItem(queuePage.coverArt, 0, 1, false)
AddItem(queuePage.songInfo, 0, 1, false)
if serverHasLyrics {
queuePage.infoFlex.AddItem(queuePage.lyrics, 0, 1, false)
}
queuePage.infoFlex.AddItem(queuePage.coverArt, 0, 1, false)
queuePage.infoFlex.SetBorder(true)
queuePage.infoFlex.SetTitle(" song info ")

Expand Down
38 changes: 22 additions & 16 deletions subsonic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,22 +281,23 @@ type Response struct {
OpenSubsonic bool

// There's no better way to do this, because Go generics are useless
RandomSongs Songs
SimilarSongs Songs
Starred Results
SearchResult3 Results
Directory Directory
Album Album
Artists Indexes
Artist Artist
ScanStatus ScanStatus
PlayQueue PlayQueue
Genres GenreEntries
SongsByGenre Songs
Indexes Indexes
LyricsList LyricsList
Playlists Playlists
Playlist Playlist
RandomSongs Songs
SimilarSongs Songs
Starred Results
SearchResult3 Results
Directory Directory
Album Album
Artists Indexes
Artist Artist
ScanStatus ScanStatus
PlayQueue PlayQueue
Genres GenreEntries
SongsByGenre Songs
Indexes Indexes
LyricsList LyricsList
Playlists Playlists
Playlist Playlist
OpenSubsonicExtensions []Extension

Error Error
}
Expand Down Expand Up @@ -330,3 +331,8 @@ type LyricsLine struct {
Start int64 `json:"start"`
Value string `json:"value"`
}

type Extension struct {
Name string
Versions []int
}
40 changes: 40 additions & 0 deletions subsonic/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net/url"
"sort"
"strconv"
"strings"

"github.com/spezifisch/stmps/logger"
)
Expand Down Expand Up @@ -664,3 +665,42 @@ func (connection *Connection) GetSongsByGenre(genre string, offset int, musicFol
}
return resp.SongsByGenre.Songs, nil
}

func (connection *Connection) HasOpenSubsonicExtension(feature string) bool {
info, err := connection.GetServerInfo()
if err != nil {
connection.logger.PrintError("HasOpenSubsonicExtension", err)
return false
}
if !info.OpenSubsonic {
return false
}
query := defaultQuery(connection)
requestUrl := connection.Host + "/rest/getOpenSubsonicExtensions" + "?" + query.Encode()
resp, err := connection.getResponse("GetOpenSubsonicExtensions", requestUrl)
if err != nil {
return false
}
m := major(info.Version)
for _, e := range resp.OpenSubsonicExtensions {
if e.Name == feature {
for _, v := range e.Versions {
if v == m {
return true
}
}
}
}
return false
}

func major(version string) int {
parts := strings.Split(version, ".")
if len(parts) > 1 {
rv, e := strconv.Atoi(parts[0])
if e == nil {
return rv
}
}
return 0
}

0 comments on commit b9ec938

Please sign in to comment.