From a0bd453132576fde647682d877829ffb833e0d62 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 15 Feb 2024 21:58:57 +0100 Subject: [PATCH] add title and game to stream live announcement --- cmd/nourybot/commands.go | 1 + cmd/nourybot/router.go | 11 +++++--- pkg/ivr/user.go | 56 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 8c884de..73f3ee6 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -133,6 +133,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply = app.createOfflineSubscription(target, cmdParams[2]) } } + case "unnotify": switch cmdParams[1] { case "live": diff --git a/cmd/nourybot/router.go b/cmd/nourybot/router.go index e5b5cd4..c4ba592 100644 --- a/cmd/nourybot/router.go +++ b/cmd/nourybot/router.go @@ -14,6 +14,7 @@ import ( "github.com/julienschmidt/httprouter" "github.com/lyx0/nourybot/internal/common" "github.com/lyx0/nourybot/internal/data" + "github.com/lyx0/nourybot/pkg/ivr" "github.com/nicklaw5/helix/v2" ) @@ -75,15 +76,19 @@ func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, _ log.Printf("got stream online event webhook: %s is live\n", liveEvent.BroadcasterUserName) w.WriteHeader(200) w.Write([]byte("ok")) - app.SendNoContext("nouryxd", fmt.Sprintf("%s went live FeelsGoodMan", liveEvent.BroadcasterUserName)) + + game := ivr.GameByUsername(liveEvent.BroadcasterUserLogin) + title := ivr.TitleByUsername(liveEvent.BroadcasterUserLogin) + + app.SendNoBanphrase("nouryxd", fmt.Sprintf("%s went live FeelsGoodMan Game: %s; Title: %s; https://twitch.tv/%s", liveEvent.BroadcasterUserName, game, title, liveEvent.BroadcasterUserLogin)) case helix.EventSubTypeStreamOffline: var offlineEvent helix.EventSubStreamOfflineEvent err = json.NewDecoder(bytes.NewReader(vals.Event)).Decode(&offlineEvent) - log.Printf("got stream online event webhook: %s is live\n", offlineEvent.BroadcasterUserName) + log.Printf("got stream offline event webhook: %s is now offline\n", offlineEvent.BroadcasterUserName) w.WriteHeader(200) w.Write([]byte("ok")) - app.SendNoContext("nouryxd", fmt.Sprintf("%s went offline FeelsBadMan", offlineEvent.BroadcasterUserName)) + app.SendNoBanphrase("nouryxd", fmt.Sprintf("%s went offline FeelsBadMan", offlineEvent.BroadcasterUserName)) } } diff --git a/pkg/ivr/user.go b/pkg/ivr/user.go index 793583f..8025ef8 100644 --- a/pkg/ivr/user.go +++ b/pkg/ivr/user.go @@ -6,8 +6,56 @@ import ( "net/http" ) -type ivrIDByUsernameResponse struct { - ID string `json:"id"` +type ivrResponse struct { + ID string `json:"id"` + Stream ivrStream `json:"stream"` +} + +type ivrStream struct { + Title string `json:"title"` + Game ivrGame `json:"game"` +} + +type ivrGame struct { + DisplayName string `json:"displayName"` +} + +func TitleByUsername(login string) string { + baseUrl := "https://api.ivr.fi/v2/twitch/user?login=" + + resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, login)) + if err != nil { + return "xd" + } + + defer resp.Body.Close() + + responseList := make([]ivrResponse, 0) + _ = json.NewDecoder(resp.Body).Decode(&responseList) + if len(responseList) == 0 { + return "xd" + } + + return responseList[0].Stream.Title +} + +func GameByUsername(login string) string { + baseUrl := "https://api.ivr.fi/v2/twitch/user?login=" + + resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, login)) + if err != nil { + return "xd" + } + + defer resp.Body.Close() + + responseList := make([]ivrResponse, 0) + _ = json.NewDecoder(resp.Body).Decode(&responseList) + if len(responseList) == 0 { + return "xd" + } + + return responseList[0].Stream.Game.DisplayName } func IDByUsernameReply(username string) string { @@ -18,7 +66,7 @@ func IDByUsernameReply(username string) string { return "xd" } - responseList := make([]ivrIDByUsernameResponse, 0) + responseList := make([]ivrResponse, 0) err = json.NewDecoder(resp.Body).Decode(&responseList) if len(responseList) == 0 { return "xd" @@ -38,7 +86,7 @@ func IDByUsername(username string) string { defer resp.Body.Close() - responseList := make([]ivrIDByUsernameResponse, 0) + responseList := make([]ivrResponse, 0) _ = json.NewDecoder(resp.Body).Decode(&responseList) if len(responseList) == 0 { return "xd"