Skip to content

Commit

Permalink
add ordered tags fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sonu27 committed Dec 26, 2023
1 parent ed73553 commit a0898c8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
15 changes: 15 additions & 0 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ type Store struct {
firestore *firestore.Client
}

func (s *Store) GetAll(ctx context.Context) ([]map[string]any, error) {
iter := s.firestore.Collection(s.collection).Documents(ctx)
dsnap, err := iter.GetAll()
if err != nil {
return nil, err
}

wallpapers := make([]map[string]any, 0, len(dsnap))
for _, v := range dsnap {
wallpapers = append(wallpapers, v.Data())
}

return wallpapers, nil
}

func (s *Store) Get(ctx context.Context, id string) (*WallpaperWithTags, error) {
doc, err := s.firestore.Collection(s.collection).Doc(id).Get(ctx)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/updater/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Image struct {
URL string `json:"url,omitempty"`
ThumbURL string `json:"thumbUrl,omitempty"`

Tags map[string]float32 `json:"tags,omitempty"`
Tags map[string]float32 `json:"tags,omitempty"`
TagsOrdered []string `json:"tagsOrdered,omitempty"`
}

func From(bw bing.Image, market string, bingURL string) (Image, error) {
Expand Down
23 changes: 23 additions & 0 deletions internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io"
"net/http"
"os"
"sort"
"strings"
"time"
)
Expand Down Expand Up @@ -172,6 +173,23 @@ func (u *Updater) Update(ctx context.Context) error {
image.Tags[strings.ToLower(v.Description)] = v.Score
}

// start: duplicate tags to t
tmp := make([]tag, 0, len(image.Tags))
for k, v := range image.Tags {
tmp = append(tmp, tag{Name: k, Score: v})
}

// sort by score
sort.SliceStable(tmp, func(i, j int) bool {
return tmp[i].Score > tmp[j].Score
})

image.TagsOrdered = make([]string, 0, len(tmp))
for _, v := range tmp {
image.TagsOrdered = append(image.TagsOrdered, strings.ReplaceAll(v.Name, " ", "-"))
}
// end: duplicate tags to t

_, err = u.firestoreClient.Upsert(ctx, image)
if err != nil {
return err
Expand Down Expand Up @@ -280,3 +298,8 @@ func (u *Updater) translateText(ctx context.Context, text string) (string, error

return resp[0].Text, nil
}

type tag struct {
Name string
Score float32
}

0 comments on commit a0898c8

Please sign in to comment.