-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
337 lines (293 loc) · 8.94 KB
/
query.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"cheapshark"
"launchpad.net/go-unityscopes/v2"
"log"
"math"
"strconv"
"time"
)
type Query struct {
scopeDirectory string
}
const windowsQueryLimit = 20
const bestDealsCategoryTemplate = `{
"schema-version": 1,
"template": {
"category-layout": "carousel",
"card-layout": "vertical",
"card-size": "large"
},
"components": {
"title": "title",
"art" : {
"aspect-ratio":0.725,
"field": "art"
},
"subtitle":"salePrice",
"attributes":"attributes"
}
}`
const savingCategoryTemplate = `{
"schema-version": 1,
"template": {
"category-layout": "horizontal-list",
"card-size": "medium",
"overlay":true
},
"components": {
"title": "title",
"art": "art",
"subtitle":"salePrice",
"attributes":"attributes"
}
}`
const cheapestCategoryTemplate = `{
"schema-version": 1,
"template": {
"category-layout": "horizontal-list",
"card-size": "medium",
"overlay":true
},
"components": {
"title": "title",
"art": "art",
"subtitle":"salePrice",
"attributes":"attributes"
}
}`
const bestGameCategoryTemplate = `{
"schema-version": 1,
"template": {
"category-layout": "horizontal-list",
"card-size": "medium",
"overlay":true
},
"components": {
"title": "title",
"art": "art",
"subtitle":"salePrice",
"attributes":"attributes"
}
}`
const bundleCategoryTemplate = `{
"schema-version": 1,
"template": {
"category-layout": "vertical-journal",
"card-layout" : "horizontal",
"card-size": "small"
},
"components": {
"title": "title",
"art": "art",
"subtitle": "username"
}
}`
const searchCategoryTemplate = `{
"schema-version": 1,
"template": {
"category-layout": "vertical-journal",
"card-layout" : "horizontal",
"card-size": "20"
},
"components": {
"title": "title",
"art": "art",
"subtitle": "salePrice"
}
}`
func (s *Query) AddQueryResults(reply *scopes.SearchReply, query string, settings Settings) error {
if query == "" {
return s.AddEmptyQueryResults(reply, query, settings)
} else {
return s.AddSearchResults(reply, query)
}
}
type Category struct {
DealsReq cheapshark.Deal
Id string
Title string
Template string
CompleteDetail bool
}
var cs cheapshark.CheapShark
func (s *Query) AddEmptyQueryResults(reply *scopes.SearchReply, query string, settings Settings) error {
steamworks := settings.Steamworks
max_price := settings.MaxPrice
// cheapshark treat 50 as no limit, but we want different behaviour
if max_price == 50 {
max_price = 51
} else if max_price == 0 {
max_price = 50
}
// limit cheapshark request if Windows is enabled, because Windows
// compatible games are too much
queryLimit := 0
if settings.Windows {
queryLimit = windowsQueryLimit
}
bestDealsReq := cheapshark.DealsRequest{SortBy: "Deal Rating", OnSale: true, Steamworks: steamworks, UpperPrice: max_price, PageSize: queryLimit}
savingDealsReq := cheapshark.DealsRequest{SortBy: "Savings", OnSale: true, Steamworks: steamworks, UpperPrice: max_price, PageSize: queryLimit}
cheapestDealsReq := cheapshark.DealsRequest{SortBy: "Price", OnSale: true, Steamworks: steamworks, UpperPrice: max_price, PageSize: queryLimit}
bestGameDealsReq := cheapshark.DealsRequest{SortBy: "Metacritic", OnSale: true, Steamworks: steamworks, UpperPrice: max_price, PageSize: queryLimit}
deals := make(chan Category)
bestCategory := createDeals(bestDealsReq, "deals", "Best Deals", bestDealsCategoryTemplate, true)
savingCategory := createDeals(savingDealsReq, "saving", "Most Saving", savingCategoryTemplate, true)
cheapCategory := createDeals(cheapestDealsReq, "cheapest", "Cheapest", cheapestCategoryTemplate, true)
bestGameCategory := createDeals(bestGameDealsReq, "best", "Popular Games", bestGameCategoryTemplate, true)
go func() {
deals <- bestCategory
deals <- savingCategory
deals <- cheapCategory
deals <- bestGameCategory
close(deals)
}()
if err := s.registerCategory(reply, deals); err != nil {
log.Println(err)
return err
}
return nil
}
func createDeals(dealsReq cheapshark.DealsRequest, id string, title string, template string, complete bool) Category {
cat := Category{
Id: id,
Title: title,
Template: template,
CompleteDetail: complete,
}
if deals, e := cs.Deals(&dealsReq); e != nil {
log.Println(e)
return cat
} else {
cat.DealsReq = deals
return cat
}
}
var stores cheapshark.Store
func (s *Query) AddSearchResults(reply *scopes.SearchReply, query string) error {
var cs cheapshark.CheapShark
if stores == nil {
stores = cs.Stores()
}
deals := make(chan Category)
go func() {
for _, store := range stores {
searchReq := cheapshark.DealsRequest{Title: query, StoreID: store.StoreID, PageSize: 5}
cat := createDeals(searchReq, store.StoreID, store.StoreName, searchCategoryTemplate, false)
deals <- cat
}
//else if err := s.registerCategory(reply, store.StoreID, store.StoreName, searchCategoryTemplate, deals, false); err != nil {
//return err
//}
}()
if err := s.registerCategory(reply, deals); err != nil {
log.Println(err)
return err
}
return nil
}
func getDateString(unixDate int64) string {
return time.Unix(unixDate, 0).Format("2 January 2006")
}
func (s *Query) registerCategory(reply *scopes.SearchReply, cats <-chan Category) error {
for cat := range cats {
log.Println("registerCategory ", cat.Title)
category := reply.RegisterCategory(cat.Id, cat.Title, "", cat.Template)
res := make(chan *scopes.CategorisedResult)
go func() {
result := scopes.NewCategorisedResult(category)
for _, d := range cat.DealsReq {
savingsF, _ := d.Savings.Float64()
releaseDate, _ := d.ReleaseDate.Int64()
releaseDateStr := ""
if r := getDateString(releaseDate); releaseDate != 0 {
releaseDateStr = r
}
storeIcon := s.getStoreIcon(d.StoreID)
if cat.CompleteDetail {
if info, err := gb.GetInfo(d.Title); err == nil {
if Filter(info.Platforms, platformFilter) {
addCategorisedGameResult(result, "http://www.cheapshark.com/redirect?dealID="+d.DealID, d.Title, d.Title, d.NormalPrice.String(), d.SalePrice.String(), strconv.Itoa(int(math.Floor(savingsF))), d.MetacriticScore.String(), d.DealRating.String(), info.Image.ThumbURL, info.Image.SmallURL, storeIcon, info.Description, releaseDateStr, s.getStoreIcon("platform"+strconv.Itoa(GetPlatformsFilter(info.Platforms))))
res <- result
//reply.Push(result)
}
continue
}
}
// cant find data from GB database, use cheapshark one
if platformFilter&8 > 0 { // only add if unknown platform is enabled
addCategorisedGameResult(result, "http://www.cheapshark.com/redirect?dealID="+d.DealID, d.Title, d.Title, d.NormalPrice.String(), d.SalePrice.String(), strconv.Itoa(int(math.Floor(savingsF))), d.MetacriticScore.String(), d.DealRating.String(), d.Thumb, d.Thumb, storeIcon, "", releaseDateStr, "")
//reply.Push(result)
res <- result
}
}
close(res)
}()
for r := range res {
reply.Push(r)
}
}
return nil
}
func addCategorisedGameResult(result *scopes.CategorisedResult, uri string, dndUri string, title string, normalPrice string, salePrice string, savings string, metacriticScore string, dealRating string, art string, bigArt string, storeIcon string, description string, releaseDate string, platformsIcon string) error {
result.SetURI(uri)
result.SetDndURI(dndUri)
result.SetTitle(title)
result.SetArt(art)
result.Set("bigArt", bigArt)
result.Set("normalPrice", normalPrice)
if salePrice == "0" {
result.Set("salePrice", "<b>FREE</b> from $"+normalPrice)
} else {
result.Set("salePrice", "<b>$"+salePrice+"</b> from $"+normalPrice)
}
result.Set("uri", uri)
if description != "" {
result.Set("description", description)
} else {
result.Set("description", "<h1>No Description</h1>")
}
type Attr struct {
Value string `json:"value"`
Icon string `json:"icon"`
}
attr := []Attr{}
if savings != "0" {
attr = append(attr, Attr{Value: savings + "%", Icon: storeIcon})
}
if metacriticScore != "0" {
attr = append(attr, Attr{Value: metacriticScore, Icon: "image://theme/starred"})
}
result.Set("attributes", attr)
completeAttr := attr
if releaseDate != "" {
completeAttr = append(completeAttr, Attr{Value: "released at " + releaseDate})
}
if platformsIcon != "" {
completeAttr = append(completeAttr, Attr{Icon: platformsIcon})
}
result.Set("completeAttributes", completeAttr)
return nil
}
var platformFilter = 0
func (s *Query) SetPlatformFilter(linux bool, osx bool, windows bool, unknown bool) {
platformFilter = 0
if linux {
platformFilter += 1
}
if osx {
platformFilter += 2
}
if windows {
platformFilter += 4
}
if unknown {
platformFilter += 8
}
}
func (s *Query) SetScopeDirectory(dir string) {
s.scopeDirectory = dir
}
func (s *Query) getStoreIcon(storeID string) string {
return s.scopeDirectory + "/" + storeID + ".png"
}