mirror of
https://github.com/Crocmagnon/display-epaper.git
synced 2024-12-04 19:53:38 +01:00
add quotes
This commit is contained in:
parent
73d35661c0
commit
2c5035ee37
6 changed files with 169 additions and 22 deletions
11
fete/fete.go
11
fete/fete.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/carlmjohnson/requests"
|
"github.com/carlmjohnson/requests"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -83,16 +82,6 @@ func (c *Client) GetFete(ctx context.Context, date time.Time) (res *Fete, err er
|
||||||
err = requests.URL("https://fetedujour.fr").
|
err = requests.URL("https://fetedujour.fr").
|
||||||
Pathf("/api/v2/%v/json-normal-%d-%d", c.config.APIKey, date.Day(), date.Month()).
|
Pathf("/api/v2/%v/json-normal-%d-%d", c.config.APIKey, date.Day(), date.Month()).
|
||||||
UserAgent("e-paper-display").
|
UserAgent("e-paper-display").
|
||||||
AddValidator(func(resp *http.Response) error {
|
|
||||||
if resp.StatusCode >= 400 {
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return fmt.Errorf("Fete API error: %s, %s", resp.Header, string(body))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}).
|
|
||||||
Client(c.client).
|
Client(c.client).
|
||||||
ToJSON(&res).
|
ToJSON(&res).
|
||||||
Fetch(ctx)
|
Fetch(ctx)
|
||||||
|
|
24
img.go
24
img.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Crocmagnon/display-epaper/epd"
|
"github.com/Crocmagnon/display-epaper/epd"
|
||||||
"github.com/Crocmagnon/display-epaper/fete"
|
"github.com/Crocmagnon/display-epaper/fete"
|
||||||
|
"github.com/Crocmagnon/display-epaper/quotes"
|
||||||
"github.com/Crocmagnon/display-epaper/transports"
|
"github.com/Crocmagnon/display-epaper/transports"
|
||||||
"github.com/Crocmagnon/display-epaper/weather"
|
"github.com/Crocmagnon/display-epaper/weather"
|
||||||
"github.com/llgcode/draw2d"
|
"github.com/llgcode/draw2d"
|
||||||
|
@ -29,7 +30,14 @@ const (
|
||||||
rightX = 530
|
rightX = 530
|
||||||
)
|
)
|
||||||
|
|
||||||
func getBlack(ctx context.Context, nowFunc func() time.Time, transportsClient *transports.Client, feteClient *fete.Client, weatherClient *weather.Client) (*image.RGBA, error) {
|
func getBlack(
|
||||||
|
ctx context.Context,
|
||||||
|
nowFunc func() time.Time,
|
||||||
|
transportsClient *transports.Client,
|
||||||
|
feteClient *fete.Client,
|
||||||
|
weatherClient *weather.Client,
|
||||||
|
quotesClient *quotes.Client,
|
||||||
|
) (*image.RGBA, error) {
|
||||||
bus, err := transportsClient.GetTCLPassages(ctx, 290)
|
bus, err := transportsClient.GetTCLPassages(ctx, 290)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error getting bus:", err)
|
log.Println("error getting bus:", err)
|
||||||
|
@ -50,6 +58,10 @@ func getBlack(ctx context.Context, nowFunc func() time.Time, transportsClient *t
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error getting weather:", err)
|
log.Println("error getting weather:", err)
|
||||||
}
|
}
|
||||||
|
quote, err := quotesClient.GetQuote(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error getting quotes:", err)
|
||||||
|
}
|
||||||
|
|
||||||
img := newWhite()
|
img := newWhite()
|
||||||
|
|
||||||
|
@ -65,10 +77,20 @@ func getBlack(ctx context.Context, nowFunc func() time.Time, transportsClient *t
|
||||||
drawDate(gc, nowFunc())
|
drawDate(gc, nowFunc())
|
||||||
drawFete(gc, fetes)
|
drawFete(gc, fetes)
|
||||||
drawWeather(gc, wthr)
|
drawWeather(gc, wthr)
|
||||||
|
drawQuote(gc, quote)
|
||||||
|
|
||||||
return img, nil
|
return img, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func drawQuote(gc *draw2dimg.GraphicContext, quote *quotes.Quote) {
|
||||||
|
if quote == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
text(gc, quote.Citation.Citation, 15, leftX, 450)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func drawWeather(gc *draw2dimg.GraphicContext, wthr *weather.Prevision) {
|
func drawWeather(gc *draw2dimg.GraphicContext, wthr *weather.Prevision) {
|
||||||
if wthr == nil {
|
if wthr == nil {
|
||||||
return
|
return
|
||||||
|
|
7
main.go
7
main.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/Crocmagnon/display-epaper/fete"
|
"github.com/Crocmagnon/display-epaper/fete"
|
||||||
|
"github.com/Crocmagnon/display-epaper/quotes"
|
||||||
"github.com/Crocmagnon/display-epaper/transports"
|
"github.com/Crocmagnon/display-epaper/transports"
|
||||||
"github.com/Crocmagnon/display-epaper/weather"
|
"github.com/Crocmagnon/display-epaper/weather"
|
||||||
"github.com/golang/freetype/truetype"
|
"github.com/golang/freetype/truetype"
|
||||||
|
@ -39,7 +40,11 @@ func main() {
|
||||||
APIKey: os.Getenv("WEATHER_API_KEY"),
|
APIKey: os.Getenv("WEATHER_API_KEY"),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := run(ctx, transportsClient, feteClient, weatherClient); err != nil {
|
quotesClient := quotes.New(nil, quotes.Config{
|
||||||
|
CacheLocation: os.Getenv("QUOTES_CACHE_LOCATION"),
|
||||||
|
})
|
||||||
|
|
||||||
|
if err := run(ctx, transportsClient, feteClient, weatherClient, quotesClient); err != nil {
|
||||||
log.Fatal("error: ", err)
|
log.Fatal("error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
105
quotes/quotes.go
Normal file
105
quotes/quotes.go
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
package quotes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/carlmjohnson/requests"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
CacheLocation string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
client *http.Client
|
||||||
|
config Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(client *http.Client, config Config) *Client {
|
||||||
|
return &Client{client: client, config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Quote struct {
|
||||||
|
Status int `json:"status"`
|
||||||
|
Code int `json:"code"`
|
||||||
|
Error string `json:"error"`
|
||||||
|
Citation struct {
|
||||||
|
Citation string `json:"citation"`
|
||||||
|
Infos struct {
|
||||||
|
Auteur string `json:"auteur"`
|
||||||
|
Acteur string `json:"acteur"`
|
||||||
|
Personnage string `json:"personnage"`
|
||||||
|
Saison string `json:"saison"`
|
||||||
|
Episode string `json:"episode"`
|
||||||
|
} `json:"infos"`
|
||||||
|
} `json:"citation"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var errTooOld = errors.New("quote is too old")
|
||||||
|
|
||||||
|
func loadFromDisk(location string) (Quote, error) {
|
||||||
|
stat, err := os.Stat(location)
|
||||||
|
if err != nil {
|
||||||
|
return Quote{}, fmt.Errorf("getting file info: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if stat.ModTime().Add(24 * time.Hour).Before(time.Now()) {
|
||||||
|
return Quote{}, errTooOld
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(location)
|
||||||
|
if err != nil {
|
||||||
|
return Quote{}, fmt.Errorf("opening quote: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var res Quote
|
||||||
|
if err = json.NewDecoder(file).Decode(&res); err != nil {
|
||||||
|
return Quote{}, fmt.Errorf("decoding quote: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q Quote) dumpToDisk(location string) error {
|
||||||
|
file, err := os.Create(location)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("creating quote: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if err = json.NewEncoder(file).Encode(q); err != nil {
|
||||||
|
return fmt.Errorf("dumping quote: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetQuote(ctx context.Context) (res *Quote, err error) {
|
||||||
|
if val, err := loadFromDisk(c.config.CacheLocation); nil == err {
|
||||||
|
log.Println("found quote in cache")
|
||||||
|
return &val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("querying quotes")
|
||||||
|
err = requests.URL("https://kaamelott.chaudie.re/api/random").
|
||||||
|
Client(c.client).
|
||||||
|
ToJSON(&res).
|
||||||
|
Fetch(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("fetching quotes: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := res.dumpToDisk(c.config.CacheLocation); err != nil {
|
||||||
|
log.Printf("error dumping files to disk: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/Crocmagnon/display-epaper/fete"
|
"github.com/Crocmagnon/display-epaper/fete"
|
||||||
|
"github.com/Crocmagnon/display-epaper/quotes"
|
||||||
"github.com/Crocmagnon/display-epaper/transports"
|
"github.com/Crocmagnon/display-epaper/transports"
|
||||||
"github.com/Crocmagnon/display-epaper/weather"
|
"github.com/Crocmagnon/display-epaper/weather"
|
||||||
"github.com/llgcode/draw2d/draw2dimg"
|
"github.com/llgcode/draw2d/draw2dimg"
|
||||||
|
@ -15,14 +16,22 @@ func run(
|
||||||
transportsClient *transports.Client,
|
transportsClient *transports.Client,
|
||||||
feteClient *fete.Client,
|
feteClient *fete.Client,
|
||||||
weatherClient *weather.Client,
|
weatherClient *weather.Client,
|
||||||
|
quotesClient *quotes.Client,
|
||||||
) error {
|
) error {
|
||||||
img, err := getBlack(ctx, func() time.Time {
|
img, err := getBlack(
|
||||||
|
ctx,
|
||||||
|
func() time.Time {
|
||||||
t, err := time.Parse(time.DateOnly, "2024-08-01zzz")
|
t, err := time.Parse(time.DateOnly, "2024-08-01zzz")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
return t
|
return t
|
||||||
}, transportsClient, feteClient, weatherClient)
|
},
|
||||||
|
transportsClient,
|
||||||
|
feteClient,
|
||||||
|
weatherClient,
|
||||||
|
quotesClient,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Crocmagnon/display-epaper/epd"
|
"github.com/Crocmagnon/display-epaper/epd"
|
||||||
"github.com/Crocmagnon/display-epaper/fete"
|
"github.com/Crocmagnon/display-epaper/fete"
|
||||||
|
"github.com/Crocmagnon/display-epaper/quotes"
|
||||||
"github.com/Crocmagnon/display-epaper/transports"
|
"github.com/Crocmagnon/display-epaper/transports"
|
||||||
"github.com/Crocmagnon/display-epaper/weather"
|
"github.com/Crocmagnon/display-epaper/weather"
|
||||||
"log"
|
"log"
|
||||||
|
@ -17,6 +18,7 @@ func run(
|
||||||
transportsClient *transports.Client,
|
transportsClient *transports.Client,
|
||||||
feteClient *fete.Client,
|
feteClient *fete.Client,
|
||||||
weatherClient *weather.Client,
|
weatherClient *weather.Client,
|
||||||
|
quotesClient *quotes.Client,
|
||||||
) error {
|
) error {
|
||||||
_, err := host.Init()
|
_, err := host.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -36,7 +38,14 @@ func run(
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
err = loop(ctx, display, transportsClient, feteClient, weatherClient)
|
err = loop(
|
||||||
|
ctx,
|
||||||
|
display,
|
||||||
|
transportsClient,
|
||||||
|
feteClient,
|
||||||
|
weatherClient,
|
||||||
|
quotesClient,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error looping: %v\n", err)
|
log.Printf("error looping: %v\n", err)
|
||||||
}
|
}
|
||||||
|
@ -52,6 +61,7 @@ func loop(
|
||||||
transportsClient *transports.Client,
|
transportsClient *transports.Client,
|
||||||
feteClient *fete.Client,
|
feteClient *fete.Client,
|
||||||
weatherClient *weather.Client,
|
weatherClient *weather.Client,
|
||||||
|
quotesClient *quotes.Client,
|
||||||
) error {
|
) error {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := display.Sleep(); err != nil {
|
if err := display.Sleep(); err != nil {
|
||||||
|
@ -66,7 +76,14 @@ func loop(
|
||||||
|
|
||||||
display.Clear()
|
display.Clear()
|
||||||
|
|
||||||
black, err := getBlack(ctx, time.Now, transportsClient, feteClient, weatherClient)
|
black, err := getBlack(
|
||||||
|
ctx,
|
||||||
|
time.Now,
|
||||||
|
transportsClient,
|
||||||
|
feteClient,
|
||||||
|
weatherClient,
|
||||||
|
quotesClient,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("getting black: %w", err)
|
return fmt.Errorf("getting black: %w", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue