-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (72 loc) · 2.05 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
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
package main
import (
"log"
"mime"
"os"
"strings"
"github.com/gocolly/colly"
)
var invalidFilenames = []string{
"jingle",
}
func main() {
crawl()
}
func crawl() {
_ = os.Mkdir("./download", os.ModePerm)
mainCollector := colly.NewCollector(
colly.AllowedDomains("ageofempires.fandom.com", "www.ageofempires.fandom.com"),
colly.CacheDir("./cache"),
)
soundsCollector := colly.NewCollector(
colly.AllowedDomains("static.wikia.nocookie.net", "www.static.wikia.nocookie.net"),
colly.CacheDir("./cache"),
)
mainCollector.OnRequest(func(r *colly.Request) {
log.Printf("Scraping URL: %s", r.URL.String())
})
mainCollector.OnHTML("h3 > span > a[href]", func(e *colly.HTMLElement) {
if e.Attr("title") == "" {
return
}
link := e.Attr("href")
if !strings.HasPrefix(link, "/wiki/") {
return
}
absoluteLink := e.Request.AbsoluteURL(link)
mainCollector.Visit(absoluteLink)
})
mainCollector.OnHTML("span.audio-button", func(e *colly.HTMLElement) {
source := e.Attr("data-src")
soundsCollector.Visit(source)
})
soundsCollector.OnResponse(func(r *colly.Response) {
log.Printf("Downloading sound: %s", r.Request.URL.String())
contentType := r.Headers.Get("Content-Type")
if contentType != "application/ogg" {
log.Printf("Content-Type not valid: %s", contentType)
return
}
contentDisposition := r.Headers.Get("Content-Disposition")
_, params, err := mime.ParseMediaType(contentDisposition)
if err != nil {
log.Printf("Error parsing Content-Disposition header: %v", err)
return
}
filename := params["filename"]
for _, invalidFilename := range invalidFilenames {
if strings.Contains(filename, invalidFilename) {
log.Printf("Invalid filename, contains: %s", invalidFilename)
return
}
}
path := "./download/" + params["filename"]
err = r.Save(path)
if err != nil {
log.Printf("Error downloading sound: %v", err)
return
}
log.Printf("Downloaded sound %s into %s", filename, path)
})
mainCollector.Visit("https://ageofempires.fandom.com/wiki/Civilizations_(Age_of_Empires_II)")
}