-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxrankapi.go
383 lines (286 loc) · 7.72 KB
/
wxrankapi.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/GanEasy/wxrankapi/job"
"github.com/GanEasy/wxrankapi/repository"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
"github.com/yizenghui/reader"
)
type (
//Stats 结构
Stats struct {
Uptime time.Time `json:"uptime"`
RequestCount uint64 `json:"requestCount"`
Statuses map[string]int `json:"statuses"`
mutex sync.RWMutex
}
)
//NewStats New Stats
func NewStats() *Stats {
return &Stats{
Uptime: time.Now(),
Statuses: make(map[string]int),
}
}
// Process is the middleware function.
func (s *Stats) Process(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if err := next(c); err != nil {
c.Error(err)
}
s.mutex.Lock()
defer s.mutex.Unlock()
s.RequestCount++
status := strconv.Itoa(c.Response().Status)
s.Statuses[status]++
return nil
}
}
// Handle is the endpoint to get stats.
func (s *Stats) Handle(c echo.Context) error {
s.mutex.RLock()
defer s.mutex.RUnlock()
return c.JSON(http.StatusOK, s)
}
//Articles 文章接口
func Articles(c echo.Context) error {
limit, _ := strconv.Atoi(c.QueryParam("limit"))
offset, _ := strconv.Atoi(c.QueryParam("offset"))
tag, _ := strconv.Atoi(c.QueryParam("tag"))
if limit <= 0 || limit > 100 {
limit = 10
}
// limit = 10
if offset < 0 || offset > 500 {
offset = 0
}
articles, err := repository.GetArticle(limit, offset, tag)
if err != nil {
}
return c.JSON(http.StatusOK, articles)
}
//NewArticles 最新收录文章接口
func NewArticles(c echo.Context) error {
limit, _ := strconv.Atoi(c.QueryParam("limit"))
tags := repository.Str2Int64(c.QueryParam("tag"))
id, _ := strconv.Atoi(c.QueryParam("id"))
if limit <= 0 || limit > 100 {
limit = 10
}
articles, _ := repository.GetArticleCursorByID(id, limit, tags)
return c.JSON(http.StatusOK, articles)
}
//HotArticles 文章接口 根据热门程序进行游标提取
func HotArticles(c echo.Context) error {
limit, _ := strconv.Atoi(c.QueryParam("limit"))
tags := repository.Str2Int64(c.QueryParam("tag"))
rank, _ := strconv.ParseFloat(c.QueryParam("rank"), 64)
if limit <= 0 || limit > 100 {
limit = 10
}
articles, _ := repository.GetArticleCursorByRank(rank, limit, tags)
return c.JSON(http.StatusOK, articles)
}
//Tags 标签列表接口
func Tags(c echo.Context) error {
t := c.QueryParam("type")
tags, err := repository.GetTagByType(t)
if err != nil {
}
return c.JSON(http.StatusOK, tags)
}
//SearchTags 标签搜索
func SearchTags(c echo.Context) error {
s := c.QueryParam("s")
tags, err := repository.SearchTags(s)
if err != nil {
}
return c.JSON(http.StatusOK, tags)
}
//Search 微信文章内容搜索入口
func Search(c echo.Context) error {
t := c.QueryParam("s")
limit, _ := strconv.Atoi(c.QueryParam("limit"))
if limit <= 0 || limit > 100 {
limit = 10
}
rank, _ := strconv.ParseFloat(c.QueryParam("rank"), 64)
articles, err := repository.GetArticlesByTitle(t, rank, limit)
if err != nil {
}
return c.JSON(http.StatusOK, articles)
}
//Tag 标签详细
func Tag(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
tag, err := repository.Tag(id)
if err != nil {
}
return c.JSON(http.StatusOK, tag)
}
//GetTagByMediaID 通过公众号ID获取标签详细
func GetTagByMediaID(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
tag, err := repository.GetTagByMediaID(id)
if err != nil {
}
return c.JSON(http.StatusOK, tag)
}
//View 阅读
func View(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
article, err := repository.View(id)
if err != nil {
}
return c.JSON(http.StatusOK, article)
}
//Like 喜欢
func Like(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
article, err := repository.Like(id)
if err != nil {
}
return c.JSON(http.StatusOK, article)
}
//Hate 讨厌
func Hate(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
article, err := repository.Hate(id)
if err != nil {
}
return c.JSON(http.StatusOK, article)
}
//Media 公众号
func Media(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
media, err := repository.GetMediaByID(id)
if err != nil {
}
return c.JSON(http.StatusOK, media)
}
//Fetch get 报料接口
func Fetch(c echo.Context) error {
url := c.QueryParam("url")
// fmt.Println(url)
if url != "" {
// repository.Post(url)
// 列队任务, 防止高并发攻击
job.JobQueue <- job.Job{
Task: &job.TaskSpider{
URL: url,
},
}
return c.JSON(http.StatusOK, "1")
}
return c.JSON(http.StatusOK, "0")
}
//JsSDK 微信JS接口
func JsSDK(c echo.Context) error {
url := c.QueryParam("url")
js, _ := repository.GetSign(url)
return c.JSON(http.StatusOK, js)
}
//Post 报料接口
func Post(c echo.Context) error {
url := c.FormValue("url")
// fmt.Println("url", url)
if url != "" {
err := repository.Post(url)
if err != nil {
return c.JSON(http.StatusOK, "0")
}
return c.JSON(http.StatusOK, "1")
}
return c.JSON(http.StatusOK, "0")
}
//GetContent 获取正文
func GetContent(c echo.Context) error {
urlStr := c.QueryParam("url")
info, err := reader.GetContent(urlStr)
if err != nil {
return c.String(http.StatusOK, "0")
}
input := []byte(info.Content)
unsafe := blackfriday.MarkdownCommon(input)
content := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
// html := fmt.Sprintf(`<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
// <link rel="preload" href="https://yize.gitlab.io/css/main.css" as="style" />
// %v`, string(content[:]))
// return c.HTML(http.StatusOK, html)
info.Content = fmt.Sprintf(`%v`, string(content[:]))
// 给图片加上 最大宽度
info.Content = strings.Replace(info.Content, `<img src=`, `<img style="max-width:100%" src=`, -1)
info.Content = strings.Replace(info.Content, `<section>`, `<div>`, -1)
info.Content = strings.Replace(info.Content, `</section>`, `</div>`, -1)
type Info struct {
Title string `json:"title"`
Content template.HTML `json:"content"`
PubAt string `json:"pub_at"`
}
return c.JSON(http.StatusOK, Info{
info.Title,
template.HTML(info.Content),
info.PubAt,
})
}
//GetList 获取列表 临时放在这里面,做小程序测试api
func GetList(c echo.Context) error {
urlStr := c.QueryParam("url")
if urlStr == "" {
return c.JSON(http.StatusOK, "0")
}
links, _ := reader.GetList(urlStr)
return c.JSON(http.StatusOK, links)
}
func main() {
e := echo.New()
e.Use(middleware.CORS())
//-------------------
// Custom middleware
//-------------------
// Stats
s := NewStats()
e.Use(s.Process)
e.GET("/stats", s.Handle) // Endpoint to get stats
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, Welcome to api.readfollow.com !\n")
})
// 请求抓取
e.GET("/fetch", Fetch)
e.POST("/post", Post)
// 获取公众号接口
e.GET("/media/:id", Media)
// 用户查看文章时请求该接口
e.GET("/view/:id", View)
// 赞同文章
e.GET("/like/:id", Like)
// 否定文章 如果否定比赞同多5票,评分为0
e.GET("/hate/:id", Hate)
// 获取微信文章接口
e.GET("/article", Articles)
// 获取微信文章接口
e.GET("/new", NewArticles)
e.GET("/hot", HotArticles)
e.GET("/jssdk", JsSDK)
// 临时做小程序api
e.GET("/minapp/getlist", GetList)
e.GET("/minapp/getcontent", GetContent)
// 获取标签接口
e.GET("/tags", Tags)
e.GET("/searchtags", SearchTags)
e.GET("/tag/:id", Tag)
e.GET("/search", Search)
e.GET("/gettagbymedia/:id", GetTagByMediaID)
e.File("/favicon.ico", "favicon.ico")
e.Logger.Fatal(e.Start(":8005"))
// e.Logger.Fatal(e.StartAutoTLS(":443"))
}