all repos — honk @ 8b00c0b7d9a4f4548561d1a78251ff47c5a87f39

my fork of honk

toys/youvegothonks.go (view raw)

 1package main
 2
 3import (
 4	"encoding/json"
 5	"flag"
 6	"fmt"
 7	"io/ioutil"
 8	"log"
 9	"net/http"
10	"net/url"
11	"os"
12	"time"
13)
14
15type Honk struct {
16	ID     int
17	Honker string
18	Noise  string
19}
20
21type HonkSet struct {
22	Honks []Honk
23}
24
25func gethonks(server, token string, wanted int) HonkSet {
26	form := make(url.Values)
27	form.Add("action", "gethonks")
28	form.Add("page", "atme")
29	form.Add("after", fmt.Sprintf("%d", wanted))
30	form.Add("wait", "30")
31	apiurl := fmt.Sprintf("https://%s/api?%s", server, form.Encode())
32	req, err := http.NewRequest("GET", apiurl, nil)
33	if err != nil {
34		log.Fatal(err)
35	}
36	req.Header.Add("Authorization", "Bearer "+token)
37	resp, err := http.DefaultClient.Do(req)
38	if err != nil {
39		log.Fatal(err)
40	}
41	defer resp.Body.Close()
42	if resp.StatusCode != 200 {
43		answer, _ := ioutil.ReadAll(resp.Body)
44		log.Fatalf("status: %d: %s", resp.StatusCode, answer)
45	}
46	var honks HonkSet
47	d := json.NewDecoder(resp.Body)
48	err = d.Decode(&honks)
49	if err != nil {
50		log.Fatal(err)
51	}
52	return honks
53}
54
55func main() {
56	server := ""
57	token := ""
58	flag.StringVar(&server, "server", server, "server to connnect")
59	flag.StringVar(&token, "token", token, "auth token to use")
60	flag.Parse()
61
62	if server == "" || token == "" {
63		flag.Usage()
64		os.Exit(1)
65	}
66
67	wanted := 0
68
69	for {
70		honks := gethonks(server, token, wanted)
71		for _, h := range honks.Honks {
72			fmt.Printf("you've got a honk from %s\n%s\n", h.Honker, h.Noise)
73			if wanted < h.ID {
74				wanted = h.ID
75			}
76		}
77		time.Sleep(1 * time.Second)
78	}
79}