This repository has been archived on 2025-07-20. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
kindle-dashboard/imagegen/imagegen.go
AKP e988d0b978
Alter 2 files
Update `drawing.go`
Update `imagegen.go`
2023-11-19 19:15:47 +00:00

78 lines
1.7 KiB
Go

package imagegen
import (
"bytes"
"fmt"
"github.com/disintegration/imaging"
"github.com/tdewolff/canvas"
"github.com/tdewolff/canvas/renderers/rasterizer"
"image"
"image/png"
"strings"
)
type Config struct {
WxLocation string
MetOfficeDatapointAPIKey string
TrainsLocation string
RTTUsername string
RTTPassword string
}
const (
padding float64 = 20
imageWidth float64 = 800
imageHeight float64 = 600
)
func Generate(conf *Config) ([]byte, error) {
// TODO: hardcoded secrets ick
locStr, wx, err := getWeatherInLocation(conf.MetOfficeDatapointAPIKey, conf.WxLocation)
if err != nil {
return nil, err
}
trains, err := getNextTrains(conf.RTTUsername, conf.RTTPassword, conf.TrainsLocation)
if err != nil {
return nil, err
}
fmt.Println(strings.Join(trains, "\n"))
for _, w := range wx {
fmt.Printf("%#v\n", w)
}
c := canvas.New(imageWidth, imageHeight)
ctx := canvas.NewContext(c)
ctx.SetCoordSystem(canvas.CartesianIV)
ctx.SetFill(canvas.White)
ctx.DrawPath(0, 0, canvas.Rectangle(imageWidth, imageHeight))
bottom := drawTitle(ctx, padding)
bottom, err = drawWeather(ctx, bottom, locStr, wx)
if err != nil {
return nil, err
}
bottom, err = drawTrains(ctx, bottom, conf.TrainsLocation, trains)
if err != nil {
return nil, err
}
//bottom += padding
//
//ctx.SetFill(canvas.Red)
//ctx.DrawPath(1, bottom, canvas.Rectangle(900, 3))
rasterizer.New(imageWidth, imageHeight, canvas.DPMM(1), canvas.DefaultColorSpace)
var img image.Image
img = rasterizer.Draw(c, canvas.DPMM(1), canvas.DefaultColorSpace)
img = imaging.Rotate270(img)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
return nil, err
}
return buf.Bytes(), nil
}