95 lines
1.8 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 09:27:29 +00:00
// resp, body, errs := gorequest.New().Get(anime.Image.Original).End()
2017-10-21 03:28:52 +00:00
2017-11-09 09:27:29 +00:00
// if len(errs) > 0 {
// color.Red(errs[0].Error())
// return errs[0]
// }
// if resp.StatusCode != http.StatusOK {
// color.Red("Status %d", resp.StatusCode)
// }
// extension := anime.Image.Original[strings.LastIndex(anime.Image.Original, "."):]
// fileName := "anime/" + anime.ID + extension
// fmt.Println(fileName)
2017-10-21 03:28:52 +00:00
2017-11-09 09:27:29 +00:00
// ioutil.WriteFile(fileName, []byte(body), 0644)
originals := path.Join(os.Getenv("GOPATH"), "/src/github.com/animenotifier/notify.moe/images/anime/original/")
system := &ipo.System{
Inputs: []ipo.Input{
&inputs.NetworkImage{
URL: anime.Image.Original,
},
},
Outputs: []ipo.Output{
&outputs.ImageFile{
Directory: originals,
BaseName: anime.ID,
},
&outputs.ImageFile{
Directory: originals,
BaseName: anime.ID,
Format: "webp",
Quality: 85,
},
},
InputProcessor: ipo.SequentialInputs,
OutputProcessor: ipo.SequentialOutputs,
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
}