-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (49 loc) · 1008 Bytes
/
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"golang.org/x/net/websocket"
)
type Frame struct {
Type string `json:"type"`
Payload Payload `json:"payload"`
}
type Payload struct {
SecondsLeft float64 `json:"seconds_left"`
}
const origin = "http://localhost/"
var wsRe = regexp.MustCompile(`wss://wss.redditmedia.com/thebutton\?h=[0-9a-f]*&e=[0-9a-f]*`)
func getWsUrl() (string, error) {
resp, err := http.Get("https://reddit.com/r/thebutton")
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
match := wsRe.Find(body)
if match == nil {
return "", errors.New("Could not find websocket URL")
}
return string(match), nil
}
func main() {
url, err := getWsUrl()
if err != nil {
log.Fatal(err)
}
ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}
for {
var data Frame
websocket.JSON.Receive(ws, &data)
fmt.Println(data.Payload.SecondsLeft)
}
}