-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·155 lines (137 loc) · 3.18 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"image"
"io/ioutil"
"log"
"net/http"
"os"
"tidbyt.dev/pixlet/encode"
"tidbyt.dev/pixlet/runtime"
)
const (
TidbytAPIPush = "https://api.tidbyt.com/v0/devices/%s/push"
APITokenEnv = "TIDBYT_API_TOKEN"
DeviceIdEnv = "TIDBYT_DEVICE_ID"
)
var (
apiToken string
deviceId string
installationID string
background bool
)
type TidbytPushJSON struct {
DeviceID string `json:"deviceID"`
Image string `json:"image"`
InstallationID string `json:"installationID"`
Background bool `json:"background"`
}
func starfile(content string) []byte {
starString :=
"load(\"render.star\", \"render\")\n" +
"def main(config):\n" +
" return render.Root(\n" +
" child = render.Box(\n" +
" render.WrappedText(\n" +
" content = \"" + content + "\"\n" +
" )\n" +
" )\n" +
" )\n"
return []byte(starString)
}
func doPost(imgContent string) bool {
apiToken = os.Getenv(APITokenEnv)
deviceId = os.Getenv(DeviceIdEnv)
payload, err := json.Marshal(
TidbytPushJSON{
DeviceID: deviceId,
Image: imgContent,
InstallationID: installationID,
Background: background,
})
if err != nil {
log.Printf("failed to marchal json: %v\n", err)
return false
}
client := &http.Client{}
req, err := http.NewRequest(
"POST",
fmt.Sprintf(TidbytAPIPush, deviceId),
bytes.NewReader(payload))
if err != nil {
log.Printf("creating POST request: %v\n", err)
return false
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiToken))
resp, err := client.Do(req)
if err != nil {
log.Printf("pushing to API: %v\n", err)
return false
}
if resp.StatusCode != 200 {
log.Printf("Tidbyt API returned status %s\n", resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
log.Println(string(body))
return false
}
return true
}
func imageGen(content string) string {
magnify := 1
script := "test.star"
src := starfile(content) // max 54 chars
applet := runtime.Applet{}
err := applet.Load(script, src, nil)
if err != nil {
log.Printf("Unable to load scriptfile %v\n", err)
}
config := map[string]string{}
roots, err := applet.Run(config)
if err != nil {
log.Printf("Error running script: %s\n", err)
os.Exit(1)
}
screens := encode.ScreensFromRoots(roots)
filter := func(input image.Image) (image.Image, error) {
if magnify <= 1 {
return input, nil
}
in, ok := input.(*image.RGBA)
if !ok {
return nil, fmt.Errorf("image not RGBA, very weird")
}
out := image.NewRGBA(
image.Rect(
0, 0,
in.Bounds().Dx()*magnify,
in.Bounds().Dy()*magnify),
)
for x := 0; x < in.Bounds().Dx(); x++ {
for y := 0; y < in.Bounds().Dy(); y++ {
for xx := 0; xx < 10; xx++ {
for yy := 0; yy < 10; yy++ {
out.SetRGBA(
x*magnify+xx,
y*magnify+yy,
in.RGBAAt(x, y),
)
}
}
}
}
return out, nil
}
var buf []byte
buf, err = screens.EncodeWebP(filter)
if err != nil {
log.Printf("Unable to encode to webp %v\n", err)
}
return base64.StdEncoding.EncodeToString(buf)
}
func main() {
encodedImgString := imageGen("This is some test content")
doPost(encodedImgString)
}