-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
sitemap_page_parser.go
53 lines (40 loc) · 1.03 KB
/
sitemap_page_parser.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
package main
import (
"bytes"
"net/url"
sitemap "github.com/oxffaa/gopher-parse-sitemap"
)
type sitemapPageParser struct {
linkFilterer linkFilterer
}
func newSitemapPageParser(f linkFilterer) *sitemapPageParser {
return &sitemapPageParser{f}
}
func (p *sitemapPageParser) Parse(u *url.URL, typ string, bs []byte) (page, error) {
if typ != "application/xml" && typ != "text/xml" {
return nil, nil
}
ls := map[string]error{}
c := func(e interface{ GetLocation() string }) error {
u, err := url.Parse(e.GetLocation())
if p.linkFilterer.IsValid(u) {
ls[u.String()] = err
}
return nil
}
err := sitemap.Parse(bytes.NewReader(bs), func(e sitemap.Entry) error {
return c(e)
})
// TODO Detect XML files as sitemaps.
if err == nil && len(ls) != 0 {
return newSitemapPage(u, ls), nil
}
err = sitemap.ParseIndex(bytes.NewReader(bs), func(e sitemap.IndexEntry) error {
return c(e)
})
// TODO Detect XML files as sitemap indices.
if err == nil && len(ls) != 0 {
return newSitemapPage(u, ls), nil
}
return nil, nil
}