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-17 18:24:39 +02:00
|
|
|
var (
|
|
|
|
subageBaseUrl = "https://api.ivr.fi/twitch/subage"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Subage(username string, streamer string) (string, error) {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", subageBaseUrl, username, streamer))
|
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)
|
|
|
|
reply := fmt.Sprintf(username + " has been subscribed to " + streamer + " for " + months + " months.")
|
2021-10-17 18:24:39 +02:00
|
|
|
return reply, nil
|
2021-10-17 17:24:23 +02:00
|
|
|
}
|
|
|
|
}
|