look up with tags

This commit is contained in:
Celogeek 2022-01-02 10:50:16 +01:00
parent 6a0a58197b
commit 8663573a31
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 37 additions and 5 deletions

View File

@ -15,11 +15,15 @@ import (
)
type ImagesTagCommand struct {
Id int `short:"i" long:"id" description:"image id to tag" required:"true"`
Id int `short:"i" long:"id" description:"image id to tag"`
TagId int `short:"t" long:"tag-id" description:"look up for the first image of this tagId"`
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"`
KeepSurveyFilter bool `short:"k" long:"keep" description:"keep survey filter"`
}
func (c *ImagesTagCommand) Execute(args []string) error {
p := piwigo.Piwigo{}
if err := p.LoadConfig(); err != nil {
return err
@ -30,6 +34,33 @@ func (c *ImagesTagCommand) Execute(args []string) error {
return err
}
if c.Id == 0 {
data := &url.Values{}
switch {
case c.TagId > 0:
data.Set("tag_id", fmt.Sprint(c.TagId))
case c.TagName != "":
data.Set("tag_name", c.TagName)
default:
return fmt.Errorf("id or tagId or tagName are required")
}
data.Set("order", "date_creation")
data.Set("per_page", "1")
var results struct {
Images []piwigotools.ImageDetails `json:"images"`
}
if err := p.Post("pwg.tags.getImages", data, &results); err != nil {
return err
}
if len(results.Images) == 0 {
return fmt.Errorf("image not found")
}
c.Id = results.Images[0].Id
}
var imgDetails piwigotools.ImageDetails
if err := p.Post("pwg.images.getInfo", &url.Values{
"image_id": []string{fmt.Sprint(c.Id)},
@ -58,6 +89,7 @@ func (c *ImagesTagCommand) Execute(args []string) error {
t := table.NewWriter()
t.AppendRows([]table.Row{
{"Id", imgDetails.Id},
{"Name", imgDetails.Name},
{"Url", imgDetails.Url},
{"CreatedAt", imgDetails.DateCreation},
@ -75,7 +107,7 @@ func (c *ImagesTagCommand) Execute(args []string) error {
exclude = regexp.MustCompile(c.ExcludeTags)
}
sel := tags.Tags.Select(exclude)
sel := tags.Tags.Select(exclude, c.KeepSurveyFilter)
fmt.Println("Selection:")
for _, name := range sel.NamesWithAgeAt(&imgDetails.DateCreation) {

View File

@ -52,7 +52,7 @@ func (t Tags) JoinIds(sep string) string {
return strings.Join(ids, sep)
}
func (t Tags) Select(exclude *regexp.Regexp) Tags {
func (t Tags) Select(exclude *regexp.Regexp, keepFilter bool) Tags {
options := make([]string, len(t))
tags := map[string]*Tag{}
for i, tag := range t {
@ -66,7 +66,7 @@ func (t Tags) Select(exclude *regexp.Regexp) Tags {
Options: options,
PageSize: 20,
}
survey.AskOne(prompt, &answer, survey.WithKeepFilter(true))
survey.AskOne(prompt, &answer, survey.WithKeepFilter(keepFilter))
result := make([]*Tag, len(answer))
for i, a := range answer {