-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqiita-trends-rss.go
113 lines (98 loc) · 2.52 KB
/
qiita-trends-rss.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"path"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/feeds"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
// TrendData qiitaのトレンド
type TrendData struct {
Trend struct {
Edges []struct {
Node struct {
CreatedAt string `json:"createdAt"`
Title string `json:"title"`
UUID string `json:"uuid"`
Author struct {
ProfileImageURL string `json:"profileImageUrl"`
URLName string `json:"urlName"`
} `json:"author"`
} `json:"node"`
} `json:"edges"`
} `json:"trend"`
}
const (
trendURL = "https://qiita.com"
internalServerError = "internal server error"
)
var port = flag.Int("port", 1234, "port mumber")
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", handleFeed)
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", *port)))
}
func handleFeed(c echo.Context) error {
res, err := http.Get(trendURL)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
feedItem := []*feeds.Item{}
doc.Find("div[data-hyperapp-app=Trend]").Each(func(i int, s *goquery.Selection) {
text, exists := s.Attr("data-hyperapp-props")
if !exists {
return
}
trends := &TrendData{}
err := json.Unmarshal([]byte(text), trends)
if err != nil {
return
}
for _, item := range trends.Trend.Edges {
url := url.URL{
Scheme: "https",
Host: "qiita.com",
Path: path.Join(item.Node.Author.URLName, "items", item.Node.UUID),
}
created, _ := time.Parse(time.RFC3339, item.Node.CreatedAt)
feedItem = append(feedItem, &feeds.Item{
Title: item.Node.Title,
Link: &feeds.Link{Href: url.String()},
Description: item.Node.Title,
Author: &feeds.Author{Name: item.Node.Author.URLName, Email: ""},
Created: created,
})
}
})
feed := &feeds.Feed{
Title: "Qiita",
Link: &feeds.Link{Href: "https://qiita.com/trend"},
Description: "Qiita trends",
Updated: time.Now(),
Items: feedItem,
}
text, err := feed.ToRss()
if err != nil {
c.Logger().Printf("%#v", err)
return c.String(http.StatusInternalServerError, internalServerError)
}
c.Response().Header().Set("Content-Type", "text/xml; charset=utf-8")
return c.String(http.StatusOK, text)
}