2018-03-18 21:41:13 +01:00
|
|
|
package animeimport
|
|
|
|
|
|
|
|
import (
|
2018-03-18 22:44:03 +01:00
|
|
|
"fmt"
|
2018-03-18 21:41:13 +01:00
|
|
|
"net/http"
|
|
|
|
|
2019-04-23 14:45:17 +09:00
|
|
|
"github.com/akyoto/color"
|
2019-04-23 14:52:55 +09:00
|
|
|
"github.com/animenotifier/kitsu"
|
2018-03-18 22:44:03 +01:00
|
|
|
|
2019-06-03 18:32:43 +09:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2018-03-18 22:44:03 +01:00
|
|
|
|
2018-03-18 21:41:13 +01:00
|
|
|
"github.com/aerogo/aero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Kitsu anime import.
|
2019-06-01 13:55:49 +09:00
|
|
|
func Kitsu(ctx aero.Context) error {
|
2018-03-18 22:44:03 +01:00
|
|
|
id := ctx.Get("id")
|
2019-11-17 16:59:34 +09:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2018-03-18 21:41:13 +01:00
|
|
|
|
2018-03-18 22:44:03 +01:00
|
|
|
if user == nil || (user.Role != "editor" && user.Role != "admin") {
|
2018-07-07 12:42:00 +09:00
|
|
|
return ctx.Error(http.StatusUnauthorized, "Not authorized")
|
2018-03-18 21:41:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 22:44:03 +01: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 17:06:42 +09:00
|
|
|
anime, characters, relations := arn.NewAnimeFromKitsuAnime(kitsuAnime)
|
2018-03-18 22:44:03 +01: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 13:55:49 +09:00
|
|
|
return nil
|
2018-03-18 21:41:13 +01:00
|
|
|
}
|