mirror-nourybot/pkg/api/ivr/subage.go

70 lines
1.7 KiB
Go
Raw Normal View History

2021-10-17 17:24:23 +02:00
package ivr
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
type subageApiResponse struct {
User string `json:"user"`
UserID string `json:"userId"`
Channel string `json:"channel"`
ChannelId string `json:"channelid"`
SubageHidden bool `json:"hidden"`
Subscribed bool `json:"subscribed"`
FollowedAt string `json:"followedAt"`
2021-10-17 18:24:39 +02:00
Cumulative cumulative `json:"cumulative"`
Streak subStreak `json:"streak"`
2021-10-17 17:24:23 +02:00
Error string `json:"error"`
}
2021-10-17 18:24:39 +02:00
type cumulative struct {
2021-10-17 17:24:23 +02:00
Months int `json:"months"`
}
2021-10-17 18:24:39 +02:00
type subStreak struct {
2021-10-17 17:24:23 +02:00
Months int `json:"months"`
}
2021-10-20 23:21:13 +02:00
// Subage returns the length a given user has been subscribed to a given channel.
2021-10-24 17:52:24 +02:00
func Subage(username, channel string) (string, error) {
2021-10-22 17:59:52 +02:00
baseUrl := "https://api.ivr.fi/twitch/subage"
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", baseUrl, username, channel))
2021-10-17 17:24:23 +02:00
if err != nil {
log.Error(err)
2021-10-17 18:24:39 +02:00
return "Something went wrong FeelsBadMan", err
2021-10-17 17:24:23 +02:00
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2021-10-17 23:02:55 +02:00
log.Error(err)
2021-10-17 17:24:23 +02:00
}
var responseObject subageApiResponse
json.Unmarshal(body, &responseObject)
// User or channel was not found
if responseObject.Error != "" {
reply := fmt.Sprintf(responseObject.Error + " FeelsBadMan")
// client.Say(channel, fmt.Sprintf(responseObject.Error+" FeelsBadMan"))
2021-10-17 18:24:39 +02:00
return reply, nil
2021-10-17 17:24:23 +02:00
}
if responseObject.SubageHidden {
reply := fmt.Sprintf(username + " has their subscription status hidden. FeelsBadMan")
2021-10-17 18:24:39 +02:00
return reply, nil
2021-10-17 17:24:23 +02:00
} else {
months := fmt.Sprint(responseObject.Cumulative.Months)
2021-10-20 23:21:13 +02:00
reply := fmt.Sprintf(username + " has been subscribed to " + channel + " for " + months + " months.")
2021-10-17 18:24:39 +02:00
return reply, nil
2021-10-17 17:24:23 +02:00
}
}