-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhackernoon.go
More file actions
370 lines (331 loc) · 9.62 KB
/
Copy pathhackernoon.go
File metadata and controls
370 lines (331 loc) · 9.62 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Package hackernoon is the library behind the hn2 command line:
// the HTTP client, request shaping, and the typed data models for Hackernoon.
//
// Data comes from the Hackernoon RSS feed (https://hackernoon.com/feed) and
// per-tag feeds (https://hackernoon.com/tagged/<tag>/feed). Both are public
// and require no API key.
package hackernoon
import (
"context"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
// DefaultUserAgent identifies the client to Hackernoon.
const DefaultUserAgent = "hn2/dev (+https://github.com/tamnd/hackernoon-cli)"
// ErrNotFound is returned when a resource cannot be located.
var ErrNotFound = errors.New("not found")
// Config holds constructor parameters.
type Config struct {
BaseURL string
UserAgent string
Rate time.Duration
Retries int
Timeout time.Duration
}
// DefaultConfig returns sensible defaults.
func DefaultConfig() Config {
return Config{
BaseURL: "https://hackernoon.com",
UserAgent: DefaultUserAgent,
Rate: 200 * time.Millisecond,
Retries: 5,
Timeout: 30 * time.Second,
}
}
// Client talks to Hackernoon over HTTP.
type Client struct {
http *http.Client
baseURL string
userAgent string
rate time.Duration
retries int
mu sync.Mutex
last time.Time
}
// NewClient returns a Client with the given config.
func NewClient(cfg Config) *Client {
if cfg.BaseURL == "" {
cfg.BaseURL = DefaultConfig().BaseURL
}
if cfg.UserAgent == "" {
cfg.UserAgent = DefaultConfig().UserAgent
}
if cfg.Timeout == 0 {
cfg.Timeout = DefaultConfig().Timeout
}
return &Client{
http: &http.Client{Timeout: cfg.Timeout},
baseURL: strings.TrimRight(cfg.BaseURL, "/"),
userAgent: cfg.UserAgent,
rate: cfg.Rate,
retries: cfg.Retries,
}
}
// get fetches a URL with pacing and retries.
func (c *Client) get(ctx context.Context, rawURL string) ([]byte, error) {
var lastErr error
for attempt := 0; attempt <= c.retries; attempt++ {
if attempt > 0 {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(backoff(attempt)):
}
}
body, retry, err := c.do(ctx, rawURL)
if err == nil {
return body, nil
}
lastErr = err
if !retry {
return nil, err
}
}
return nil, fmt.Errorf("get %s: %w", rawURL, lastErr)
}
func (c *Client) do(ctx context.Context, rawURL string) ([]byte, bool, error) {
c.pace()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return nil, false, err
}
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Accept", "application/rss+xml, application/xml, text/xml, */*")
resp, err := c.http.Do(req)
if err != nil {
return nil, true, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode >= 500 {
return nil, true, fmt.Errorf("http %d", resp.StatusCode)
}
if resp.StatusCode != http.StatusOK {
return nil, false, fmt.Errorf("http %d", resp.StatusCode)
}
b, err := io.ReadAll(io.LimitReader(resp.Body, 8<<20))
if err != nil {
return nil, true, err
}
return b, false, nil
}
func (c *Client) pace() {
c.mu.Lock()
defer c.mu.Unlock()
if c.rate <= 0 {
return
}
if wait := c.rate - time.Since(c.last); wait > 0 {
time.Sleep(wait)
}
c.last = time.Now()
}
func backoff(attempt int) time.Duration {
d := time.Duration(attempt) * 500 * time.Millisecond
if d > 5*time.Second {
d = 5 * time.Second
}
return d
}
// ─── RSS wire types ───────────────────────────────────────────────────────────
type rssFeed struct {
XMLName xml.Name `xml:"rss"`
Channel rssChannel `xml:"channel"`
}
type rssChannel struct {
Items []rssItem `xml:"item"`
}
type rssItem struct {
Title string `xml:"title"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Description string `xml:"description"`
Creator string `xml:"creator"`
Categories []string `xml:"category"`
Content string `xml:"encoded"`
}
func (c *Client) fetchFeed(ctx context.Context, feedURL string, limit int) ([]Story, error) {
body, err := c.get(ctx, feedURL)
if err != nil {
return nil, err
}
var feed rssFeed
if err := xml.Unmarshal(body, &feed); err != nil {
return nil, fmt.Errorf("parse feed %s: %w", feedURL, err)
}
items := feed.Channel.Items
if limit > 0 && limit < len(items) {
items = items[:limit]
}
out := make([]Story, 0, len(items))
for i, it := range items {
out = append(out, rssItemToStory(&it, i+1))
}
return out, nil
}
func rssItemToStory(it *rssItem, rank int) Story {
summary := stripTags(it.Description)
if len([]rune(summary)) > 200 {
rs := []rune(summary)
summary = string(rs[:199]) + "…"
}
tags := make([]string, 0, len(it.Categories))
seen := map[string]bool{}
for _, cat := range it.Categories {
cat = strings.TrimSpace(cat)
if cat != "" && !seen[cat] {
seen[cat] = true
tags = append(tags, cat)
}
}
link := strings.TrimSuffix(it.Link, "?source=rss")
return Story{
Rank: rank,
Title: stripCDATA(it.Title),
Author: stripCDATA(it.Creator),
Date: parseRSSDate(it.PubDate),
Summary: summary,
Tags: strings.Join(tags, ","),
URL: link,
}
}
// ─── Latest ──────────────────────────────────────────────────────────────────
// Latest returns the most recent stories from the main feed.
func (c *Client) Latest(ctx context.Context, limit int) ([]Story, error) {
return c.fetchFeed(ctx, c.baseURL+"/feed", limit)
}
// ─── Search ──────────────────────────────────────────────────────────────────
// Search filters the main feed for stories matching the query string (title,
// tags, author, or summary). If no matches are found it falls back to the
// per-tag feed for the first query word.
func (c *Client) Search(ctx context.Context, query string, limit int) ([]Story, error) {
// Try main feed first
stories, err := c.Latest(ctx, 0)
if err != nil {
return nil, err
}
q := strings.ToLower(strings.TrimSpace(query))
var matched []Story
for _, s := range stories {
if storyMatchesQuery(s, q) {
matched = append(matched, s)
}
}
// If no in-feed hits, try the per-tag feed for the first query word
if len(matched) == 0 {
word := strings.Fields(q)[0]
tagFeed := c.baseURL + "/tagged/" + url.PathEscape(word) + "/feed"
tagStories, tagErr := c.fetchFeed(ctx, tagFeed, 0)
if tagErr == nil {
for _, s := range tagStories {
if storyMatchesQuery(s, q) {
matched = append(matched, s)
}
}
// if still nothing, return everything from the tag feed
if len(matched) == 0 {
matched = tagStories
}
}
}
if limit > 0 && limit < len(matched) {
matched = matched[:limit]
}
// re-rank
for i := range matched {
matched[i].Rank = i + 1
}
return matched, nil
}
func storyMatchesQuery(s Story, q string) bool {
haystack := strings.ToLower(s.Title + " " + s.Tags + " " + s.Author + " " + s.Summary)
for _, word := range strings.Fields(q) {
if !strings.Contains(haystack, word) {
return false
}
}
return true
}
// ─── Trending ─────────────────────────────────────────────────────────────────
// Trending returns stories from the feed with the most tags (as a proxy for
// popular/trending content). Falls back to latest when the feed is short.
func (c *Client) Trending(ctx context.Context, limit int) ([]Story, error) {
stories, err := c.Latest(ctx, 0)
if err != nil {
return nil, err
}
// Sort by tag count descending (more tags = more cross-topic = more popular)
sorted := make([]Story, len(stories))
copy(sorted, stories)
sortByTagCount(sorted)
if limit > 0 && limit < len(sorted) {
sorted = sorted[:limit]
}
for i := range sorted {
sorted[i].Rank = i + 1
}
return sorted, nil
}
// sortByTagCount is a simple insertion sort (feed is small, <=20 items).
func sortByTagCount(ss []Story) {
for i := 1; i < len(ss); i++ {
key := ss[i]
keyCount := strings.Count(key.Tags, ",")
j := i - 1
for j >= 0 && strings.Count(ss[j].Tags, ",") < keyCount {
ss[j+1] = ss[j]
j--
}
ss[j+1] = key
}
}
// ─── helpers ─────────────────────────────────────────────────────────────────
func parseRSSDate(s string) string {
formats := []string{
time.RFC1123Z,
time.RFC1123,
"Mon, 02 Jan 2006 15:04:05 -0700",
"Mon, 2 Jan 2006 15:04:05 -0700",
"Mon, 02 Jan 2006 15:04:05 MST",
}
for _, f := range formats {
if t, err := time.Parse(f, s); err == nil {
return t.UTC().Format(time.RFC3339)
}
}
return s
}
func stripCDATA(s string) string {
s = strings.TrimPrefix(s, "<![CDATA[")
s = strings.TrimSuffix(s, "]]>")
return strings.TrimSpace(s)
}
func stripTags(s string) string {
var b strings.Builder
inTag := false
for _, r := range s {
switch {
case r == '<':
inTag = true
case r == '>':
inTag = false
case !inTag:
b.WriteRune(r)
}
}
out := b.String()
out = strings.ReplaceAll(out, "&", "&")
out = strings.ReplaceAll(out, "<", "<")
out = strings.ReplaceAll(out, ">", ">")
out = strings.ReplaceAll(out, """, `"`)
out = strings.ReplaceAll(out, "'", "'")
out = strings.ReplaceAll(out, "'", "'")
out = strings.TrimSpace(out)
return out
}