110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/andreykaipov/goobs"
|
|
"github.com/andreykaipov/goobs/api/requests/inputs"
|
|
"github.com/andreykaipov/goobs/api/requests/sceneitems"
|
|
"github.com/andreykaipov/goobs/api/typedefs"
|
|
)
|
|
|
|
/*
|
|
TODO:
|
|
|
|
1. Set password with a flag instead of text in the code
|
|
4. Document functions
|
|
*/
|
|
var password string
|
|
|
|
func init() {
|
|
flag.StringVar(&password, "p", "", "your obs websocket password")
|
|
flag.Parse()
|
|
}
|
|
|
|
func main() {
|
|
// change this password lol!
|
|
sleepTime := 1.0
|
|
client, err := goobs.New("localhost:4455", goobs.WithPassword(password))
|
|
if err != nil {
|
|
log.Fatalf("Authentication failed, password %v absent or incorrect.", password)
|
|
}
|
|
defer client.Disconnect()
|
|
params := sceneitems.NewGetSceneItemListParams().WithSceneName("cmus")
|
|
scil, err := client.SceneItems.GetSceneItemList(params)
|
|
if err != nil {
|
|
// i should force create the scene if it does not exist. but this is solely for me.
|
|
// failing in this way is probably smarter since it forces to arrange the scene how i want it.
|
|
log.Fatalf("Expecting a scene named \"cmus\", but the scene was not found.\n%v", err.Error())
|
|
}
|
|
out, err := CmusRemoteOutput()
|
|
if err != nil {
|
|
log.Fatalf("%v\nCmus is likely not running.", err.Error())
|
|
}
|
|
path, err := getAttribute(out, "file ")
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
var prevPath string
|
|
for {
|
|
out, err = CmusRemoteOutput()
|
|
if err != nil {
|
|
log.Fatalf("%v\nCmus is likely not running.", err.Error())
|
|
}
|
|
path, err = getAttribute(out, "file ")
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
if path != prevPath {
|
|
artist, err := getAttribute(out, "tag artist ", "tag albumartist ", "tag composer ")
|
|
if err != nil {
|
|
log.Printf("%v does not have an artist listed.", path)
|
|
artist = "Unknown"
|
|
}
|
|
title, err := getAttribute(out, "tag title ")
|
|
if err != nil {
|
|
log.Printf("%v does not have an title listed.", path)
|
|
title = "Unknown"
|
|
}
|
|
album, err := getAttribute(out, "tag album ")
|
|
if err != nil {
|
|
log.Printf("%v does not have an album listed.", path)
|
|
album = "Single"
|
|
}
|
|
|
|
art, err := retrieveArt(path)
|
|
artFile, err := writeArtFile(art)
|
|
for i := range scil.SceneItems {
|
|
if scil.SceneItems[i].SourceName == "Artist" {
|
|
updateItem(client, scil.SceneItems[i], artist, "text")
|
|
}
|
|
if scil.SceneItems[i].SourceName == "Song" {
|
|
updateItem(client, scil.SceneItems[i], title, "text")
|
|
}
|
|
if scil.SceneItems[i].SourceName == "Album" {
|
|
updateItem(client, scil.SceneItems[i], album, "text")
|
|
}
|
|
if scil.SceneItems[i].SourceName == "Art" {
|
|
updateItem(client, scil.SceneItems[i], artFile, "file")
|
|
}
|
|
}
|
|
}
|
|
time.Sleep(time.Duration(sleepTime) * time.Second)
|
|
prevPath = path
|
|
}
|
|
}
|
|
|
|
func updateItem(client *goobs.Client, sI *typedefs.SceneItem, value string, key string) {
|
|
params := inputs.NewSetInputSettingsParams().WithInputName(sI.SourceName).WithInputUuid(sI.SourceUuid)
|
|
params.InputSettings = make(map[string]any)
|
|
params.InputSettings[key] = value
|
|
_, err := client.Inputs.SetInputSettings(params)
|
|
if err != nil {
|
|
// i should probably forcefully create the item element missing, but
|
|
// this is fine for personal use.
|
|
log.Fatal(err)
|
|
}
|
|
}
|