-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotSearch.go
188 lines (159 loc) · 4.92 KB
/
botSearch.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
package main
import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
)
type bots struct {
BotOverall []groupBots `json: "content"`
}
type groupBots struct {
FileName string `json: "fileName"`
FileQuality string `json: "fileQuality"`
BotSpecies []botContent `json: "botSpecies"`
}
type botContent struct {
BotName string `json: "botName"`
PackNumber string `json: "packNumber"`
FileSize string `json: "fileSize"`
FileName string `json: "fileName"`
MessageCall string `json: "messageCall"`
}
func botSearchMain(stack []string) []bots {
// testlink
// https://nibl.co.uk/bots.php?search=[HorribleSubs] Gamers! - 04 [720p]
fmt.Println("this is the stack in botsearchmain", stack)
tempBotLinks := createBotLinks(stack)
fmt.Println("this is tempBotLinks", tempBotLinks)
// format the combinedBotQuery
// replace spaces with %20
// newCombinedQuery := "https://nibl.co.uk/bots.php?search=gamers"
temp := startBotSearch(tempBotLinks)
slcT, _ := json.MarshalIndent(temp, "", " ")
fmt.Println(string(slcT))
return temp
}
func startBotSearch(tempBotLinks []string) []bots {
var wg sync.WaitGroup
var allBots []bots
for _, singleQuery := range tempBotLinks {
wg.Add(1)
var botCollection []groupBots
go func(singleQuery string) {
tempBotCollection := accessBotPage(singleQuery, botCollection)
tempBot := bots{
BotOverall: tempBotCollection,
}
allBots = append(allBots, tempBot)
wg.Done()
}(singleQuery)
}
wg.Wait()
return allBots
}
func createBotLinks(stack []string) []string {
var botLinks []string
for _, j := range stack {
baseLink := "https://nibl.co.uk/bots.php?search="
// botQuery := "[HorribleSubs] Gamers! - 04 [720p]"
// botQuery := "gamers 04"
combinedBotQuery := baseLink + j
newCombinedQuery := strings.Replace(combinedBotQuery, " ", "%20", -1)
// testCombine := "https://nibl.co.uk/bots.php?search=gamers"
fmt.Println("this is replaced strings", newCombinedQuery)
botLinks = append(botLinks, newCombinedQuery)
}
return botLinks
}
func accessBotPage(combinedQuery string, collection []groupBots) []groupBots {
// var wg sync.WaitGroup
waitBot := make(chan []groupBots)
// wg.Add(1)
go scrapeBotPage(combinedQuery, collection, waitBot)
// wg.Wait()
result := <-waitBot
return result
}
func scrapeBotPage(combinedQuery string, collection []groupBots, waitBot chan []groupBots) {
fmt.Println("scrapeBotPage is running")
doc, err := goquery.NewDocument(combinedQuery)
if err != nil {
log.Fatal(err)
}
doc.Find(".botlistitem").Each(func(index int, item *goquery.Selection) {
// use index to determine if jp event only/event character only
// fmt.Println("botname is", item.Text())
botName := item.Find(".name").Text()
packNumber := item.Find(".packnumber").Text()
fileSize := item.Find(".filesize").Text()
fileName := item.Find(".filename").Text()
tempBot := botContent{
BotName: botName,
PackNumber: packNumber,
FileSize: fileSize,
FileName: formatFileName(fileName),
MessageCall: createMessage(botName, packNumber),
}
// fmt.Println("this is the filename", fileName)
// fmt.Println("this is the botName", botName)
// add quality to tempGroupBot
// fmt.Println("this is the tempBot before going", tempBot)
// fmt.Println("this is the collection before going", collection)
if index == 0 {
var tempCollection []botContent
tempGroupBot := groupBots{
FileName: tempBot.FileName,
FileQuality: extractQuality(tempBot.FileName),
BotSpecies: append(tempCollection, tempBot),
}
collection = append(collection, tempGroupBot)
} else {
inCollection := false
for i := range collection {
// if same file name, FINE
// kept appending on new last one because it's still going over infinite collection
if collection[i].FileName == tempBot.FileName {
collection[i].BotSpecies = append(collection[i].BotSpecies, tempBot)
inCollection = true
}
}
if inCollection == false {
var tempCollection []botContent
tempGroupBot := groupBots{
FileName: tempBot.FileName,
FileQuality: extractQuality(tempBot.FileName),
BotSpecies: append(tempCollection, tempBot),
}
collection = append(collection, tempGroupBot)
}
}
})
waitBot <- collection
}
func formatFileName(name string) string {
tempString := strings.Replace(name, "\n", "", -1)
tempString = strings.Replace(tempString, "[s]", "", -1)
tempString = strings.TrimSpace(tempString)
return tempString
}
func createMessage(bot, pack string) string {
// /msg KareRaisu xdcc send #9924
return fmt.Sprintf("/msg %s xdcc send #%s", bot, pack)
}
func extractQuality(name string) string {
var quality string
re := regexp.MustCompile(`\[(.*?)\]`)
m := re.FindAllStringSubmatch(name, -1)
// fmt.Println("this is m", m)
if len(m) > 1 {
fmt.Println("this is extract quality", m)
quality = m[1][1]
} else {
quality = "Not a media file"
}
return quality
}