-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsitemap.go
97 lines (88 loc) · 2.1 KB
/
sitemap.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sitemap
import (
"fmt"
"time"
"bytes"
"io/ioutil"
"compress/gzip"
"os"
"strings"
)
const (
header = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`
footer = ` </urlset>`
template = `
<url>
<loc>%s</loc>
<lastmod>%s</lastmod>
<changefreq>%s</changefreq>
<priority>%.1f</priority>
</url> `
indexHeader = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`
indexFooter = `
</sitemapindex>
`
indexTemplate = `
<sitemap>
<loc>%s%s</loc>
<lastmod>%s</lastmod>
</sitemap>
`
)
type Item struct {
Loc string
LastMod time.Time
Changefreq string
Priority float32
}
func (item *Item) String() string {
//2012-08-30T01:23:57+08:00
//Mon Jan 2 15:04:05 -0700 MST 2006
return fmt.Sprintf(template, item.Loc, item.LastMod.Format("2006-01-02T15:04:05+08:00"), item.Changefreq, item.Priority)
}
func SiteMap(f string, items []*Item) error {
var buffer bytes.Buffer
buffer.WriteString(header)
for _, item := range (items) {
_, err := buffer.WriteString(item.String())
if err != nil {
return err
}
}
fo, err := os.Create(f)
if err != nil {
return err
}
defer fo.Close()
buffer.WriteString(footer)
zip := gzip.NewWriter(fo)
defer zip.Close()
_, err = zip.Write(buffer.Bytes())
if err != nil {
return err
}
return err
}
func SiteMapIndex(folder, indexFile, baseurl string) error {
var buffer bytes.Buffer
buffer.WriteString(indexHeader)
fs, err := ioutil.ReadDir(folder)
if err != nil {
return err
}
for _, f := range fs {
if strings.HasSuffix(f.Name(), ".xml.gz") {
fmt.Println(f.Name())
s := fmt.Sprintf(indexTemplate, baseurl, f.Name(), time.Now().Format("2006-01-02T15:04:05+08:00"))
//fmt.Println(s)
buffer.WriteString(s)
}
}
buffer.WriteString(indexFooter)
err = ioutil.WriteFile(indexFile, buffer.Bytes(), 0755)
return err
}