-
Notifications
You must be signed in to change notification settings - Fork 4
/
html_parser.go
232 lines (206 loc) · 6.29 KB
/
html_parser.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
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
package main
import (
"log"
"regexp"
"strings"
"time"
)
func ExtractHtmlTitle(htmlData []byte) string {
titleReg := regexp.MustCompile(HTML_TITLE_REG)
matches := titleReg.FindSubmatch(htmlData)
if len(matches) != 2 {
return ""
}
return string(matches[1])
}
func FilterHtmlWithoutPattern(htmlData []byte, pattern string) bool {
html := string(htmlData)
for _, str := range PATTERN_ALL_REGEX.Split(pattern, -1) {
if "" == str {
continue
}
if !strings.Contains(html, str) {
log.Printf("[ERROR] target html does not contain %s", str)
if *gDebug {
log.Println("======= debug: target html data =======")
log.Println(string(htmlData))
log.Println("==============")
}
return false
}
}
return true
}
func ParseIndexHtml(feedTar *FeedTarget) (feed *Feed, ok bool) {
feed = new(Feed)
for urlInd, tarURL := range feedTar.URLs {
// get cache
indexCache, err := FetchHtml(tarURL, feedTar)
if nil == indexCache || nil != err {
log.Printf("[ERROR] failed to download index web page %s", tarURL.String())
// just ignore the sucker, feed.URL = nil
continue
}
// minify html
htmlData := MinifyHtml(RemoveJunkContent(indexCache.Html))
// extract feed entry title and link
indRegs := FindIndexRegs(feedTar, tarURL)
for ind, indexReg := range indRegs {
if nil == indexReg {
log.Printf("[ERROR] cannot find index regex for %s", tarURL.String())
continue
}
// make a copy of source data
var htmlDataCopy []byte
if ind+1 == len(indRegs) {
htmlDataCopy = htmlData
} else {
htmlDataCopy = make([]byte, len(htmlData))
copy(htmlDataCopy, htmlData)
}
// filter html with index filter
indexFilterReg := FindIndexFilterReg(feedTar, indexReg)
if nil != indexFilterReg {
htmlDataCopy = RegexpFilter(indexFilterReg, htmlDataCopy)
if nil == htmlDataCopy {
// failed to filter htmlData
continue
}
}
matches := indexReg.FindAllSubmatch(htmlDataCopy, -1)
if nil == matches {
log.Printf("[ERROR] failed to match index html %s, pattern %s did not match", tarURL.String(), indexReg.String())
if *gDebug {
log.Println("======= debug: target html data =======")
log.Println(string(htmlDataCopy))
log.Println("==============")
}
// ignore this
continue
}
entries := make([]*FeedEntry, len(matches))
for matchInd, match := range matches {
entries[matchInd] = new(FeedEntry)
entry := entries[matchInd] // pointer of FeedEntry
entry.IndexPattern = indexReg
for patInd, patName := range indexReg.SubexpNames() {
switch patName {
case PATTERN_TITLE:
entry.Title = string(match[patInd])
case PATTERN_LINK:
// normalize entry link which may be relative
entry.Link, err = tarURL.Parse(string(match[patInd]))
if nil != err {
log.Printf("[ERROR] error parsing entry link %s: %s", entry.Link, err)
}
case PATTERN_PUBDATE:
var pubDate time.Time
pubDate, err = ParsePubDate(FindPubDateReg(feedTar, feed.URL), string(match[patInd]))
if nil != err {
log.Printf("[ERROR] error parsing pubdate of link %s: %s", entry.Link, err)
} else {
entry.PubDate = &pubDate
}
}
}
}
// add entries to feed
feed.Entries = append(feed.Entries, entries...)
}
if 0 == urlInd {
feed.Title = feedTar.Title
feed.Description = feedTar.Description
// use first index page and url
feed.URL = tarURL
dateNow := time.Now()
if nil == indexCache.LastModified {
feed.LastModified = &dateNow
} else {
feed.LastModified = indexCache.LastModified
}
} else {
// use later lastmod time
if nil != indexCache.LastModified && feed.LastModified.Before(*indexCache.LastModified) {
feed.LastModified = indexCache.LastModified
}
}
}
return feed, true
}
func ParseContentHtml(feedTar *FeedTarget, feed *Feed) (ok bool) {
validEntries := make([]*FeedEntry, 1)
validEntryInd := 0
for entryInd, entry := range feed.Entries {
if nil == entry {
log.Printf("[ERROR] failed to parse content html: entry is nil")
continue
}
contentReg := FindContentReg(feedTar, feed.URL, entry.IndexPattern)
if nil == contentReg {
log.Printf("[ERROR] failed to find content regex for entry %s", entry.Link.String())
return
}
// check entry link
if nil == entry.Link {
log.Printf("[ERROR] entry link is nil, ignore this. entry index is %d", entryInd)
continue
}
// wait some time
if *gVerbose {
log.Printf("waiting for %d seconds before sending request to %s", feedTar.ReqInterval, entry.Link.String())
}
time.Sleep(feedTar.ReqInterval * time.Second)
cache, err := FetchHtml(entry.Link, feedTar)
if nil == cache || nil != err {
log.Printf("[ERROR] failed to download web page %s, will remove this entry", entry.Link.String())
// ignore this entry, entry.Cache = nil
continue
} else {
validEntries = append(validEntries[:validEntryInd], entry)
validEntryInd += 1
}
entry.Cache = cache
htmlData := MinifyHtml(RemoveJunkContent(cache.Html))
// filter html with content filter
contFilterReg := FindContentFilterReg(feedTar, contentReg)
if nil != contFilterReg {
htmlData := RegexpFilter(contFilterReg, htmlData)
if nil == htmlData {
// failed to filter htmlData
continue
}
}
// extract feed entry content(description)
match := contentReg.FindSubmatch(htmlData)
if nil == match {
log.Printf("[ERROR] failed to match content html %s, pattern %s match failed", entry.Link.String(), contentReg.String())
if *gDebug {
log.Println("======= debug: target html data =======")
log.Println(string(htmlData))
log.Println("==============")
}
// ignore this sucker
continue
}
for patInd, patName := range contentReg.SubexpNames() {
switch patName {
case PATTERN_CONTENT:
entry.Content = match[patInd]
case PATTERN_PUBDATE:
var pubDate time.Time
pubDate, err = ParsePubDate(FindPubDateReg(feedTar, feed.URL), string(match[patInd]))
if nil != err {
log.Printf("[ERROR] error parsing pubdate of link %s: %s", entry.Link, err)
} else {
entry.PubDate = &pubDate
}
}
}
if 0 == len(entry.Content) {
// just print a warning message if content is empty
log.Printf("[WARN] feed entry has no description: %s", entry.Link.String())
}
}
feed.Entries = validEntries
return true
}