add parsing date, remap categories, improve try

This commit is contained in:
Celogeek 2021-12-20 11:29:20 +01:00
parent ad8253ff18
commit 2247deac18
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
5 changed files with 77 additions and 2 deletions

View File

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

26
internal/piwigo/time.go Normal file
View File

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

View File

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

View File

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

View File

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