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"
type StreamList struct {
Total string `json:"total"`
Total int `json:"total"`
Streams []Stream `json:"items"`
}
type Channel struct {

126
simp.go
View File

@ -3,8 +3,8 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
@ -17,7 +17,7 @@ import (
"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 {
DimensionServer string `yaml:"dimension"`
HolodexToken string `yaml:"api_token"`
@ -51,7 +51,7 @@ func newSimp(filename string, client *mautrix.Client) (*simp, error) {
change: make(chan int),
client: client,
}
yamlFile, err := ioutil.ReadFile(filename)
yamlFile, err := os.ReadFile(filename)
if err == nil {
err = yaml.Unmarshal(yamlFile, cnf)
} else {
@ -106,7 +106,10 @@ func (s *simp) Sync() error {
}
embed := content.ID != ""
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)
if v.IsLive() {
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 {
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 {
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
for k := range v.Subs {
subs += k.String() + " "
}
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(
room,
@ -181,21 +199,36 @@ func (s *simp) Update(v *Vtuber) error {
for _, room := range s.State.rooms {
if v.IsLive() {
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 {
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 {
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
for k := range v.Subs {
subs += k.String() + " "
}
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(
room,
@ -232,22 +265,29 @@ func (s *simp) Run() {
log.Info("stopping runner")
return
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 {
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)
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 {
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)
cmd, err := mbl.ParseCommand(evt, "simp")
var cmd []string
cmd, err = mbl.ParseCommand(evt, "simp")
if err != nil {
if err != mbl.ErrCmdParseNoPrefix {
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))
}
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":
var statmsg string
vlist := []string{}
@ -284,40 +327,61 @@ func (s *simp) SetupMatrix() error {
s.State.maxStream,
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":
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":
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]
var subbed bool
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
subbed = true
}
}
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 {
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":
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:
// 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) {
//log.Infof("<%[1]s> %[4]s (%[2]s/%[3]s)", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
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)
mevt := evt.Content.AsMember()
log.Printf("FBL %v", mevt.Membership)
if mevt.Membership == event.MembershipInvite {
_, err := s.State.client.JoinRoomByID(evt.RoomID)
_, err = s.State.client.JoinRoomByID(evt.RoomID)
if err != nil {
log.Errorf("error joining room %v", evt.RoomID)
} else {
@ -368,7 +432,7 @@ func (s *simp) NewYT(videoName, videoID, roomID string) *YoutubeWidget {
Category: "widget",
Type: "youtube",
},
LastUpdatedTs: time.Now().UnixNano() / int64(time.Millisecond),
LastUpdatedTS: time.Now().UnixNano() / int64(time.Millisecond),
},
},
CreatorUserID: string(s.State.client.UserID),

View File

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

View File

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