Added CSV export

This commit is contained in:
Eduard Urbach 2021-11-22 23:07:33 +09:00
parent 093385b153
commit a9ba01e610
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 54 additions and 4 deletions

43
pages/export/CSV.go Normal file
View File

@ -0,0 +1,43 @@
package export
import (
"bytes"
"fmt"
"github.com/aerogo/aero"
)
// CSV renders the anime list items in plain text format.
func CSV(ctx aero.Context) error {
animeList, err := getAnimeList(ctx)
if err != nil {
return err
}
buffer := bytes.Buffer{}
// Header
buffer.WriteString("Title,Status,Episodes,Overall,Story,Visuals,Soundtrack,Rewatched\n")
// List items
for _, item := range animeList.Items {
anime := item.Anime()
fmt.Fprintf(
&buffer,
"%s,%s,%d,%.1f,%.1f,%.1f,%.1f,%d\n",
anime.Title.Canonical,
item.Status,
item.Episodes,
item.Rating.Overall,
item.Rating.Story,
item.Rating.Visuals,
item.Rating.Soundtrack,
item.RewatchCount,
)
}
ctx.Response().SetHeader("Content-Type", "text/csv")
ctx.Response().SetHeader("Content-Disposition", "attachment; filename=\"anime-list.csv\"")
return ctx.String(buffer.String())
}

View File

@ -7,8 +7,8 @@ import (
"github.com/aerogo/aero"
)
// Text renders the anime list items in plain text format.
func Text(ctx aero.Context) error {
// TXT renders the anime list items in plain text format.
func TXT(ctx aero.Context) error {
animeList, err := getAnimeList(ctx)
if err != nil {

View File

@ -7,6 +7,7 @@ import (
// 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/csv", export.CSV)
app.Get("/user/:nick/animelist/export/txt", export.TXT)
app.Get("/user/:nick/animelist/export/json", export.JSON)
}

View File

@ -64,6 +64,12 @@ component SettingsAccounts(user *arn.User)
h3.widget-title
Icon("upload")
span Export
.widget-section
label CSV:
a.button(href=user.Link() + "/animelist/export/csv", target="_blank")
Icon("upload")
span Export as CSV
.widget-section
label JSON:
@ -73,6 +79,6 @@ component SettingsAccounts(user *arn.User)
.widget-section
label TXT:
a.button(href=user.Link() + "/animelist/export/text", target="_blank")
a.button(href=user.Link() + "/animelist/export/txt", target="_blank")
Icon("upload")
span Export as TXT