Updated MAL tools

This commit is contained in:
Eduard Urbach 2018-10-30 09:35:30 +09:00
parent a559227b65
commit 85cd5538b6
2 changed files with 70 additions and 28 deletions

View File

@ -17,6 +17,7 @@ const (
delayBetweenRequests = 1100 * time.Millisecond delayBetweenRequests = 1100 * time.Millisecond
userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36" userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36"
animeDirectory = "anime" animeDirectory = "anime"
characterDirectory = "character"
) )
var headers = map[string]string{ var headers = map[string]string{
@ -33,41 +34,59 @@ func main() {
} }
// Filter anime with MAL ID // Filter anime with MAL ID
animes := []*arn.Anime{} var animes []*arn.Anime
for anime := range arn.StreamAnime() { if objectType == "all" || objectType == "anime" {
malID := anime.GetMapping("myanimelist/anime") animes = arn.FilterAnime(func(anime *arn.Anime) bool {
return anime.GetMapping("myanimelist/anime") != ""
})
if malID == "" { color.Yellow("Found %d anime", len(animes))
continue
}
animes = append(animes, anime) // Sort so that we download the most important ones first
arn.SortAnimeByQuality(animes)
// Create anime directory if it's missing
os.Mkdir(animeDirectory, 0777)
} }
color.Yellow("Found %d anime", len(animes)) // Filter characters with MAL ID
var characters []*arn.Character
if objectType == "all" || objectType == "character" {
characters = arn.FilterCharacters(func(character *arn.Character) bool {
return character.GetMapping("myanimelist/character") != ""
})
color.Yellow("Found %d characters", len(characters))
// Sort so that we download the most important ones first
arn.SortCharactersByLikes(characters)
// Create character directory if it's missing
os.Mkdir(characterDirectory, 0777)
}
// We don't need the database anymore // We don't need the database anymore
arn.Node.Close() arn.Node.Close()
// Create anime directory if it's missing
os.Mkdir(animeDirectory, 0777)
// Create crawler // Create crawler
malCrawler := crawler.New( malCrawler := crawler.New(
headers, headers,
delayBetweenRequests, delayBetweenRequests,
len(animes), len(animes)+len(characters),
) )
// Sort so that we download the most important ones first
arn.SortAnimeByQuality(animes)
// Queue up URLs // Queue up URLs
count := 0 count := 0
for _, anime := range animes { for _, anime := range animes {
queue(anime, malCrawler) queueAnime(anime, malCrawler)
count++
}
for _, character := range characters {
queueCharacter(character, malCrawler)
count++ count++
} }
@ -78,7 +97,7 @@ func main() {
malCrawler.Wait() malCrawler.Wait()
} }
func queue(anime *arn.Anime, malCrawler *crawler.Crawler) { func queueAnime(anime *arn.Anime, malCrawler *crawler.Crawler) {
malID := anime.GetMapping("myanimelist/anime") malID := anime.GetMapping("myanimelist/anime")
url := "https://myanimelist.net/anime/" + malID url := "https://myanimelist.net/anime/" + malID
filePath := fmt.Sprintf("%s/%s.html.gz", animeDirectory, malID) filePath := fmt.Sprintf("%s/%s.html.gz", animeDirectory, malID)
@ -95,3 +114,21 @@ func queue(anime *arn.Anime, malCrawler *crawler.Crawler) {
Raw: true, Raw: true,
}) })
} }
func queueCharacter(character *arn.Character, malCrawler *crawler.Crawler) {
malID := character.GetMapping("myanimelist/character")
url := "https://myanimelist.net/character/" + malID
filePath := fmt.Sprintf("%s/%s.html.gz", characterDirectory, 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,
Raw: true,
})
}

View File

@ -8,23 +8,19 @@ import (
) )
// Shell parameters // Shell parameters
var animeID string var objectType string
var objectID string
// Shell flags // Shell flags
func init() { func init() {
flag.StringVar(&animeID, "id", "", "ID of the notify.moe anime you want to refresh") flag.StringVar(&objectType, "type", "all", "all | anime | character")
flag.StringVar(&objectID, "id", "", "ID of the notify.moe anime/character you want to refresh")
flag.Parse() flag.Parse()
} }
// InvokeShellArgs ... // InvokeShellArgs ...
func InvokeShellArgs() bool { func InvokeShellArgs() bool {
if animeID != "" { if objectID != "" {
anime, err := arn.GetAnime(animeID)
if err != nil {
panic(err)
}
// Create crawler // Create crawler
malCrawler := crawler.New( malCrawler := crawler.New(
headers, headers,
@ -32,8 +28,17 @@ func InvokeShellArgs() bool {
1, 1,
) )
// Queue switch objectType {
queue(anime, malCrawler) case "anime":
anime, err := arn.GetAnime(objectID)
arn.PanicOnError(err)
queueAnime(anime, malCrawler)
case "character":
character, err := arn.GetCharacter(objectID)
arn.PanicOnError(err)
queueCharacter(character, malCrawler)
}
// Wait // Wait
malCrawler.Wait() malCrawler.Wait()