2017-07-09 21:22:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-10 14:58:34 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-07-09 21:22:31 +00:00
|
|
|
"sort"
|
2017-07-10 14:58:34 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-07-09 21:22:31 +00:00
|
|
|
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/twist"
|
|
|
|
"github.com/fatih/color"
|
|
|
|
)
|
|
|
|
|
2017-07-10 14:58:34 +00:00
|
|
|
var rateLimiter = time.NewTicker(500 * time.Millisecond)
|
|
|
|
|
2017-07-09 21:22:31 +00:00
|
|
|
func main() {
|
|
|
|
// Replace this with ID list from twist.moe later
|
2017-07-10 14:58:34 +00:00
|
|
|
currentAnime, err := arn.FilterAnime(func(anime *arn.Anime) bool {
|
|
|
|
return anime.Status == "current"
|
|
|
|
})
|
|
|
|
arn.PanicOnError(err)
|
|
|
|
|
|
|
|
color.Yellow("Refreshing twist.moe links for %d anime", len(currentAnime))
|
|
|
|
|
|
|
|
for count, anime := range currentAnime {
|
|
|
|
// Wait for rate limiter
|
|
|
|
<-rateLimiter.C
|
|
|
|
|
|
|
|
// anime, animeErr := arn.GetAnime(animeID)
|
|
|
|
|
|
|
|
// if animeErr != nil {
|
|
|
|
// color.Red("Error fetching anime from the database with ID %s: %v", animeID, animeErr)
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
animeID := anime.ID
|
2017-07-09 21:22:31 +00:00
|
|
|
|
2017-07-10 14:58:34 +00:00
|
|
|
// Log
|
|
|
|
fmt.Fprintf(os.Stdout, "[%d / %d] ", count+1, len(currentAnime))
|
|
|
|
|
|
|
|
// Get twist.moe feed
|
2017-07-09 21:22:31 +00:00
|
|
|
feed, err := twist.GetFeedByKitsuID(animeID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
color.Red("Error querying ID %s: %v", animeID, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
episodes := feed.Episodes
|
|
|
|
|
|
|
|
// Sort by episode number
|
|
|
|
sort.Slice(episodes, func(a, b int) bool {
|
2017-07-09 22:21:53 +00:00
|
|
|
return episodes[a].Number < episodes[b].Number
|
2017-07-09 21:22:31 +00:00
|
|
|
})
|
|
|
|
|
2017-07-10 14:58:34 +00:00
|
|
|
for _, episode := range episodes {
|
|
|
|
arnEpisode := anime.EpisodeByNumber(episode.Number)
|
|
|
|
|
|
|
|
if arnEpisode == nil {
|
|
|
|
color.Red("Anime %s Episode %d not found", anime.ID, episode.Number)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if arnEpisode.Links == nil {
|
|
|
|
arnEpisode.Links = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
arnEpisode.Links["twist.moe"] = strings.Replace(episode.Link, "https://test.twist.moe/", "https://twist.moe/", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
arn.PanicOnError(anime.Episodes().Save())
|
|
|
|
color.Green("Found %d episodes for anime %s", len(episodes), animeID)
|
2017-07-09 21:22:31 +00:00
|
|
|
}
|
|
|
|
}
|