-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathurl.go
98 lines (82 loc) · 2.03 KB
/
url.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
98
package gositemap
import (
"encoding/xml"
"fmt"
"time"
)
type ChangeFreq string
type InvalidPriorityError struct {
msg string
}
func (e *InvalidPriorityError) Error() string {
return e.msg
}
const (
Always ChangeFreq = "always"
Hourly ChangeFreq = "hourly"
Daily ChangeFreq = "daily"
Weekly ChangeFreq = "weekly"
Monthly ChangeFreq = "monthly"
Yearly ChangeFreq = "yearly"
Never ChangeFreq = "never"
)
type po float64
func (f po) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
s := fmt.Sprintf("%.6f", f)
return xml.Attr{Name: name, Value: s}, nil
}
type url struct {
*base
XMLName xml.Name `xml:"url"`
Loc string `xml:"loc"`
LastMod string `xml:"lastmod,omitempty"`
ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
Priority po `xml:"priority,omitempty"`
Token []xml.Token
}
func NewUrl() *url {
return &url{
base: &base{},
Loc: "",
LastMod: "",
ChangeFreq: "",
Priority: 0,
}
}
// 网址
func (u *url) SetLoc(loc string) *url {
u.Loc = loc
return u
}
// 最后一次修改时间
func (u *url) SetLastmod(lastMod time.Time) *url {
u.LastMod = lastMod.Format(time.RFC3339)
return u
}
// 更新频率
func (u *url) SetChangefreq(freq ChangeFreq) *url {
u.ChangeFreq = freq
return u
}
// 网页优先级
func (u *url) SetPriority(priority float64) *url {
if priority < 0 || priority > 1 {
panic(InvalidPriorityError{"Valid values range from 0.0 to 1.0"})
}
u.Priority = po(priority)
return u
}
// 对于单个网页上的多个视频,为该网页创建一个 <loc> 标记,并为该网页上的每个视频创建一个子级 <video> 元素。
func (u *url) AppendVideo(video *video) {
u.setNs(VideoXmlNS)
u.Token = append(u.Token, video)
}
// 对于单个网页上的多个图片,每个 <url> 标记最多可包含 1000 个 <image:image> 标记。
func (u *url) AppendImage(image *image) {
u.setNs(ImageXmlNS)
u.Token = append(u.Token, image)
}
func (u *url) AppendNews(news *news) {
u.setNs(NewsXmlNS)
u.Token = append(u.Token, news)
}