mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
put api lookup logic into its own package
This commit is contained in:
parent
bd4d2dbe21
commit
c8b8ebfed4
2 changed files with 68 additions and 53 deletions
|
@ -0,0 +1,65 @@
|
||||||
|
package api
|
||||||
|
|
||||||
|
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"`
|
||||||
|
Cumulative Cumulative `json:"cumulative"`
|
||||||
|
Streak SubStreak `json:"streak"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cumulative struct {
|
||||||
|
Months int `json:"months"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SubStreak struct {
|
||||||
|
Months int `json:"months"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func IvrSubage(username string, streamer string) string {
|
||||||
|
resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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"))
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
|
||||||
|
if responseObject.SubageHidden {
|
||||||
|
|
||||||
|
reply := fmt.Sprintf(username + " has their subscription status hidden. FeelsBadMan")
|
||||||
|
return reply
|
||||||
|
} else {
|
||||||
|
months := fmt.Sprint(responseObject.Cumulative.Months)
|
||||||
|
reply := fmt.Sprintf(username + " has been subscribed to " + streamer + " for " + months + " months.")
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,62 +1,12 @@
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gempir/go-twitch-irc/v2"
|
"github.com/gempir/go-twitch-irc/v2"
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/lyx0/nourybot/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
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"`
|
|
||||||
Cumulative Cumulative `json:"cumulative"`
|
|
||||||
Streak SubStreak `json:"streak"`
|
|
||||||
Error string `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Cumulative struct {
|
|
||||||
Months int `json:"months"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type SubStreak struct {
|
|
||||||
Months int `json:"months"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Subage(channel string, username string, streamer string, client *twitch.Client) {
|
func Subage(channel string, username string, streamer string, client *twitch.Client) {
|
||||||
resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer))
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
ivrResponse := api.IvrSubage(username, streamer)
|
||||||
|
client.Say(channel, ivrResponse)
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var responseObject subageApiResponse
|
|
||||||
json.Unmarshal(body, &responseObject)
|
|
||||||
|
|
||||||
// User or channel was not found
|
|
||||||
if responseObject.Error != "" {
|
|
||||||
client.Say(channel, fmt.Sprintf(responseObject.Error+" FeelsBadMan"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if responseObject.SubageHidden {
|
|
||||||
client.Say(channel, fmt.Sprintf(username+" has their subscription status hidden. FeelsBadMan"))
|
|
||||||
} else {
|
|
||||||
months := fmt.Sprint(responseObject.Cumulative.Months)
|
|
||||||
client.Say(channel, fmt.Sprintf(username+" has been subscribed to "+streamer+" for "+months+" months."))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue