49 lines
982 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"
2019-04-23 05:45:17 +00:00
"github.com/akyoto/color"
2019-04-23 05:52:55 +00:00
"github.com/animenotifier/kitsu"
2018-03-18 21:44:03 +00:00
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-03-18 21:44:03 +00:00
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.
2019-06-01 04:55:49 +00:00
func Kitsu(ctx aero.Context) error {
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
2019-08-28 08:06:42 +00:00
anime, characters, relations := arn.NewAnimeFromKitsuAnime(kitsuAnime)
2018-03-18 21:44:03 +00:00
// Add user ID to the anime
anime.CreatedBy = user.ID
// Save in database
anime.Save()
characters.Save()
relations.Save()
// Log
fmt.Println(color.GreenString("✔"), anime.ID, anime.Title.Canonical)
2019-06-01 04:55:49 +00:00
return nil
2018-03-18 20:41:13 +00:00
}