-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss_parser.go
More file actions
159 lines (144 loc) · 3.95 KB
/
rss_parser.go
File metadata and controls
159 lines (144 loc) · 3.95 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
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
package godrudge
import (
"fmt"
"regexp"
"strings"
"charm.land/lipgloss/v2"
"github.com/mmcdole/gofeed"
"golang.org/x/net/html"
)
const (
topHeadline = "top headline"
mainHeadline = "main headline"
firstColumnHeadline = "first column"
secondColumnHeadline = "second column"
thirdColumnHeadline = "third column"
)
func (c *Client) fetchRSS() error {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(c.rssFeedURL)
if err != nil {
return err
}
c.rssFeed = feed
return nil
}
func (c *Client) parseRSS() error {
feed := c.rssFeed
c.Page.HeadlineColumns = make([][]Headline, 3)
c.Page.TopHeadlines = []Headline{}
c.Page.MainHeadlines = []Headline{}
for _, item := range feed.Items {
if item.PublishedParsed != nil {
var headlineStyle lipgloss.Style
var isRed bool
if isRed, _ = isFeedHeadlineRed(item.Description); isRed {
headlineStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF8080"))
} else {
headlineStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#999CFF"))
}
headline := Headline{Title: item.Title, URL: item.GUID, Style: headlineStyle, IsRed: isRed}
headlineType, err := getFeedHeadlineType(item.Description)
if err != nil {
return err
}
switch headlineType {
case topHeadline:
c.Page.TopHeadlines = append(c.Page.TopHeadlines, headline)
case mainHeadline:
c.Page.MainHeadlines = append(c.Page.MainHeadlines, headline)
case firstColumnHeadline:
c.Page.HeadlineColumns[0] = append(c.Page.HeadlineColumns[0], headline)
case secondColumnHeadline:
c.Page.HeadlineColumns[1] = append(c.Page.HeadlineColumns[1], headline)
case thirdColumnHeadline:
c.Page.HeadlineColumns[2] = append(c.Page.HeadlineColumns[2], headline)
default:
return fmt.Errorf("failure parsing RSS headlines")
}
}
}
return nil
}
func isFeedHeadlineRed(description string) (bool, error) {
doc, err := html.Parse(strings.NewReader(description))
if err != nil {
return false, err
}
var traverseFirstTree func(*html.Node) bool
traverseFirstTree = func(node *html.Node) bool {
if node.Type == html.ElementNode && node.Data == "font" {
colorVal := extractNodeAttr(node, "color")
if strings.ToLower(colorVal) == "red" {
return true
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
if traverseFirstTree(c) {
return true
}
}
return false
}
for c := doc.FirstChild; c != nil; c = c.NextSibling {
if traverseFirstTree(c) {
return true, nil
}
}
return false, nil
}
func getFeedHeadlineType(description string) (string, error) {
doc, err := html.Parse(strings.NewReader(description))
if err != nil {
return "", err
}
var traverseFirstTree func(*html.Node) string
traverseFirstTree = func(node *html.Node) string {
if node.Type == html.TextNode && strings.Contains(strings.ToLower(node.Data), "headline") {
re := regexp.MustCompile(`(\w+)\sheadline`)
headlineType := re.FindStringSubmatch(strings.ToLower(node.Data))[1]
switch headlineType {
case "top":
return topHeadline
case "main":
return mainHeadline
default:
return ""
}
} else if node.Type == html.TextNode && strings.Contains(strings.ToLower(node.Data), "column") {
re := regexp.MustCompile(`(\w+)\scolumn`)
headlineType := re.FindStringSubmatch(strings.ToLower(node.Data))[1]
switch headlineType {
case "first":
return firstColumnHeadline
case "second":
return secondColumnHeadline
case "third":
return thirdColumnHeadline
default:
return ""
}
}
for c := node.FirstChild; c != nil; c = c.NextSibling {
if result := traverseFirstTree(c); result != "" {
return result
}
}
return ""
}
result := ""
for c := doc.FirstChild; c != nil; c = c.NextSibling {
if result = traverseFirstTree(c); result != "" {
break
}
}
return result, nil
}
func extractNodeAttr(n *html.Node, attrKey string) string {
for _, attr := range n.Attr {
if attr.Key == attrKey {
return attr.Val
}
}
return ""
}