コード例 #1
0
ファイル: feed_updater.go プロジェクト: jackc/tpr
func parseRSS(body []byte) (*data.ParsedFeed, error) {
	type Item struct {
		Link    string `xml:"link"`
		Title   string `xml:"title"`
		Date    string `xml:"date"`
		PubDate string `xml:"pubDate"`
	}

	type Channel struct {
		Title       string `xml:"title"`
		Description string `xml:"description"`
		Item        []Item `xml:"item"`
	}

	var rss struct {
		Channel Channel `xml:"channel"`
		Item    []Item  `xml:"item"`
	}

	err := parseXML(body, &rss)
	if err != nil {
		return nil, err
	}

	var feed data.ParsedFeed
	if rss.Channel.Title != "" {
		feed.Name = rss.Channel.Title
	} else {
		feed.Name = rss.Channel.Description
	}

	var items []Item
	if len(rss.Item) > 0 {
		items = rss.Item
	} else {
		items = rss.Channel.Item
	}

	feed.Items = make([]data.ParsedItem, len(items))
	for i, item := range items {
		feed.Items[i].URL = item.Link
		feed.Items[i].Title = item.Title
		if item.Date != "" {
			feed.Items[i].PublicationTime, _ = parseTime(item.Date)
		}
		if item.PubDate != "" {
			feed.Items[i].PublicationTime, _ = parseTime(item.PubDate)
		}
	}

	if !feed.IsValid() {
		return nil, errors.New("Invalid RSS")
	}

	return &feed, nil
}
コード例 #2
0
ファイル: feed_updater.go プロジェクト: jackc/tpr
func parseAtom(body []byte) (*data.ParsedFeed, error) {
	type Link struct {
		Href string `xml:"href,attr"`
	}

	type Entry struct {
		Link      Link   `xml:"link"`
		Title     string `xml:"title"`
		Published string `xml:"published"`
		Updated   string `xml:"updated"`
	}

	var atom struct {
		Title string  `xml:"title"`
		Entry []Entry `xml:"entry"`
	}

	err := parseXML(body, &atom)
	if err != nil {
		return nil, err
	}

	var feed data.ParsedFeed
	feed.Name = atom.Title
	feed.Items = make([]data.ParsedItem, len(atom.Entry))
	for i, entry := range atom.Entry {
		feed.Items[i].URL = entry.Link.Href
		feed.Items[i].Title = entry.Title
		if entry.Published != "" {
			feed.Items[i].PublicationTime, _ = parseTime(entry.Published)
		}
		if entry.Updated != "" {
			feed.Items[i].PublicationTime, _ = parseTime(entry.Updated)
		}
	}

	if !feed.IsValid() {
		return nil, errors.New("Invalid Atom")
	}

	return &feed, nil
}