├── .gitignore ├── go.mod ├── assert └── douyacun_qrcode.jpg ├── base.go ├── go.sum ├── LICENSE ├── image.go ├── options.go ├── url.go ├── news.go ├── sitemap.go ├── sitemap_test.go ├── README.md └── video.go /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/douyacun/gositemap 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /assert/douyacun_qrcode.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/douyacun/gositemap/HEAD/assert/douyacun_qrcode.jpg -------------------------------------------------------------------------------- /base.go: -------------------------------------------------------------------------------- 1 | package gositemap 2 | 3 | type xmlns int8 4 | 5 | const ( 6 | ImageXmlNS xmlns = 1 7 | VideoXmlNS xmlns = 2 8 | NewsXmlNS xmlns = 4 9 | ) 10 | 11 | type base struct { 12 | xmlns xmlns 13 | } 14 | 15 | // image: 0001 16 | // video: 0010 17 | // news: 0100 18 | func (b *base) setNs(xmlns xmlns) { 19 | b.xmlns = b.xmlns | xmlns 20 | } 21 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= 2 | github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= 3 | github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= 4 | github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= 5 | github.com/ikeikeikeike/go-sitemap-generator v2.0.1+incompatible h1:Ry3DcY2hILVzZiaE3IOYyU5/oSTfgsFmOHuPg+WfYX4= 6 | github.com/ikeikeikeike/go-sitemap-generator v2.0.1+incompatible/go.mod h1:QI+zWsz6yQyxkG9LWNcnu0f7aiAE5tPdsZOsICgmd1c= 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 douyacun 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /image.go: -------------------------------------------------------------------------------- 1 | package gositemap 2 | 3 | import "encoding/xml" 4 | 5 | type image struct { 6 | XMLName xml.Name `xml:"image:image"` 7 | Loc string `xml:"image:loc"` 8 | Caption string `xml:"image:caption,omitempty"` 9 | GeoLocation string `xml:"image:geo_location,omitempty"` 10 | Title string `xml:"image:title,omitempty"` 11 | License string `xml:"image:license,omitempty"` 12 | } 13 | 14 | func NewImage() *image { 15 | return &image{} 16 | } 17 | 18 | // 图片的网址。某些情况下,图片网址可能与您的主网站不在同一个网域中 19 | // loc是网页的地址,这里是图片的访问路径 20 | func (i *image) SetLoc(loc string) *image { 21 | i.Loc = loc 22 | return i 23 | } 24 | 25 | // 图片的说明 26 | func (i *image) SetCaption(caption string) *image { 27 | i.Caption = caption 28 | return i 29 | } 30 | 31 | // 图片的地理位置。例如 Limerick, Ireland。 32 | func (i *image) SetGeoLocation(geoLocation string) *image { 33 | i.GeoLocation = geoLocation 34 | return i 35 | } 36 | 37 | // 图片的标题。 38 | func (i *image) SetTitle(title string) *image { 39 | i.Title = title 40 | return i 41 | } 42 | 43 | // 图片的授权许可所在的网址。 44 | func (i *image) SetLicense(license string) *image { 45 | i.License = license 46 | return i 47 | } 48 | -------------------------------------------------------------------------------- /options.go: -------------------------------------------------------------------------------- 1 | package gositemap 2 | 3 | import ( 4 | "os" 5 | "path" 6 | ) 7 | 8 | const ( 9 | // MaxSitemapLinks defines max links per sitemap 10 | MaxSitemapLinks = 50000 11 | ) 12 | 13 | type options struct { 14 | defaultHost string 15 | publicPath string 16 | filename string 17 | compress bool 18 | pretty bool 19 | maxLinks int 20 | } 21 | 22 | func NewOptions() *options { 23 | pwd, _ := os.Getwd() 24 | return &options{ 25 | defaultHost: "http://www.example.com", 26 | publicPath: pwd, 27 | filename: "sitemap.xml", 28 | compress: false, 29 | pretty: false, 30 | maxLinks: MaxSitemapLinks, 31 | } 32 | } 33 | 34 | func (o *options) SetDefaultHost(host string) { 35 | o.defaultHost = host 36 | } 37 | 38 | func (o *options) SetPublicPath(path string) { 39 | o.publicPath = path 40 | } 41 | 42 | func (o *options) SetFilename(filename string) { 43 | if path.Ext(filename) != ".xml" { 44 | filename = filename + ".xml" 45 | } 46 | o.filename = filename 47 | } 48 | 49 | func (o *options) SetCompress(compress bool) { 50 | o.compress = compress 51 | } 52 | 53 | func (o *options) SetPretty(pretty bool) { 54 | o.pretty = pretty 55 | } 56 | 57 | func (o *options) SetMaxLinks(max int) { 58 | if max < MaxSitemapLinks && max > 0 { 59 | o.maxLinks = max 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /url.go: -------------------------------------------------------------------------------- 1 | package gositemap 2 | 3 | import ( 4 | "encoding/xml" 5 | "fmt" 6 | "time" 7 | ) 8 | 9 | type ChangeFreq string 10 | 11 | type InvalidPriorityError struct { 12 | msg string 13 | } 14 | 15 | func (e *InvalidPriorityError) Error() string { 16 | return e.msg 17 | } 18 | 19 | const ( 20 | Always ChangeFreq = "always" 21 | Hourly ChangeFreq = "hourly" 22 | Daily ChangeFreq = "daily" 23 | Weekly ChangeFreq = "weekly" 24 | Monthly ChangeFreq = "monthly" 25 | Yearly ChangeFreq = "yearly" 26 | Never ChangeFreq = "never" 27 | ) 28 | 29 | type po float64 30 | 31 | func (f po) MarshalXMLAttr(name xml.Name) (xml.Attr, error) { 32 | s := fmt.Sprintf("%.6f", f) 33 | return xml.Attr{Name: name, Value: s}, nil 34 | } 35 | 36 | type url struct { 37 | *base 38 | XMLName xml.Name `xml:"url"` 39 | Loc string `xml:"loc"` 40 | LastMod string `xml:"lastmod,omitempty"` 41 | ChangeFreq ChangeFreq `xml:"changefreq,omitempty"` 42 | Priority po `xml:"priority,omitempty"` 43 | Token []xml.Token 44 | } 45 | 46 | func NewUrl() *url { 47 | return &url{ 48 | base: &base{}, 49 | Loc: "", 50 | LastMod: "", 51 | ChangeFreq: "", 52 | Priority: 0, 53 | } 54 | } 55 | 56 | // 网址 57 | func (u *url) SetLoc(loc string) *url { 58 | u.Loc = loc 59 | return u 60 | } 61 | 62 | // 最后一次修改时间 63 | func (u *url) SetLastmod(lastMod time.Time) *url { 64 | u.LastMod = lastMod.Format(time.RFC3339) 65 | return u 66 | } 67 | 68 | // 更新频率 69 | func (u *url) SetChangefreq(freq ChangeFreq) *url { 70 | u.ChangeFreq = freq 71 | return u 72 | } 73 | 74 | // 网页优先级 75 | func (u *url) SetPriority(priority float64) *url { 76 | if priority < 0 || priority > 1 { 77 | panic(InvalidPriorityError{"Valid values range from 0.0 to 1.0"}) 78 | } 79 | u.Priority = po(priority) 80 | return u 81 | } 82 | 83 | // 对于单个网页上的多个视频,为该网页创建一个 标记,并为该网页上的每个视频创建一个子级