168 lines
3.4 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-10-21 03:28:52 +00:00
"strings"
"time"
2017-11-09 09:27:29 +00:00
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-10-21 03:28:52 +00:00
"github.com/aerogo/flow/jobqueue"
2017-11-09 09:27:29 +00:00
"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"
)
var ticker = time.NewTicker(50 * time.Millisecond)
func main() {
color.Yellow("Downloading anime images")
2017-11-01 19:12:38 +00:00
defer arn.Node.Close()
2017-10-21 03:28:52 +00:00
jobs := jobqueue.New(work)
2017-11-09 09:27:29 +00:00
for anime := range arn.StreamAnime() {
2017-10-21 03:28:52 +00:00
jobs.Queue(anime)
}
results := jobs.Wait()
color.Green("Finished downloading %d anime images.", len(results))
}
func work(job interface{}) interface{} {
anime := job.(*arn.Anime)
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 10:28:27 +00:00
webpQuality := 85
jpegQuality := 85
2017-11-09 09:27:29 +00:00
system := &ipo.System{
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
return nil
}