avoid unneccessary commands

This commit is contained in:
Octopus Octopus 2024-03-13 22:57:11 -05:00
parent 9755424d5e
commit 31d2dbebec
2 changed files with 47 additions and 41 deletions

BIN
cmus2obs

Binary file not shown.

88
main.go
View File

@ -19,7 +19,6 @@ import (
_ "image/gif" _ "image/gif"
"image/jpeg" "image/jpeg"
_ "image/jpeg"
_ "image/png" _ "image/png"
) )
@ -29,62 +28,69 @@ const (
) )
func main() { func main() {
prevFilepath := ""
for { for {
c := exec.Command("cmus-remote", "-Q") c := exec.Command("cmus-remote", "-Q")
o, _ := c.Output() o, _ := c.Output()
remoteResp := strings.Split(string(o), "\n") remoteResp := strings.Split(string(o), "\n")
//Album
album, err := getAttribute(remoteResp, "tag album ")
if err != nil {
album = "Unknown"
}
//artist
artist, err := getAttribute(remoteResp, "tag artist ")
if err != nil {
artist = "Unknown"
}
// Title
title, err := getAttribute(remoteResp, "tag title ")
if err != nil {
title = "Unknown"
}
filepath, err := getAttribute(remoteResp, "file ") filepath, err := getAttribute(remoteResp, "file ")
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
img := make([]byte, 0) if filepath != prevFilepath {
if strings.HasSuffix(filepath, ".flac") { //Album
img, err = getFlacArt(filepath) album, err := getAttribute(remoteResp, "tag album ")
if err != nil { if err != nil {
album = "Unknown"
}
//artist
artist, err := getAttribute(remoteResp, "tag artist ")
if err != nil {
artist = "Unknown"
}
// Title
title, err := getAttribute(remoteResp, "tag title ")
if err != nil {
title = "Unknown"
}
// Image
img := make([]byte, 0)
if strings.HasSuffix(filepath, ".flac") {
img, err = getFlacArt(filepath)
if err != nil {
img = defaultArt()
}
} else if strings.HasSuffix(filepath, ".mp3") {
img, err = getMP3Art(filepath)
if err != nil {
img = defaultArt()
}
} else {
img = defaultArt() img = defaultArt()
} }
} else if strings.HasSuffix(filepath, ".mp3") {
img, err = getMP3Art(filepath) imgBuff := bytes.NewBuffer(img)
imgOrig, _, err := image.Decode(imgBuff)
if err != nil { if err != nil {
img = defaultArt() log.Fatal(err.Error())
} }
} else { imgOut := image.NewRGBA(image.Rect(0, 0, IMAGE_SIZE, IMAGE_SIZE))
img = defaultArt()
draw.BiLinear.Scale(imgOut, imgOut.Rect, imgOrig, imgOrig.Bounds(), draw.Over, nil)
jpeg.Encode(imgBuff, imgOut, nil)
writeTxt("SongAlbum", album)
writeTxt("SongArtist", artist)
writeTxt("SongTitle", title)
writeJpg("AlbumArt", img)
} }
imgBuff := bytes.NewBuffer(img) prevFilepath = filepath
imgOrig, _, err := image.Decode(imgBuff)
if err != nil {
log.Fatal(err.Error())
}
imgOut := image.NewRGBA(image.Rect(0, 0, IMAGE_SIZE, IMAGE_SIZE))
draw.BiLinear.Scale(imgOut, imgOut.Rect, imgOrig, imgOrig.Bounds(), draw.Over, nil)
jpeg.Encode(imgBuff, imgOut, nil)
writeTxt("SongAlbum", album)
writeTxt("SongArtist", artist)
writeTxt("SongTitle", title)
writeJpg("AlbumArt", img)
time.Sleep(TIMER * time.Second) time.Sleep(TIMER * time.Second)
} }
} }