92 lines
1.6 KiB
Go
Raw Permalink Normal View History

2017-11-09 17:31:32 +00:00
package main
import (
2018-03-20 03:31:52 +00:00
"flag"
2018-03-20 00:43:23 +00:00
"fmt"
"io/ioutil"
2017-11-09 17:31:32 +00:00
"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"
2017-11-09 17:31:32 +00:00
)
func main() {
2018-03-20 00:43:23 +00:00
color.Yellow("Updating anime thumbnails")
defer color.Green("Finished.")
2018-03-21 04:15:03 +00:00
defer arn.Node.Close()
2017-11-09 17:31:32 +00:00
2018-03-20 03:31:52 +00:00
// Parse flags
var animeID string
flag.StringVar(&animeID, "id", "", "ID of the anime that you want to refresh")
flag.Parse()
2017-11-09 17:31:32 +00:00
2018-03-20 03:31:52 +00:00
// Refresh 1 anime in case ID was specified
if animeID != "" {
anime, _ := arn.GetAnime(animeID)
2017-11-09 17:31:32 +00:00
2018-03-20 03:31:52 +00:00
if anime != nil {
sync(anime)
2017-11-09 17:31:32 +00:00
}
2018-03-20 03:31:52 +00:00
return
}
2017-11-09 17:31:32 +00:00
2018-03-20 03:31:52 +00:00
// Otherwise refresh all anime
for anime := range arn.StreamAnime() {
sync(anime)
}
}
2017-11-09 17:31:32 +00:00
2018-03-20 03:31:52 +00:00
// sync refreshes the image of the given anime.
func sync(anime *arn.Anime) {
base := path.Join(arn.Root, "/images/anime/original/", anime.ID)
if _, err := os.Stat(base + ".png"); err == nil {
update(anime, base+".png")
return
}
if _, err := os.Stat(base + ".jpg"); err == nil {
update(anime, base+".jpg")
return
}
if _, err := os.Stat(base + ".jpeg"); err == nil {
update(anime, base+".jpg")
return
}
if _, err := os.Stat(base + ".gif"); err == nil {
update(anime, base+".gif")
return
}
if _, err := os.Stat(base + ".webp"); err == nil {
update(anime, base+".webp")
return
2017-11-09 17:31:32 +00:00
}
}
2018-03-20 00:43:23 +00:00
2018-03-20 03:31:52 +00:00
// update expects a file to load as image for the anime and updates it.
2018-03-20 00:43:23 +00:00
func update(anime *arn.Anime, filePath string) {
fmt.Println(anime.ID, anime)
data, err := ioutil.ReadFile(filePath)
if err != nil {
2018-03-20 03:40:00 +00:00
color.Red(err.Error())
return
}
err = anime.SetImageBytes(data)
if err != nil {
color.Red(err.Error())
2018-03-20 00:43:23 +00:00
return
}
anime.Save()
}