all repos — honk @ 0f15b008db60466e6ae08733903f6a9db9bb4a23

my fork of honk

toys/autobonker.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	"strings"
 13	"time"
 14)
 15
 16type Honk struct {
 17	ID     int
 18	XID    string
 19	Honker string
 20}
 21
 22type HonkSet struct {
 23	Honks []Honk
 24}
 25
 26func gethonks(server, token string, wanted int) HonkSet {
 27	form := make(url.Values)
 28	form.Add("action", "gethonks")
 29	form.Add("page", "atme")
 30	form.Add("after", fmt.Sprintf("%d", wanted))
 31	form.Add("wait", "30")
 32	apiurl := fmt.Sprintf("https://%s/api?%s", server, form.Encode())
 33	req, err := http.NewRequest("GET", apiurl, nil)
 34	if err != nil {
 35		log.Fatal(err)
 36	}
 37	req.Header.Add("Authorization", "Bearer "+token)
 38	resp, err := http.DefaultClient.Do(req)
 39	if err != nil {
 40		log.Fatal(err)
 41	}
 42	defer resp.Body.Close()
 43	if resp.StatusCode != 200 {
 44		answer, _ := ioutil.ReadAll(resp.Body)
 45		log.Fatalf("status: %d: %s", resp.StatusCode, answer)
 46	}
 47	var honks HonkSet
 48	d := json.NewDecoder(resp.Body)
 49	err = d.Decode(&honks)
 50	if err != nil {
 51		log.Fatal(err)
 52	}
 53	return honks
 54}
 55
 56func bonk(server, token string, honk Honk) {
 57	log.Printf("bonking %s from %s", honk.XID, honk.Honker)
 58	form := make(url.Values)
 59	form.Add("action", "zonkit")
 60	form.Add("wherefore", "bonk")
 61	form.Add("what", honk.XID)
 62	apiurl := fmt.Sprintf("https://%s/api", server)
 63	req, err := http.NewRequest("POST", apiurl, strings.NewReader(form.Encode()))
 64	if err != nil {
 65		log.Fatal(err)
 66	}
 67	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
 68	req.Header.Add("Authorization", "Bearer "+token)
 69	resp, err := http.DefaultClient.Do(req)
 70	if err != nil {
 71		log.Fatal(err)
 72	}
 73	defer resp.Body.Close()
 74	if resp.StatusCode != 200 {
 75		answer, _ := ioutil.ReadAll(resp.Body)
 76		log.Fatalf("status: %d: %s", resp.StatusCode, answer)
 77	}
 78}
 79
 80func main() {
 81	server := ""
 82	token := ""
 83	flag.StringVar(&server, "server", server, "server to connnect")
 84	flag.StringVar(&token, "token", token, "auth token to use")
 85	flag.Parse()
 86
 87	if server == "" || token == "" {
 88		flag.Usage()
 89		os.Exit(1)
 90	}
 91
 92	wanted := 0
 93
 94	for {
 95		honks := gethonks(server, token, wanted)
 96		for i, h := range honks.Honks {
 97			bonk(server, token, h)
 98			if i > 0 {
 99				time.Sleep(3 * time.Second)
100			}
101			if wanted < h.ID {
102				wanted = h.ID
103			}
104
105		}
106		time.Sleep(3 * time.Second)
107	}
108}