api update, minor cleanup
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
stryan 2023-11-27 20:34:32 -05:00
parent c564430dde
commit b0ad50f907
4 changed files with 100 additions and 39 deletions

View File

@ -3,7 +3,7 @@ package main
import "time" import "time"
type StreamList struct { type StreamList struct {
Total string `json:"total"` Total int `json:"total"`
Streams []Stream `json:"items"` Streams []Stream `json:"items"`
} }
type Channel struct { type Channel struct {

126
simp.go
View File

@ -3,8 +3,8 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/url" "net/url"
"os"
"strings" "strings"
"time" "time"
@ -17,7 +17,7 @@ import (
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
) )
//SimpConf is the simpbot conf that's not matrix // SimpConf is the simpbot conf that's not matrix
type SimpConf struct { type SimpConf struct {
DimensionServer string `yaml:"dimension"` DimensionServer string `yaml:"dimension"`
HolodexToken string `yaml:"api_token"` HolodexToken string `yaml:"api_token"`
@ -51,7 +51,7 @@ func newSimp(filename string, client *mautrix.Client) (*simp, error) {
change: make(chan int), change: make(chan int),
client: client, client: client,
} }
yamlFile, err := ioutil.ReadFile(filename) yamlFile, err := os.ReadFile(filename)
if err == nil { if err == nil {
err = yaml.Unmarshal(yamlFile, cnf) err = yaml.Unmarshal(yamlFile, cnf)
} else { } else {
@ -106,7 +106,10 @@ func (s *simp) Sync() error {
} }
embed := content.ID != "" embed := content.ID != ""
if v.IsLive() != embed { if v.IsLive() != embed {
s.Update(v) err := s.Update(v)
if err != nil {
log.Warn("error updating", "error", err)
}
} }
} }
@ -141,21 +144,36 @@ func (s *simp) PopulateRoom(room id.RoomID) error {
log.Printf("Updating vtuber %v", v.Name) log.Printf("Updating vtuber %v", v.Name)
if v.IsLive() { if v.IsLive() {
if v.AnnounceLive { if v.AnnounceLive {
s.State.client.SendText(room, v.LiveMsg) _, err := s.State.client.SendText(room, v.LiveMsg)
if err != nil {
log.Warn("error sending message to room", "error", err)
}
} else { } else {
if isValidURL(v.LiveMsg) { if isValidURL(v.LiveMsg) {
s.State.client.SendNotice(room, fmt.Sprintf("%v has gone live", v.Name)) _, err := s.State.client.SendNotice(room, fmt.Sprintf("%v has gone live", v.Name))
if err != nil {
log.Warn("error updating state", "error", err)
}
} else { } else {
s.State.client.SendNotice(room, v.LiveMsg) _, err := s.State.client.SendNotice(room, v.LiveMsg)
if err != nil {
log.Warn("error updating state", "error", err)
}
} }
} }
s.State.client.SendNotice(room, fmt.Sprintf("%v's Title: %v", v.Name, v.CurrentStreamTitle)) _, err := s.State.client.SendNotice(room, fmt.Sprintf("%v's Title: %v", v.Name, v.CurrentStreamTitle))
if err != nil {
log.Warn("error sending notice", "error", err)
}
var subs string var subs string
for k := range v.Subs { for k := range v.Subs {
subs += k.String() + " " subs += k.String() + " "
} }
if len(v.Subs) > 0 { if len(v.Subs) > 0 {
s.State.client.SendText(room, fmt.Sprintf("Pinging %v", subs)) _, err = s.State.client.SendText(room, fmt.Sprintf("Pinging %v", subs))
if err != nil {
log.Warn("error sending text", "error", err)
}
} }
resp, err := s.State.client.SendStateEvent( resp, err := s.State.client.SendStateEvent(
room, room,
@ -181,21 +199,36 @@ func (s *simp) Update(v *Vtuber) error {
for _, room := range s.State.rooms { for _, room := range s.State.rooms {
if v.IsLive() { if v.IsLive() {
if v.AnnounceLive { if v.AnnounceLive {
s.State.client.SendText(room, v.LiveMsg) _, err := s.State.client.SendText(room, v.LiveMsg)
if err != nil {
log.Warn("error sending text", "error", err)
}
} else { } else {
if isValidURL(v.LiveMsg) { if isValidURL(v.LiveMsg) {
s.State.client.SendNotice(room, fmt.Sprintf("%v has gone live", v.Name)) _, err := s.State.client.SendNotice(room, fmt.Sprintf("%v has gone live", v.Name))
if err != nil {
return err
}
} else { } else {
s.State.client.SendNotice(room, v.LiveMsg) _, err := s.State.client.SendNotice(room, v.LiveMsg)
if err != nil {
return err
}
} }
} }
s.State.client.SendNotice(room, fmt.Sprintf("%v's Title: %v", v.Name, v.CurrentStreamTitle)) _, err := s.State.client.SendNotice(room, fmt.Sprintf("%v's Title: %v", v.Name, v.CurrentStreamTitle))
if err != nil {
log.Warn("error sending notice", "error", err)
}
var subs string var subs string
for k := range v.Subs { for k := range v.Subs {
subs += k.String() + " " subs += k.String() + " "
} }
if len(v.Subs) > 0 { if len(v.Subs) > 0 {
s.State.client.SendText(room, fmt.Sprintf("Pinging %v", subs)) _, err = s.State.client.SendText(room, fmt.Sprintf("Pinging %v", subs))
if err != nil {
log.Warn("error sending text", "error", err)
}
} }
resp, err := s.State.client.SendStateEvent( resp, err := s.State.client.SendStateEvent(
room, room,
@ -232,22 +265,29 @@ func (s *simp) Run() {
log.Info("stopping runner") log.Info("stopping runner")
return return
case v := <-s.State.change: case v := <-s.State.change:
s.Update(s.State.vtubers[v]) err := s.Update(s.State.vtubers[v])
if err != nil {
log.Warn("error updating vtube memory", "error", err)
}
} }
} }
} }
func (s *simp) SetupMatrix() error { func (s *simp) SetupMatrix() error {
mbl.SetupAccountDataStore(s.State.client, "s.batch") err := mbl.SetupAccountDataStore(s.State.client, "s.batch")
if err != nil {
return err
}
syncer := s.State.client.Syncer.(*mautrix.DefaultSyncer) syncer := s.State.client.Syncer.(*mautrix.DefaultSyncer)
uid := s.State.client.UserID.String() uid := s.State.client.UserID.String()
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) { syncer.OnEventType(event.EventMessage, func(_ mautrix.EventSource, evt *event.Event) {
if evt.Sender == s.State.client.UserID { if evt.Sender == s.State.client.UserID {
return // ignore events from self return // ignore events from self
} }
log.Debugf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body) log.Debugf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
cmd, err := mbl.ParseCommand(evt, "simp") var cmd []string
cmd, err = mbl.ParseCommand(evt, "simp")
if err != nil { if err != nil {
if err != mbl.ErrCmdParseNoPrefix { if err != mbl.ErrCmdParseNoPrefix {
log.Printf("invalid command: %v", err) log.Printf("invalid command: %v", err)
@ -267,7 +307,10 @@ func (s *simp) SetupMatrix() error {
vlist = append(vlist, fmt.Sprintf("%v%v", vt.Name, ann)) vlist = append(vlist, fmt.Sprintf("%v%v", vt.Name, ann))
} }
infomsg = fmt.Sprintf("Currently Simping For: \n%v", strings.Join(vlist, "\n")) infomsg = fmt.Sprintf("Currently Simping For: \n%v", strings.Join(vlist, "\n"))
s.State.client.SendText(evt.RoomID, infomsg) _, err = s.State.client.SendText(evt.RoomID, infomsg)
if err != nil {
log.Warn("error sending text", "error", err)
}
case "stats": case "stats":
var statmsg string var statmsg string
vlist := []string{} vlist := []string{}
@ -284,40 +327,61 @@ func (s *simp) SetupMatrix() error {
s.State.maxStream, s.State.maxStream,
len(s.State.vtubers), len(s.State.vtubers),
) )
s.State.client.SendText(evt.RoomID, statmsg) _, err = s.State.client.SendText(evt.RoomID, statmsg)
if err != nil {
log.Warn("error sending text", "error", err)
}
case "version": case "version":
s.State.client.SendText(evt.RoomID, "not implemented") _, err = s.State.client.SendText(evt.RoomID, "not implemented")
if err != nil {
log.Warn("error sending text", "error", err)
}
case "subscribe": case "subscribe":
if len(cmd) < 3 { if len(cmd) < 3 {
s.State.client.SendText(evt.RoomID, "Need a member to subscribe to") _, err = s.State.client.SendText(evt.RoomID, "Need a member to subscribe to")
if err != nil {
log.Warn("error sending text", "error", err)
}
} }
vt := cmd[1] vt := cmd[1]
var subbed bool var subbed bool
for _, v := range s.State.vtubers { for _, v := range s.State.vtubers {
if strings.ToUpper(v.Name) == strings.ToUpper(vt) { if strings.EqualFold(v.Name, vt) {
v.Subs[evt.Sender] = true v.Subs[evt.Sender] = true
subbed = true subbed = true
} }
} }
if subbed { if subbed {
s.State.client.SendText(evt.RoomID, "subbed") _, err = s.State.client.SendText(evt.RoomID, "subbed")
if err != nil {
log.Warn("error sending text", "error", err)
}
} else { } else {
s.State.client.SendText(evt.RoomID, "could not identify talent to subscribe to") _, err = s.State.client.SendText(evt.RoomID, "could not identify talent to subscribe to")
if err != nil {
log.Warn("error sending text", "error", err)
}
} }
case "help": case "help":
s.State.client.SendText(evt.RoomID, "Supported commands: info,version,stats,subscribe") _, err = s.State.client.SendText(evt.RoomID, "Supported commands: info,version,stats,subscribe")
if err != nil {
log.Warn("error sending text", "error", err)
}
default: default:
// command not found // command not found
s.State.client.SendText(evt.RoomID, "command not recognized") _, err = s.State.client.SendText(evt.RoomID, "command not recognized")
if err != nil {
log.Warn("error sending text", "error", err)
}
} }
}) })
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) { syncer.OnEventType(event.StateMember, func(_ mautrix.EventSource, evt *event.Event) {
//log.Infof("<%[1]s> %[4]s (%[2]s/%[3]s)", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body) // log.Infof("<%[1]s> %[4]s (%[2]s/%[3]s)", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
mevt := evt.Content.AsMember() mevt := evt.Content.AsMember()
log.Printf("FBL %v", mevt.Membership) log.Printf("FBL %v", mevt.Membership)
if mevt.Membership == event.MembershipInvite { if mevt.Membership == event.MembershipInvite {
_, err := s.State.client.JoinRoomByID(evt.RoomID) _, err = s.State.client.JoinRoomByID(evt.RoomID)
if err != nil { if err != nil {
log.Errorf("error joining room %v", evt.RoomID) log.Errorf("error joining room %v", evt.RoomID)
} else { } else {
@ -368,7 +432,7 @@ func (s *simp) NewYT(videoName, videoID, roomID string) *YoutubeWidget {
Category: "widget", Category: "widget",
Type: "youtube", Type: "youtube",
}, },
LastUpdatedTs: time.Now().UnixNano() / int64(time.Millisecond), LastUpdatedTS: time.Now().UnixNano() / int64(time.Millisecond),
}, },
}, },
CreatorUserID: string(s.State.client.UserID), CreatorUserID: string(s.State.client.UserID),

View File

@ -3,7 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
@ -61,11 +61,11 @@ func (v *Vtuber) Update(apiKey string) error {
if err != nil { if err != nil {
return fmt.Errorf("error sending request %w", err) return fmt.Errorf("error sending request %w", err)
} }
defer res.Body.Close() defer func() { _ = res.Body.Close() }()
if res.StatusCode != 200 { if res.StatusCode != 200 {
return fmt.Errorf("bad status code %v", res.StatusCode) return fmt.Errorf("bad status code %v", res.StatusCode)
} }
jsonBody, err := ioutil.ReadAll(res.Body) jsonBody, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return fmt.Errorf("error reading response: %w", err) return fmt.Errorf("error reading response: %w", err)
} }

View File

@ -10,7 +10,7 @@ type DimensionAppMetadata struct {
WrapperID string `json:"wrapperId"` WrapperID string `json:"wrapperId"`
ScalarWrapperID string `json:"scalarWrapperId"` ScalarWrapperID string `json:"scalarWrapperId"`
Integration Integration `json:"integration"` Integration Integration `json:"integration"`
LastUpdatedTs int64 `json:"lastUpdatedTs"` LastUpdatedTS int64 `json:"lastUpdatedTs"`
} }
type VideoData struct { type VideoData struct {
VideoURL string `json:"videoUrl"` VideoURL string `json:"videoUrl"`
@ -27,6 +27,3 @@ type YoutubeWidget struct {
RoomID string `json:"roomId"` RoomID string `json:"roomId"`
EventID string `json:"eventId"` EventID string `json:"eventId"`
} }
type unsignedAge struct {
Age int `json:"age"`
}