diff --git a/pages/export/CSV.go b/pages/export/CSV.go new file mode 100644 index 00000000..5ada1250 --- /dev/null +++ b/pages/export/CSV.go @@ -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()) +} diff --git a/pages/export/Text.go b/pages/export/TXT.go similarity index 90% rename from pages/export/Text.go rename to pages/export/TXT.go index 6175da91..f7412e5b 100644 --- a/pages/export/Text.go +++ b/pages/export/TXT.go @@ -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 { diff --git a/pages/index/exportroutes/exportroutes.go b/pages/index/exportroutes/exportroutes.go index 2012f7f1..5a4081b1 100644 --- a/pages/index/exportroutes/exportroutes.go +++ b/pages/index/exportroutes/exportroutes.go @@ -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) } diff --git a/pages/settings/accounts.pixy b/pages/settings/accounts.pixy index 7af02ed4..e38302c0 100644 --- a/pages/settings/accounts.pixy +++ b/pages/settings/accounts.pixy @@ -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