53 lines
871 B
Go
53 lines
871 B
Go
package spacepanel_aggregator
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func Poll(url string) {
|
|
for true {
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
ErrorinPoll(url)
|
|
fmt.Println(err.Error())
|
|
} else {
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
ErrorinPoll(url)
|
|
resp.Body.Close()
|
|
} else {
|
|
resp.Body.Close()
|
|
|
|
var parsed V13
|
|
err = json.Unmarshal(data, &parsed)
|
|
if err != nil {
|
|
ErrorinPoll(url)
|
|
} else {
|
|
if parsed.State.Open != nil {
|
|
open := parsed.State.Open.(bool)
|
|
if open {
|
|
spacestates[url] = Open
|
|
} else {
|
|
spacestates[url] = Close
|
|
}
|
|
} else {
|
|
spacestates[url] = Unknown
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
time.Sleep(sleeptime)
|
|
}
|
|
}
|
|
|
|
func ErrorinPoll(url string) {
|
|
if spacestates[url] != Unknown {
|
|
spacestates[url] = Outdated
|
|
}
|
|
}
|