-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsearch.go
71 lines (60 loc) · 1.86 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
package main
import (
"fmt"
"hatt/assets"
"hatt/configuration"
"hatt/htmlParsers"
"hatt/specificScrapers"
"hatt/variables"
"reflect"
"strings"
"sync"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
func (a *App) Search(userInput string, websites []string, categories []string) []variables.ItemList {
variables.CURRENT_INPUT = userInput
variables.SELECTED_CATEGORIES = categories
configs := []configuration.Config{}
for _, website := range websites {
var conf configuration.Config = assets.DeserializeWebsiteConf(website + ".json")
configs = append(configs, conf)
}
variables.RESULTS = []variables.ItemList{}
var wg sync.WaitGroup
for _, config := range configs {
var items []variables.Item
var specificFunction reflect.Value
wg.Add(1)
go func(config configuration.Config) {
fmt.Println("starting with : " + config.Name)
if config.SpecificScraper {
t := specificScrapers.T{}
specificFunction = reflect.ValueOf(t).MethodByName(strings.Title(config.Name))
items = specificFunction.Call(nil)[0].Interface().([]variables.Item)
} else {
items = htmlParsers.ScrapePlainHtml(config)
}
result := variables.ItemList{
Website: config.Name,
CompatibleDownloaders: []variables.CompatibleDownloader{},
Items: items,
}
for _, downloader := range config.CompatibleDownloaders {
for _, downloaderInfo := range variables.CompatibleDownloaders {
if downloaderInfo.Name == downloader {
result.CompatibleDownloaders = append(result.CompatibleDownloaders, variables.CompatibleDownloader{
Name: downloader,
Link: downloaderInfo.Link,
})
}
}
}
variables.RESULTS = append(variables.RESULTS, result)
wg.Done()
fmt.Println("done with : " + config.Name)
runtime.EventsEmit(a.ctx, "websiteDone", result)
}(config)
}
wg.Wait()
return variables.RESULTS
}