Added shell commands to MAL download

This commit is contained in:
Eduard Urbach 2018-04-12 18:57:28 +02:00
parent a358562af5
commit 87b11eb50d
2 changed files with 76 additions and 23 deletions

View File

@ -13,12 +13,24 @@ import (
const ( const (
// The maximum age of files we accept until we force a refresh. // The maximum age of files we accept until we force a refresh.
maxAge = 7 * 24 * time.Hour maxAge = 24 * time.Hour
delayBetweenRequests = 1100 * time.Millisecond delayBetweenRequests = 1100 * time.Millisecond
userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
) )
var headers = map[string]string{
"User-Agent": userAgent,
"Accept-Encoding": "gzip",
}
func main() { func main() {
defer color.Green("Finished.")
// Called with arguments?
if InvokeShellArgs() {
return
}
// Filter anime with MAL ID // Filter anime with MAL ID
animes := []*arn.Anime{} animes := []*arn.Anime{}
@ -42,10 +54,7 @@ func main() {
// Create crawler // Create crawler
malCrawler := crawler.New( malCrawler := crawler.New(
map[string]string{ headers,
"User-Agent": userAgent,
"Accept-Encoding": "gzip",
},
delayBetweenRequests, delayBetweenRequests,
len(animes), len(animes),
) )
@ -57,21 +66,7 @@ func main() {
count := 0 count := 0
for _, anime := range animes { for _, anime := range animes {
malID := anime.GetMapping("myanimelist/anime") queue(anime, malCrawler)
url := "https://myanimelist.net/anime/" + malID
filePath := fmt.Sprintf("files/anime-%s.html", malID)
fileInfo, err := os.Stat(filePath)
if err == nil && time.Since(fileInfo.ModTime()) <= maxAge {
// fmt.Println(color.YellowString(url), "skip")
continue
}
malCrawler.Queue(&crawler.Task{
URL: url,
Destination: filePath,
})
count++ count++
} }
@ -80,7 +75,21 @@ func main() {
// Wait for completion // Wait for completion
malCrawler.Wait() malCrawler.Wait()
}
// Finished
color.Green("Finished.") func queue(anime *arn.Anime, malCrawler *crawler.Crawler) {
malID := anime.GetMapping("myanimelist/anime")
url := "https://myanimelist.net/anime/" + malID
filePath := fmt.Sprintf("files/anime-%s.html", malID)
fileInfo, err := os.Stat(filePath)
if err == nil && time.Since(fileInfo.ModTime()) <= maxAge {
// fmt.Println(color.YellowString(url), "skip")
return
}
malCrawler.Queue(&crawler.Task{
URL: url,
Destination: filePath,
})
} }

View File

@ -0,0 +1,44 @@
package main
import (
"flag"
"github.com/aerogo/crawler"
"github.com/animenotifier/arn"
)
// Shell parameters
var animeID string
// Shell flags
func init() {
flag.StringVar(&animeID, "id", "", "ID of the notify.moe anime you want to refresh")
flag.Parse()
}
// InvokeShellArgs ...
func InvokeShellArgs() bool {
if animeID != "" {
anime, err := arn.GetAnime(animeID)
if err != nil {
panic(err)
}
// Create crawler
malCrawler := crawler.New(
headers,
delayBetweenRequests,
1,
)
// Queue
queue(anime, malCrawler)
// Wait
malCrawler.Wait()
return true
}
return false
}