diff --git a/cmd/set.go b/cmd/set.go index 2585ae2..77f35e8 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -22,14 +22,14 @@ func init() { "range", "r", "1y", - "range for setList search (default is 1 Year).", + "range for setList search.", ) setCmd.PersistentFlags().StringVarP( &setPurity, "purity", "p", "110", - "purity for the setList search (default is 110).", + "purity for the setList search.", ) setCmd.PersistentFlags(). StringVarP( @@ -37,7 +37,7 @@ func init() { "categories", "c", "010", - "categories for the setList search (default is 010).", + "categories for the setList search.", ) setCmd.PersistentFlags(). StringVarP( @@ -45,7 +45,7 @@ func init() { "sort", "s", "toplist", - "sort by for results, valid sorts: date_added, relevance, random, views, favorites, setlist (default is toplist)", + "sort by for results, valid sorts: date_added, relevance, random, views, favorites, setlist.", ) setCmd.PersistentFlags(). StringVarP( @@ -53,7 +53,15 @@ func init() { "order", "o", "desc", - "sort order for results, valid sorts: asc desc (default is desc)", + "sort order for results, valid sorts: asc desc.", + ) + setCmd.PersistentFlags(). + IntVarP( + &setPage, + "maxPage", + "m", + 5, + "number of pages to randomly choose wallpaper from.", ) setCmd.PersistentFlags(). BoolVarP( @@ -61,7 +69,21 @@ func init() { "localPath", "l", false, - "set to true if the argument is to a directory or an image file (default is false)", + "set if the argument is to a directory or an image file.", + ) + setCmd.PersistentFlags(). + StringSliceVar( + &setRatios, + "ratios", + []string{"16x9", "16x10"}, + "ratios to search for.", + ) + setCmd.PersistentFlags(). + StringVar( + &setAtLeast, + "at-least", + "2560x1440", + "minimum resolution for results.", ) } @@ -71,6 +93,9 @@ var ( setCategories string setSorting string setOrder string + setAtLeast string + setRatios []string + setPage int localPath bool setCmd = &cobra.Command{ Use: "set", @@ -111,12 +136,9 @@ func set(args []string) error { Sorting: setSorting, Order: setOrder, TopRange: setRange, - AtLeast: wallhaven.Resolution{Width: 2560, Height: 1440}, - Ratios: []wallhaven.Ratio{ - {Horizontal: 16, Vertical: 9}, - {Horizontal: 16, Vertical: 10}, - }, - Page: r.Intn(5) + 1, + AtLeast: setAtLeast, + Ratios: setRatios, + Page: r.Intn(setPage) + 1, } log.Println(args) if len(args) > 0 { diff --git a/src/wallhaven/search.go b/src/wallhaven/search.go index 06b34f1..08daf12 100644 --- a/src/wallhaven/search.go +++ b/src/wallhaven/search.go @@ -2,7 +2,6 @@ package wallhaven import ( "encoding/json" - "fmt" "io" "net/http" "net/url" @@ -13,107 +12,6 @@ import ( "strings" ) -// Search Types - -// Category is an enum used to represent wallpaper categories -type Category string - -// Purity is an enum used to represent -type Purity string - -// Sort enum specifies the various sort types accepted by WH api -type Sort int - -// Sort Enum Values -const ( - DateAdded Sort = iota + 1 - Relevance - Random - Views - Favorites - Toplist -) - -func (s Sort) String() string { - str := [...]string{"", "date_added", "relevance", "random", "views", "favorites", "toplist"} - return str[s] -} - -// Order enum specifies the sort orders accepted by WH api -type Order int - -// Sort Enum Values -const ( - Desc Order = iota + 1 - Asc -) - -func (o Order) String() string { - str := [...]string{"", "desc", "asc"} - return str[o] -} - -// Privacy enum specifies the collection privacy returned by WH api -type Privacy int - -// Privacy Enum Values -const ( - Private Privacy = iota - Public -) - -func (p Privacy) String() string { - str := [...]string{"private", "public"} - return str[p] -} - -// TopRange is used to specify the time window for 'top' result when topList is chosen as sort param -type TopRange int - -// Enum for TopRange values -const ( - Day TopRange = iota + 1 - ThreeDay - Week - Month - ThreeMonth - SixMonth - Year -) - -func (t TopRange) String() string { - str := [...]string{"1d", "3d", "1w", "1M", "3M", "6M", "1y"} - return str[t] -} - -// Resolution specifies the image resolution to find -type Resolution struct { - Width int64 - Height int64 -} - -func (r Resolution) String() string { - return fmt.Sprintf("%vx%v", r.Width, r.Height) -} - -func (r Resolution) isValid() bool { - return r.Width > 0 && r.Height > 0 -} - -// Ratio may be used to specify the aspect ratio of the search -type Ratio struct { - Horizontal int - Vertical int -} - -func (r Ratio) String() string { - return fmt.Sprintf("%vx%v", r.Horizontal, r.Vertical) -} - -func (r Ratio) isValid() bool { - return r.Vertical > 0 && r.Horizontal > 0 -} - // WallpaperID is a string representing a wallpaper type WallpaperID string @@ -161,9 +59,9 @@ type Search struct { Sorting string Order string TopRange string - AtLeast Resolution - Resolutions []Resolution - Ratios []Ratio + AtLeast string + Resolutions []string + Ratios []string Colors []string // Colors is an array of hex colors represented as strings in #RRGGBB format Page int } @@ -185,30 +83,14 @@ func (s Search) toQuery() url.Values { if s.TopRange != "" && s.Sorting == "toplist" { v.Add("topRange", s.TopRange) } - if s.AtLeast.isValid() { - v.Add("atleast", s.AtLeast.String()) + if s.AtLeast != "" { + v.Add("atleast", s.AtLeast) } if len(s.Resolutions) > 0 { - outRes := []string{} - for _, res := range s.Resolutions { - if res.isValid() { - outRes = append(outRes, res.String()) - } - } - if len(outRes) > 0 { - v.Add("resolutions", strings.Join(outRes, ",")) - } + v.Add("resolutions", strings.Join(s.Ratios, ",")) } if len(s.Ratios) > 0 { - outRat := []string{} - for _, rat := range s.Ratios { - if rat.isValid() { - outRat = append(outRat, rat.String()) - } - } - if len(outRat) > 0 { - v.Add("ratios", strings.Join(outRat, ",")) - } + v.Add("ratios", strings.Join(s.Ratios, ",")) } if len(s.Colors) > 0 { v.Add("colors", strings.Join([]string(s.Colors), ","))