From 2247deac1892add1a5a4706b38ce65a42baba055 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Mon, 20 Dec 2021 11:29:20 +0100 Subject: [PATCH] add parsing date, remap categories, improve try --- internal/piwigo/categories.go | 33 +++++++++++++++++++++++++++ internal/piwigo/time.go | 26 +++++++++++++++++++++ internal/piwigocli/categories_list.go | 4 +++- internal/piwigocli/images_details.go | 14 ++++++++++++ internal/piwigocli/method_try.go | 2 +- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 internal/piwigo/categories.go create mode 100644 internal/piwigo/time.go diff --git a/internal/piwigo/categories.go b/internal/piwigo/categories.go new file mode 100644 index 0000000..ad73380 --- /dev/null +++ b/internal/piwigo/categories.go @@ -0,0 +1,33 @@ +package piwigo + +import "net/url" + +type Categories struct { + Categories []Category `json:"categories"` +} + +type Category struct { + Id int `json:"id"` + Name string `json:"name"` + ImagesCount int `json:"nb_images"` + Url string `json:"url"` +} + +func (p *Piwigo) Categories() (map[int]Category, error) { + var categories Categories + + err := p.Post("pwg.categories.getList", &url.Values{ + "fullname": []string{"true"}, + "recursive": []string{"true"}, + }, &categories) + if err != nil { + return nil, err + } + + result := map[int]Category{} + + for _, category := range categories.Categories { + result[category.Id] = category + } + return result, nil +} diff --git a/internal/piwigo/time.go b/internal/piwigo/time.go new file mode 100644 index 0000000..48c1f5f --- /dev/null +++ b/internal/piwigo/time.go @@ -0,0 +1,26 @@ +package piwigo + +import ( + "strings" + "time" +) + +type TimeResult time.Time + +func (c *TimeResult) UnmarshalJSON(b []byte) error { + value := strings.Trim(string(b), `"`) //get rid of " + if value == "" || value == "null" { + return nil + } + + t, err := time.Parse("2006-01-02 15:04:05", value) //parse time + if err != nil { + return err + } + *c = TimeResult(t) //set result using the pointer + return nil +} + +func (c TimeResult) MarshalJSON() ([]byte, error) { + return []byte(`"` + time.Time(c).Format(time.RFC3339) + `"`), nil +} diff --git a/internal/piwigocli/categories_list.go b/internal/piwigocli/categories_list.go index 6a79c1c..404dbe8 100644 --- a/internal/piwigocli/categories_list.go +++ b/internal/piwigocli/categories_list.go @@ -15,6 +15,7 @@ type Category struct { Id int `json:"id"` Name string `json:"name"` ImagesCount int `json:"nb_images"` + Url string `json:"url"` } type GetCategoriesListResponse struct { @@ -43,12 +44,13 @@ func (c *CategoriesListCommand) Execute(args []string) error { t := table.NewWriter() - t.AppendHeader(table.Row{"Id", "Name", "Images"}) + t.AppendHeader(table.Row{"Id", "Name", "Images", "Url"}) for _, category := range resp.Categories { t.AppendRow(table.Row{ category.Id, category.Name, category.ImagesCount, + category.Url, }) } diff --git a/internal/piwigocli/images_details.go b/internal/piwigocli/images_details.go index c159e74..1d78ad6 100644 --- a/internal/piwigocli/images_details.go +++ b/internal/piwigocli/images_details.go @@ -11,6 +11,9 @@ type ImagesDetailsCommand struct { } type GetImagesDetailsResponse struct { + Categories []piwigo.Category `json:"categories"` + DateAvailable piwigo.TimeResult `json:"date_available"` + DateCreation piwigo.TimeResult `json:"date_creation"` } func (c *ImagesDetailsCommand) Execute(args []string) error { @@ -30,5 +33,16 @@ func (c *ImagesDetailsCommand) Execute(args []string) error { }, &resp); err != nil { return err } + + categories, err := p.Categories() + if err != nil { + return err + } + + for i, category := range resp.Categories { + resp.Categories[i] = categories[category.Id] + } + + piwigo.DumpResponse(resp) return nil } diff --git a/internal/piwigocli/method_try.go b/internal/piwigocli/method_try.go index 138783f..02fea3a 100644 --- a/internal/piwigocli/method_try.go +++ b/internal/piwigocli/method_try.go @@ -23,7 +23,7 @@ func (c *MethodTryCommand) Execute(args []string) error { return err } - var result map[string]interface{} + var result interface{} params := &url.Values{} for _, arg := range args { r := strings.SplitN(arg, "=", 2)