mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 10:12:37 +02:00
add image details with tag age
This commit is contained in:
parent
2247deac18
commit
c302a10c75
@ -1,10 +1,10 @@
|
|||||||
package piwigo
|
package piwigo
|
||||||
|
|
||||||
import "net/url"
|
import (
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
type Categories struct {
|
type Categories []Category
|
||||||
Categories []Category `json:"categories"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Category struct {
|
type Category struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
@ -14,7 +14,9 @@ type Category struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Piwigo) Categories() (map[int]Category, error) {
|
func (p *Piwigo) Categories() (map[int]Category, error) {
|
||||||
var categories Categories
|
var categories struct {
|
||||||
|
Categories `json:"categories"`
|
||||||
|
}
|
||||||
|
|
||||||
err := p.Post("pwg.categories.getList", &url.Values{
|
err := p.Post("pwg.categories.getList", &url.Values{
|
||||||
"fullname": []string{"true"},
|
"fullname": []string{"true"},
|
||||||
@ -31,3 +33,11 @@ func (p *Piwigo) Categories() (map[int]Category, error) {
|
|||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Categories) Names() []string {
|
||||||
|
names := []string{}
|
||||||
|
for _, category := range c {
|
||||||
|
names = append(names, category.Name)
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
30
internal/piwigo/tags.go
Normal file
30
internal/piwigo/tags.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package piwigo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tags []Tag
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
LastModified TimeResult `json:"lastmodified"`
|
||||||
|
Birthdate TimeResult `json:"birthdate"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
UrlName string `json:"url_name"`
|
||||||
|
ImageUrl string `json:"page_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Tags) NamesWithAgeAt(createdAt TimeResult) []string {
|
||||||
|
names := []string{}
|
||||||
|
for _, category := range c {
|
||||||
|
bd := category.Birthdate.AgeAt(createdAt)
|
||||||
|
if bd != "" {
|
||||||
|
names = append(names, fmt.Sprintf("%s (%s)", category.Name, bd))
|
||||||
|
} else {
|
||||||
|
names = append(names, category.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package piwigo
|
package piwigo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -22,5 +23,93 @@ func (c *TimeResult) UnmarshalJSON(b []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c TimeResult) MarshalJSON() ([]byte, error) {
|
func (c TimeResult) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(`"` + time.Time(c).Format(time.RFC3339) + `"`), nil
|
switch s := c.String(); s {
|
||||||
|
case "":
|
||||||
|
return []byte("null"), nil
|
||||||
|
default:
|
||||||
|
return []byte(`"` + s + `"`), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c TimeResult) String() string {
|
||||||
|
t := c.toTime()
|
||||||
|
if t.IsZero() {
|
||||||
|
return ""
|
||||||
|
} else {
|
||||||
|
return t.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c TimeResult) toTime() time.Time {
|
||||||
|
return time.Time(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c TimeResult) AgeAt(createdAt TimeResult) string {
|
||||||
|
var year, month, day, hour, min, sec int
|
||||||
|
a := c.toTime()
|
||||||
|
if a.IsZero() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
b := createdAt.toTime()
|
||||||
|
if b.IsZero() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Location() != b.Location() {
|
||||||
|
b = b.In(a.Location())
|
||||||
|
}
|
||||||
|
if a.After(b) {
|
||||||
|
a, b = b, a
|
||||||
|
}
|
||||||
|
y1, M1, d1 := a.Date()
|
||||||
|
y2, M2, d2 := b.Date()
|
||||||
|
|
||||||
|
h1, m1, s1 := a.Clock()
|
||||||
|
h2, m2, s2 := b.Clock()
|
||||||
|
|
||||||
|
year = int(y2 - y1)
|
||||||
|
month = int(M2 - M1)
|
||||||
|
day = int(d2 - d1)
|
||||||
|
hour = int(h2 - h1)
|
||||||
|
min = int(m2 - m1)
|
||||||
|
sec = int(s2 - s1)
|
||||||
|
|
||||||
|
// Normalize negative values
|
||||||
|
if sec < 0 {
|
||||||
|
sec += 60
|
||||||
|
min--
|
||||||
|
}
|
||||||
|
if min < 0 {
|
||||||
|
min += 60
|
||||||
|
hour--
|
||||||
|
}
|
||||||
|
if hour < 0 {
|
||||||
|
hour += 24
|
||||||
|
day--
|
||||||
|
}
|
||||||
|
if day < 0 {
|
||||||
|
// days in month:
|
||||||
|
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
|
||||||
|
day += 32 - t.Day()
|
||||||
|
month--
|
||||||
|
}
|
||||||
|
if month < 0 {
|
||||||
|
month += 12
|
||||||
|
year--
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case year == 1:
|
||||||
|
return fmt.Sprintf("%d months old", 12+month)
|
||||||
|
case year > 1:
|
||||||
|
return fmt.Sprintf("%d years old", year)
|
||||||
|
case month == 1:
|
||||||
|
return fmt.Sprintf("%d month old", month)
|
||||||
|
case month > 1:
|
||||||
|
return fmt.Sprintf("%d months old", month)
|
||||||
|
case day > 1:
|
||||||
|
return fmt.Sprintf("%d days old", day)
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%d day, %d hour", day, hour)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,41 @@
|
|||||||
package piwigocli
|
package piwigocli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/celogeek/piwigo-cli/internal/piwigo"
|
"github.com/celogeek/piwigo-cli/internal/piwigo"
|
||||||
|
"github.com/jedib0t/go-pretty/v6/table"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ImagesDetailsCommand struct {
|
type ImagesDetailsCommand struct {
|
||||||
Id string `short:"i" long:"id" description:"ID of the images" required:"true"`
|
Id string `short:"i" long:"id" description:"ID of the images" required:"true"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Derivative struct {
|
||||||
|
Height int `json:"height"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
type GetImagesDetailsResponse struct {
|
type GetImagesDetailsResponse struct {
|
||||||
Categories []piwigo.Category `json:"categories"`
|
Id int `json:"id"`
|
||||||
DateAvailable piwigo.TimeResult `json:"date_available"`
|
Md5 string `json:"md5sum"`
|
||||||
DateCreation piwigo.TimeResult `json:"date_creation"`
|
Name string `json:"name"`
|
||||||
|
DateAvailable piwigo.TimeResult `json:"date_available"`
|
||||||
|
DateCreation piwigo.TimeResult `json:"date_creation"`
|
||||||
|
LastModified piwigo.TimeResult `json:"lastmodified"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
Url string `json:"page_url"`
|
||||||
|
ImageUrl string `json:"element_url"`
|
||||||
|
Filename string `json:"file"`
|
||||||
|
Filesize int64 `json:"filesize"`
|
||||||
|
Categories piwigo.Categories `json:"categories"`
|
||||||
|
Tags piwigo.Tags `json:"tags"`
|
||||||
|
Derivatives map[string]Derivative `json:"derivatives"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ImagesDetailsCommand) Execute(args []string) error {
|
func (c *ImagesDetailsCommand) Execute(args []string) error {
|
||||||
@ -43,6 +65,42 @@ func (c *ImagesDetailsCommand) Execute(args []string) error {
|
|||||||
resp.Categories[i] = categories[category.Id]
|
resp.Categories[i] = categories[category.Id]
|
||||||
}
|
}
|
||||||
|
|
||||||
piwigo.DumpResponse(resp)
|
t := table.NewWriter()
|
||||||
|
t.AppendHeader(table.Row{"Key", "Value"})
|
||||||
|
t.AppendRows([]table.Row{
|
||||||
|
{"Id", resp.Id},
|
||||||
|
{"Md5", resp.Md5},
|
||||||
|
{"Name", resp.Name},
|
||||||
|
{"DateAvailable", resp.DateAvailable},
|
||||||
|
{"DateCreation", resp.DateCreation},
|
||||||
|
{"LastModified", resp.LastModified},
|
||||||
|
{"Width", resp.Width},
|
||||||
|
{"Height", resp.Height},
|
||||||
|
{"Url", resp.Url},
|
||||||
|
{"ImageUrl", resp.ImageUrl},
|
||||||
|
{"Filename", resp.Filename},
|
||||||
|
{"Filesize", resp.Filesize},
|
||||||
|
{"Categories", strings.Join(resp.Categories.Names(), "\n")},
|
||||||
|
{"Tags", strings.Join(resp.Tags.NamesWithAgeAt(resp.DateCreation), "\n")},
|
||||||
|
// {"Derivatives", resp.Derivatives},
|
||||||
|
})
|
||||||
|
t.SetOutputMirror(os.Stdout)
|
||||||
|
t.SetStyle(table.StyleLight)
|
||||||
|
t.Render()
|
||||||
|
|
||||||
|
fmt.Println("Derivatives:")
|
||||||
|
t = table.NewWriter()
|
||||||
|
t.AppendHeader(table.Row{"Name", "Width", "Height", "Url"})
|
||||||
|
for k, v := range resp.Derivatives {
|
||||||
|
t.AppendRow(table.Row{
|
||||||
|
k,
|
||||||
|
v.Width,
|
||||||
|
v.Height,
|
||||||
|
v.Url,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
t.SetOutputMirror(os.Stdout)
|
||||||
|
t.SetStyle(table.StyleLight)
|
||||||
|
t.Render()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user