-
Notifications
You must be signed in to change notification settings - Fork 3
/
xmlsitemap.go
51 lines (39 loc) · 933 Bytes
/
xmlsitemap.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
package main
import (
"encoding/xml"
"net/url"
"strings"
)
func getXMLSitemap(xmlSitemapURL url.URL, userAgent string) (XMLSitemap, error) {
response, readErr := readURL(xmlSitemapURL, userAgent)
if readErr != nil {
return XMLSitemap{}, readErr
}
if !strings.Contains(string(response.Body()), "</urlset>") {
return XMLSitemap{}, XmlSitemapError{"Invalid content"}
}
var urlSet XMLSitemap
unmarshalError := xml.Unmarshal(response.Body(), &urlSet)
if unmarshalError != nil {
return XMLSitemap{}, unmarshalError
}
return urlSet, nil
}
type XMLSitemap struct {
URLs []URL `xml:"url"`
}
type URL struct {
Location string `xml:"loc"`
}
type XmlSitemapError struct {
message string
}
func (sitemapIndexError XmlSitemapError) Error() string {
return sitemapIndexError.message
}
func isInvalidXMLSitemapContent(err error) bool {
if err == nil {
return false
}
return err.Error() == "Invalid content"
}