fix(spotify): parse information correctly for PWA on Windows

resolves #3310
This commit is contained in:
Jan De Dobbeleer 2023-01-09 06:47:39 +01:00 committed by Jan De Dobbeleer
parent b8c09f92ab
commit 39594b0d34
2 changed files with 31 additions and 12 deletions

View file

@ -11,30 +11,43 @@ func (s *Spotify) Enabled() bool {
// Can be either "Spotify xxx" or the song name "Candlemass - Spellbreaker" // Can be either "Spotify xxx" or the song name "Candlemass - Spellbreaker"
windowTitle, err := s.env.QueryWindowTitles("spotify.exe", `^(Spotify.*)|(.*\s-\s.*)$`) windowTitle, err := s.env.QueryWindowTitles("spotify.exe", `^(Spotify.*)|(.*\s-\s.*)$`)
if err == nil { if err == nil {
return s.parseSpotifyTitle(windowTitle, " - ") return s.parseNativeTitle(windowTitle)
} }
windowTitle, err = s.env.QueryWindowTitles("msedge.exe", `^(Spotify.*)`) windowTitle, err = s.env.QueryWindowTitles("msedge.exe", `^(Spotify.*)`)
if err != nil { if err != nil {
return false return false
} }
return s.parseWebSpotifyTitle(windowTitle) return s.parseWebTitle(windowTitle)
} }
func (s *Spotify) parseWebSpotifyTitle(windowTitle string) bool { func (s *Spotify) parseNativeTitle(windowTitle string) bool {
windowTitle = strings.TrimPrefix(windowTitle, "Spotify - ") separator := " - "
return s.parseSpotifyTitle(windowTitle, " • ")
}
func (s *Spotify) parseSpotifyTitle(windowTitle, separator string) bool {
if !strings.Contains(windowTitle, separator) { if !strings.Contains(windowTitle, separator) {
s.Status = stopped s.Status = stopped
return false return false
} }
infos := strings.Split(windowTitle, separator) index := strings.Index(windowTitle, separator)
s.Artist = infos[0] s.Artist = windowTitle[0:index]
// remove first element and concat others(a song can contains also a " - ") s.Track = windowTitle[index+len(separator):]
s.Track = strings.Join(infos[1:], separator) s.Status = playing
s.resolveIcon()
return true
}
func (s *Spotify) parseWebTitle(windowTitle string) bool {
windowTitle = strings.TrimPrefix(windowTitle, "Spotify - ")
separator := " • "
if !strings.Contains(windowTitle, separator) {
s.Status = stopped
return false
}
index := strings.Index(windowTitle, separator)
s.Track = windowTitle[0:index]
s.Artist = windowTitle[index+len(separator):]
s.Status = playing s.Status = playing
s.resolveIcon() s.resolveIcon()
return true return true

View file

@ -63,10 +63,16 @@ func TestSpotifyWindowsPWA(t *testing.T) {
}{ }{
{ {
Case: "Playing", Case: "Playing",
ExpectedString: "\ue602 Snow in Stockholm - Sarah, the Illstrumentalist", ExpectedString: "\ue602 Sarah, the Illstrumentalist - Snow in Stockholm",
ExpectedEnabled: true, ExpectedEnabled: true,
Title: "Spotify - Snow in Stockholm • Sarah, the Illstrumentalist", Title: "Spotify - Snow in Stockholm • Sarah, the Illstrumentalist",
}, },
{
Case: "Playing",
ExpectedString: "\ue602 Main one - Bring the drama",
ExpectedEnabled: true,
Title: "Spotify - Bring the drama • Main one",
},
{ {
Case: "Stopped", Case: "Stopped",
ExpectedEnabled: false, ExpectedEnabled: false,