-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathdetector.go
More file actions
79 lines (70 loc) · 1.73 KB
/
Copy pathdetector.go
File metadata and controls
79 lines (70 loc) · 1.73 KB
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
package gofeed
import (
"bytes"
"io"
"strings"
"github.com/mmcdole/gofeed/internal/shared"
)
// 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
// FeedTypeJSON represents a JSON feed
FeedTypeJSON
)
// DetectFeedType attempts to determine the type of feed by looking for an XML
// root element or the start of a JSON object. It does not validate the entire
// document; the selected format parser performs full validation. It returns
// FeedTypeUnknown when the reader fails before the type can be determined.
func DetectFeedType(feed io.Reader) FeedType {
buffer := new(bytes.Buffer)
if _, err := buffer.ReadFrom(feed); err != nil {
return FeedTypeUnknown
}
var firstChar byte
loop:
for {
ch, err := buffer.ReadByte()
if err != nil {
return FeedTypeUnknown
}
// ignore leading whitespace & byte order marks
switch ch {
case ' ', '\r', '\n', '\t':
case 0xFE, 0xFF, 0x00, 0xEF, 0xBB, 0xBF: // utf 8-16-32 bom
default:
firstChar = ch
buffer.UnreadByte()
break loop
}
}
if firstChar == '<' {
// Check if it's an XML based feed
p := shared.NewXMLParser(bytes.NewReader(buffer.Bytes()))
_, 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
}
} else if firstChar == '{' {
return FeedTypeJSON
}
return FeedTypeUnknown
}