-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl_controller.go
237 lines (197 loc) · 4.88 KB
/
crawl_controller.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
233
234
235
236
237
package controllers
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/gorilla/mux"
"github.com/gosu-team/fptu-api/config"
"github.com/gosu-team/fptu-api/lib"
)
// MediumURLs ...
var MediumURLs = []string{"https://codeburst.io",
"https://medium.freecodecamp.org",
"https://hackernoon.com",
"https://medium.com/javascript-scene",
"https://medium.com/dev-channel"}
// FPTURLs ...
var FPTURLs = []string{"https://daihoc.fpt.edu.vn"}
// CodeDaoURLS ...
var CodeDaoURLS = []string{"https://toidicodedao.com", "https://codeaholicguy.com/"}
// Feed ...
type Feed struct {
Title string `json:"title"`
Link string `json:"link"`
Author string `json:"author"`
Description string `json:"description"`
Image string `json:"image"`
}
// Item ...
type Item struct {
Title string `json:"title"`
PubDate string `json:"pubDate"`
Link string `json:"link"`
GUID string `json:"guid"`
Author string `json:"author"`
Thumbnail string `json:"thumbnail"`
Description string `json:"description"`
Content string `json:"content"`
Categories []string `json:"categories"`
}
// MiniItem ...
type MiniItem struct {
Title string `json:"title"`
PubDate string `json:"pubDate"`
GUID string `json:"guid"`
Thumbnail string `json:"thumbnail"`
Description string `json:"description"`
}
// FeedReponse ...
type FeedReponse struct {
Status string `json:"status"`
Feed Feed `json:"feed"`
Items []Item `json:"items"`
}
// MiniFeedReponse ...
type MiniFeedReponse struct {
Items []MiniItem `json:"items"`
}
func getFeeds(body []byte) (*FeedReponse, error) {
var f = new(FeedReponse)
err := json.Unmarshal(body, &f)
if err != nil {
fmt.Println("Whoops:", err)
}
return f, err
}
func minimizeItems(items []Item) []MiniItem {
var miniItems []MiniItem
for _, v := range items {
miniItems = append(miniItems, MiniItem{
Title: v.Title,
PubDate: v.PubDate,
GUID: v.GUID,
Thumbnail: v.Thumbnail,
Description: v.Description,
})
}
return miniItems
}
func resolveMediumURL(url string) string {
urlParts := strings.Split(url, "/")
mediumChannel := urlParts[3]
return "https://medium.com/feed/" + mediumChannel
}
func getFeedFromURL(url string) *FeedReponse {
if strings.Contains(url, "https://medium.com/") {
url = resolveMediumURL(url)
} else {
url = url + "/feed"
}
// Get and parse API
apiKey := os.Getenv("API_KEY")
resp, err := http.Get("http://api.rss2json.com/v1/api.json?rss_url=" + url + "&api_key=" + apiKey + "&count=10&order_by=pubDate")
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
feed, err := getFeeds([]byte(body))
return feed
}
func findArticleByID(name string, items []Item, id string) int {
for index, v := range items {
switch name {
case "medium":
if v.GUID == "https://medium.com/p/"+id {
return index
}
case "codedao":
if v.GUID == "http://toidicodedao.com/?p="+id || v.GUID == "http://codeaholicguy.com/?p="+id {
return index
}
case "fpt":
if v.GUID == "https://daihoc.fpt.edu.vn/?p="+id+"/" {
return index
}
default:
if v.GUID == id {
return index
}
}
}
return -1
}
func getDataFromURLs(urlArr []string) []Item {
var itemCrawl []Item
for index := range urlArr {
crawl := getFeedFromURL(urlArr[index])
itemCrawl = append(itemCrawl, crawl.Items...)
}
return itemCrawl
}
func getDataFromSite(name string) []Item {
// Reference to system cache
c := config.GetCache()
defaultExpiration := config.GetDefaultExpiration()
var urls []string
switch name {
case "medium":
urls = MediumURLs
case "codedao":
urls = CodeDaoURLS
case "fpt":
urls = FPTURLs
default:
urls = MediumURLs
}
// Check cache and use data from cache
var articles []Item
articleGot, found := c.Get(name)
if found {
articles, _ = articleGot.([]Item)
} else {
articlesCache := getDataFromURLs(urls)
c.Set(name, articlesCache, defaultExpiration)
articles = articlesCache
}
return articles
}
// GetHomeFeedHandler ...
func GetHomeFeedHandler(w http.ResponseWriter, r *http.Request) {
res := lib.Response{ResponseWriter: w}
vars := mux.Vars(r)
name := vars["name"]
if len(name) == 0 {
res.SendNotFound()
return
}
articles := getDataFromSite(name)
if len(articles) == 0 {
res.SendBadRequest("Cannot crawl this page")
return
}
res.SendOK(minimizeItems(articles))
}
// GetPostFeedHandler ...
func GetPostFeedHandler(w http.ResponseWriter, r *http.Request) {
res := lib.Response{ResponseWriter: w}
vars := mux.Vars(r)
name := vars["name"]
id := vars["id"]
if len(name) == 0 || len(id) == 0 {
res.SendNotFound()
return
}
articles := getDataFromSite(name)
index := findArticleByID(name, articles, id)
if index == -1 {
res.SendBadRequest("Cannot found that post")
return
}
res.SendOK(articles[index])
}