174 lines
3.5 KiB
Go
Raw Normal View History

2017-10-21 03:28:52 +00:00
package main
import (
"fmt"
2017-11-09 09:27:29 +00:00
"os"
"path"
2017-11-09 12:28:06 +00:00
"runtime"
2017-10-21 03:28:52 +00:00
"strings"
"time"
2017-11-09 09:27:29 +00:00
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/aerogo/ipo"
"github.com/aerogo/ipo/inputs"
"github.com/aerogo/ipo/outputs"
2017-10-21 03:28:52 +00:00
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
2017-11-09 12:28:06 +00:00
var ticker = time.NewTicker(100 * time.Millisecond)
2017-10-21 03:28:52 +00:00
func main() {
color.Yellow("Downloading anime images")
2017-11-01 19:12:38 +00:00
defer arn.Node.Close()
2017-11-09 12:28:06 +00:00
allAnime := arn.AllAnime()
2017-10-21 03:28:52 +00:00
2017-11-09 12:28:06 +00:00
for index, anime := range allAnime {
2017-11-09 12:32:34 +00:00
fmt.Printf("%d / %d\n", index+1, len(allAnime))
2017-11-09 12:28:06 +00:00
work(anime)
2017-10-21 03:28:52 +00:00
}
2017-11-09 12:28:06 +00:00
color.Green("Finished downloading anime images.")
2017-11-09 11:12:04 +00:00
// Give file buffers some time, just to be safe
time.Sleep(time.Second)
2017-10-21 03:28:52 +00:00
}
2017-11-09 12:28:06 +00:00
func work(anime *arn.Anime) error {
2017-10-23 23:32:12 +00:00
if !strings.HasPrefix(anime.Image.Original, "//media.kitsu.io/anime/") {
2017-10-21 03:28:52 +00:00
return nil
}
<-ticker.C
2017-11-09 10:28:27 +00:00
originals := path.Join(os.Getenv("GOPATH"), "/src/github.com/animenotifier/notify.moe/images/anime/original/")
large := path.Join(os.Getenv("GOPATH"), "/src/github.com/animenotifier/notify.moe/images/anime/large/")
medium := path.Join(os.Getenv("GOPATH"), "/src/github.com/animenotifier/notify.moe/images/anime/medium/")
small := path.Join(os.Getenv("GOPATH"), "/src/github.com/animenotifier/notify.moe/images/anime/small/")
2017-11-09 09:27:29 +00:00
2017-11-09 10:28:27 +00:00
largeSize := 250
mediumSize := 142
smallSize := 55
2017-10-21 03:28:52 +00:00
2017-11-09 11:12:04 +00:00
webpQuality := 80
jpegQuality := 80
2017-11-09 09:27:29 +00:00
2017-11-09 12:28:06 +00:00
system := ipo.System{
2017-11-09 09:27:29 +00:00
Inputs: []ipo.Input{
&inputs.NetworkImage{
URL: anime.Image.Original,
},
},
Outputs: []ipo.Output{
2017-11-09 10:28:27 +00:00
// Original
2017-11-09 09:27:29 +00:00
&outputs.ImageFile{
Directory: originals,
BaseName: anime.ID,
},
2017-11-09 10:28:27 +00:00
// Large
&outputs.ImageFile{
Directory: large,
BaseName: anime.ID,
Size: largeSize,
Quality: jpegQuality,
},
2017-11-09 09:27:29 +00:00
&outputs.ImageFile{
2017-11-09 10:28:27 +00:00
Directory: large,
BaseName: anime.ID + "@2",
Size: largeSize * 2,
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: large,
BaseName: anime.ID,
Size: largeSize,
Format: "webp",
Quality: webpQuality,
},
&outputs.ImageFile{
Directory: large,
BaseName: anime.ID + "@2",
Size: largeSize * 2,
Format: "webp",
Quality: webpQuality,
},
// Medium
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID,
Size: mediumSize,
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID + "@2",
Size: mediumSize * 2,
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID,
Size: mediumSize,
Format: "webp",
Quality: webpQuality,
},
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID + "@2",
Size: mediumSize * 2,
Format: "webp",
Quality: webpQuality,
},
// Small
&outputs.ImageFile{
Directory: small,
BaseName: anime.ID,
Size: smallSize,
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: small,
BaseName: anime.ID + "@2",
Size: smallSize * 2,
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: small,
2017-11-09 09:27:29 +00:00
BaseName: anime.ID,
2017-11-09 10:28:27 +00:00
Size: smallSize,
Format: "webp",
Quality: webpQuality,
},
&outputs.ImageFile{
Directory: small,
BaseName: anime.ID + "@2",
Size: smallSize * 2,
2017-11-09 09:27:29 +00:00
Format: "webp",
2017-11-09 10:28:27 +00:00
Quality: webpQuality,
2017-11-09 09:27:29 +00:00
},
},
InputProcessor: ipo.SequentialInputs,
2017-11-09 10:28:27 +00:00
OutputProcessor: ipo.ParallelOutputs,
2017-10-21 03:28:52 +00:00
}
2017-11-09 09:27:29 +00:00
err := system.Run()
2017-10-21 03:28:52 +00:00
2017-11-09 09:27:29 +00:00
if err != nil {
fmt.Println(err)
}
2017-10-21 03:28:52 +00:00
2017-11-09 12:28:06 +00:00
// Try to free up some memory
system.Inputs = nil
system.Outputs = nil
runtime.GC()
2017-10-21 03:28:52 +00:00
return nil
}