From b9ec93869704aaca743aed6a236f391f9071b5f4 Mon Sep 17 00:00:00 2001 From: "Sean E. Russell" Date: Wed, 25 Dec 2024 09:57:12 -0600 Subject: [PATCH] fix: only adds lyrics support if the server has the OpenSubsonic lyrics extension --- event_loop.go | 14 +++++++++----- page_queue.go | 29 +++++++++++++++++------------ subsonic/api.go | 38 ++++++++++++++++++++++---------------- subsonic/connection.go | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 33 deletions(-) diff --git a/event_loop.go b/event_loop.go index bce5f2f..2284a43 100644 --- a/event_loop.go +++ b/event_loop.go @@ -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() }) @@ -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("") + } } }) diff --git a/page_queue.go b/page_queue.go index c82f29c..b81ebb9 100644 --- a/page_queue.go +++ b/page_queue.go @@ -162,15 +162,18 @@ 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) @@ -178,9 +181,11 @@ func (ui *Ui) createQueuePage() *QueuePage { 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 ") diff --git a/subsonic/api.go b/subsonic/api.go index 57f189f..f969d1f 100644 --- a/subsonic/api.go +++ b/subsonic/api.go @@ -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 } @@ -330,3 +331,8 @@ type LyricsLine struct { Start int64 `json:"start"` Value string `json:"value"` } + +type Extension struct { + Name string + Versions []int +} diff --git a/subsonic/connection.go b/subsonic/connection.go index c4f7838..7a919cb 100644 --- a/subsonic/connection.go +++ b/subsonic/connection.go @@ -17,6 +17,7 @@ import ( "net/url" "sort" "strconv" + "strings" "github.com/spezifisch/stmps/logger" ) @@ -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 +}