248 lines
5.4 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
2018-02-23 10:00:37 +00:00
var useCache bool
2017-11-09 13:36:47 +00:00
// Shell flags
func init() {
flag.IntVar(&from, "from", 0, "From index")
flag.IntVar(&to, "to", 0, "To index")
2018-02-23 10:00:37 +00:00
flag.BoolVar(&useCache, "cache", false, "Use cache")
2017-11-09 13:36:47 +00:00
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-11 22:19:31 +00:00
largeSizeX := 250
largeSizeY := 0
2017-10-21 03:28:52 +00:00
2017-11-11 22:19:31 +00:00
mediumSizeX := 142
mediumSizeY := 200
smallSizeX := 55
smallSizeY := 78
2017-11-10 09:47:53 +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)
2018-02-23 10:00:37 +00:00
// Define the input sources
sources := []ipo.Input{}
// If we use the file system cache, start by searching those files
if useCache {
sources = append(sources,
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"),
},
2018-02-23 10:00:37 +00:00
)
}
// Afterwards, use the network sources
sources = append(sources,
&inputs.NetworkImage{
2018-03-16 21:40:38 +00:00
URL: kitsuOriginal + anime.Image.Extension,
2018-02-23 10:00:37 +00:00
},
&inputs.NetworkImage{
URL: kitsuOriginal + ".png",
},
&inputs.NetworkImage{
URL: kitsuOriginal + ".jpg",
2017-11-09 09:27:29 +00:00
},
2018-02-23 10:00:37 +00:00
&inputs.NetworkImage{
URL: kitsuOriginal + ".jpeg",
},
)
// Define the system
system := ipo.System{
Inputs: sources,
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,
2017-11-11 22:19:31 +00:00
Width: largeSizeX,
Height: largeSizeY,
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",
2017-11-11 22:19:31 +00:00
Width: largeSizeX * 2,
Height: largeSizeY * 2,
2017-11-09 10:28:27 +00:00
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: large,
BaseName: anime.ID,
2017-11-11 22:19:31 +00:00
Width: largeSizeX,
Height: largeSizeY,
2017-11-09 10:28:27 +00:00
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",
2017-11-11 22:19:31 +00:00
Width: largeSizeX * 2,
Height: largeSizeY * 2,
2017-11-09 10:28:27 +00:00
Format: "webp",
Quality: webpQuality,
},
// Medium
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID,
2017-11-11 22:19:31 +00:00
Width: mediumSizeX,
Height: mediumSizeY,
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",
2017-11-11 22:19:31 +00:00
Width: mediumSizeX * 2,
Height: mediumSizeY * 2,
2017-11-09 10:28:27 +00:00
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: medium,
BaseName: anime.ID,
2017-11-11 22:19:31 +00:00
Width: mediumSizeX,
Height: mediumSizeY,
2017-11-09 10:28:27 +00:00
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",
2017-11-11 22:19:31 +00:00
Width: mediumSizeX * 2,
Height: mediumSizeY * 2,
2017-11-09 10:28:27 +00:00
Format: "webp",
Quality: webpQuality,
},
// Small
&outputs.ImageFile{
Directory: small,
BaseName: anime.ID,
2017-11-11 22:19:31 +00:00
Width: smallSizeX,
Height: smallSizeY,
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",
2017-11-11 22:19:31 +00:00
Width: smallSizeX * 2,
Height: smallSizeY * 2,
2017-11-09 10:28:27 +00:00
Quality: jpegQuality,
},
&outputs.ImageFile{
Directory: small,
2017-11-09 09:27:29 +00:00
BaseName: anime.ID,
2017-11-11 22:19:31 +00:00
Width: smallSizeX,
Height: smallSizeY,
2017-11-09 10:28:27 +00:00
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",
2017-11-11 22:19:31 +00:00
Width: smallSizeX * 2,
Height: smallSizeY * 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
}