94 lines
2.0 KiB
Go
Raw Normal View History

2017-06-20 23:27:14 +02:00
package main
import (
2017-06-21 00:34:18 +02:00
"fmt"
2017-06-20 23:27:14 +02:00
"strings"
2017-06-23 18:22:48 +02:00
"github.com/aerogo/flow"
2017-06-20 23:27:14 +02:00
"github.com/animenotifier/arn"
2017-06-21 00:34:18 +02:00
"github.com/fatih/color"
2017-06-20 23:27:14 +02:00
)
func main() {
2017-06-21 00:34:18 +02:00
color.Yellow("Updating search index")
2017-06-23 18:22:48 +02:00
flow.Parallel(updateAnimeIndex, updateUserIndex)
2017-06-21 00:34:18 +02:00
color.Green("Finished.")
2017-06-20 23:27:14 +02:00
}
func updateAnimeIndex() {
animeSearchIndex := arn.NewSearchIndex()
// Anime
2017-06-27 12:39:41 +02:00
animeStream, err := arn.StreamAnime()
2017-06-20 23:27:14 +02:00
if err != nil {
panic(err)
}
for anime := range animeStream {
2017-07-04 14:34:58 +02:00
if anime.Title.Canonical != "" {
animeSearchIndex.TextToID[strings.ToLower(anime.Title.Canonical)] = anime.ID
2017-06-28 02:51:11 +02:00
}
2017-07-04 14:34:58 +02:00
if anime.Title.Romaji != "" {
animeSearchIndex.TextToID[strings.ToLower(anime.Title.Romaji)] = anime.ID
2017-06-21 00:34:18 +02:00
}
2017-07-04 14:34:58 +02:00
// Make sure we only include Japanese titles that
// don't overlap with the English titles.
if anime.Title.Japanese != "" && animeSearchIndex.TextToID[strings.ToLower(anime.Title.Japanese)] == "" {
2017-06-30 01:14:24 +02:00
animeSearchIndex.TextToID[strings.ToLower(anime.Title.Japanese)] = anime.ID
}
2017-07-04 14:34:58 +02:00
// Same with English titles, don't overwrite other stuff.
if anime.Title.English != "" && animeSearchIndex.TextToID[strings.ToLower(anime.Title.English)] == "" {
animeSearchIndex.TextToID[strings.ToLower(anime.Title.English)] = anime.ID
2017-07-02 00:49:05 +02:00
}
2017-06-30 01:14:24 +02:00
for _, synonym := range anime.Title.Synonyms {
synonym = strings.ToLower(synonym)
if synonym != "" && len(synonym) <= 10 {
animeSearchIndex.TextToID[synonym] = anime.ID
}
2017-06-21 00:34:18 +02:00
}
2017-06-20 23:27:14 +02:00
}
2017-06-21 00:34:18 +02:00
fmt.Println(len(animeSearchIndex.TextToID), "anime titles")
2017-06-20 23:27:14 +02:00
// Save in database
err = arn.DB.Set("SearchIndex", "Anime", animeSearchIndex)
if err != nil {
panic(err)
}
}
func updateUserIndex() {
userSearchIndex := arn.NewSearchIndex()
// Users
2017-06-27 14:38:36 +02:00
userStream, err := arn.StreamUsers()
2017-06-20 23:27:14 +02:00
if err != nil {
panic(err)
}
for user := range userStream {
2017-06-21 00:34:18 +02:00
if user.IsActive() && user.Nick != "" {
userSearchIndex.TextToID[strings.ToLower(user.Nick)] = user.ID
}
2017-06-20 23:27:14 +02:00
}
2017-06-21 00:34:18 +02:00
fmt.Println(len(userSearchIndex.TextToID), "user names")
2017-06-20 23:27:14 +02:00
// Save in database
err = arn.DB.Set("SearchIndex", "User", userSearchIndex)
if err != nil {
panic(err)
}
}