alba/backend/main.go

129 lines
2.7 KiB
Go
Raw Normal View History

2022-04-20 11:16:55 +02:00
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"text/template"
"time"
2022-04-20 12:08:03 +02:00
"github.com/mmcdole/gofeed"
2022-04-20 11:16:55 +02:00
"gogs.carduccidante.edu.it/karmen/core/util/pandoc"
tplutil "gogs.carduccidante.edu.it/karmen/core/util/template"
)
var funcmap = template.FuncMap{
2022-04-26 21:58:12 +02:00
"kitchen": func() string {
return time.Now().Format(time.Kitchen)
},
2022-04-21 11:29:16 +02:00
"day": func() int {
_, _, day := time.Now().Date()
return day
},
"month": func() int {
_, month, _ := time.Now().Date()
return int(month)
},
"year": func() int {
year, _, _ := time.Now().Date()
return year
},
"news": func() []*gofeed.Item {
result := make([]*gofeed.Item, 0)
2022-04-20 11:16:55 +02:00
2022-04-21 11:29:16 +02:00
fp := gofeed.NewParser()
2022-04-27 19:41:34 +02:00
feed, _ := fp.ParseURL("https://www.agi.it/cronaca/rss")
2022-04-21 11:29:16 +02:00
for _, it := range feed.Items {
result = append(result, it)
}
return result[:2]
},
"weather": func() []*gofeed.Item {
result := make([]*gofeed.Item, 0)
fp := gofeed.NewParser()
feed, _ := fp.ParseURL("https://www.meteowebcam.eu/rss.xml")
for _, it := range feed.Items {
result = append(result, it)
}
return result[:1]
2022-04-20 11:16:55 +02:00
},
}
2022-04-26 11:57:18 +02:00
func convertPDFToBMP(filename string) error {
2022-04-20 11:30:18 +02:00
out, err := exec.Command("pdftoppm", filename, "./data/screen", "-png").CombinedOutput()
2022-04-20 11:16:55 +02:00
if string(out) != "" {
2022-04-20 11:30:18 +02:00
return fmt.Errorf("pdftoppm: %s", string(out))
2022-04-20 11:16:55 +02:00
}
if err != nil {
return err
}
2022-04-26 11:57:18 +02:00
out, err = exec.Command("convert", "./data/screen-1.png",
"-rotate", "-90",
"-resize", "800x480!",
"-dither", "none",
"-remap", "./data/palette.gif",
"BMP3:./data/screen.bmp",
).CombinedOutput()
if string(out) != "" {
return fmt.Errorf("covert: %s", string(out))
}
if err != nil {
return err
}
2022-04-20 11:16:55 +02:00
return nil
}
func serveScreen(res http.ResponseWriter, req *http.Request) {
2022-04-20 11:30:18 +02:00
tpl, err := tplutil.LoadTextTemplate("./data/screen.tpl.md", funcmap)
2022-04-20 11:16:55 +02:00
if err != nil {
panic(err)
}
f, err := os.Create("./data/screen.md")
if err != nil {
panic(err)
}
defer f.Close()
2022-04-20 12:08:03 +02:00
var data struct {
Title string
Item *gofeed.Item
}
2022-04-20 11:16:55 +02:00
log.Println("Generating markdown file from template.")
2022-04-20 12:08:03 +02:00
if err := tpl.Execute(f, data); err != nil {
2022-04-20 11:16:55 +02:00
panic(err)
}
log.Println("Converting markdown to pdf.")
2022-04-26 11:25:39 +02:00
// err = pandoc.Convert("./data/screen.md", "./data/screen.pdf", "-V", "geometry:papersize={3.8in,6in}", "-V", "pagestyle:empty")
err = pandoc.Convert("./data/screen.md", "./data/screen.pdf", "-V", "pagestyle:empty")
2022-04-20 11:16:55 +02:00
if err != nil {
panic(err)
}
2022-04-26 11:57:18 +02:00
log.Println("Converting PDF to monochrome BMP.")
err = convertPDFToBMP("./data/screen.pdf")
2022-04-20 11:30:18 +02:00
if err != nil {
panic(err)
}
2022-04-20 11:16:55 +02:00
log.Println("Serving screen.png...")
2022-04-26 11:57:18 +02:00
http.ServeFile(res, req, "./data/screen.bmp")
2022-04-20 11:16:55 +02:00
}
func main() {
http.HandleFunc("/", serveScreen)
log.Println("Start alba backend...")
2022-04-20 12:43:54 +02:00
http.ListenAndServe(":3000", nil)
2022-04-20 11:16:55 +02:00
}