/
post.go
134 lines (115 loc) · 3.07 KB
/
post.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"fmt"
"image/png"
"log"
"math/rand"
"mime/multipart"
"net/http"
"time"
"github.com/unixpickle/gocube"
"github.com/unixpickle/rubiksimg"
)
// PostLoop runs forever, checking if the day changes and
// posting a new scramble whenever it does.
// The postNow channel can be used to trigger premature posts.
func PostLoop(s *State, loc *time.Location, postNow <-chan struct{}) {
scrambles := scrambleGenerator()
lastPost := time.Now().In(loc)
for {
forcePost := false
sleepTimeout := time.After(time.Minute / 2)
select {
case <-postNow:
forcePost = true
case <-sleepTimeout:
}
if s.NeedAccessToken() || s.NeedGroupID() {
continue
}
now := time.Now().In(loc)
if s.DaysRemaining() <= 0 {
log.Println("Token expired.")
s.Reset()
continue
}
if forcePost || now.Day() != lastPost.Day() {
lastPost = now
postScramble(s, <-scrambles)
}
}
}
func postScramble(s *State, scramble []gocube.Move) {
token, groupID := s.PostInfo()
if token == "" || groupID == "" {
log.Println("Missing token or group ID while posting")
return
}
log.Println("Posting scramble:", scramble)
u := "https://graph.facebook.com/v2.5/" + groupID + "/photos?access_token=" + token
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("source", "cube.png")
part.Write(imageForScramble(scramble))
writer.WriteField("caption", messageForScramble(scramble))
writer.Close()
req, _ := http.NewRequest("POST", u, body)
req.Header.Add("Content-Type", writer.FormDataContentType())
client := http.Client{}
resp, _ := client.Do(req)
if resp != nil {
resp.Body.Close()
}
}
func scrambleGenerator() <-chan []gocube.Move {
rand.Seed(time.Now().UnixNano())
ch := make(chan []gocube.Move)
go func() {
p1Moves := gocube.NewPhase1Moves()
p1Heuristic := gocube.NewPhase1Heuristic(p1Moves)
p2Moves := gocube.NewPhase2Moves()
p2Heuristic := gocube.NewPhase2Heuristic(p2Moves, false)
tables := gocube.SolverTables{p1Heuristic, p1Moves, p2Heuristic, p2Moves}
for {
state := gocube.RandomCubieCube()
solver := gocube.NewSolverTables(state, 30, tables)
timeout := time.After(time.Second * 30)
solution := <-solver.Solutions()
SolutionLoop:
for {
select {
case sol, ok := <-solver.Solutions():
if !ok {
break SolutionLoop
}
solution = sol
case <-timeout:
break SolutionLoop
}
}
solver.Stop()
ch <- solution
}
}()
return ch
}
func messageForScramble(scramble []gocube.Move) string {
return "Scramble of the day:\n" + stringForScramble(scramble)
}
func imageForScramble(scramble []gocube.Move) []byte {
cube := gocube.SolvedCubieCube()
for _, move := range scramble {
cube.Move(move)
}
stickers := cube.StickerCube()
caption := stringForScramble(scramble)
image := rubiksimg.GenerateCaptionedImage(666, 33, caption, stickers)
var output bytes.Buffer
png.Encode(&output, image)
return output.Bytes()
}
func stringForScramble(scramble []gocube.Move) string {
solutionStr := fmt.Sprint(scramble)
return solutionStr[1 : len(solutionStr)-1]
}