diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5422cbd --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +############################# +# OS generated files: + +.DS_Store +Icon? +Thumbs.db + +############################# +# Dropbox: + +.dropbox + +############################# +# Go specific files: + +_obj +_test +*.[568vq] +[568vq].out +*.o +*.a +*.so +*.exe + diff --git a/README b/README index e69de29..09f030d 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +Simple RSS parser, tested with Wordpress feeds. \ No newline at end of file diff --git a/rss.go b/rss.go new file mode 100644 index 0000000..97554e5 --- /dev/null +++ b/rss.go @@ -0,0 +1,68 @@ +/* +Simple RSS parser, tested with Wordpress feeds. +*/ +package rss + +import ( + "encoding/xml" + "net/http" + "time" +) + +type Feed struct { + Title string + Link string + Description string + Language string + Item []Item +} + +type Item struct { + Title string + Link string + PubDate string + Description string + Enclosure struct { + URL string `xml:"attr"` + } +} + +func (self *Item) ParsePubDate() (time.Time, error) { + t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", self.PubDate) // Wordpress format + if err != nil { + t, err = time.Parse(time.RFC822, self.PubDate) // RSS 2.0 spec + } + return t, err +} + +func (self *Item) FormatPubDate(format string) (string, error) { + t, err := self.ParsePubDate() + if err != nil { + return "", err + } + return t.Format(format), nil +} + +func (self *Item) MustFormatPubDate(format string) string { + s, err := self.FormatPubDate(format) + if err != nil { + return err.Error() + } + return s +} + +func Read(url string) (feed *Feed, err error) { + response, err := http.Get(url) + if err != nil { + return nil, err + } + defer response.Body.Close() + var rss struct { + Channel Feed + } + err = xml.Unmarshal(response.Body, &rss) + if err != nil { + return nil, err + } + return &rss.Channel, nil +}