-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscraper.go
225 lines (174 loc) · 5.41 KB
/
scraper.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
package main
import (
"log"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/yhat/scrape"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var entryMatcher func(n *html.Node) bool
var authorMatcher func(n *html.Node) bool
var dateMatcher func(n *html.Node) bool
var entryListMatcher func(n *html.Node) bool
var topicListMatcher func(n *html.Node) bool
var contentMatcher func(n *html.Node) bool
func init() {
entryListMatcher = func(n *html.Node) bool {
return strings.Contains(scrape.Attr(n, "id"), "entry-item-list")
}
entryMatcher = func(n *html.Node) bool {
return strings.Contains(scrape.Attr(n, "class"), "content")
}
authorMatcher = func(n *html.Node) bool {
return strings.Contains(scrape.Attr(n, "class"), "entry-author")
}
dateMatcher = func(n *html.Node) bool {
return strings.Contains(scrape.Attr(n, "class"), "entry-date")
}
topicListMatcher = func(n *html.Node) bool {
return strings.Contains(scrape.Attr(n, "class"), "topic-list")
}
contentMatcher = func(n *html.Node) bool {
return strings.Contains(scrape.Attr(n, "id"), "content-body")
}
}
// GetEntries gets string for topic with parameters and returns a list of entries
func GetEntries(text string, parameter Parameter) []Entry {
baseURL := "https://eksisozluk.com/?q=" + url.QueryEscape(text)
resp, err := http.Get(baseURL)
if err != nil {
panic(err)
}
redirectedURL := resp.Request.URL.String()
entryList := make([]Entry, 0)
startPage := parameter.PageNumber
for parameter.Limit > len(entryList) {
// with similar title (by Turkish characters for example) eksisozluk redirects page
// to a valid url already containts some query parameters -> ?nr=true&rf=...
paginationURL := redirectedURL
if startPage != 0 {
if strings.Contains(redirectedURL, "?") {
paginationURL += "&p=" + strconv.Itoa(startPage)
} else {
paginationURL += "?p=" + strconv.Itoa(startPage)
}
}
if parameter.Sukela {
paginationURL = paginationURL + "&a=nice"
}
additionalEntryList := getEntries(paginationURL)
if len(additionalEntryList) == 0 {
break
}
if len(entryList)+len(additionalEntryList) > parameter.Limit {
entryList = append(entryList, additionalEntryList[0:(parameter.Limit-len(entryList))]...)
} else {
entryList = append(entryList, additionalEntryList...)
}
startPage = startPage + 1
}
return entryList
}
// GetPopularTopics gets parameters and returns a list of topics
func GetPopularTopics(parameter Parameter) []Topic {
baseURL := "https://eksisozluk.com/basliklar/gundem"
topicList := make([]Topic, 0)
startPage := parameter.PageNumber
for parameter.Limit > len(topicList) {
paginationURL := baseURL
if startPage != 0 {
paginationURL = baseURL + "?p=" + strconv.Itoa(startPage)
}
additionalTopicList := getTopics(paginationURL)
if len(additionalTopicList) == 0 {
break
}
if len(topicList)+len(additionalTopicList) > parameter.Limit {
topicList = append(topicList, additionalTopicList[0:(parameter.Limit-len(topicList))]...)
} else {
topicList = append(topicList, additionalTopicList...)
}
startPage = startPage + 1
}
return topicList
}
func getEntries(eksiURL string) []Entry {
log.Println("URL to check: " + eksiURL)
resp, err := http.Get(eksiURL)
if err != nil {
panic(err)
}
root, err := html.Parse(resp.Body)
if err != nil {
panic(err)
}
entryList := make([]Entry, 0)
entryListNode, found := scrape.Find(root, entryListMatcher)
if found == false {
return entryList
}
scrapedEntries := scrape.FindAll(entryListNode, entryMatcher)
if len(scrapedEntries) == 0 {
return entryList
}
for _, scrappedEntry := range scrapedEntries {
authorNode, authorCheck := scrape.Find(scrappedEntry.Parent, authorMatcher)
dateNode, dateCheck := scrape.Find(scrappedEntry.Parent, dateMatcher)
entry := Entry{}
entry.Text = scrape.Text(scrappedEntry)
if authorCheck {
entry.Author = scrape.Text(authorNode)
}
if dateCheck {
idDate := scrape.Text(dateNode)
splitted := strings.SplitAfterN(idDate, " ", 2)
if len(splitted) > 1 {
entry.ID = strings.TrimSpace(splitted[0])
entry.Date = strings.TrimSpace(splitted[1])
} else {
// No time info is found (it can happen on old entries)
entry.ID = strings.TrimSpace("00:00")
entry.Date = strings.TrimSpace(splitted[0])
}
}
entryList = append(entryList, entry)
}
return entryList
}
func getTopics(topicURL string) []Topic {
log.Println("Checking URL: " + topicURL)
topicList := make([]Topic, 0)
resp, err := http.Get(topicURL)
if err != nil {
panic(err)
}
root, err := html.Parse(resp.Body)
if err != nil {
panic(err)
}
contentNode, ok := scrape.Find(root, contentMatcher)
if !ok {
panic("Not found any content!")
}
topicListNode := scrape.FindAll(contentNode, topicListMatcher)
if !ok {
panic("Not found any topic list!")
}
topicLists := scrape.FindAll(topicListNode[1], scrape.ByTag(atom.Li))
for _, topicNode := range topicLists {
topic := Topic{}
topicLink, _ := scrape.Find(topicNode, scrape.ByTag(atom.A))
topic.Link = "https://eksisozluk.com" + scrape.Attr(topicLink, "href")
titleAndCount := scrape.Text(topicNode)
countIndex := strings.LastIndex(titleAndCount, " ")
topic.Title = strings.TrimSpace(titleAndCount[0:countIndex])
countString := titleAndCount[countIndex:]
topicCountInt, _ := strconv.Atoi(strings.TrimSpace(countString))
topic.Count = int64(topicCountInt)
topicList = append(topicList, topic)
}
return topicList
}