-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliveSearch.go
517 lines (433 loc) · 13.9 KB
/
liveSearch.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"sync"
)
type episodesSuggested struct {
EpisodeNumber int `json: "groupedEpisode"`
Compilation []tempSuggested `json: "compilation"`
}
type xdcc struct {
Content []xdccContent `json: "content"`
}
type xdccContent struct {
Name string `json: "name"`
Number int `json: "number"`
BotId int `json: "botId"`
Size string `json: "size"`
EpisodeNumber int `json: "episodeNumber"`
LastModified string `json: "lastModified"`
}
type tempSuggested struct {
Suggestion string `json: "suggestion"`
SuggestionEpisode int `json: "suggestionEpisode"`
SuggestionContent []xdccContent `json: "suggestionContent"`
}
type errorMessage struct {
Error bool `json: "errorExist"`
ErrorMessage string `json: "errorMessage"`
}
// type compiledSuggest
func tempSearchMain(querySuggestion string) ([]episodesSuggested, errorMessage) {
var compiledSuggestions []episodesSuggested
var err errorMessage
querySuggestion = strings.TrimSpace(querySuggestion)
lengthCheck := len(querySuggestion)
switch lengthCheck {
case 0:
err = errorMessage{
Error: true,
ErrorMessage: "Invalid query entered",
}
default:
// query this with match query
// 3-gatsu no lion 4, 10
// before match query, remove the quality tag [720] for example
quality, newQuery := getQuality(querySuggestion)
episodeNumbers, errMsg, arrType := getEpisode(newQuery)
querySuggestion = matchQuery(newQuery)
fmt.Println("this is episode number", episodeNumbers)
fmt.Println("this is the error message", errMsg)
fmt.Println("this is the quality", quality)
if errMsg {
err = errorMessage{
Error: errMsg,
ErrorMessage: "Your query episode numbers are inverted!",
}
} else {
// test case
querySuggestion = strings.Replace(querySuggestion, " ", "%20", -1)
fmt.Println("this is the querySuggestion", querySuggestion)
temp := xdcc{}
var compilation []episodesSuggested
var collection []tempSuggested
// make a slice for suggestions
compiledSuggestions = findPacklist(querySuggestion, episodeNumbers, quality, arrType, &temp, collection, compilation)
// pretty print tempSuggestion
// slcT, _ := json.MarshalIndent(compiledSuggestions, "", " ")
// fmt.Println(string(slcT))
// form the query here once you figure out what the user wants
// ex. gamers! 9 will return did you mean [HorribleSubs] Gamers! - 09[480].mkv?
err = errorMessage{
Error: errMsg,
ErrorMessage: "",
}
}
}
return compiledSuggestions, err
}
func findPacklist(query string, episode []int, quality string, arrType string, x *xdcc, collection []tempSuggested, compilation []episodesSuggested) []episodesSuggested {
var tempEpisodeHold []int
var wg sync.WaitGroup
if arrType == "continuous" {
for i := episode[0]; i <= episode[1]; i++ {
tempEpisodeHold = append(tempEpisodeHold, i)
}
episode = tempEpisodeHold
}
fmt.Println("this is in packlist before searching query")
if len(episode) < 1 {
unlimitedEps := -1
queryString := fmt.Sprintf("https://api.nibl.co.uk:8080/nibl/search?query=%s&episodeNumber=%d", query, unlimitedEps)
fmt.Println("this is the unique query string", queryString)
getJSON(queryString, x)
if len(x.Content) > 0 {
fmt.Println("this is a valid query")
// slcT, _ := json.MarshalIndent(x.Content, "", " ")
// fmt.Println("this is the collection of responses")
// fmt.Println(string(slcT))
collection = createSuggestion(x, collection, quality)
tempCollection := episodesSuggested{
Compilation: collection,
EpisodeNumber: -1,
}
compilation = append(compilation, tempCollection)
}
// needs own sorting method
if len(compilation) > 0 {
sortByEpisodeCollection(compilation)
}
} else {
// ranging up to a nonexistent value like 1-29 for blen
var newCollection []tempSuggested
for _, singleEP := range episode {
wg.Add(1)
go func(singleEP int) {
queryString := fmt.Sprintf("https://api.nibl.co.uk:8080/nibl/search?query=%s&episodeNumber=%d", query, singleEP)
fmt.Println("this is the unique query string", queryString)
fmt.Println("this is the quality", quality)
getJSON(queryString, x)
// had to create new array for collection, it was reusing the old collection
// hence the duplicate values
if len(x.Content) > 0 {
fmt.Println("this is a valid query")
// slcT, _ := json.MarshalIndent(x.Content, "", " ")
// fmt.Println("this is the collection of responses")
// fmt.Println(string(slcT))
newCollection := createSuggestion(x, newCollection, quality)
tempCollection := episodesSuggested{
Compilation: newCollection,
EpisodeNumber: singleEP,
}
// slcT, _ := json.MarshalIndent(tempCollection, "", " ")
// fmt.Println(string(slcT))
compilation = append(compilation, tempCollection)
wg.Done()
}
}(singleEP)
}
wg.Wait()
sortByEpisodeCompilation(compilation)
}
return compilation
}
func sortByEpisodeCompilation(compilation []episodesSuggested) []episodesSuggested {
sort.Slice(compilation, func(i, j int) bool {
return compilation[i].EpisodeNumber < compilation[j].EpisodeNumber
})
return compilation
}
func sortByEpisodeCollection(compilation []episodesSuggested) []episodesSuggested {
// fmt.Println("this is compilation", compilation)
tempCompilation := compilation[0].Compilation
sort.Slice(tempCompilation, func(i, j int) bool {
return tempCompilation[i].SuggestionEpisode < tempCompilation[j].SuggestionEpisode
})
// slcT, _ := json.MarshalIndent(tempCompilation[0].SuggestionEpisode, "", " ")
// slcT2, _ := json.MarshalIndent(compilation[0].Compilation[1].SuggestionEpisode, "", " ")
// slcT3, _ := json.MarshalIndent(compilation, "", " ")
// fmt.Println("this is the collection of responses")
// fmt.Println(string(slcT))
// fmt.Println(string(slcT2))
// fmt.Println(string(slcT3))
return compilation
}
func createSuggestion(x *xdcc, collection []tempSuggested, quality string) []tempSuggested {
for _, j := range x.Content {
// suggest := expCheck(j.Name, episode, quality)
suggest := j.Name
qualityExist := checkQuality(suggest, quality)
if len(suggest) > 0 && qualityExist {
run := true
for k := range collection {
if suggest == collection[k].Suggestion {
collection[k].SuggestionContent = append(collection[k].SuggestionContent, j)
// fmt.Println(k)
// slcT, _ := json.MarshalIndent(collection, "", " ")
// fmt.Println("this is the current collection")
// fmt.Println(string(slcT))
run = false
}
}
if run {
var tempSuggestionContent []xdccContent
temp := tempSuggested{
Suggestion: suggest,
SuggestionEpisode: j.EpisodeNumber,
SuggestionContent: append(tempSuggestionContent, j),
}
collection = append(collection, temp)
// // fmt.Println("this is the length of the collection", len(collection))
// // fmt.Println("we are on index", i)
// // fmt.Println("this is the else if")
}
}
}
// collection = groupDuplicates(collection)
// do a final group here in case
return collection
}
func checkQuality(name string, quality string) bool {
var isQuality bool
if len(quality) > 0 {
check := regexp.MustCompile(quality)
c := check.FindAllStringSubmatch(name, -1)
if len(c) > 0 {
isQuality = true
}
} else if len(quality) == 0 {
isQuality = true
}
return isQuality
}
func getQuality(name string) (string, string) {
var tempQualityArr string
var tempQuality string
quality := regexp.MustCompile(`\[(.*?)\]`)
q := quality.FindAllStringSubmatch(name, -1)
if len(q) > 0 {
fmt.Println("this is tempquality", q)
tempQualityArr = q[0][0]
tempQuality = q[0][1]
}
newName := strings.Replace(name, tempQualityArr, "", -1)
return tempQuality, newName
}
func getEpisode(name string) ([]int, bool, string) {
var episodeNumbers []string
var arrType string
queryOnly := matchQuery(name)
fmt.Println("this is query only", queryOnly)
// name = strings.Replace(name, queryOnly, "", -1)
// fmt.Println("this is the new name", name)
// use go routines and channels to get back values
cont1 := make(chan []string)
cont2 := make(chan bool)
single1 := make(chan []string)
multiple1 := make(chan []string)
go matchContinuous(name, cont1, cont2)
go matchSingle(name, single1)
go matchMultiple(name, multiple1)
arrCont := <-cont1
errMsg := <-cont2
arrSingle := <-single1
arrMult := <-multiple1
fmt.Println("this is arrCont", arrCont)
fmt.Println("there is an error", errMsg)
fmt.Println("this is arrSingle", arrSingle)
fmt.Println("this is arr multiple", arrMult)
if len(arrCont) == len(arrMult) && len(arrCont) > 0 {
episodeNumbers = arrCont
arrType = "continuous"
} else if len(arrMult) > len(arrCont) && len(arrMult) > len(arrSingle) {
episodeNumbers = arrMult
arrType = "multiple"
} else {
episodeNumbers = arrSingle
arrType = "single"
}
episodeNumbersInt := convertAndSort(episodeNumbers)
return episodeNumbersInt, errMsg, arrType
}
func convertAndSort(nums []string) []int {
var tempNumbers []int
for _, x := range nums {
y, err := strconv.Atoi(x)
if err != nil {
panic(err)
}
tempNumbers = append(tempNumbers, y)
}
sort.Ints(tempNumbers)
return tempNumbers
}
func matchContinuous(name string, cont1 chan []string, cont2 chan bool) {
var tempEpisodes []string
var errMsg bool
continuousEpisodes := regexp.MustCompile(" [0-9]+-[0-9]+")
c := continuousEpisodes.FindAllStringSubmatch(name, -1)
if len(c) > 0 {
tempString := strings.Split(strings.TrimSpace(c[0][0]), "-")
tempEpisodes = append(tempEpisodes, tempString[0])
tempEpisodes = append(tempEpisodes, tempString[1])
x, err := strconv.Atoi(tempString[0])
if err != nil {
panic(err)
}
y, err := strconv.Atoi(tempString[1])
if err != nil {
panic(err)
}
if y < x || y == x {
errMsg = true
}
}
cont1 <- tempEpisodes
cont2 <- errMsg
}
func matchSingle(name string, single1 chan []string) {
var tempEpisodes []string
singleEpisode := regexp.MustCompile(" [0-9]+")
s := singleEpisode.FindAllStringSubmatch(name, -1)
if len(s) > 0 {
tempEpisodes = append(tempEpisodes, strings.TrimSpace(s[0][0]))
}
single1 <- tempEpisodes
}
func matchMultiple(name string, multiple1 chan []string) {
var tempEpisodes []string
// process these values next, send back as an array of strings for episode numbers in getEpisode method
tempName := strings.Replace(name, matchQuery(name), "", -1)
multipleEpisodes := regexp.MustCompile(" ?[0-9]+,?")
m := multipleEpisodes.FindAllStringSubmatch(tempName, -1)
if len(m) > 0 {
for _, s := range m {
dupeExist := false
tempVal := strings.TrimSpace(strings.Replace(s[0], ",", "", -1))
if len(tempEpisodes) > 0 {
for _, u := range tempEpisodes {
if tempVal == u {
dupeExist = true
}
}
}
if dupeExist == false {
tempEpisodes = append(tempEpisodes, tempVal)
}
}
}
multiple1 <- tempEpisodes
}
func matchQuery(name string) string {
// have to be able to find something before a space, number, then comma
// ex: space9,
var onlyQuery string
var newName string
var cutPoint int
fmt.Println("this is name", name)
fmt.Println("this is match query running")
// handle the case where it's a space then a bracket or parentheses
for i := 0; i < len(name); i++ {
if i+1 < len(name) {
if name[i] == 32 && 48 <= name[i+1] && name[i+1] <= 57 {
// it won't check i+1 because it doesn't exist in the case that there is no number
fmt.Println("this is inside the if statement")
cutPoint = i
break
}
} else {
fmt.Println("this is the else statement")
}
}
fmt.Println("this is the cutpoint value", cutPoint)
if cutPoint == 0 {
newName = name
} else {
newName = name[:cutPoint]
}
fmt.Println("this is newName", newName)
if len(newName) < 1 {
newName = strings.TrimSpace(name)
}
onlyQuery = newName
return onlyQuery
}
func getJSON(url string, x *xdcc) {
rs, err := http.Get(url)
// Process response
if err != nil {
panic(err) // More idiomatic way would be to print the error and die unless it's a serious error
}
defer rs.Body.Close()
bodyBytes, err := ioutil.ReadAll(rs.Body)
if err != nil {
panic(err)
}
err = json.Unmarshal(bodyBytes, &x)
// b, err := json.MarshalIndent(x, "", " ")
// if err != nil {
// fmt.Println("error:", err)
// }
// // fmt.Println(string(b))
}
// func groupDuplicates(c []tempSuggested) []tempSuggested {
// for i := 0; i < len(c); i++ {
// for j := i + 1; j < len(c); j++ {
// if c[i].Suggestion == c[j].Suggestion {
// // remove duplicate
// c[i].SuggestionContent = append(c[i].SuggestionContent, c[j].SuggestionContent...)
// c = append(c[:j], c[j+1:]...)
// j--
// }
// }
// }
// return c
// }
// func expCheck(name string, episode int, quality string) string {
// queryString := ""
// if episode == -1 {
// buildRegex := "(?s)" + "\\" + "[" + "(.*)" + "\\" + "]" + " (.*) - " + "(.*)" + " " + "\\" + "[(" + quality + ")" + "\\" + "]"
// // re := regexp.MustCompile(`(?s)\[(.*)\] (.*) - (.*) \[(.*)\]`)
// re := regexp.MustCompile(buildRegex)
// m := re.FindAllStringSubmatch(name, -1)
// // also do a regex to find if the numbers 480, 720, or 1080 are in the string, or all
// // there are some edge cases in which they aren't in brackets
// // [DameDesuYo] Blend S - 04 (1920x1080 10bit AAC) [7CA7EB0F].mkv
// if len(m) > 0 {
// // fmt.Printf("Capture key: %s", m[0][0])
// // fmt.Println("")
// queryString = m[0][0]
// }
// } else {
// t := strconv.Itoa(episode)
// if episode < 10 {
// t = "0" + t
// }
// buildRegex := "(?s)" + "\\" + "[" + "(.*)" + "\\" + "]" + " (.*) - " + "(" + t + ")" + " " + "\\" + "[(" + quality + ")" + "\\" + "]"
// re := regexp.MustCompile(buildRegex)
// m := re.FindAllStringSubmatch(name, -1)
// if len(m) > 0 {
// // fmt.Printf("Capture key: %s", m[0][0])
// // fmt.Println("")
// queryString = m[0][0]
// }
// }
// return queryString
// }