fixes for go1 xml unmarshalling
This commit is contained in:
parent
8b8af3175c
commit
28c80e3abe
1 changed files with 40 additions and 26 deletions
66
rss.go
66
rss.go
|
@ -4,71 +4,85 @@ Simple RSS parser, tested with Wordpress feeds.
|
||||||
package rss
|
package rss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Feed struct {
|
type Channel struct {
|
||||||
Title string
|
Title string `xml:"title"`
|
||||||
Link string
|
Link string `xml:"link"`
|
||||||
Description string
|
Description string `xml:"description"`
|
||||||
Language string
|
Language string `xml:"language"`
|
||||||
Item []Item
|
LastBuildDate Date `xml:"lastBuildDate"`
|
||||||
|
Item []Item `xml:"item"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ItemEnclosure struct {
|
||||||
|
URL string `xml:"url,attr"`
|
||||||
|
Type string `xml:"type,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
Title string
|
Title string `xml:"title"`
|
||||||
Link string
|
Link string `xml:"link"`
|
||||||
PubDate string
|
Comments string `xml:"comments"`
|
||||||
Description string
|
PubDate Date `xml:"pubDate"`
|
||||||
Enclosure struct {
|
GUID string `xml:"guid"`
|
||||||
URL string `xml:"attr"`
|
Category []string `xml:"category"`
|
||||||
}
|
Enclosure ItemEnclosure `xml:"enclosure"`
|
||||||
|
Description string `xml:"description"`
|
||||||
|
Content string `xml:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Item) ParsePubDate() (time.Time, error) {
|
type Date string
|
||||||
t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", self.PubDate) // Wordpress format
|
|
||||||
|
func (self Date) Parse() (time.Time, error) {
|
||||||
|
t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 -0700", string(self)) // Wordpress format
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t, err = time.Parse(time.RFC822, self.PubDate) // RSS 2.0 spec
|
t, err = time.Parse(time.RFC822, string(self)) // RSS 2.0 spec
|
||||||
}
|
}
|
||||||
return t, err
|
return t, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Item) FormatPubDate(format string) (string, error) {
|
func (self Date) Format(format string) (string, error) {
|
||||||
t, err := self.ParsePubDate()
|
t, err := self.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return t.Format(format), nil
|
return t.Format(format), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Item) MustFormatPubDate(format string) string {
|
func (self Date) MustFormat(format string) string {
|
||||||
s, err := self.FormatPubDate(format)
|
s, err := self.Format(format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func Read(url string) (feed *Feed, err error) {
|
func Read(url string) (*Channel, error) {
|
||||||
response, err := http.Get(url)
|
response, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
var buf bytes.Buffer
|
text, err := ioutil.ReadAll(response.Body)
|
||||||
_, err = buf.ReadFrom(response.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
fmt.Println(string(text))
|
||||||
|
|
||||||
var rss struct {
|
var rss struct {
|
||||||
Channel Feed
|
Channel Channel `xml:"channel"`
|
||||||
}
|
}
|
||||||
err = xml.Unmarshal(buf.Bytes(), &rss)
|
err = xml.Unmarshal(text, &rss)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &rss.Channel, nil
|
return &rss.Channel, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue