-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_hackernews_item.go
62 lines (51 loc) · 1.5 KB
/
table_hackernews_item.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
package hackernews
import (
"context"
"io/ioutil"
"net/http"
"strconv"
"github.com/PaulRosset/go-hacknews"
"github.com/turbot/steampipe-plugin-sdk/v3/plugin"
)
func tableHackernewsItem(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "hackernews_item",
Description: "Stories, comments, jobs, Ask HNs and even polls are just items. This table includes the most recent items posted to Hacker News.",
List: &plugin.ListConfig{
Hydrate: itemList,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("id"),
Hydrate: getItem,
},
Columns: itemCols(),
}
}
func itemList(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
resp, err := http.Get("https://hacker-news.firebaseio.com/v0/maxitem.json")
if err != nil {
plugin.Logger(ctx).Error("hackernews_item.itemList", "query_error", err)
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
plugin.Logger(ctx).Error("hackernews_item.itemList", "read_body_error", err)
return nil, err
}
max64, err := strconv.ParseInt(string(body), 10, 0)
if err != nil {
plugin.Logger(ctx).Error("hackernews_item.itemList", "parse_max_item_error", err)
return nil, err
}
max := int(max64)
// default to 5000 max items, but settable in config
limit := 5000
config := GetConfig(d.Connection)
if config.MaxItems != nil {
limit = *config.MaxItems
}
for i := max; i >= max-limit; i-- {
d.StreamListItem(ctx, &hacknews.Post{Id: i})
}
return nil, nil
}