From 093385b15350b439d5da96114b8c8c2c3cf188a2 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 22 Nov 2021 22:41:53 +0900 Subject: [PATCH] Added text export --- pages/export/JSON.go | 17 +++++++++++ pages/export/Text.go | 36 ++++++++++++++++++++++++ pages/export/getAnimeList.go | 32 +++++++++++++++++++++ pages/index.go | 2 ++ pages/index/exportroutes/exportroutes.go | 12 ++++++++ pages/settings/accounts.pixy | 10 +++++-- 6 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 pages/export/JSON.go create mode 100644 pages/export/Text.go create mode 100644 pages/export/getAnimeList.go create mode 100644 pages/index/exportroutes/exportroutes.go diff --git a/pages/export/JSON.go b/pages/export/JSON.go new file mode 100644 index 00000000..babaae8c --- /dev/null +++ b/pages/export/JSON.go @@ -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) +} diff --git a/pages/export/Text.go b/pages/export/Text.go new file mode 100644 index 00000000..6175da91 --- /dev/null +++ b/pages/export/Text.go @@ -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()) +} diff --git a/pages/export/getAnimeList.go b/pages/export/getAnimeList.go new file mode 100644 index 00000000..c921baed --- /dev/null +++ b/pages/export/getAnimeList.go @@ -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 +} diff --git a/pages/index.go b/pages/index.go index 074e697e..d8477745 100644 --- a/pages/index.go +++ b/pages/index.go @@ -12,6 +12,7 @@ import ( "github.com/animenotifier/notify.moe/pages/index/companyroutes" "github.com/animenotifier/notify.moe/pages/index/coreroutes" "github.com/animenotifier/notify.moe/pages/index/exploreroutes" + "github.com/animenotifier/notify.moe/pages/index/exportroutes" "github.com/animenotifier/notify.moe/pages/index/forumroutes" "github.com/animenotifier/notify.moe/pages/index/grouproutes" "github.com/animenotifier/notify.moe/pages/index/importroutes" @@ -43,6 +44,7 @@ func Configure(app *aero.Application) { grouproutes.Register(app) searchroutes.Register(app) importroutes.Register(app) + exportroutes.Register(app) shoproutes.Register(app) settingsroutes.Register(app) staffroutes.Register(app) diff --git a/pages/index/exportroutes/exportroutes.go b/pages/index/exportroutes/exportroutes.go new file mode 100644 index 00000000..2012f7f1 --- /dev/null +++ b/pages/index/exportroutes/exportroutes.go @@ -0,0 +1,12 @@ +package exportroutes + +import ( + "github.com/aerogo/aero" + "github.com/animenotifier/notify.moe/pages/export" +) + +// Register registers the page routes. +func Register(app *aero.Application) { + app.Get("/user/:nick/animelist/export/text", export.Text) + app.Get("/user/:nick/animelist/export/json", export.JSON) +} diff --git a/pages/settings/accounts.pixy b/pages/settings/accounts.pixy index 97c91742..7af02ed4 100644 --- a/pages/settings/accounts.pixy +++ b/pages/settings/accounts.pixy @@ -67,6 +67,12 @@ component SettingsAccounts(user *arn.User) .widget-section label JSON: - a.button(href="/api/animelist/" + user.ID, target="_blank") + a.button(href=user.Link() + "/animelist/export/json", target="_blank") Icon("upload") - span Export anime list as JSON + span Export as JSON + + .widget-section + label TXT: + a.button(href=user.Link() + "/animelist/export/text", target="_blank") + Icon("upload") + span Export as TXT