-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
sitemap_fetcher.go
50 lines (37 loc) · 895 Bytes
/
sitemap_fetcher.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
package main
import (
"bytes"
"fmt"
"net/url"
sitemap "github.com/oxffaa/gopher-parse-sitemap"
)
type sitemapFetcher struct {
client httpClient
}
func newSitemapFetcher(c httpClient) *sitemapFetcher {
return &sitemapFetcher{c}
}
func (f *sitemapFetcher) Fetch(uu *url.URL) (map[string]struct{}, error) {
u := *uu
u.Path = "sitemap.xml"
r, err := f.client.Get(&u, nil)
if err != nil {
return nil, f.formatGetError(err)
}
us := map[string]struct{}{}
bs, err := r.Body()
if err != nil {
return nil, f.formatGetError(err)
}
err = sitemap.Parse(bytes.NewReader(bs), func(e sitemap.Entry) error {
us[e.GetLocation()] = struct{}{}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to parse sitemap.xml: %v", err)
}
return us, nil
}
func (*sitemapFetcher) formatGetError(err error) error {
return fmt.Errorf("failed to GET sitemap.xml: %v", err)
}