Skip to content

Instantly share code, notes, and snippets.

@koenbollen
Last active June 11, 2018 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koenbollen/8d4f4fdbab51dd697f3d2e2695c5ed16 to your computer and use it in GitHub Desktop.
Save koenbollen/8d4f4fdbab51dd697f3d2e2695c5ed16 to your computer and use it in GitHub Desktop.
Post changes in Fortnite's server status to Discord.
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
const statusURL = "https://lightswitch-public-service-prod06.ol.epicgames.com/lightswitch/api/service/bulk/status?serviceId=Fortnite"
const webhook = "https://discordapp.com/api/webhooks/.../..."
const interval = 298 * time.Second
const template = "Status %q: %s"
func notify(status, message string) {
log.Println("Sending message to Discord")
client := &http.Client{}
form := &url.Values{}
form.Set("content", fmt.Sprintf(template, status, message))
req, _ := http.NewRequest("POST", webhook, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
func check(prev string) (next string) {
defer func() {
err := recover()
if err != nil {
log.Println("panic:", err)
}
}()
log.Println("Checking status...")
client := &http.Client{}
req, _ := http.NewRequest("GET", statusURL, nil)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
data := make([]map[string]interface{}, 0)
err = json.NewDecoder(resp.Body).Decode(&data)
resp.Body.Close()
if err != nil {
panic(err)
}
status := data[0]["status"].(string)
message := data[0]["message"].(string)
log.Println("Status:", status)
if prev != "" && prev != status {
notify(status, message)
}
return status
}
func main() {
prev := ""
for {
prev = check(prev)
time.Sleep(interval)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment