Implemented kitsu import

This commit is contained in:
Eduard Urbach 2018-03-18 22:44:03 +01:00
parent 771e6b026c
commit 9339dc054a
2 changed files with 37 additions and 4 deletions

View File

@ -1,19 +1,49 @@
package animeimport
import (
"fmt"
"net/http"
"github.com/animenotifier/kitsu"
"github.com/fatih/color"
"github.com/animenotifier/arn"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/utils"
)
// Kitsu anime import.
func Kitsu(ctx *aero.Context) string {
// id := ctx.Get("id")
// user := utils.GetUser(ctx)
id := ctx.Get("id")
user := utils.GetUser(ctx)
if true {
if user == nil || (user.Role != "editor" && user.Role != "admin") {
return ctx.Error(http.StatusUnauthorized, "Not authorized", nil)
}
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)
return ""
}

View File

@ -30,12 +30,15 @@ export function malDiffFilterAnime(arn: AnimeNotifier, input: HTMLInputElement)
// Import Kitsu anime
export async function importKitsuAnime(arn: AnimeNotifier, button: HTMLButtonElement) {
let response = await fetch(`/api/import/kitsu/anime/${button.dataset.id}`, {
let newTab = window.open()
let animeId = button.dataset.id
let response = await fetch(`/api/import/kitsu/anime/${animeId}`, {
method: "POST",
credentials: "same-origin"
})
if(response.ok) {
newTab.location.href = `/anime/${animeId}`
arn.reloadContent()
} else {
arn.statusMessage.showError(await response.text())