Added text export

This commit is contained in:
Eduard Urbach 2021-11-22 22:41:53 +09:00
parent 9f133ee752
commit 093385b153
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 107 additions and 2 deletions

17
pages/export/JSON.go Normal file
View 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
View 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())
}

View 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
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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