mirror-nourybot/pkg/ivr/user.go

102 lines
2.2 KiB
Go
Raw Normal View History

package ivr
import (
"encoding/json"
"fmt"
"net/http"
)
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"`
}
2024-02-28 11:53:39 +01:00
// TitleByUsername returns the current title of a supplied twitch channel.
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
}
2024-02-28 11:53:39 +01:00
// GameByUsername returns the current game of a supplied twitch channel.
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
}
2024-02-28 11:53:39 +01:00
// IDByUsernameReply returns the twitch user id of a supplied
// twitch username in the format of "username=id"
func IDByUsernameReply(username string) string {
baseUrl := "https://api.ivr.fi/v2/twitch/user?login="
resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, username))
if err != nil {
return "xd"
}
responseList := make([]ivrResponse, 0)
err = json.NewDecoder(resp.Body).Decode(&responseList)
if len(responseList) == 0 {
return "xd"
}
reply := fmt.Sprintf("%s=%s", username, responseList[0].ID)
return reply
}
2024-02-28 11:53:39 +01:00
// IDByUsername returns the twitch user id for a given twitch username.
func IDByUsername(username string) string {
baseUrl := "https://api.ivr.fi/v2/twitch/user?login="
resp, err := http.Get(fmt.Sprintf("%s%s", baseUrl, username))
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].ID
}