-
Notifications
You must be signed in to change notification settings - Fork 3
/
hn.go
66 lines (57 loc) · 1.61 KB
/
hn.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
package main
import (
"fmt"
hn "github.com/munrocape/hn/hnclient"
"strings"
"time"
)
var (
HnClient *hn.Client
currentHnResponse string
currentHnTimestamp time.Time
)
func getHnTop10() (string, error) {
var err error
if ExpiredResponse(currentHnTimestamp) {
currentHnResponse, err = generateNewHnResponse()
}
return currentHnResponse, err
}
func generateNewHnResponse() (string, error) {
c := getHnClient()
var stories []int
count := 10
stories, err := c.GetTopStories(count)
if err != nil {
return "", err
}
var urls [11]string
urls[0] = "Top Stories from <www.news.ycombinator.com|Hacker News>"
for index, element := range stories[:count] {
index = index + 1
item, err := c.GetItem(element)
if err == nil {
if item.Type == "story" {
if item.Url == "" {
// It is an AskHN post
urls[index] = fmt.Sprintf("%d. <https://news.ycombinator.com/item?id=%d|%s> - [<https://news.ycombinator.com/item?id=%d|%d comments>]", index, element, item.Title, element, item.Descendants)
} else {
urls[index] = fmt.Sprintf("%d. <%s|%s> - [<https://news.ycombinator.com/item?id=%d|%d comments>]", index, item.Url, item.Title, element, item.Descendants)
}
} else {
urls[index] = fmt.Sprintf("%d. <https://news.ycombinator.com/item?id=%d|%s>", index, element, item.Title)
}
} else {
urls[index] = "Server Error - Firebase did not return the story information."
}
}
response := strings.Join(urls[:], "\n")
currentHnTimestamp = time.Now().Local()
return response, nil
}
func getHnClient() *hn.Client {
if HnClient == nil {
HnClient = hn.NewClient()
}
return HnClient
}