-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
76 lines (68 loc) · 2.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"bytes"
"net/http"
"io"
"io/ioutil"
"encoding/json"
"strconv"
)
func CheckSpeeds(w http.ResponseWriter, r *http.Request, header Header) {
var speeds Speeds
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 10485))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, &speeds); err != nil {
w.Header().Set("Content-Type", "applicatin/json; charset=UTF-8")
w.WriteHeader(422)
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
i, err := strconv.ParseFloat(speeds.Download, 32)
if err != nil {
panic(err)
}
if (i < 10.0) {
SendTweet(speeds, header)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(speeds); err != nil {
panic(err)
}
}
func SendTweet(speeds Speeds, header Header) {
status := "Switched from BCS 50 Mbps $50/month to @TWC 15 Mbps $50/month to actually get Download: " + speeds.Download + " Mbps Upload: " + speeds.Upload + " Mbps. Not a happy camper."
status = QEscape(status)
header = UpdateHeaderValues(header, status)
client := &http.Client{}
req, err := http.NewRequest("POST", "https://api.twitter.com/1.1/statuses/update.json?include_entities=true", bytes.NewBufferString("status=" + status))
if err != nil {
panic(err)
}
headerString := GetCompleteHeaderString(header)
fmt.Printf("%s\n\n", headerString)
req.Header.Add("Authorization", headerString)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len("status=" + status)))
res, err := client.Do(req)
if err != nil {
panic(err)
}
body, _ := ioutil.ReadAll(io.LimitReader(res.Body, 10485678))
fmt.Printf("%s\n\n", string(body))
res.Body.Close()
}
func main() {
header := GetHeaderValues()
http.HandleFunc("/speeds", func(w http.ResponseWriter, r *http.Request) {
CheckSpeeds(w, r, *header)
})
http.ListenAndServe(":8080", nil)
}