216 lines
4.7 KiB
Go
Raw Normal View History

2017-10-21 03:28:52 +00:00
package main
import (
2017-11-09 13:36:47 +00:00
"flag"
2017-10-21 03:28:52 +00:00
"fmt"
2017-11-09 09:27:29 +00:00
"os"
"path"
2017-11-09 12:28:06 +00:00
"runtime"
2017-11-09 19:17:05 +00:00
"strconv"
2017-10-21 03:28:52 +00:00
"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 15:41:58 +00:00
var ticker = time.NewTicker(50 * time.Millisecond)
2017-11-09 13:36:47 +00:00
// Shell parameters
var from int
var to int
// Shell flags
func init() {
flag.IntVar(&from, "from", 0, "From index")
flag.IntVar(&to, "to", 0, "To index")
flag.Parse()
}
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 13:36:47 +00:00
if from < 0 {
from = 0
}
2017-11-09 19:17:05 +00:00
allAnime := arn.FilterAnime(func(anime *arn.Anime) bool {
id, _ := strconv.Atoi(anime.ID)
return id >= from && id <= to
})
2017-11-09 13:36:47 +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-21 03:28:52 +00:00
<-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
2017-11-10 09:44:15 +00:00
mediumSize := 150
2017-11-09 10:28:27 +00:00
smallSize := 55
2017-10-21 03:28:52 +00:00
2017-11-09 15:17:02 +00:00
webpQuality := 70
jpegQuality := 70
2017-11-09 09:27:29 +00:00
2017-11-10 09:29:42 +00:00
qualityBonusLowDPI := 10
qualityBonusMedium := 10
qualityBonusSmall := 10
2017-11-09 19:03:25 +00:00
kitsuOriginal := fmt.Sprintf("https://media.kitsu.io/anime/poster_images/%s/original", anime.ID)
2017-11-09 12:28:06 +00:00
system := ipo.System{
2017-11-09 09:27:29 +00:00
Inputs: []ipo.Input{
2017-11-09 15:41:58 +00:00
&inputs.FileSystemImage{
URL: path.Join(originals, anime.ID+".png"),
},
&inputs.FileSystemImage{
URL: path.Join(originals, anime.ID+".jpg"),
},
&inputs.FileSystemImage{
URL: path.Join(originals, anime.ID+".jpeg"),
},
&inputs.FileSystemImage{
URL: path.Join(originals, anime.ID+".gif"),
},
2017-11-09 09:27:29 +00:00
&inputs.NetworkImage{
2017-11-09 19:03:25 +00:00
URL: kitsuOriginal + anime.ImageExtension,
2017-11-09 09:27:29 +00:00
},
2017-11-09 19:22:36 +00:00
&inputs.NetworkImage{
URL: kitsuOriginal + ".png",
},
&inputs.NetworkImage{
URL: kitsuOriginal + ".jpg",
},
&inputs.NetworkImage{
URL: kitsuOriginal + ".jpeg",
},
2017-11-09 09:27:29 +00:00
},
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,
2017-11-10 09:29:42 +00:00
Quality: jpegQuality + qualityBonusLowDPI,
2017-11-09 10:28:27 +00:00
},
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",
2017-11-10 09:29:42 +00:00
Quality: webpQuality + qualityBonusLowDPI,
2017-11-09 10:28:27 +00:00
},
&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,
2017-11-10 09:29:42 +00:00
Quality: jpegQuality + qualityBonusLowDPI + qualityBonusMedium,
2017-11-09 10:28:27 +00:00
},
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID + "@2",
Size: mediumSize * 2,
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID,
Size: mediumSize,
Format: "webp",
2017-11-10 09:29:42 +00:00
Quality: webpQuality + qualityBonusLowDPI + qualityBonusMedium,
2017-11-09 10:28:27 +00:00
},
&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,
2017-11-10 09:29:42 +00:00
Quality: jpegQuality + qualityBonusLowDPI + qualityBonusSmall,
2017-11-09 10:28:27 +00:00
},
&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",
2017-11-10 09:29:42 +00:00
Quality: webpQuality + qualityBonusLowDPI + qualityBonusSmall,
2017-11-09 10:28:27 +00:00
},
&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
}