96 lines
1.9 KiB
Go
Raw Normal View History

2018-03-08 18:54:08 +00:00
package main
import (
2018-03-08 19:07:03 +00:00
"fmt"
2018-03-08 20:00:51 +00:00
"os"
2018-03-08 18:54:08 +00:00
"time"
2018-03-08 19:07:03 +00:00
"github.com/animenotifier/arn"
"github.com/fatih/color"
2018-03-08 18:54:08 +00:00
"github.com/aerogo/crawler"
)
2018-03-08 20:00:51 +00:00
const (
// The maximum age of files we accept until we force a refresh.
2018-04-12 16:57:28 +00:00
maxAge = 24 * time.Hour
2018-03-26 00:12:06 +00:00
delayBetweenRequests = 1100 * time.Millisecond
2018-04-11 09:07:00 +00:00
userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
2018-03-08 20:00:51 +00:00
)
2018-03-08 18:54:08 +00:00
2018-04-12 16:57:28 +00:00
var headers = map[string]string{
"User-Agent": userAgent,
"Accept-Encoding": "gzip",
}
2018-03-08 18:54:08 +00:00
func main() {
2018-04-12 16:57:28 +00:00
defer color.Green("Finished.")
// Called with arguments?
if InvokeShellArgs() {
return
}
2018-03-08 20:00:51 +00:00
// Filter anime with MAL ID
animes := []*arn.Anime{}
2018-03-08 19:07:03 +00:00
for anime := range arn.StreamAnime() {
malID := anime.GetMapping("myanimelist/anime")
if malID == "" {
continue
}
2018-03-08 20:00:51 +00:00
animes = append(animes, anime)
}
color.Yellow("Found %d anime", len(animes))
2018-03-09 01:34:50 +00:00
// We don't need the database anymore
arn.Node.Close()
2018-04-11 09:07:00 +00:00
// Create files directory if it's missing
os.Mkdir("files", 0777)
2018-03-08 20:00:51 +00:00
// Create crawler
malCrawler := crawler.New(
2018-04-12 16:57:28 +00:00
headers,
2018-03-08 23:22:36 +00:00
delayBetweenRequests,
2018-03-08 20:00:51 +00:00
len(animes),
)
// Sort so that we download the most important ones first
2018-03-14 00:02:41 +00:00
arn.SortAnimeByQuality(animes)
2018-03-08 20:00:51 +00:00
// Queue up URLs
count := 0
for _, anime := range animes {
2018-04-12 16:57:28 +00:00
queue(anime, malCrawler)
2018-03-08 19:07:03 +00:00
count++
}
2018-03-08 18:54:08 +00:00
2018-03-28 22:26:19 +00:00
// Log number of links
2018-03-08 19:07:03 +00:00
color.Yellow("Queued up %d links", count)
2018-03-28 22:26:19 +00:00
// Wait for completion
2018-03-08 18:54:08 +00:00
malCrawler.Wait()
2018-04-12 16:57:28 +00:00
}
func queue(anime *arn.Anime, malCrawler *crawler.Crawler) {
malID := anime.GetMapping("myanimelist/anime")
url := "https://myanimelist.net/anime/" + malID
2018-04-24 01:41:27 +00:00
filePath := fmt.Sprintf("anime/anime-%s.html", malID)
2018-04-12 16:57:28 +00:00
fileInfo, err := os.Stat(filePath)
if err == nil && time.Since(fileInfo.ModTime()) <= maxAge {
// fmt.Println(color.YellowString(url), "skip")
return
}
2018-03-28 22:26:19 +00:00
2018-04-12 16:57:28 +00:00
malCrawler.Queue(&crawler.Task{
URL: url,
Destination: filePath,
})
2018-03-08 18:54:08 +00:00
}