go-feedparser

Simple RSS and ATOM feed parser

git clone https://git.8pit.net/go-feedparser.git

  1// This program is free software: you can redistribute it and/or modify
  2// it under the terms of the GNU General Public License as published by
  3// the Free Software Foundation, either version 3 of the License, or
  4// (at your option) any later version.
  5//
  6// This program is distributed in the hope that it will be useful,
  7// but WITHOUT ANY WARRANTY; without even the implied warranty of
  8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9// GNU General Public License for more details.
 10//
 11// You should have received a copy of the GNU General Public License
 12// along with this program. If not, see <http://www.gnu.org/licenses/>.
 13//
 14// This is a slightly modified version of 'encoding/xml/read_test.go'.
 15// Copyright 2009 The Go Authors. All rights reserved. Use of this
 16// source code is governed by a BSD-style license that can be found in
 17// the LICENSE file.
 18
 19package feedparser
 20
 21import (
 22	"io"
 23	"io/ioutil"
 24	"sort"
 25	"time"
 26)
 27
 28// parseFunc describes a function which implements a feed parser.
 29type parseFunc func([]byte) (Feed, error)
 30
 31// parsers lists all known feed parsers.
 32var parsers = []parseFunc{parseAtom, parseRss}
 33
 34// Feed represents a generic feed.
 35type Feed struct {
 36	// Title for the feed.
 37	Title string
 38
 39	// Feed type (either atom or rss).
 40	Type string
 41
 42	// URL to the website.
 43	Link string
 44
 45	// Description or subtitle for the feed.
 46	Description string
 47
 48	// Categories the feed belongs to.
 49	Categories []string
 50
 51	// Email address of the feed author.
 52	Author string
 53
 54	// Last time the feed was updated.
 55	Updated time.Time
 56
 57	// URL to image for the feed.
 58	Image string
 59
 60	// Software used to generate the feed.
 61	Generator string
 62
 63	// Information about rights, for example copyrights.
 64	Rights string
 65
 66	// Feed Items
 67	Items []Item
 68}
 69
 70// Item represents a generic feed item.
 71type Item struct {
 72	// Universally unique item ID.
 73	ID string
 74
 75	// Title of the item.
 76	Title string
 77
 78	// URL for the item.
 79	Link string
 80
 81	// Content of the item.
 82	Content string
 83
 84	// Email address of the item author.
 85	Author string
 86
 87	// Categories the item belongs to.
 88	Categories []string
 89
 90	// Time the item was published.
 91	PubDate time.Time
 92
 93	// URL to media attachment.
 94	Attachment string
 95}
 96
 97// Parse tries to parse the content of the given reader. It also sorts all items
 98// by there publication date. Meaning that the first item is guaranteed to be
 99// the most recent one.
100func Parse(r io.Reader) (f Feed, err error) {
101	data, err := ioutil.ReadAll(r)
102	if err != nil {
103		return
104	}
105
106	for _, p := range parsers {
107		f, err = p(data)
108		if err == nil {
109			break
110		}
111	}
112
113	if err != nil {
114		return
115	}
116
117	sort.Sort(byDate(f.Items))
118	return
119}