move tree, simplify categories

This commit is contained in:
Celogeek 2021-12-31 13:23:28 +01:00
parent ed52dd691c
commit 222fce0e3f
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
5 changed files with 26 additions and 19 deletions

View File

@ -5,10 +5,10 @@ import (
"net/url" "net/url"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/celogeek/piwigo-cli/internal/piwigo" "github.com/celogeek/piwigo-cli/internal/piwigo"
"github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools"
"github.com/celogeek/piwigo-cli/internal/tree"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
) )
@ -51,7 +51,7 @@ func (c *ImagesListCommand) Execute(args []string) error {
if !ok { if !ok {
return fmt.Errorf("category doesn't exists") return fmt.Errorf("category doesn't exists")
} }
rootCatName = rootCat.Name + " / " rootCatName = rootCat.Name
} }
filter := regexp.MustCompile("") filter := regexp.MustCompile("")
@ -59,7 +59,7 @@ func (c *ImagesListCommand) Execute(args []string) error {
filter = regexp.MustCompile("(?i)" + c.Filter) filter = regexp.MustCompile("(?i)" + c.Filter)
} }
rootTree := piwigotools.NewTree() rootTree := tree.New()
bar := progressbar.Default(1, "listing") bar := progressbar.Default(1, "listing")
for page := 0; ; page++ { for page := 0; ; page++ {
@ -80,9 +80,11 @@ func (c *ImagesListCommand) Execute(args []string) error {
for _, image := range resp.Images { for _, image := range resp.Images {
for _, cat := range image.Categories { for _, cat := range image.Categories {
filename := filepath.Join( filename, _ := filepath.Rel(rootCatName,
strings.ReplaceAll(categories[cat.Id].Name[len(rootCatName):], " / ", "/"), filepath.Join(
categories[cat.Id].Name,
image.Filename, image.Filename,
),
) )
if !filter.MatchString(filename) { if !filter.MatchString(filename) {
continue continue

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/url" "net/url"
"strings"
"github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools"
) )
@ -21,22 +22,25 @@ func (p *Piwigo) Categories() (piwigotools.Categories, error) {
}, &result); err != nil { }, &result); err != nil {
return nil, err return nil, err
} }
for _, category := range result.Categories {
category.Name = strings.ReplaceAll(category.Name, " / ", "/")
}
return result.Categories, nil return result.Categories, nil
} }
func (p *Piwigo) CategoryFromId() (map[int]piwigotools.Category, error) { func (p *Piwigo) CategoryFromId() (map[int]*piwigotools.Category, error) {
categories, err := p.Categories() categories, err := p.Categories()
if err != nil { if err != nil {
return nil, err return nil, err
} }
result := map[int]piwigotools.Category{} result := map[int]*piwigotools.Category{}
for _, category := range categories { for _, category := range categories {
result[category.Id] = category result[category.Id] = category
} }
return result, nil return result, nil
} }
func (p *Piwigo) CategoryFromName(catId int) (map[string]piwigotools.Category, error) { func (p *Piwigo) CategoryFromName(catId int) (map[string]*piwigotools.Category, error) {
var results CategoriesResult var results CategoriesResult
err := p.Post("pwg.categories.getList", &url.Values{ err := p.Post("pwg.categories.getList", &url.Values{
@ -46,13 +50,14 @@ func (p *Piwigo) CategoryFromName(catId int) (map[string]piwigotools.Category, e
return nil, err return nil, err
} }
categoriesId := map[string]piwigotools.Category{} categoriesId := map[string]*piwigotools.Category{}
ok := false ok := false
for _, category := range results.Categories { for _, category := range results.Categories {
switch category.Id { switch category.Id {
case catId: case catId:
ok = true ok = true
default: default:
category.Name = strings.ReplaceAll(category.Name, " / ", "/")
categoriesId[category.Name] = category categoriesId[category.Name] = category
} }
} }

View File

@ -178,7 +178,7 @@ func (p *Piwigo) ScanTree(
dirname := norm.NFC.String(dir.Name()) dirname := norm.NFC.String(dir.Name())
category, ok := categoryFromName[dirname] category, ok := categoryFromName[dirname]
if !ok { if !ok {
category = piwigotools.Category{} category = &piwigotools.Category{}
p.mu.Lock() p.mu.Lock()
err = p.Post("pwg.categories.add", &url.Values{ err = p.Post("pwg.categories.add", &url.Values{
"name": []string{strings.ReplaceAll(dirname, "'", `\'`)}, "name": []string{strings.ReplaceAll(dirname, "'", `\'`)},

View File

@ -1,6 +1,6 @@
package piwigotools package piwigotools
type Categories []Category type Categories []*Category
type Category struct { type Category struct {
Id int `json:"id"` Id int `json:"id"`

View File

@ -1,4 +1,4 @@
package piwigotools package tree
import ( import (
"path/filepath" "path/filepath"
@ -13,17 +13,17 @@ type Tree interface {
TreeView() chan string TreeView() chan string
} }
type node struct { func New() Tree {
Name string
Nodes map[string]*node
}
func NewTree() Tree {
return &node{ return &node{
Name: ".", Name: ".",
} }
} }
type node struct {
Name string
Nodes map[string]*node
}
func (t *node) Add(name string) Tree { func (t *node) Add(name string) Tree {
if t.Nodes == nil { if t.Nodes == nil {
t.Nodes = map[string]*node{} t.Nodes = map[string]*node{}