summaryrefslogtreecommitdiff
path: root/rss.go
blob: ecf6f24fa0e4c1f9095e680a5bec4b4345971581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Package rss implements RSS fetcher
package rss

import (
	"encoding/xml"
	"net/http"

	"code.google.com/p/go-charset/charset"
	_ "code.google.com/p/go-charset/data"
)

// RSS container
type RSS struct {
	Version string  `xml:"version,attr"`
	Channel Channel `xml:"channel"`
}

// Channel container
type Channel struct {
	Link        string `xml:"link"`
	Language    string `xml:"language"`
	Title       string `xml:"title"`
	Description string `xml:"description"`
	PubDate     string `xml:"pubDate"`
	Items       []Item `xml:"item"`
}

// Item container
type Item struct {
	Author  string `xml:"author"`
	Link    string `xml:"link"`
	GUID    string `xml:"guid"`
	Title   string `xml:"title"`
	PubDate string `xml:"pubDate"`
}

// Fetch and parse RSS from given URL
func Fetch(url string) (rss RSS, err error) {
	resp, err := http.Get(url)
	if err != nil {
		return rss, err
	}
	defer resp.Body.Close()
	decoder := xml.NewDecoder(resp.Body)
	decoder.CharsetReader = charset.NewReader
	err = decoder.Decode(&rss)
	if err != nil {
		return rss, err
	}
	return rss, nil
}