GoGronkh/gparser/sitemap.go

108 lines
3 KiB
Go

package main
import (
"encoding/xml"
"io/ioutil"
"time"
"git.1750studios.com/gronkhDE/gogronkh/config"
"git.1750studios.com/gronkhDE/gogronkh/database"
)
type urlset struct {
Xmlns string `xml:"xmlns,attr"`
Url []URL `xml:"url"`
}
type URL struct {
Location string `xml:"loc"`
LastMod time.Time `xml:"lastmod,omitempty"`
ChangeFreq string `xml:"changefreq"`
Priority float32 `xml:"priority"`
}
func WriteSitemap(path string, sitemap urlset) (error) {
smap, err := xml.Marshal(sitemap)
if err != nil {
return err
}
out := xml.Header + string(smap)
err = ioutil.WriteFile(path, []byte(out), 0644)
return err
}
func GenerateSitemap() {
var uset urlset
uset.Xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
var temp URL
temp.Location = config.C.SiteUrl + "/"
temp.LastMod = time.Now()
temp.ChangeFreq = "hourly"
temp.Priority = 1.0
uset.Url = append(uset.Url, temp)
temp.Location = config.C.SiteUrl + "/lets-play/"
temp.LastMod = time.Now()
temp.ChangeFreq = "monthly"
temp.Priority = 0.75
uset.Url = append(uset.Url, temp)
temp.Location = config.C.SiteUrl + "/testet/"
temp.LastMod = time.Now()
temp.ChangeFreq = "monthly"
temp.Priority = 0.75
uset.Url = append(uset.Url, temp)
temp.Location = config.C.SiteUrl + "/zeige/"
temp.LastMod = time.Now()
temp.ChangeFreq = "yearly"
temp.Priority = 0.75
uset.Url = append(uset.Url, temp)
genLPs(&uset)
genLTs(&uset)
genEPs(&uset)
WriteSitemap(config.C.ImageDirectory + "sitemap.xml", uset)
}
func genLPs(uset *urlset) {
var LPs []database.LetsPlay
database.Db.Where("aired IS NOT NULL").Find(&LPs)
for _, LP := range LPs {
if LP.Aired.IsZero() == false {
var temp URL
temp.Location = config.C.SiteUrl + "/lets-play/" + LP.Slug.String + "/"
temp.LastMod = LP.Aired
temp.ChangeFreq = "daily"
temp.Priority = 0.5
uset.Url = append(uset.Url, temp)
}
}
}
func genLTs(uset *urlset) {
var LTs []database.LetsTest
database.Db.Find(&LTs)
for _, LT := range LTs {
var temp URL
temp.Location = config.C.SiteUrl + "/testet/" + LT.Slug.String + "/"
temp.LastMod = LT.Aired
temp.ChangeFreq = "never"
temp.Priority = 0.5
uset.Url = append(uset.Url, temp)
}
}
func genEPs(uset *urlset) {
var EPs []database.Episode
database.Db.Find(&EPs)
for _, EP := range EPs {
if EP.Aired.IsZero() == false {
var LP database.LetsPlay
database.Db.Model(&EP).Related(&LP)
var temp URL
temp.Location = config.C.SiteUrl + "/lets-play/" + LP.Slug.String + "/" + EP.Slug.String + "/"
temp.LastMod = EP.Aired
temp.ChangeFreq = "never"
temp.Priority = 0.5
uset.Url = append(uset.Url, temp)
}
}
}