mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 10:12:37 +02:00
flat listing
This commit is contained in:
parent
2bd9b5533f
commit
2ea0a2ffef
@ -3,24 +3,28 @@ package piwigocli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/celogeek/piwigo-cli/internal/piwigo"
|
"github.com/celogeek/piwigo-cli/internal/piwigo"
|
||||||
|
"github.com/schollz/progressbar/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ImagesListCommand struct {
|
type ImagesListCommand struct {
|
||||||
Recursive bool `short:"r" long:"recursive" description:"recursive listing"`
|
Recursive bool `short:"r" long:"recursive" description:"recursive listing"`
|
||||||
CategoryId int `short:"c" long:"category" description:"list for this category" required:"true"`
|
CategoryId int `short:"c" long:"category" description:"list for this category"`
|
||||||
|
Filter string `short:"x" long:"filter" description:"Regexp filter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ImagesListResult struct {
|
type ImagesListResult struct {
|
||||||
Images []*piwigo.ImagesDetails `json:"images"`
|
Images []*piwigo.ImagesDetails `json:"images"`
|
||||||
Pagination struct {
|
Paging struct {
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
PerPage int `json:"per_page"`
|
PerPage int `json:"per_page"`
|
||||||
Total int `json:"total_count"`
|
Total int `json:"total_count,string"`
|
||||||
} `json:"page"`
|
} `json:"paging"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ImagesListCommand) Execute(args []string) error {
|
func (c *ImagesListCommand) Execute(args []string) error {
|
||||||
@ -34,26 +38,68 @@ func (c *ImagesListCommand) Execute(args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp ImagesListResult
|
|
||||||
data := &url.Values{}
|
|
||||||
data.Set("cat_id", fmt.Sprint(c.CategoryId))
|
|
||||||
data.Set("recursive", fmt.Sprintf("%v", c.Recursive))
|
|
||||||
if err := p.Post("pwg.categories.getImages", data, &resp); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
categories, err := p.Categories()
|
categories, err := p.Categories()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, image := range resp.Images {
|
var rootCatName string
|
||||||
for _, category := range image.Categories {
|
if c.CategoryId > 0 {
|
||||||
fmt.Printf("%s/%s\n", strings.ReplaceAll(categories[category.Id].Name, " / ", "/"), image.Filename)
|
rootCat, ok := categories[c.CategoryId]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("category doesn't exists")
|
||||||
}
|
}
|
||||||
|
rootCatName = rootCat.Name + " / "
|
||||||
}
|
}
|
||||||
|
|
||||||
// piwigo.DumpResponse(resp)
|
filter := regexp.MustCompile("")
|
||||||
|
if c.Filter != "" {
|
||||||
|
filter = regexp.MustCompile("(?i)" + c.Filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
var result []string
|
||||||
|
bar := progressbar.Default(1, "listing")
|
||||||
|
for page := 0; ; page++ {
|
||||||
|
var resp ImagesListResult
|
||||||
|
data := &url.Values{}
|
||||||
|
if c.CategoryId > 0 {
|
||||||
|
data.Set("cat_id", fmt.Sprint(c.CategoryId))
|
||||||
|
}
|
||||||
|
data.Set("recursive", fmt.Sprintf("%v", c.Recursive))
|
||||||
|
data.Set("page", fmt.Sprint(page))
|
||||||
|
if err := p.Post("pwg.categories.getImages", data, &resp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if page == 0 {
|
||||||
|
bar.ChangeMax(resp.Paging.Total)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, image := range resp.Images {
|
||||||
|
for _, category := range image.Categories {
|
||||||
|
cat, ok := categories[category.Id]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
catName := strings.ReplaceAll(cat.Name[len(rootCatName):], " / ", "/")
|
||||||
|
filename := fmt.Sprintf("%s/%s", catName, image.Filename)
|
||||||
|
if filter.MatchString(filename) {
|
||||||
|
result = append(result, filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bar.Add(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.Paging.Count < resp.Paging.PerPage {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bar.Close()
|
||||||
|
|
||||||
|
sort.Strings(result)
|
||||||
|
for _, r := range result {
|
||||||
|
fmt.Println(r)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user