-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.go
62 lines (56 loc) · 1.57 KB
/
crawler.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
package main
import (
"strings"
"github.com/haukened/mirrorselect/internal/llog"
"github.com/biter777/countries"
"github.com/gocolly/colly"
)
var currentCC string
func crawlLaunchpad(desiredCC string) (mirrors []Mirror, err error) {
c := colly.NewCollector(
colly.AllowedDomains("launchpad.net"), // only visit launchpad.net
colly.MaxDepth(1), // only scrape the first page, dont recurse
)
c.OnError(func(r *colly.Response, e error) {
err = e
})
c.OnHTML("table#mirrors_list > tbody", func(h *colly.HTMLElement) {
h.ForEach("tr", func(_ int, row *colly.HTMLElement) {
row.ForEach("*", func(_ int, cell *colly.HTMLElement) {
if cell.Attr("colspan") == "2" {
cName := strings.TrimSpace(cell.Text)
switch cName {
case "":
return
case "Total":
return
default:
country := parseCountry(cName)
currentCC = country.Alpha2()
llog.Debugf("Updated country to %s (%s)", country.Info().Name, country.Alpha2())
}
} else if cell.Attr("href") != "" {
link := cell.Attr("href")
if strings.HasPrefix(link, "http") && currentCC == desiredCC {
mirror, ok := NewMirror(link)
if ok {
mirrors = append(mirrors, mirror)
}
}
}
})
})
})
c.OnScraped(func(r *colly.Response) {
llog.Debug("Finished scraping launchpad.net")
})
err = c.Visit("https://launchpad.net/ubuntu/+archivemirrors")
return
}
func parseCountry(c string) countries.CountryCode {
cS := strings.Split(c, ",")
if len(cS) == 0 {
return countries.Unknown
}
return countries.ByName(cS[0])
}