sitemap

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 12 Imported by: 0

README

go-sitemap

Go Report Card Go Reference

Golang module to work with Sitemaps

Get:

go get github.com/g0rbe/go-sitemap@latest

Get the latest tag (if Go module proxy is not updated):

go get "github.com/g0rbe/go-sitemap@$(curl -s 'https://api.github.com/repos/g0rbe/go-sitemap/tags' | jq -r '.[0].name')"

Get the latest commit (if Go module proxy is not updated):

go get "github.com/g0rbe/go-sitemap@$(curl -s 'https://api.github.com/repos/g0rbe/go-sitemap/commits' | jq -r '.[0].sha')"

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrTooManyRequests = errors.New("429 Too Many Requests")
	ErrBadRequest      = errors.New("400 Bad Request")
	ErrNotFound        = errors.New("404 Not Found")
	ErrUnauthorized    = errors.New("401 Unauthorized")
	ErrForbidden       = errors.New("403 Forbidden")
)
View Source
var (
	ErrEmptyLoc = errors.New("empty loc")
)
View Source
var (
	ErrInvalidChangeFrequency = errors.New("invalid changefreq")
)
View Source
var (
	ErrInvalidFormat = errors.New("invalid format")
)
View Source
var (
	ErrNotURLSet = errors.New("not urlset")
)

Functions

This section is empty.

Types

type ChangeFrequency added in v0.2.0

type ChangeFrequency byte

ChangeFrequency type used for the <changefreq> field

var (
	ChangeFreqAlways  ChangeFrequency = 1 // "always"
	ChangeFreqHourly  ChangeFrequency = 2 // "hourly"
	ChangeFreqDaily   ChangeFrequency = 3 // "daily"
	ChangeFreqWeekly  ChangeFrequency = 4 // "weekly"
	ChangeFreqMonthly ChangeFrequency = 5 // "monthly"
	ChangeFreqYearly  ChangeFrequency = 6 // "yearly"
	ChangeFreqNever   ChangeFrequency = 7 // "never"
)

Valid values for ChangeFrequency

func ParseChangeFreq added in v0.2.0

func ParseChangeFreq(v string) ChangeFrequency

ParseChangeFreq parses ChangeFrequency from v.

If v is not a valid ChangeFrequency, returns ChangeFrequency(255)

func (ChangeFrequency) IsEmpty added in v0.2.0

func (f ChangeFrequency) IsEmpty() bool

func (ChangeFrequency) IsSet added in v0.2.0

func (f ChangeFrequency) IsSet() bool

func (ChangeFrequency) MarshalXML added in v0.2.0

func (f ChangeFrequency) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (ChangeFrequency) String added in v0.2.0

func (f ChangeFrequency) String() string

String returns the string value of ChangeFrequency.

If f is not a valid ChangeFrequency, returns the value ChangeFrequency as a string.

func (*ChangeFrequency) UnmarshalXML added in v0.2.0

func (f *ChangeFrequency) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml.Unmarshaler interface

type ContentTypeError added in v0.2.0

type ContentTypeError struct {
	ContentType string
}

func NewContentTypeError added in v0.2.0

func NewContentTypeError(ct string) ContentTypeError

func (ContentTypeError) Error added in v0.2.0

func (cte ContentTypeError) Error() string

type Format added in v0.2.0

type Format byte
const (
	FormatURLSet Format = 1 // "urlset"
	FormatIndex  Format = 2 // "sitemapindex"
)

func FetchFormat

func FetchFormat(url string) (Format, []byte, error)

FetchFormat downloads the XML from the url, unmarshals it and returns the sitemap format and the downloaded data. The format is either FormatURLSet or FormatIndex.

If invalid format found, returns ErrorInvalidFormat

Example
f, _, err := sitemap.FetchFormat("https://gorbe.io/sitemap.xml")
if err != nil {
	// handle error
}

fmt.Printf("%s\n", f) // "urlset"
Output:

func GetFormat

func GetFormat(data []byte) (Format, error)

GetFormat unmarshals the XML data and returns the sitemap format. The format is either FormatURLSet or FormatIndex.

If invalid format found, returns ErrorInvalidFormat

Example
data := []byte(`
<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

   <url>

      <loc>http://www.example.com/</loc>

      <lastmod>2005-01-01</lastmod>

      <changefreq>monthly</changefreq>

      <priority>0.8</priority>

   </url>

</urlset>`)

f, err := sitemap.GetFormat(data)
if err != nil {
	// handle error
}

fmt.Printf("%s\n", f) // "urlset"
Output:

func (Format) String added in v0.2.0

func (f Format) String() string

type Index

type Index struct {
	// contains filtered or unexported fields
}

func FetchIndex added in v0.2.0

func FetchIndex(url string) (*Index, error)

func NewIndex added in v0.2.0

func NewIndex(urls []URL) *Index

func (*Index) MarshalXML added in v0.2.0

func (i *Index) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Index) Sitemaps added in v0.2.0

func (i *Index) Sitemaps() []string

func (*Index) URLs added in v0.2.1

func (i *Index) URLs() []URL

func (*Index) UnmarshalXML added in v0.2.0

func (i *Index) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type LastModification added in v0.2.0

type LastModification int64

LastModification is the <lastmod> field stored in Unix timestamp.

func ParseLastModification added in v0.2.0

func ParseLastModification(s string) (LastModification, error)

func (LastModification) IsEmpty added in v0.2.0

func (t LastModification) IsEmpty() bool

func (LastModification) MarshalXML added in v0.2.0

func (l LastModification) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (LastModification) String added in v0.2.0

func (t LastModification) String() string

func (*LastModification) UnmarshalXML added in v0.2.0

func (t *LastModification) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml.Unmarshaler interface

type Location

type Location []byte

Location type used for the <loc> field

func (Location) Bytes added in v0.2.0

func (l Location) Bytes() []byte

func (Location) EqualTo added in v0.2.0

func (l Location) EqualTo(j Location) bool

func (Location) IsEmpty added in v0.2.0

func (l Location) IsEmpty() bool

func (Location) MarshalXML added in v0.2.0

func (l Location) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (Location) String

func (l Location) String() string

func (*Location) UnmarshalXML

func (l *Location) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml.Unmarshaler interface

type Priority

type Priority float64

Priority type used for the priority field

func (Priority) IsEmpty added in v0.2.0

func (p Priority) IsEmpty() bool

func (Priority) MarshalXML added in v0.2.0

func (p Priority) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (Priority) String

func (p Priority) String() string

func (*Priority) UnmarshalXML

func (p *Priority) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements the xml.Unmarshaler interface

type Sitemap

type Sitemap struct {
	// contains filtered or unexported fields
}

func Fetch

func Fetch(url string) (*Sitemap, error)

func FetchSitemap added in v0.2.0

func FetchSitemap(url string) (*Sitemap, error)

func FetchSitemaps added in v0.2.0

func FetchSitemaps(urls []string) (*Sitemap, error)

func NewSitemap added in v0.2.0

func NewSitemap(urls []URL) *Sitemap

func (*Sitemap) AddURLs added in v0.2.1

func (s *Sitemap) AddURLs(u []URL)

func (*Sitemap) MarshalXML added in v0.2.0

func (s *Sitemap) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Sitemap) NumPages added in v0.2.0

func (s *Sitemap) NumPages() int

func (*Sitemap) Pages added in v0.2.0

func (s *Sitemap) Pages() []string

func (*Sitemap) URLs added in v0.2.1

func (s *Sitemap) URLs() []URL

func (*Sitemap) UnmarshalXML added in v0.2.0

func (s *Sitemap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

type URL

type URL struct {
	Loc        Location         `xml:"loc"`
	LastMod    LastModification `xml:"lastmod,omitempty"`
	ChangeFreq ChangeFrequency  `xml:"changefreq,omitempty"`
	Priority   Priority         `xml:"priority,omitempty"`
}

func (URL) MarshalXML added in v0.2.0

func (u URL) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (URL) String

func (u URL) String() string

func (*URL) UnmarshalXML added in v0.2.0

func (u *URL) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL