move weather api logic to own package

This commit is contained in:
lyx0 2021-10-17 17:24:36 +02:00
parent f5f157abec
commit 9e16da650f
3 changed files with 29 additions and 87 deletions

26
pkg/api/aiden/weather.go Normal file
View file

@ -0,0 +1,26 @@
package aiden
import (
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
func Weather(location string) string {
resp, err := http.Get(fmt.Sprintf("https://customapi.aidenwallis.co.uk/api/v1/misc/weather/%s", location))
if err != nil {
log.Error(err)
return "Something went wrong FeelsBadMan"
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
return "Something went wrong FeelsBadMan"
}
return string(body)
}

View file

@ -1,65 +0,0 @@
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
}
}

View file

@ -1,31 +1,12 @@
package commands package commands
import ( import (
"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/aiden"
) )
func Weather(channel string, location string, client *twitch.Client) { func Weather(channel string, location string, client *twitch.Client) {
resp, err := http.Get(fmt.Sprintf("https://customapi.aidenwallis.co.uk/api/v1/misc/weather/%s", location)) reply := aiden.Weather(location)
if err != nil { client.Say(channel, reply)
client.Say(channel, "Something went wrong FeelsBadMan")
log.Error(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
client.Say(channel, "Something went wrong FeelsBadMan")
log.Error(err)
return
}
client.Say(channel, string(body))
} }