-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsitemapindex.go
50 lines (39 loc) · 990 Bytes
/
sitemapindex.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 (
"encoding/xml"
"net/url"
"strings"
)
func getSitemapIndex(xmlSitemapURL url.URL, userAgent string) (SitemapIndex, error) {
response, readErr := readURL(xmlSitemapURL, userAgent)
if readErr != nil {
return SitemapIndex{}, readErr
}
if !strings.Contains(string(response.Body()), "</sitemapindex>") {
return SitemapIndex{}, SitemapIndexError{"Invalid content"}
}
var sitemapIndex SitemapIndex
unmarshalError := xml.Unmarshal(response.Body(), &sitemapIndex)
if unmarshalError != nil {
return SitemapIndex{}, unmarshalError
}
return sitemapIndex, nil
}
type SitemapIndex struct {
Sitemaps []URL `xml:"sitemap"`
}
type Sitemap struct {
Location string `xml:"loc"`
}
type SitemapIndexError struct {
message string
}
func (sitemapIndexError SitemapIndexError) Error() string {
return sitemapIndexError.message
}
func isInvalidSitemapIndexContent(err error) bool {
if err == nil {
return false
}
return err.Error() == "Invalid content"
}