forked from mmcdole/gofeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detector.go
49 lines (42 loc) · 1.01 KB
/
detector.go
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
package gofeed
import (
"io"
"strings"
"github.com/mmcdole/gofeed/internal/shared"
"github.com/mmcdole/goxpp"
"golang.org/x/net/html/charset"
)
// FeedType represents one of the possible feed
// types that we can detect.
type FeedType int
const (
// FeedTypeUnknown represents a feed that could not have its
// type determiend.
FeedTypeUnknown FeedType = iota
// FeedTypeAtom repesents an Atom feed
FeedTypeAtom
// FeedTypeRSS represents an RSS feed
FeedTypeRSS
)
// DetectFeedType attempts to determine the type of feed
// by looking for specific xml elements unique to the
// various feed types.
func DetectFeedType(feed io.Reader) FeedType {
fr := shared.NewXMLSanitizerReader(feed)
p := xpp.NewXMLPullParser(fr, false, charset.NewReaderLabel)
_, err := shared.FindRoot(p)
if err != nil {
return FeedTypeUnknown
}
name := strings.ToLower(p.Name)
switch name {
case "rdf":
return FeedTypeRSS
case "rss":
return FeedTypeRSS
case "feed":
return FeedTypeAtom
default:
return FeedTypeUnknown
}
}