Added text export
This commit is contained in:
17
pages/export/JSON.go
Normal file
17
pages/export/JSON.go
Normal file
@ -0,0 +1,17 @@
|
||||
package export
|
||||
|
||||
import (
|
||||
"github.com/aerogo/aero"
|
||||
)
|
||||
|
||||
// JSON renders the anime list items in JSON format.
|
||||
func JSON(ctx aero.Context) error {
|
||||
animeList, err := getAnimeList(ctx)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx.Response().SetHeader("Content-Disposition", "attachment; filename=\"anime-list.json\"")
|
||||
return ctx.JSON(animeList)
|
||||
}
|
36
pages/export/Text.go
Normal file
36
pages/export/Text.go
Normal file
@ -0,0 +1,36 @@
|
||||
package export
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
)
|
||||
|
||||
// Text renders the anime list items in plain text format.
|
||||
func Text(ctx aero.Context) error {
|
||||
animeList, err := getAnimeList(ctx)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buffer := bytes.Buffer{}
|
||||
|
||||
for _, item := range animeList.Items {
|
||||
anime := item.Anime()
|
||||
fmt.Fprintf(&buffer, "Title: %s\n", anime.Title.Canonical)
|
||||
fmt.Fprintf(&buffer, "Status: %s\n", item.Status)
|
||||
fmt.Fprintf(&buffer, "Episodes: %d\n", item.Episodes)
|
||||
fmt.Fprintf(&buffer, "Overall: %.1f\n", item.Rating.Overall)
|
||||
fmt.Fprintf(&buffer, "Story: %.1f\n", item.Rating.Story)
|
||||
fmt.Fprintf(&buffer, "Visuals: %.1f\n", item.Rating.Visuals)
|
||||
fmt.Fprintf(&buffer, "Soundtrack: %.1f\n", item.Rating.Soundtrack)
|
||||
fmt.Fprintf(&buffer, "Rewatched: %d\n", item.RewatchCount)
|
||||
fmt.Fprintf(&buffer, "Notes: %s\n", item.Notes)
|
||||
buffer.WriteString("\n")
|
||||
}
|
||||
|
||||
ctx.Response().SetHeader("Content-Disposition", "attachment; filename=\"anime-list.txt\"")
|
||||
return ctx.Text(buffer.String())
|
||||
}
|
32
pages/export/getAnimeList.go
Normal file
32
pages/export/getAnimeList.go
Normal file
@ -0,0 +1,32 @@
|
||||
package export
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/notify.moe/arn"
|
||||
)
|
||||
|
||||
func getAnimeList(ctx aero.Context) (*arn.AnimeList, error) {
|
||||
nick := ctx.Get("nick")
|
||||
user := arn.GetUserFromContext(ctx)
|
||||
viewUser, err := arn.GetUserByNick(nick)
|
||||
|
||||
if err != nil {
|
||||
return nil, ctx.Error(http.StatusNotFound, "User not found", err)
|
||||
}
|
||||
|
||||
// Fetch all eligible items
|
||||
animeList := viewUser.AnimeList()
|
||||
|
||||
if animeList == nil {
|
||||
return nil, ctx.Error(http.StatusNotFound, "Anime list not found")
|
||||
}
|
||||
|
||||
// Filter private items
|
||||
if user == nil || user.ID != viewUser.ID {
|
||||
animeList = animeList.WithoutPrivateItems()
|
||||
}
|
||||
|
||||
return animeList, nil
|
||||
}
|
Reference in New Issue
Block a user