79 lines
1.4 KiB
Go
Raw Normal View History

2018-03-20 20:09:15 +00:00
package main
import (
"bytes"
"flag"
"fmt"
"image"
"io/ioutil"
"os"
"path"
2019-04-23 05:45:17 +00:00
"github.com/akyoto/color"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-03-20 20:09:15 +00:00
)
func main() {
color.Yellow("Updating anime image average colors")
defer color.Green("Finished.")
2018-03-21 04:15:03 +00:00
defer arn.Node.Close()
2018-03-20 20:09:15 +00:00
// Parse flags
var animeID string
flag.StringVar(&animeID, "id", "", "ID of the anime that you want to refresh")
flag.Parse()
// Refresh 1 anime in case ID was specified
if animeID != "" {
anime, _ := arn.GetAnime(animeID)
if anime != nil {
work(anime)
}
return
}
// Otherwise refresh all anime
for anime := range arn.StreamAnime() {
work(anime)
}
}
// work refreshes the average color of the given anime.
func work(anime *arn.Anime) {
2018-03-23 14:05:14 +00:00
base := path.Join(arn.Root, "/images/anime/medium/", anime.ID)
2018-03-20 20:09:15 +00:00
if _, err := os.Stat(base + ".jpg"); err != nil {
color.Red(err.Error())
return
}
update(anime, base+".jpg")
}
// update expects a file to load as image for the anime and update the average color.
func update(anime *arn.Anime, filePath string) {
fmt.Println(anime.ID, anime)
// Load
data, err := ioutil.ReadFile(filePath)
if err != nil {
color.Red(err.Error())
return
}
// Decode
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
color.Red(err.Error())
return
}
2018-03-20 20:22:16 +00:00
anime.Image.AverageColor = arn.GetAverageColor(img)
2018-03-20 20:09:15 +00:00
anime.Save()
}