forked from peterhellberg/hn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive.go
140 lines (114 loc) · 3.62 KB
/
live.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
package hn
import "context"
// LiveService communicates with the news
// related pageTypes in the Hacker News API
type LiveService interface {
Stories(context.Context, string) ([]int, error)
MaxItem(context.Context) (int, error)
Updates(context.Context) (*Updates, error)
}
// liveService implements LiveService.
type liveService struct {
client *Client
}
// Updates contains the latest updated items and profiles
type Updates struct {
Items []int `json:"items"`
Profiles []string `json:"profiles"`
}
// TopStories gets the ids of the stories on the Front (Top) page, in order.
func (c *Client) TopStories(ctx context.Context) ([]int, error) {
return c.Live.Stories(ctx, "top")
}
// NewStories gets the ids of the stories on the New page, in order.
func (c *Client) NewStories(ctx context.Context) ([]int, error) {
return c.Live.Stories(ctx, "new")
}
// BestStories gets the ids of the stories on the Best page, in order.
func (c *Client) BestStories(ctx context.Context) ([]int, error) {
return c.Live.Stories(ctx, "best")
}
// AskStories gets the ids of the stories on the Ask page, in order.
func (c *Client) AskStories(ctx context.Context) ([]int, error) {
return c.Live.Stories(ctx, "ask")
}
// ShowStories gets the ids of the stories on the Show page, in order.
func (c *Client) ShowStories(ctx context.Context) ([]int, error) {
return c.Live.Stories(ctx, "show")
}
// JobStories gets the ids of the stories on the Show page, in order.
func (c *Client) JobStories(ctx context.Context) ([]int, error) {
return c.Live.Stories(ctx, "job")
}
// Stories gets the ids of the stories for the given pageType, where pageType is one of "top","new","best","ask","show",or "jobs"
func (c *Client) Stories(ctx context.Context, pageType string) ([]int, error) {
return c.Live.Stories(ctx, pageType)
}
// Stories retrieves the current stories for the given page (where page is one of "top","new","best","ask", or "show")
func (s *liveService) Stories(ctx context.Context, pageType string) ([]int, error) {
req, err := s.client.NewRequest(ctx, s.path(pageType))
if err != nil {
return nil, err
}
var value []int
_, err = s.client.Do(req, &value)
if err != nil {
return nil, err
}
return value, nil
}
func (s *liveService) path(pageType string) string {
validPageTypes := map[string]string{
"top": "topstories.json",
"new": "newstories.json",
"best": "beststories.json",
"ask": "askstories.json",
"show": "showstories.json",
"job": "jobstories.json",
}
path, ok := validPageTypes[pageType]
if !ok {
panic("Invalid pageType: " + pageType)
}
return path
}
// MaxItem is a convenience method proxying Live.MaxItem
func (c *Client) MaxItem(ctx context.Context) (int, error) {
return c.Live.MaxItem(ctx)
}
// MaxItem retrieves the current largest item id
func (s *liveService) MaxItem(ctx context.Context) (int, error) {
req, err := s.client.NewRequest(ctx, s.maxItemPath())
if err != nil {
return 0, err
}
var value int
_, err = s.client.Do(req, &value)
if err != nil {
return 0, err
}
return value, nil
}
func (s *liveService) maxItemPath() string {
return "maxitem.json"
}
// Updates is a convenience method proxying Live.Updates
func (c *Client) Updates(ctx context.Context) (*Updates, error) {
return c.Live.Updates(ctx)
}
// Updates retrieves the current largest item id
func (s *liveService) Updates(ctx context.Context) (*Updates, error) {
req, err := s.client.NewRequest(ctx, s.updatesPath())
if err != nil {
return nil, err
}
var value Updates
_, err = s.client.Do(req, &value)
if err != nil {
return nil, err
}
return &value, nil
}
func (s *liveService) updatesPath() string {
return "updates.json"
}