iterate over image to tag

This commit is contained in:
Celogeek 2022-01-02 11:28:09 +01:00
parent 8663573a31
commit eb59b8111d
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 126 additions and 94 deletions

View File

@ -20,9 +20,16 @@ type ImagesTagCommand struct {
TagName string `short:"T" long:"tag" description:"look up for the first image of this tagName"` TagName string `short:"T" long:"tag" description:"look up for the first image of this tagName"`
ExcludeTags string `short:"x" long:"exclude" description:"exclude tag from selection"` ExcludeTags string `short:"x" long:"exclude" description:"exclude tag from selection"`
KeepSurveyFilter bool `short:"k" long:"keep" description:"keep survey filter"` KeepSurveyFilter bool `short:"k" long:"keep" description:"keep survey filter"`
MaxImages int `short:"m" long:"max" description:"loop on a maximum number of images" default:"1"`
} }
func (c *ImagesTagCommand) Execute(args []string) error { func (c *ImagesTagCommand) Execute(args []string) error {
if c.MaxImages < 0 || c.MaxImages > 100 {
return fmt.Errorf("maxImages should be between 1 and 100")
}
if c.Id > 0 {
c.MaxImages = 1
}
p := piwigo.Piwigo{} p := piwigo.Piwigo{}
if err := p.LoadConfig(); err != nil { if err := p.LoadConfig(); err != nil {
@ -34,7 +41,27 @@ func (c *ImagesTagCommand) Execute(args []string) error {
return err return err
} }
if c.Id == 0 { var tags struct {
Tags piwigotools.Tags `json:"tags"`
}
if err := p.Post("pwg.tags.getAdminList", nil, &tags); err != nil {
return err
}
sort.Slice(tags.Tags, func(i, j int) bool {
return tags.Tags[i].Name < tags.Tags[j].Name
})
var exclude *regexp.Regexp
if c.ExcludeTags != "" {
exclude = regexp.MustCompile(c.ExcludeTags)
}
selectTags := tags.Tags.Selector(exclude, c.KeepSurveyFilter)
imagesToTags := make([]int, 0, c.MaxImages)
if c.Id > 0 {
imagesToTags = append(imagesToTags, c.Id)
} else {
data := &url.Values{} data := &url.Values{}
switch { switch {
@ -47,44 +74,33 @@ func (c *ImagesTagCommand) Execute(args []string) error {
} }
data.Set("order", "date_creation") data.Set("order", "date_creation")
data.Set("per_page", "1") data.Set("per_page", fmt.Sprint(c.MaxImages))
var results struct { var results struct {
Images []piwigotools.ImageDetails `json:"images"` Images []piwigotools.ImageDetails `json:"images"`
} }
if err := p.Post("pwg.tags.getImages", data, &results); err != nil { if err := p.Post("pwg.tags.getImages", data, &results); err != nil {
return err return err
} }
for _, img := range results.Images {
if len(results.Images) == 0 { imagesToTags = append(imagesToTags, img.Id)
return fmt.Errorf("image not found")
} }
c.Id = results.Images[0].Id
} }
for _, imgId := range imagesToTags {
for {
var imgDetails piwigotools.ImageDetails var imgDetails piwigotools.ImageDetails
if err := p.Post("pwg.images.getInfo", &url.Values{ if err := p.Post("pwg.images.getInfo", &url.Values{
"image_id": []string{fmt.Sprint(c.Id)}, "image_id": []string{fmt.Sprint(imgId)},
}, &imgDetails); err != nil { }, &imgDetails); err != nil {
return err return err
} }
var tags struct {
Tags piwigotools.Tags `json:"tags"`
}
if err := p.Post("pwg.tags.getAdminList", &url.Values{
"image_id": []string{fmt.Sprint(c.Id)},
}, &tags); err != nil {
return err
}
sort.Slice(tags.Tags, func(i, j int) bool {
return tags.Tags[i].Name < tags.Tags[j].Name
})
img, err := imgDetails.Preview(25) img, err := imgDetails.Preview(25)
if err != nil { if err != nil {
return err return err
} }
fmt.Println("\033[2J") // clear screen
fmt.Println(img) fmt.Println(img)
t := table.NewWriter() t := table.NewWriter()
@ -102,18 +118,26 @@ func (c *ImagesTagCommand) Execute(args []string) error {
t.SetStyle(table.StyleLight) t.SetStyle(table.StyleLight)
t.Render() t.Render()
var exclude *regexp.Regexp fmt.Println()
if c.ExcludeTags != "" {
exclude = regexp.MustCompile(c.ExcludeTags)
}
sel := tags.Tags.Select(exclude, c.KeepSurveyFilter) sel := selectTags()
fmt.Println("Selection:") fmt.Println("Selection:")
for _, name := range sel.NamesWithAgeAt(&imgDetails.DateCreation) { for _, name := range sel.NamesWithAgeAt(&imgDetails.DateCreation) {
fmt.Printf(" - %s\n", name) fmt.Printf(" - %s\n", name)
} }
if len(sel) == 0 {
exit := false
survey.AskOne(&survey.Confirm{
Message: "Selection is empty, exit:",
Default: false,
}, &exit)
if exit {
return nil
}
}
confirmSel := false confirmSel := false
survey.AskOne(&survey.Confirm{ survey.AskOne(&survey.Confirm{
Message: "Confirm:", Message: "Confirm:",
@ -121,12 +145,12 @@ func (c *ImagesTagCommand) Execute(args []string) error {
}, &confirmSel) }, &confirmSel)
if !confirmSel { if !confirmSel {
return nil continue
} }
fmt.Println("Applying changes...") fmt.Println("Applying changes...")
data := &url.Values{} data := &url.Values{}
data.Set("image_id", fmt.Sprint(c.Id)) data.Set("image_id", fmt.Sprint(imgId))
data.Set("multiple_value_mode", "replace") data.Set("multiple_value_mode", "replace")
data.Set("tag_ids", sel.JoinIds(",")) data.Set("tag_ids", sel.JoinIds(","))
@ -134,6 +158,8 @@ func (c *ImagesTagCommand) Execute(args []string) error {
return err return err
} }
fmt.Println("Done!") fmt.Println("Done!")
break
}
}
return nil return nil
} }

View File

@ -52,25 +52,31 @@ func (t Tags) JoinIds(sep string) string {
return strings.Join(ids, sep) return strings.Join(ids, sep)
} }
func (t Tags) Select(exclude *regexp.Regexp, keepFilter bool) Tags { func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool) func() Tags {
options := make([]string, len(t)) options := make([]string, 0, len(t))
tags := map[string]*Tag{} tags := map[string]*Tag{}
for i, tag := range t { for _, tag := range t {
options[i] = tag.Name if exclude != nil && exclude.MatchString(tag.Name) {
continue
}
options = append(options, tag.Name)
tags[tag.Name] = tag tags[tag.Name] = tag
} }
return func() Tags {
answer := []string{} answer := []string{}
prompt := &survey.MultiSelect{
survey.AskOne(&survey.MultiSelect{
Message: "Tags:", Message: "Tags:",
Options: options, Options: options,
PageSize: 20, PageSize: 20,
} }, &answer, survey.WithKeepFilter(keepFilter))
survey.AskOne(prompt, &answer, survey.WithKeepFilter(keepFilter))
result := make([]*Tag, len(answer)) result := make([]*Tag, len(answer))
for i, a := range answer { for i, a := range answer {
result[i] = tags[a] result[i] = tags[a]
} }
return result return result
}
} }