-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (61 loc) · 1.53 KB
/
main.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
package main
import (
"encoding/json"
"io/ioutil"
"sync"
)
// Main function
func main() {
var wg sync.WaitGroup
// Load every category
categories := LoadCategory()
// Iterate categories
for i, _ := range categories {
wg.Add(1)
go func(i int) {
// Load every page
err := categories[i].GetPage()
if err != nil {
panic(err)
}
// Load every content url
err = categories[i].GetPageContent()
if err != nil {
panic(err)
}
wg.Done()
}(i)
}
wg.Wait()
// Iterate category
for categoryIndex, _ := range categories {
wg.Add(1)
var contentGroup sync.WaitGroup
// Crawl every content crawler from each category parallel
go func(i int) {
// Iterate content url from each category
for contentIndex, _ := range *categories[i].Contents {
// Add wait group for each page
contentGroup.Add(1)
// Spawn routine to get promotions from content url
go (*categories[i].Contents)[contentIndex].GetPromotions(&contentGroup)
// Wait if routine is spawn more than pages to make sure we don't act like DDOS
if contentIndex + 1 % categories[i].Pages == 0 {
contentGroup.Wait()
}
}
// Wait for all left routines
contentGroup.Wait()
// State if category content crawling is finished
wg.Done()
}(categoryIndex)
}
wg.Wait()
// Print it like the format
var content = map[string]interface{}{}
for _, category := range categories {
content[category.Id] = category.Contents
}
file, _ := json.MarshalIndent(content, "", " ")
_ = ioutil.WriteFile("solution.json", file, 0644)
}