-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
66 lines (53 loc) · 1.29 KB
/
transform.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"errors"
"fmt"
"io"
"strings"
"github.com/beevik/etree"
)
type ErrXMLParseFailed error
func NewErrXMLParseFailed(err error) ErrXMLParseFailed {
return ErrXMLParseFailed(fmt.Errorf("failed to parse: %w", err))
}
type ErrUnExpectedFormat string
func (e ErrUnExpectedFormat) Error() string {
return fmt.Sprintf("unexpected feed format: %s", string(e))
}
type TransformConfig struct {
ProxyOrigin string
Org string
Channel string
UseRedirector bool
DietMode bool
}
func Transform(feed io.Reader, conf TransformConfig) (io.WriterTo, error) {
doc := etree.NewDocument()
if _, err := doc.ReadFrom(feed); err != nil {
return nil, NewErrXMLParseFailed(err)
}
if doc.Root() == nil {
return nil, NewErrXMLParseFailed(errors.New("document has no root element"))
}
var editor FeedEditor
format := strings.ToLower(doc.Root().Tag)
if format == "rss" {
editor = RSSFeedEditor{}
} else if format == "feed" {
editor = AtomFeedEditor{}
} else if format == "rdf" {
editor = RDFFeedEditor{}
} else {
return nil, ErrUnExpectedFormat(format)
}
editor.UpdateFeedTitle(doc)
if conf.DietMode {
editor.DietEntryContent(doc)
} else {
editor.RemoveEntryContent(doc)
}
if conf.UseRedirector {
editor.TapRedirector(doc, conf)
}
return doc, nil
}