3 Commits

View File

@@ -4,6 +4,7 @@ import (
"encoding/xml"
"fmt"
"math"
"path"
)
type ChangeFrequency string
@@ -45,13 +46,14 @@ type sitemapXML struct {
}
type SiteMap struct {
urls []*urlXML
BaseURL string
urls []*urlXML
BaseURL string
MaxURLPerSiteMap int
}
func (m *SiteMap) AddURL(url string, lastMod *string, freq *ChangeFrequency, prio *float64) {
m.urls = append(m.urls, &urlXML{
Loc: fmt.Sprintf("https://%s/%s", m.BaseURL, url),
Loc: fmt.Sprintf("https://%s", path.Join(m.BaseURL, url)),
LastMod: lastMod,
ChangeFreq: freq,
Priority: prio,
@@ -63,13 +65,12 @@ func prefixXML(content []byte) []byte {
}
func (m *SiteMap) SiteMapIndex() ([]byte, error) {
nSiteMaps := int(math.Floor(float64(len(m.urls))/50000 + 1))
nSiteMaps := int(math.Floor(float64(len(m.urls))/float64(m.MaxURLPerSiteMap) + 1))
index := &siteMapIndexXML{}
for i := 0; i < nSiteMaps; i++ {
index.SiteMaps = append(index.SiteMaps, &sitemapXML{
Loc: fmt.Sprintf("https://%s/sitemap%d.xml", m.BaseURL, i),
Loc: fmt.Sprintf("https://%s/sitemap/%d", m.BaseURL, i),
})
}
@@ -82,13 +83,17 @@ func (m *SiteMap) SiteMapIndex() ([]byte, error) {
}
func (m *SiteMap) SiteMap(id int) ([]byte, error) {
if m.MaxURLPerSiteMap*(id) >= len(m.urls) {
return nil, nil
}
siteMap := &urlSetXML{}
var urls []*urlXML
if 50000*(id+1) >= len(m.urls) {
urls = m.urls[50000*id:]
if m.MaxURLPerSiteMap*(id+1) >= len(m.urls) {
urls = m.urls[m.MaxURLPerSiteMap*id:]
} else {
urls = m.urls[50000*id : 50000*(id+1)]
urls = m.urls[m.MaxURLPerSiteMap*id : m.MaxURLPerSiteMap*(id+1)]
}
siteMap.URLs = append(siteMap.URLs, urls...)