Skip to content

Commit

Permalink
feat: video sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
sosolyht committed Apr 28, 2023
1 parent 7f441e1 commit d410065
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 19 deletions.
82 changes: 81 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/sosolyht/go-sitemap/sitemap"
import (
"github.com/sosolyht/go-sitemap/sitemap"
)

func main() {
s := sitemap.NewSitemap()
Expand All @@ -12,4 +14,82 @@ func main() {
for i := range links {
s.AddURL(links[i])
}

vs := sitemap.NewVideoSitemap()

videoURLs := []sitemap.VideoURL{
{
Loc: "https://www.example.com/videos/video1.html",
Videos: []sitemap.Video{
{
ThumbnailLoc: "https://www.example.com/thumbnail/thumbnail1.png",
Title: "example1",
Description: "example1 desc",
ContentLoc: "https://www.example.com",
PlayerLoc: "https://www.example.com",
Duration: nil,
Rating: nil,
ViewCount: nil,
PublicationDate: nil,
ExpirationDate: nil,
FamilyFriendly: nil,
Restriction: nil,
Price: nil,
RequiresSubscription: nil,
Uploader: nil,
Live: nil,
},
},
},
{
Loc: "https://www.example.com/videos/video2.html",
Videos: []sitemap.Video{
{
ThumbnailLoc: "https://www.example.com/thumbnail/thumbnail2.png",
Title: "example2",
Description: "example2 desc",
ContentLoc: "https://www.example.com",
PlayerLoc: "https://www.example.com",
Duration: nil,
Rating: nil,
ViewCount: nil,
PublicationDate: nil,
ExpirationDate: nil,
FamilyFriendly: nil,
Restriction: nil,
Price: nil,
RequiresSubscription: nil,
Uploader: nil,
Live: nil,
},
},
},
{
Loc: "https://www.example.com/videos/video3.html",
Videos: []sitemap.Video{
{
ThumbnailLoc: "https://www.example.com/thumbnail/thumbnail3.png",
Title: "example3",
Description: "example3 desc",
ContentLoc: "https://www.example.com",
PlayerLoc: "https://www.example.com",
Duration: nil,
Rating: nil,
ViewCount: nil,
PublicationDate: nil,
ExpirationDate: nil,
FamilyFriendly: nil,
Restriction: nil,
Price: nil,
RequiresSubscription: nil,
Uploader: nil,
Live: nil,
},
},
},
}

for i := range videoURLs {
vs.AddVideoURL(videoURLs[i])
}
}
20 changes: 2 additions & 18 deletions sitemap/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,9 @@ import (
"time"
)

const (
xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
)

type ChangeFrequency string

const (
ALWAYS ChangeFrequency = "always"
HOURLY ChangeFrequency = "hourly"
DAILY ChangeFrequency = "daily"
WEEKLY ChangeFrequency = "weekly"
MONTHLY ChangeFrequency = "monthly"
YEARLY ChangeFrequency = "yearly"
NEVER ChangeFrequency = "never"
)

type sitemap struct {
XMLName xml.Name `xml:"urlset"`
XMLNS string `xml:"xmlns,attr"`
Xmlns string `xml:"xmlns,attr"`
URL []URLs `xml:"url,omitempty"`
}

Expand All @@ -40,7 +24,7 @@ type URLs struct {

func NewSitemap() *sitemap {
return &sitemap{
XMLNS: xmlns,
Xmlns: xmlns,
}
}

Expand Down
18 changes: 18 additions & 0 deletions sitemap/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sitemap

const (
xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
xmlnsVideo = "http://www.google.com/schemas/sitemap-video/1.1"
)

type ChangeFrequency string

const (
ALWAYS ChangeFrequency = "always"
HOURLY ChangeFrequency = "hourly"
DAILY ChangeFrequency = "daily"
WEEKLY ChangeFrequency = "weekly"
MONTHLY ChangeFrequency = "monthly"
YEARLY ChangeFrequency = "yearly"
NEVER ChangeFrequency = "never"
)
86 changes: 86 additions & 0 deletions sitemap/video_sitemap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package sitemap

import (
"encoding/xml"
"os"
"time"
)

type videoSitemap struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
XmlnsVideo string `xml:"xmlns:video,attr"`
URL []VideoURL
}

type VideoURL struct {
XMLName xml.Name `xml:"url"`
Loc string `xml:"loc"`
Videos []Video `xml:"video"`
}

type Video struct {
ThumbnailLoc string `xml:"video:thumbnail_loc"`
Title string `xml:"video:title"`
Description string `xml:"video:description"`
ContentLoc string `xml:"video:content_loc"`
PlayerLoc string `xml:"video:player_loc"`
Duration *int `xml:"video:duration,omitempty"` // Optional
Rating *float64 `xml:"video:rating,omitempty"` // Optional
ViewCount *int `xml:"video:view_count,omitempty"` // Optional
PublicationDate *time.Time `xml:"video:publication_date,omitempty"` // Optional
ExpirationDate *time.Time `xml:"video:expiration_date,omitempty"` // Optional
FamilyFriendly *string `xml:"video:family_friendly,omitempty"` // Optional
Restriction *Restriction `xml:"video:restriction,omitempty"` // Optional
Price *Price `xml:"video:price,omitempty"` // Optional
RequiresSubscription *string `xml:"video:requires_subscription,omitempty"` // Optional
Uploader *Uploader `xml:"video:uploader,omitempty"` // Optional
Live *string `xml:"video:live,omitempty"` // Optional
}

type Restriction struct {
Relationship string `xml:"relationship,attr"`
Country string `xml:",chardata"`
}

type Price struct {
Currency string `xml:"currency,attr"`
Amount float64 `xml:",chardata"`
}

type Uploader struct {
Info string `xml:"info,attr"`
Name string `xml:",chardata"`
}

func NewVideoSitemap() *videoSitemap {
return &videoSitemap{
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
XmlnsVideo: "http://www.google.com/schemas/sitemap-video/1.1",
}
}

func (v *videoSitemap) AddVideoURL(url VideoURL) (err error) {
v.URL = append(v.URL, url)
xmlBytes, err := xml.MarshalIndent(v, "", " ")
if err != nil {
return err
}

sitemapFile, err := os.Create("sitemaps/sitemap_video.xml")
if err != nil {
return err
}

defer sitemapFile.Close()

if _, err = sitemapFile.Write([]byte(xml.Header)); err != nil {
return err
}

if _, err = sitemapFile.Write(xmlBytes); err != nil {
return err
}

return
}
33 changes: 33 additions & 0 deletions sitemaps/sitemap_video.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>https://www.example.com/videos/video1.html</loc>
<video>
<video:thumbnail_loc>https://www.example.com/thumbnail/thumbnail1.png</video:thumbnail_loc>
<video:title>example1</video:title>
<video:description>example1 desc</video:description>
<video:content_loc>https://www.example.com</video:content_loc>
<video:player_loc>https://www.example.com</video:player_loc>
</video>
</url>
<url>
<loc>https://www.example.com/videos/video2.html</loc>
<video>
<video:thumbnail_loc>https://www.example.com/thumbnail/thumbnail2.png</video:thumbnail_loc>
<video:title>example2</video:title>
<video:description>example2 desc</video:description>
<video:content_loc>https://www.example.com</video:content_loc>
<video:player_loc>https://www.example.com</video:player_loc>
</video>
</url>
<url>
<loc>https://www.example.com/videos/video3.html</loc>
<video>
<video:thumbnail_loc>https://www.example.com/thumbnail/thumbnail3.png</video:thumbnail_loc>
<video:title>example3</video:title>
<video:description>example3 desc</video:description>
<video:content_loc>https://www.example.com</video:content_loc>
<video:player_loc>https://www.example.com</video:player_loc>
</video>
</url>
</urlset>

0 comments on commit d410065

Please sign in to comment.