From 7dd1b8c85192816290a4d872aeb8417f7578eace Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Fri, 11 Sep 2020 14:27:14 -0400 Subject: [PATCH] add an autobonker --- toys/Makefile | 5 +- toys/README | 2 + toys/autobonker.go | 108 ++++++++++++++++++++++++++++++++++++++++++ toys/sprayandpray.go | 1 - toys/youvegothonks.go | 8 ++-- 5 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 toys/autobonker.go diff --git a/toys/Makefile b/toys/Makefile index dcf7319..4b75678 100644 --- a/toys/Makefile +++ b/toys/Makefile @@ -1,5 +1,8 @@ -all: gettoken saytheday sprayandpray youvegothonks +all: autobonker gettoken saytheday sprayandpray youvegothonks + +autobonker: autobonker.go + go build autobonker.go gettoken: gettoken.go go build gettoken.go diff --git a/toys/README b/toys/README index 71479b7..c2da266 100644 --- a/toys/README +++ b/toys/README @@ -2,6 +2,8 @@ These are all standalone programs, meant to be compiled individually. A little of this, a little of that. +autobonker.go - repeats mentioned posts + gettoken.go - obtains an authorization token saytheday.go - posts a new honk that's a date based look and say sequence diff --git a/toys/autobonker.go b/toys/autobonker.go new file mode 100644 index 0000000..045b453 --- /dev/null +++ b/toys/autobonker.go @@ -0,0 +1,108 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "net/url" + "os" + "strings" + "time" +) + +type Honk struct { + ID int + XID string + Honker string +} + +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) + } + req.Header.Add("Authorization", "Bearer "+token) + 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 bonk(server, token string, honk Honk) { + log.Printf("bonking %s from %s", honk.XID, honk.Honker) + form := make(url.Values) + form.Add("action", "zonkit") + form.Add("wherefore", "bonk") + form.Add("what", honk.XID) + apiurl := fmt.Sprintf("https://%s/api", server) + req, err := http.NewRequest("POST", apiurl, strings.NewReader(form.Encode())) + if err != nil { + log.Fatal(err) + } + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.Header.Add("Authorization", "Bearer "+token) + 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) + } +} + +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 i, h := range honks.Honks { + bonk(server, token, h) + if i > 0 { + time.Sleep(3 * time.Second) + } + if wanted < h.ID { + wanted = h.ID + } + + } + time.Sleep(3 * time.Second) + } +} diff --git a/toys/sprayandpray.go b/toys/sprayandpray.go index 44ee302..94de273 100644 --- a/toys/sprayandpray.go +++ b/toys/sprayandpray.go @@ -11,7 +11,6 @@ import ( "strings" ) - func sendmsg(server, token, msg, rcpt string) { form := make(url.Values) form.Add("token", token) diff --git a/toys/youvegothonks.go b/toys/youvegothonks.go index aa02ae9..3892c1b 100644 --- a/toys/youvegothonks.go +++ b/toys/youvegothonks.go @@ -1,11 +1,11 @@ package main import ( + "encoding/json" "flag" "fmt" "io/ioutil" "log" - "encoding/json" "net/http" "net/url" "os" @@ -13,9 +13,9 @@ import ( ) type Honk struct { - ID int + ID int Honker string - Noise string + Noise string } type HonkSet struct { @@ -33,7 +33,7 @@ func gethonks(server, token string, wanted int) HonkSet { if err != nil { log.Fatal(err) } - req.Header.Add("Authorization", "Bearer " + token) + req.Header.Add("Authorization", "Bearer "+token) resp, err := http.DefaultClient.Do(req) if err != nil { log.Fatal(err)