Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Google News namespace #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Entry interface {
GetLastModified() *time.Time
GetChangeFrequency() Frequency
GetPriority() float32
GetNews() *News
}

// IndexEntry is an interface describes an element \ an URL in a sitemap index file.
Expand Down
48 changes: 48 additions & 0 deletions sitemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,54 @@ func TestParseSitemapIndex(t *testing.T) {
}
}

func TestParseSitemapNews(t *testing.T) {
var (
counter int
sb strings.Builder
)
err := ParseFromFile("./testdata/sitemap-news.xml", func(e Entry) error {
counter++

fmt.Fprintln(&sb, e.GetLocation())
lastmod := e.GetLastModified()
if lastmod != nil {
fmt.Fprintln(&sb, lastmod.Format(time.RFC3339))
}
fmt.Fprintln(&sb, e.GetChangeFrequency())
fmt.Fprintln(&sb, e.GetPriority())
news := e.GetNews()
if news == nil {
return nil
}
fmt.Fprintln(&sb, news.Publication.Name)
fmt.Fprintln(&sb, news.Publication.Language)
if news.GetPublicationDate() != nil {
fmt.Fprintln(&sb, news.GetPublicationDate().Format(time.RFC3339))
}
fmt.Fprintln(&sb, news.Title)
return nil
})

if err != nil {
t.Errorf("Parsing failed with error %s", err)
}

if counter != 4 {
t.Errorf("Expected 4 elements, but given only %d", counter)
}

expected, err := ioutil.ReadFile("./testdata/sitemap-news.golden")
if err != nil {
t.Errorf("Can't read golden file due to %s", err)
}

if sb.String() != string(expected) {
t.Logf("%s", sb.String())
t.Logf("%s", expected)
t.Error("Unxepected result")
}
}

/*
* Private API tests
*/
Expand Down
33 changes: 29 additions & 4 deletions sitemap_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ type sitemapEntry struct {
ParsedLastModified *time.Time
ChangeFrequency Frequency `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
News *News `xml:"news,omitempty"`
}

type News struct {
Publication struct {
Name string `xml:"name,omitempy"`
Language string `xml:"language,omitempy"`
} `xml:"publication,omitempy"`
PublicationDate string `xml:"publication_date,omitempy"`
ParsedPublicationDate *time.Time
Title string `xml:"title,omitempy"`
}

func newSitemapEntry() *sitemapEntry {
Expand All @@ -33,6 +44,17 @@ func (e *sitemapEntry) GetPriority() float32 {
return e.Priority
}

func (e *sitemapEntry) GetNews() *News {
return e.News
}

func (n *News) GetPublicationDate() *time.Time {
if n.ParsedPublicationDate == nil && n.PublicationDate != "" {
n.ParsedPublicationDate = parseDateTime(n.PublicationDate)
}
return n.ParsedPublicationDate
}

type sitemapIndexEntry struct {
Location string `xml:"loc"`
LastModified string `xml:"lastmod,omitempty"`
Expand Down Expand Up @@ -61,11 +83,14 @@ func parseDateTime(value string) *time.Time {

t, err := time.Parse(time.RFC3339, value)
if err != nil {
// second chance
// try parse as short format
t, err = time.Parse("2006-01-02", value)
t, err = time.Parse("2006-01-02T15:04-07:00", value)
if err != nil {
return nil
// second chance
// try parse as short format
t, err = time.Parse("2006-01-02", value)
if err != nil {
return nil
}
}
}

Expand Down
27 changes: 27 additions & 0 deletions testdata/sitemap-news.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
http://HOST/
always
0.5
http://HOST/tools/
2015-05-07T19:13:09+09:00
always
0.5
HOST news
en
2015-05-07T00:00:00Z
tools
http://HOST/contribution-to-oss/
2015-05-07T00:00:00Z
monthly
0.5
HOST news
en
2015-05-07T19:20:00+01:00
contribution-to-oss
http://HOST/page-1/
2015-05-07T19:13:09+09:00
monthly
0.9
HOST news
en
2015-05-07T19:20:30+01:00
page-1
45 changes: 45 additions & 0 deletions testdata/sitemap-news.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>http://HOST/</loc>
</url>
<url>
<loc>http://HOST/tools/</loc>
<lastmod>2015-05-07T19:13:09+09:00</lastmod>
<news:news>
<news:publication>
<news:name>HOST news</news:name>
<news:language>en</news:language>
</news:publication>
<news:publication_date>2015-05-07</news:publication_date>
<news:title>tools</news:title>
</news:news>
</url>
<url>
<loc>http://HOST/contribution-to-oss/</loc>
<lastmod>2015-05-07</lastmod>
<changefreq>monthly</changefreq>
<news:news>
<news:publication>
<news:name>HOST news</news:name>
<news:language>en</news:language>
</news:publication>
<news:publication_date>2015-05-07T19:20+01:00</news:publication_date>
<news:title>contribution-to-oss</news:title>
</news:news>
</url>
<url>
<loc>http://HOST/page-1/</loc>
<lastmod>2015-05-07T19:13:09+09:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
<news:news>
<news:publication>
<news:name>HOST news</news:name>
<news:language>en</news:language>
</news:publication>
<news:publication_date>2015-05-07T19:20:30.45+01:00</news:publication_date>
<news:title>page-1</news:title>
</news:news>
</url>
</urlset>