-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
95 lines (79 loc) · 1.98 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"net/http"
"github.com/gocolly/colly"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
// uses https://github.com/HackerNews/API#new-top-and-best-stories api to get newest stories (https://news.ycombinator.com/newest)
func getHnNewestPostsIds() ([]int, error) {
resp, err := http.Get("https://hacker-news.firebaseio.com/v0/newstories.json?print=pretty")
if err != nil {
return nil, err
}
defer resp.Body.Close()
var ids []int
err = json.NewDecoder(resp.Body).Decode(&ids)
if err != nil {
return nil, err
}
return ids, nil
}
// Link represents a link
type Link struct {
title string
url string
}
func processHnPost(id string) []Link {
c := colly.NewCollector()
var posts []Link
c.OnHTML("span.titleline > a[href]", func(e *colly.HTMLElement) {
post := Link{
title: e.Text,
url: e.Attr("href"),
}
posts = append(posts, post)
})
hnURL := fmt.Sprintf("https://news.ycombinator.com/item?id=%s", id)
c.Visit(hnURL)
return posts
}
func processLobstersPost(id string) {
}
// get all urls from https://lobste.rs/newest
func getLobstersNewestUrls(last int) []string {
c := colly.NewCollector()
var links []string
// find and visit all links
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
links = append(links, link)
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("visiting", r.URL)
})
c.Visit("https://lobste.rs/newest")
return links
}
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Get("/hn-newest/{last}", func(w http.ResponseWriter, r *http.Request) {
lastStr := chi.URLParam(r, "last")
last, err := strconv.Atoi(lastStr)
if err != nil {
http.Error(w, "Invalid parameter", http.StatusBadRequest)
return
}
links := getLobstersNewestUrls(last)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(links)
})
if err := http.ListenAndServe(":3000", r); err != nil {
log.Fatal(err)
}
}