-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
173 lines (144 loc) · 3.09 KB
/
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"cmp"
"fmt"
"net/url"
"slices"
"strings"
"sync"
"time"
"golang.org/x/net/html"
)
type Item struct {
Rank int
By string `json:"by"`
ID int `json:"id"`
Kids []int `json:"kids"`
Time int64 `json:"time"`
Title string `json:"title"`
Type string `json:"type"`
Comments []*Comment
mu sync.RWMutex
}
func (i *Item) AddComment(c *Comment) {
i.mu.Lock()
defer i.mu.Unlock()
i.Comments = append(i.Comments, c)
slices.SortFunc(i.Comments, func(i, j *Comment) int {
return cmp.Compare(i.Rank, j.Rank)
})
}
func humanize(t time.Time) string {
d := time.Since(t)
switch {
case d < time.Minute:
return "just now"
case d < time.Hour:
return fmt.Sprintf("%d minutes ago", int(d.Minutes()))
case d < time.Hour*24:
return fmt.Sprintf("%d hours ago", int(d.Hours()))
default:
return fmt.Sprintf("%d days ago", int(d.Hours()/24))
}
}
func HTMLText(t string) string {
root, err := html.Parse(strings.NewReader(html.UnescapeString(t)))
if err != nil {
panic(err)
}
var sb strings.Builder
type state struct {
element string
attributes map[string]string
}
var fn func(state, *html.Node)
fn = func(s state, n *html.Node) {
switch n.Type {
case html.TextNode:
text := n.Data
switch s.element {
case "a":
if val, ok := s.attributes["href"]; ok {
text = fmt.Sprintf("(%s %s)", n.Data, val)
if n.Data == val {
text = val
} else if trim := strings.TrimSuffix(n.Data, "..."); strings.HasPrefix(val, trim) {
// HN truncates long links and appends "..."
text = val
}
}
}
sb.WriteString(text)
case html.ElementNode:
switch n.Data {
case "a":
s.element = n.Data
s.attributes = make(map[string]string)
for _, attr := range n.Attr {
// discard Namespace
s.attributes[attr.Key] = attr.Val
}
case "p":
sb.WriteString("\n\n")
}
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
fn(s, child)
}
}
fn(state{}, root)
return sb.String()
}
type Story struct {
*Item
Descendants int `json:"descendants"`
Score int `json:"score"`
Text string `json:"text"`
URL string `json:"url"`
}
func NewStory(Rank int) *Story {
return &Story{
Item: &Item{
Rank: Rank,
},
}
}
func (s Story) FilterValue() string {
return s.Title()
}
func (s Story) Title() string {
var sb strings.Builder
fmt.Fprintf(&sb, "%d. %s", s.Rank+1, s.Item.Title)
if s.URL != "" {
link, err := url.Parse(s.URL)
if err != nil {
panic(err)
}
fmt.Fprintf(&sb, " (%s)", link.Host)
}
return sb.String()
}
func (s Story) Description() string {
var prefix string
switch {
case s.Rank < 10:
prefix = strings.Repeat(" ", 3)
case s.Rank < 100:
prefix = strings.Repeat(" ", 4)
default:
prefix = strings.Repeat(" ", 5)
}
return fmt.Sprintf("%s%d points by %s %s | %d comments", prefix, s.Score, s.By, humanize(time.Unix(s.Time, 0)), s.Descendants)
}
type Comment struct {
*Item
Parent int `json:"parent"`
Text string `json:"text"`
}
func NewComment(rank int) *Comment {
return &Comment{
Item: &Item{
Rank: rank,
},
}
}