-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.go
250 lines (217 loc) · 4.89 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package wallhaven
import (
"fmt"
"net/url"
"strconv"
"strings"
)
// Search Types
// Category is an enum used to represent wallpaper categories
type Category int
// Enum for Category Types
const (
General Category = 0x100
Anime Category = 0x010
People Category = 0x001
)
func (c Category) String() string {
return strconv.FormatInt(int64(c), 2)
}
// Purity is an enum used to represent
type Purity int
// Enum for purity types
const (
SFW Purity = 0x100
Sketchy Purity = 0x010
NSFW Purity = 0x001
)
func (p Purity) String() string {
return strconv.FormatInt(int64(p), 2)
}
// 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
SixMonth
Year
)
func (t TopRange) String() string {
str := [...]string{"", "1d", "3d", "1w", "1m", "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
}
// Q is used to hold the Q params for various fulltext options that the WH Search supports
type Q struct {
Tags []string
ExcudeTags []string
UserName string
TagID int
Type string //Type is one of png/jpg
Like WallpaperID
}
func (q Q) toQuery() url.Values {
var sb strings.Builder
for _, tag := range q.Tags {
sb.WriteString("+")
sb.WriteString(tag)
}
for _, etag := range q.ExcudeTags {
sb.WriteString("-")
sb.WriteString(etag)
}
if len(q.UserName) > 0 {
sb.WriteString("@")
sb.WriteString(q.UserName)
}
if len(q.Type) > 0 {
sb.WriteString("type:")
sb.WriteString(q.Type)
}
out := url.Values{}
val := sb.String()
if len(val) > 0 {
out.Set("q", val)
}
return out
}
// Search provides various parameters to search for on wallhaven
type Search struct {
Query Q
Categories Category
Purities Purity
Sorting Sort
Order Order
TopRange TopRange
AtLeast Resolution
Resolutions []Resolution
Ratios []Ratio
Colors []string //Colors is an array of hex colors represented as strings in #RRGGBB format
Page int
}
func (s Search) toQuery() url.Values {
v := s.Query.toQuery()
if s.Categories > 0 {
v.Add("categories", s.Categories.String())
}
if s.Purities > 0 {
v.Add("purity", s.Purities.String())
}
if s.Sorting > 0 {
v.Add("sorting", s.Sorting.String())
}
if s.Order > 0 {
v.Add("order", s.Order.String())
}
if s.TopRange > 0 && s.Sorting == Toplist {
v.Add("topRange", s.TopRange.String())
}
if s.AtLeast.isValid() {
v.Add("atleast", s.AtLeast.String())
}
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, ","))
}
}
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, ","))
}
}
if len(s.Colors) > 0 {
v.Add("colors", strings.Join([]string(s.Colors), ","))
}
if s.Page > 0 {
v.Add("page", strconv.Itoa(s.Page))
}
return v
}
// SearchWallpapers performs a search on WH given a set of criteria.
// Note that this API behaves slightly differently than the various
// single item apis as it also includes the metadata for paging purposes
func SearchWallpapers(search *Search) (*SearchResults, error) {
resp, err := getWithValues("/search/", search.toQuery())
if err != nil {
return nil, err
}
out := &SearchResults{}
err = processResponse(resp, out)
if err != nil {
return nil, err
}
return out, nil
}