electricboogaloo/main.go

151 lines
4.7 KiB
Go

package main
import (
"flag"
"fmt"
"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. Scene transition is jumpy sometimes, probably doesn't like the 'cmus' scene changing while the scene item 'cmus' in 'streaming' is mid transition. (add a delay?)
1. Document functions
*/
var password string
func init() {
flag.StringVar(&password, "p", "", "your obs websocket password")
flag.Parse()
}
func main() {
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()
cmusParams := sceneitems.NewGetSceneItemListParams().WithSceneName("cmus")
streamParams := sceneitems.NewGetSceneItemListParams().WithSceneName("stream")
cmusSCIL, err := client.SceneItems.GetSceneItemList(cmusParams)
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())
}
streamSCIL, err := client.SceneItems.GetSceneItemList(streamParams)
if err != nil {
log.Fatalf("Expecting a scene named \"stream\", but the scene was not found.\n%v", 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 {
//stream scene
for _, sI := range streamSCIL.SceneItems {
if sI.SourceName == "cmus" {
enableItem(client, "stream", sI, false)
}
}
//cmus scene
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)
if err != nil {
log.Fatal(err)
}
artFile, err := writeArtFile(art)
if err != nil {
log.Printf("could not parse the art file, using default.jpg unprocessed.")
artFile = "./default.jpg"
}
color, err := processBGColor(artFile)
if err != nil {
log.Printf("Could not determine color, setting to default color.\n%v", err)
color = 0xff424242
}
for _, sI := range cmusSCIL.SceneItems {
if sI.SourceName == "Artist" {
updateItem(client, sI, artist, "text")
}
if sI.SourceName == "Song" {
updateItem(client, sI, title, "text")
}
if sI.SourceName == "Album" {
updateItem(client, sI, album, "text")
}
if sI.SourceName == "Art" {
updateItem(client, sI, artFile, "file")
}
if sI.SourceName == "Color" {
updateItem(client, sI, color, "color")
}
}
// stream scene
for _, sI := range streamSCIL.SceneItems {
if sI.SourceName == "cmus" {
enableItem(client, "stream", sI, true)
}
}
}
time.Sleep(time.Duration(sleepTime) * time.Second)
prevPath = path
}
}
func updateItem(client *goobs.Client, sI *typedefs.SceneItem, value any, 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)
}
}
func enableItem(client *goobs.Client, sceneName string, sI *typedefs.SceneItem, enabled bool) {
params := sceneitems.NewSetSceneItemEnabledParams().WithSceneName(sceneName).WithSceneUuid(sI.SourceUuid).WithSceneItemId(sI.SceneItemID).WithSceneItemEnabled(enabled)
_, err := client.SceneItems.SetSceneItemEnabled(params)
if err != nil {
log.Printf("could not set %v to %v.\n%v\n", sI.SourceName, enabled, err)
}
}
func getItemSettings(client *goobs.Client, sI *typedefs.SceneItem) {
params := inputs.NewGetInputSettingsParams().WithInputName(sI.SourceName).WithInputUuid(sI.SourceUuid)
gis, err := client.Inputs.GetInputSettings(params)
if err != nil {
// this is a helper function, it should not be running usually. this is fine for debugging/development
log.Fatal(err)
}
fmt.Printf("%v\n", gis)
}