Updated README for installation and usage example & updated rss package for more documentation

Also updated (self Date) to (d Date) for correct Go formatting
This commit is contained in:
Patrick van de Glind 2015-06-01 13:57:58 +02:00
parent 7da7a7b819
commit d60b90e720
2 changed files with 45 additions and 15 deletions

24
README
View file

@ -1,3 +1,23 @@
Simple RSS parser, tested with Wordpress feeds. Simple RSS parser, tested with various feeds.
License: Public Domain License: Public Domain
## Installation
go get github.com/ungerik/go-rss
## Usage
import "github.com/ungerik/go-rss"
channel, err := rss.Read("https://en.blog.wordpress.com/feed/")
if err != nil {
fmt.Println(err)
}
fmt.Println(channel.Title)
for _, item := range channel.Item {
fmt.Println(item.Title)
}

36
rss.go
View file

@ -1,6 +1,4 @@
/** //Package rss provides a Simple RSS parser, tested with various feeds.
* Simple RSS parser, tested with various feeds.
*/
package rss package rss
import ( import (
@ -9,17 +7,19 @@ import (
"time" "time"
"code.google.com/p/go-charset/charset" "code.google.com/p/go-charset/charset"
_ "code.google.com/p/go-charset/data" _ "code.google.com/p/go-charset/data" //initialize only
) )
const ( const (
wordpressDateFormat = "Mon, 02 Jan 2006 15:04:05 -0700" wordpressDateFormat = "Mon, 02 Jan 2006 15:04:05 -0700"
) )
//Fetcher interface
type Fetcher interface { type Fetcher interface {
Get(url string) (resp *http.Response, err error) Get(url string) (resp *http.Response, err error)
} }
//Channel struct for RSS
type Channel struct { type Channel struct {
Title string `xml:"title"` Title string `xml:"title"`
Link string `xml:"link"` Link string `xml:"link"`
@ -29,11 +29,13 @@ type Channel struct {
Item []Item `xml:"item"` Item []Item `xml:"item"`
} }
//ItemEnclosure struct for each Item Enclosure
type ItemEnclosure struct { type ItemEnclosure struct {
URL string `xml:"url,attr"` URL string `xml:"url,attr"`
Type string `xml:"type,attr"` Type string `xml:"type,attr"`
} }
//Item struct for each Item in the Channel
type Item struct { type Item struct {
Title string `xml:"title"` Title string `xml:"title"`
Link string `xml:"link"` Link string `xml:"link"`
@ -46,40 +48,48 @@ type Item struct {
Content string `xml:"content"` Content string `xml:"content"`
} }
//Date type
type Date string type Date string
func (self Date) Parse() (time.Time, error) { //Parse (Date function) and returns Time, error
t, err := self.ParseWithFormat(wordpressDateFormat) func (d Date) Parse() (time.Time, error) {
t, err := d.ParseWithFormat(wordpressDateFormat)
if err != nil { if err != nil {
t, err = self.ParseWithFormat(time.RFC822) // RSS 2.0 spec t, err = d.ParseWithFormat(time.RFC822) // RSS 2.0 spec
} }
return t, err return t, err
} }
func (self Date) ParseWithFormat(format string) (time.Time, error) { //ParseWithFormat (Date function), takes a string and returns Time, error
return time.Parse(format, string(self)) func (d Date) ParseWithFormat(format string) (time.Time, error) {
return time.Parse(format, string(d))
} }
func (self Date) Format(format string) (string, error) { //Format (Date function), takes a string and returns string, error
t, err := self.Parse() func (d Date) Format(format string) (string, error) {
t, err := d.Parse()
if err != nil { if err != nil {
return "", err return "", err
} }
return t.Format(format), nil return t.Format(format), nil
} }
func (self Date) MustFormat(format string) string { //MustFormat (Date function), take a string and returns string
s, err := self.Format(format) func (d Date) MustFormat(format string) string {
s, err := d.Format(format)
if err != nil { if err != nil {
return err.Error() return err.Error()
} }
return s return s
} }
//Read a string url and returns a Channel struct, error
func Read(url string) (*Channel, error) { func Read(url string) (*Channel, error) {
return ReadWithClient(url, http.DefaultClient) return ReadWithClient(url, http.DefaultClient)
} }
//ReadWithClient a string url and custom client that must match the Fetcher interface
//returns a Channel struct, error
func ReadWithClient(url string, client Fetcher) (*Channel, error) { func ReadWithClient(url string, client Fetcher) (*Channel, error) {
response, err := client.Get(url) response, err := client.Get(url)
if err != nil { if err != nil {