forked from hnrss/hnrss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonfeed.go
58 lines (50 loc) · 1.6 KB
/
jsonfeed.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
package main
import "fmt"
const hackerNewsAuthorFormatStr = "https://news.ycombinator.com/user?id=%s"
// https://jsonfeed.org/version/1
type JSONFeed struct {
Version string `json:"version"`
Title string `json:"title"`
Description string `json:"description"`
Link string `json:"home_page_url"`
Items []JSONFeedItem `json:"items"`
}
type JSONFeedItem struct {
ID string `json:"id"`
Title string `json:"title"`
ContentHTML string `json:"content_html,omitempty"`
URL string `json:"url"`
ExternalURL string `json:"external_url"`
Published string `json:"date_published"`
Author JSONFeedItemAuthor `json:"author"`
}
type JSONFeedItemAuthor struct {
Name string `json:"name"`
URL string `json:"url"`
}
func NewJSONFeed(results *AlgoliaSearchResponse, op *OutputParams) *JSONFeed {
j := JSONFeed{
Version: "https://jsonfeed.org/version/1",
Title: op.Title,
Link: op.Link,
Description: "Hacker News RSS",
}
for _, hit := range results.Hits {
item := JSONFeedItem{
ID: hit.GetPermalink(),
Title: hit.GetTitle(),
URL: hit.GetURL(op.LinkTo),
ExternalURL: hit.GetPermalink(),
Published: Timestamp("jsonfeed", hit.GetCreatedAt()),
Author: JSONFeedItemAuthor{
Name: hit.Author,
URL: fmt.Sprintf(hackerNewsAuthorFormatStr, hit.Author),
},
}
if op.Description != descriptionDisabledFlag {
item.ContentHTML = hit.GetDescription()
}
j.Items = append(j.Items, item)
}
return &j
}