50 lines
998 B
Go
Raw Normal View History

2018-03-18 20:41:13 +00:00
package animeimport
import (
2018-03-18 21:44:03 +00:00
"fmt"
2018-03-18 20:41:13 +00:00
"net/http"
2018-03-18 21:44:03 +00:00
"github.com/animenotifier/kitsu"
"github.com/fatih/color"
"github.com/animenotifier/arn"
2018-03-18 20:41:13 +00:00
"github.com/aerogo/aero"
2018-03-18 21:44:03 +00:00
"github.com/animenotifier/notify.moe/utils"
2018-03-18 20:41:13 +00:00
)
// Kitsu anime import.
func Kitsu(ctx *aero.Context) string {
2018-03-18 21:44:03 +00:00
id := ctx.Get("id")
user := utils.GetUser(ctx)
2018-03-18 20:41:13 +00:00
2018-03-18 21:44:03 +00:00
if user == nil || (user.Role != "editor" && user.Role != "admin") {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusUnauthorized, "Not authorized")
2018-03-18 20:41:13 +00:00
}
2018-03-18 21:44:03 +00:00
kitsuAnimeObj, err := arn.Kitsu.Get("Anime", id)
if kitsuAnimeObj == nil {
return ctx.Error(http.StatusNotFound, "Kitsu anime not found", err)
}
kitsuAnime := kitsuAnimeObj.(*kitsu.Anime)
// Convert
anime, characters, relations, episodes := arn.NewAnimeFromKitsuAnime(kitsuAnime)
// Add user ID to the anime
anime.CreatedBy = user.ID
// Save in database
anime.Save()
characters.Save()
relations.Save()
episodes.Save()
// Log
fmt.Println(color.GreenString("✔"), anime.ID, anime.Title.Canonical)
2018-03-18 20:41:13 +00:00
return ""
}