all repos — honk @ eb1a21b4677fa0c1df8d6fb2d74a5dc2713cca80

my fork of honk

add the toys collection
Ted Unangst tedu@tedunangst.com
Thu, 07 Nov 2019 16:50:52 -0500
commit

eb1a21b4677fa0c1df8d6fb2d74a5dc2713cca80

parent

e0431c50ef391b28e0be4ee5e17535ab2a6350b9

4 files changed, 147 insertions(+), 0 deletions(-)

jump to
A toys/Makefile

@@ -0,0 +1,8 @@

+ +all: gettoken saytheday + +gettoken: gettoken.go + go build gettoken.go + +saytheday: saytheday.go + go build saytheday.go
A toys/README

@@ -0,0 +1,3 @@

+These are all standalone programs, meant to be compiled individually. + +A little of this, a little of that.
A toys/gettoken.go

@@ -0,0 +1,53 @@

+package main + +import ( + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "net/url" + "os" + "strings" +) + +func main() { + server := "" + username := "" + password := "" + + flag.StringVar(&server, "server", server, "server to connnect") + flag.StringVar(&username, "username", username, "username to use") + flag.StringVar(&password, "password", password, "password to use") + flag.Parse() + + if server == "" || username == "" || password == "" { + flag.Usage() + os.Exit(1) + } + + form := make(url.Values) + form.Add("username", username) + form.Add("password", password) + form.Add("gettoken", "1") + loginurl := fmt.Sprintf("https://%s/dologin", server) + req, err := http.NewRequest("POST", loginurl, strings.NewReader(form.Encode())) + if err != nil { + log.Fatal(err) + } + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + answer, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + if resp.StatusCode != 200 { + log.Fatalf("status: %d: %s", resp.StatusCode, answer) + } + fmt.Println(string(answer)) +}
A toys/saytheday.go

@@ -0,0 +1,83 @@

+package main + +import ( + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "net/url" + "os" + "strings" + "time" +) + +func lookandsay(n int) string { + s := "1" + + numbers := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"} + var buf strings.Builder + for i := 2; i <= n; i++ { + count := 1 + prev := s[0] + for j := 1; j < len(s); j++ { + d := s[j] + if d == prev { + count++ + } else { + buf.WriteString(numbers[count]) + buf.WriteByte(prev) + count = 1 + prev = d + } + } + buf.WriteString(numbers[count]) + buf.WriteByte(prev) + s = buf.String() + buf.Reset() + } + return s +} + +func honkahonk(server, token, noise string) { + form := make(url.Values) + form.Add("token", token) + form.Add("action", "honk") + form.Add("noise", noise) + 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") + resp, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + answer, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + if resp.StatusCode != 200 { + 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) + } + + day := time.Now().Day() + say := lookandsay(day) + + honkahonk(server, token, say) +}