honk/toys/youvegothonks.go

80 lines
1.5 KiB
Go
Raw Normal View History

2019-11-12 07:09:08 +01:00
package main
import (
2020-09-11 20:27:14 +02:00
"encoding/json"
2019-11-12 07:09:08 +01:00
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
)
type Honk struct {
2020-09-11 20:27:14 +02:00
ID int
2019-11-12 07:09:08 +01:00
Honker string
2020-09-11 20:27:14 +02:00
Noise string
2019-11-12 07:09:08 +01:00
}
type HonkSet struct {
Honks []Honk
}
func gethonks(server, token string, wanted int) HonkSet {
form := make(url.Values)
form.Add("action", "gethonks")
form.Add("page", "atme")
form.Add("after", fmt.Sprintf("%d", wanted))
form.Add("wait", "30")
apiurl := fmt.Sprintf("https://%s/api?%s", server, form.Encode())
req, err := http.NewRequest("GET", apiurl, nil)
if err != nil {
log.Fatal(err)
}
2020-09-11 20:27:14 +02:00
req.Header.Add("Authorization", "Bearer "+token)
2019-11-12 07:09:08 +01:00
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
answer, _ := ioutil.ReadAll(resp.Body)
log.Fatalf("status: %d: %s", resp.StatusCode, answer)
}
var honks HonkSet
d := json.NewDecoder(resp.Body)
err = d.Decode(&honks)
if err != nil {
log.Fatal(err)
}
return honks
}
func main() {
server := ""
token := ""
flag.StringVar(&server, "server", server, "server to connnect")
flag.StringVar(&token, "token", token, "auth token to use")
flag.Parse()
if server == "" || token == "" {
flag.Usage()
os.Exit(1)
}
wanted := 0
for {
honks := gethonks(server, token, wanted)
for _, h := range honks.Honks {
fmt.Printf("you've got a honk from %s\n%s\n", h.Honker, h.Noise)
if wanted < h.ID {
wanted = h.ID
}
}
time.Sleep(1 * time.Second)
}
}