File: rss.go

package info (click to toggle)
golang-github-tideland-golib 4.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,144 kB
  • sloc: makefile: 4
file content (445 lines) | stat: -rw-r--r-- 12,822 bytes parent folder | download | duplicates (2)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Tideland Go Library - RSS Feed
//
// Copyright (C) 2012-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package rss

//--------------------
// IMPORTS
//--------------------

import (
	"encoding/xml"
	"io"
	"net/http"
	"net/url"
	"time"

	"github.com/tideland/golib/errors"
	"github.com/tideland/golib/feed/utils"
)

//--------------------
// CONST
//--------------------

// Version of the RSS.
const (
	Version = "2.0"
)

const (
	rssDate   = "Mon, 02 Jan 2006 15:04 MST"
	rssDateV1 = "Mon, 02 Jan 2006 15:04:05 MST"
	rssDateV2 = "02 Jan 2006 15:04 MST"
	rssDateV3 = "Mon, 02 Jan 2006 15:04 -0700"
	rssDateV4 = "02 Jan 2006 15:04 -0700"
)

//--------------------
// MODEL
//--------------------

// RSS is the root element of the document.
type RSS struct {
	XMLName string  `xml:"rss"`
	Version string  `xml:"version,attr"`
	Channel Channel `xml:"channel"`
}

// Validate checks if the RSS document is valid.
func (r *RSS) Validate() error {
	if r.Version != Version {
		return errors.New(ErrValidation, errorMessages, "invalid RSS document version: %q", r.Version)
	}
	return r.Channel.Validate()
}

// Channel is the one channel element of the RSS document.
type Channel struct {
	Title          string      `xml:"title"`
	Description    string      `xml:"description"`
	Link           string      `xml:"link"`
	Categories     []*Category `xml:"category,omitempty"`
	Cloud          *Cloud      `xml:"cloud,omitempty"`
	Copyright      string      `xml:"copyright,omitempty"`
	Docs           string      `xml:"docs,omitempty"`
	Generator      string      `xml:"generator,omitempty"`
	Image          *Image      `xml:"image,omitempty"`
	Language       string      `xml:"language,omitempty"`
	LastBuildDate  string      `xml:"lastBuildDate,omitempty"`
	ManagingEditor string      `xml:"managingEditor,omitempty"`
	PubDate        string      `xml:"pubDate,omitempty"`
	Rating         string      `xml:"rating,omitempty"`
	SkipDays       *SkipDays   `xml:"skipDays,omitempty"`
	SkipHours      *SkipHours  `xml:"skipHours,omitempty"`
	TextInput      string      `xml:"textInput,omitempty"`
	TTL            int         `xml:"ttl,omitempty"`
	WebMaster      string      `xml:"webMaster,omitempty"`
	Items          []*Item     `xml:"item,omitempty"`
}

// Validate checks if the cannel is valid.
func (c Channel) Validate() error {
	if c.Title == "" {
		return errors.New(ErrValidation, errorMessages, "channel title must not be empty")
	}
	if c.Description == "" {
		return errors.New(ErrValidation, errorMessages, "channel description must not be empty")
	}
	if c.Link == "" {
		return errors.New(ErrValidation, errorMessages, "channel link must not be empty")
	}
	if _, err := url.Parse(c.Link); err != nil {
		return errors.Annotate(err, ErrParsing, errorMessages, "channel link")
	}
	for _, category := range c.Categories {
		if err := category.Validate(); err != nil {
			return err
		}
	}
	if c.Cloud != nil {
		if err := c.Cloud.Validate(); err != nil {
			return err
		}
	}
	if c.Docs != "" && c.Docs != "http://blogs.law.harvard.edu/tech/rss" {
		return errors.New(ErrValidation, errorMessages, "docs %q is not valid", c.Docs)
	}
	if c.Image != nil {
		if err := c.Image.Validate(); err != nil {
			return err
		}
	}
	if c.Language != "" {
		// TODO(mue) Language has to be validated.
	}
	if c.LastBuildDate != "" {
		if _, err := ParseTime(c.LastBuildDate); err != nil {
			return errors.Annotate(err, ErrParsing, errorMessages, "channel last build date")
		}
	}
	if c.PubDate != "" {
		if _, err := ParseTime(c.PubDate); err != nil {
			return errors.Annotate(err, ErrParsing, errorMessages, "channel publication date")
		}
	}
	if c.SkipDays != nil {
		if err := c.SkipDays.Validate(); err != nil {
			return err
		}
	}
	if c.SkipHours != nil {
		if err := c.SkipHours.Validate(); err != nil {
			return err
		}
	}
	if c.TTL < 0 {
		return errors.New(ErrValidation, errorMessages, "channel ttl is below zero")
	}
	for _, item := range c.Items {
		if err := item.Validate(); err != nil {
			return err
		}
	}
	return nil
}

// Category identifies a category or tag to which the feed belongs.
type Category struct {
	Category string `xml:",chardata"`
	Domain   string `xml:"domain,attr,omitempty"`
}

// Validate checks if the category is valid.
func (c *Category) Validate() error {
	if c.Category == "" {
		return errors.New(ErrValidation, errorMessages, "channel category must not be empty")
	}
	return nil
}

// Cloud indicates that updates to the feed can be monitored using a web service
// that implements the RssCloud application programming interface.
type Cloud struct {
	Domain            string `xml:"domain,attr"`
	Port              int    `xml:"port,attr,omitempty"`
	Path              string `xml:"path,attr"`
	RegisterProcedure string `xml:"registerProcedure,attr"`
	Protocol          string `xml:"protocol,attr"`
}

// Validate checks if the cloud is valid.
func (c *Cloud) Validate() error {
	if c.Domain == "" {
		return errors.New(ErrValidation, errorMessages, "cloud domain must not be empty")
	}
	if c.Path == "" || c.Path[0] != '/' {
		return errors.New(ErrValidation, errorMessages, "cloud path %q must not be empty and has to start with a slash", c.Path)
	}
	if c.Port < 1 || c.Port > 65535 {
		return errors.New(ErrValidation, errorMessages, "cloud port %d is out of range", c.Port)
	}
	return nil
}

// Image supplies a graphical logo for the feed .
type Image struct {
	Link        string `xml:"link"`
	Title       string `xml:"title"`
	URL         string `xml:"url"`
	Description string `xml:"description,omitempty"`
	Height      int    `xml:"height,omitempty"`
	Width       int    `xml:"width,omitempty"`
}

// Validate checks if the image is valid.
func (i *Image) Validate() error {
	if _, err := url.Parse(i.Link); err != nil {
		return errors.New(ErrValidation, errorMessages, "image link is not parsable", i.Link)
	}
	if i.Title == "" {
		return errors.New(ErrValidation, errorMessages, "image title must not be empty")
	}
	if _, err := url.Parse(i.URL); err != nil {
		return errors.Annotate(err, ErrParsing, errorMessages, "image title")
	}
	if i.Height < 1 || i.Height > 400 {
		return errors.New(ErrValidation, errorMessages, "image height %d is out of range from 1 to 400", i.Height)
	}
	if i.Width < 1 || i.Width > 144 {
		return errors.New(ErrValidation, errorMessages, "image width %d is out of range from 1 to 144", i.Width)
	}
	return nil
}

// SkipDays identifies days of the week during which the feed is not updated.
type SkipDays struct {
	Days []string `xml:"day"`
}

// Validate checks if the skip days are valid.
func (s *SkipDays) Validate() error {
	skipDays := map[string]bool{
		"Monday":    true,
		"Tuesday":   true,
		"Wednesday": true,
		"Thursday":  true,
		"Friday":    true,
		"Saturday":  true,
		"Sunday":    true,
	}
	for _, day := range s.Days {
		if !skipDays[day] {
			return errors.New(ErrValidation, errorMessages, "skip day %q is invalid", day)
		}
	}
	return nil
}

// SkipHours identifies the hours of the day during which the feed is not updated.
type SkipHours struct {
	Hours []int `xml:"hour"`
}

// Validate checks if the skip hours are valid.
func (s *SkipHours) Validate() error {
	for _, hour := range s.Hours {
		if hour < 0 || hour > 23 {
			return errors.New(ErrValidation, errorMessages, "skip hour %d is out of range from 0 to 23", hour)
		}
	}
	return nil
}

// TextInput defines a form to submit a text query to the feed's publisher over
// the Common Gateway Interface (CGI).
type TextInput struct {
	Description string `xml:"description"`
	Link        string `xml:"link"`
	Name        string `xml:"name"`
	Title       string `xml:"title"`
}

// Validate checks if the text input is valid.
func (t *TextInput) Validate() error {
	if t.Description == "" {
		return errors.New(ErrValidation, errorMessages, "text input description must not be empty")
	}
	if _, err := url.Parse(t.Link); err != nil {
		return errors.Annotate(err, ErrParsing, errorMessages, "text input link")
	}
	if t.Name == "" {
		return errors.New(ErrValidation, errorMessages, "text input name must not be empty")
	}
	if t.Title == "" {
		return errors.New(ErrValidation, errorMessages, "text input title must not be empty")
	}
	return nil
}

// Item represents distinct content published in the feed such as a news article,
// weblog entry or some other form of discrete update. It must contain either a
// title or description.
type Item struct {
	Title       string      `xml:"title,omitempty"`
	Description string      `xml:"description,omitempty"`
	Author      string      `xml:"author,omitempty"`
	Categories  []*Category `xml:"category,omitempty"`
	Comments    string      `xml:"comments,omitempty"`
	Enclosure   *Enclosure  `xml:"enclosure,omitempty"`
	GUID        *GUID       `xml:"guid,omitempty"`
	Link        string      `xml:"link,omitempty"`
	PubDate     string      `xml:"pubDate,omitempty"`
	Source      *Source     `xml:"source,omitempty"`
}

// Validate checks if the item is valid.
func (i *Item) Validate() error {
	if i.Title == "" {
		if i.Description == "" {
			return errors.New(ErrValidation, errorMessages, "item title or description must not be empty")
		}
	}
	if i.Comments != "" {
		if _, err := url.Parse(i.Comments); err != nil {
			return errors.Annotate(err, ErrParsing, errorMessages, "item comments")
		}
	}
	if i.Enclosure != nil {
		if err := i.Enclosure.Validate(); err != nil {
			return err
		}
	}
	if i.GUID != nil {
		if err := i.GUID.Validate(); err != nil {
			return err
		}
	}
	if i.Link != "" {
		if _, err := url.Parse(i.Link); err != nil {
			return errors.Annotate(err, ErrParsing, errorMessages, "item link")
		}
	}
	if i.PubDate != "" {
		if _, err := ParseTime(i.PubDate); err != nil {
			return errors.Annotate(err, ErrParsing, errorMessages, "item publcation date")
		}
	}
	if i.Source != nil {
		if err := i.Source.Validate(); err != nil {
			return err
		}
	}
	return nil
}

// Enclosure associates a media object such as an audio or video file with the item.
type Enclosure struct {
	Length int64  `xml:"length,attr"`
	Type   string `xml:"type,attr"`
	URL    string `xml:"url,attr"`
}

// Validate checks if the enclosure is valid.
func (e *Enclosure) Validate() error {
	if e.Length < 1 {
		return errors.New(ErrValidation, errorMessages, "item enclosure length %d is too small", e.Length)
	}
	if e.Type == "" {
		return errors.New(ErrValidation, errorMessages, "item enclosure type must not be empty")
	}
	if _, err := url.Parse(e.URL); err != nil {
		return errors.Annotate(err, ErrParsing, errorMessages, "item enclosure url")
	}
	return nil
}

// GUID provides a string that uniquely identifies the item.
type GUID struct {
	GUID        string `xml:",chardata"`
	IsPermaLink bool   `xml:"isPermaLink,attr,omitempty"`
}

// Validate checks if the GUID is valid.
func (g *GUID) Validate() error {
	if g.IsPermaLink {
		if _, err := url.Parse(g.GUID); err != nil {
			return errors.Annotate(err, ErrParsing, errorMessages, "item GUID")
		}
	}
	return nil
}

// Source indicates the fact that the item has been republished from another RSS feed.
type Source struct {
	Source string `xml:",chardata"`
	URL    string `xml:"url,attr"`
}

// Validate checks if the source is valid.
func (s *Source) Validate() error {
	if s.Source == "" {
		return errors.New(ErrValidation, errorMessages, "item source must not be empty")
	}
	if _, err := url.Parse(s.URL); err != nil {
		return errors.Annotate(err, ErrParsing, errorMessages, "item source URL")
	}
	return nil
}

//--------------------
// FUNCTIONS
//--------------------

// ParseTime analyzes the RSS date/time string and returns it as Go time.
func ParseTime(s string) (t time.Time, err error) {
	formats := []string{rssDate, rssDateV1, rssDateV2, rssDateV3, rssDateV4, time.RFC822, time.RFC822Z}
	for _, format := range formats {
		t, err = time.Parse(format, s)
		if err == nil {
			return
		}
	}
	return
}

// ComposeTime takes a Go time and converts it into a valid RSS time string.
func ComposeTime(t time.Time) string {
	return t.Format(rssDate)
}

// Encode writes the RSS document to the writer.
func Encode(w io.Writer, rss *RSS) error {
	enc := xml.NewEncoder(w)
	if _, err := w.Write([]byte(xml.Header)); err != nil {
		return err
	}
	return enc.Encode(rss)
}

// Decode reads the RSS document from the reader.
func Decode(r io.Reader) (*RSS, error) {
	dec := xml.NewDecoder(r)
	dec.CharsetReader = utils.CharsetReader
	rss := &RSS{}
	if err := dec.Decode(rss); err != nil {
		return nil, err
	}
	return rss, nil
}

// Get retrieves an RSS document from the given URL.
func Get(u *url.URL) (*RSS, error) {
	resp, err := http.Get(u.String())
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	return Decode(resp.Body)
}

// EOF