-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSJ.go
218 lines (187 loc) · 5.38 KB
/
WSJ.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
package scraper
import (
"encoding/xml"
"fmt"
"golang.org/x/net/html"
"strings"
)
// WSJ new source types
type WSJArticle struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
// article body
Data string
}
// TODO: add errors
func (article WSJArticle) GetLink() string { return article.Link }
func (article WSJArticle) GetDescription() string { return article.Description }
func (article WSJArticle) GetTitle() string { return article.Title }
func (article WSJArticle) GetData() string { return article.Data }
// use ptrs for the next two because we want the article changed
func (article *WSJArticle) SetData(data string) { article.Data = data }
func (article *WSJArticle) DoParse(parser *html.Tokenizer) error {
// find the start of the article
// starts at the top of the html body, ends at the article tag
articleTagLoop:
for {
token := parser.Next()
switch {
case token == html.ErrorToken:
fmt.Println("OH NOSE!!!! ERROR")
fmt.Println(token)
return nil
case token == html.StartTagToken:
tmp := parser.Token()
isStartArticle := tmp.Data == "article"
if isStartArticle {
break articleTagLoop
}
}
}
// find the article header, which has author, time etc
// starts at the article tag, ends at the article header
// TODO: get author info and such here
articleStartLoop:
for {
token := parser.Next()
switch {
case token == html.ErrorToken:
return nil
case token == html.StartTagToken:
tmp := parser.Token()
isStartArticleBody := tmp.Data == "div"
// loop until we are at the first paragraph of the article body
if isStartArticleBody {
isStartArticleBody = false
for _, attr := range tmp.Attr {
if attr.Key == "class" && attr.Val == "clearfix byline-wrap" {
isStartArticleBody = true
break
}
}
if isStartArticleBody {
break articleStartLoop
}
}
}
}
// find the start of the article
// starts at the end of the article header, ends at the first article paragraph
articleBodyStartLoop:
for {
token := parser.Next()
switch {
case token == html.ErrorToken:
return nil
case token == html.StartTagToken:
tmp := parser.Token()
isStartArticleBody := tmp.Data == "p"
if isStartArticleBody {
break articleBodyStartLoop
}
}
}
// pull the article out of the html
// starts at first paragraph, returns at the end of the article
isInParagraph := true // true because we start inside the first paragraph
depth := 1 // one because this loop starts at first paragraph
for {
token := parser.Next()
switch {
case token == html.ErrorToken:
fmt.Println("hit err, depth is:", depth)
return nil
case token == html.StartTagToken:
depth++
tmp := parser.Token()
isParagraph := tmp.Data == "p"
if isParagraph {
// start of a new paragraph
if depth != 1 {
parser.Next()
tmp = parser.Token()
fmt.Println("ERROR: hit new paragraph while depth == ", depth, ": ", tmp.Data)
}
if isInParagraph {
fmt.Println("ERROR: hit unexpected new paragraph tag while in paragraph")
}
isInParagraph = true
}
// text can have embeded links
isLink := tmp.Data == "a"
if isLink {
if !isInParagraph {
parser.Next()
tmp = parser.Token()
fmt.Println("WARN: hit link outside of a paragraph: ", tmp.Data)
continue
}
// if we are in a paragraph, append the link name
parser.Next()
tmp = parser.Token()
newBody := article.GetData() + tmp.Data
article.SetData(newBody)
}
case token == html.EndTagToken:
depth--
tmp := parser.Token().Data
if depth == -1 {
// done with article when we are at a higher level than it
return nil
}
if tmp == "p" {
// add a paragraph and trim the space
article.SetData(strings.TrimSpace(article.GetData() + "\n"))
isInParagraph = false
}
default:
if !isInParagraph {
// if not inside a text paragraph, continue on
continue
}
// get the paragraph text and append it to the article body
// TODO: look into using a string builder instead of adding things on
tmp := parser.Token()
newBody := article.GetData()
// add a space on the left just in case there is a comment or something
newBody = newBody + strings.TrimSpace(tmp.Data) + " "
article.SetData(newBody)
}
}
}
type WSJRSSChannel struct {
XMLName xml.Name `xml:"channel"`
Articles []WSJArticle `xml:"item"`
}
func (channel *WSJRSSChannel) GetArticle(slot int) Article {
if slot >= channel.GetNumArticles() {
// Check that the request doesn't go out of bounds
// TODO: errors
return nil
}
return &channel.Articles[slot]
}
func (channel *WSJRSSChannel) GetNumArticles() int {
return len(channel.Articles)
}
func (channel *WSJRSSChannel) ClearArticles() bool {
channel.Articles = nil
return true
}
type WSJRSS struct {
XMLName xml.Name `xml:"rss"`
Channel WSJRSSChannel `xml:"channel"`
RSSLink string
// TODO: actually set string to the value of the link
}
func (rss *WSJRSS) GetLink() string { return "http://www.wsj.com/xml/rss/3_7041.xml" }
func (rss *WSJRSS) GetChannel() RSSChannel {
// return a pointer to the channel, interfaces implicitly have ptrs if they are there
tmp := &rss.Channel
return tmp
}
// make sure all the structs implement the interfaces
var _ RSS = (*WSJRSS)(nil)
var _ RSSChannel = (*WSJRSSChannel)(nil)
var _ Article = (*WSJArticle)(nil)