-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathweb.go
125 lines (101 loc) · 2.6 KB
/
web.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"jaytaylor.com/html2text"
)
func fetchWebpage(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(body), nil
}
const HN_SEARCH_URL = "https://hn.algolia.com/api/v1/"
type Comment struct {
Author string `json:"author"`
Text string `json:"text"`
Children []Comment `json:"children"`
}
func sanitize(input string) string {
sanitized, _ := html2text.FromString(input)
return sanitized
}
func safeRequest(url string) *http.Response {
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Failed to fetch URL: %s\n", url)
return nil
}
return resp
}
func fetchComments(storyID string) []string {
resp := safeRequest(HN_SEARCH_URL + "items/" + storyID)
if resp == nil {
return nil
}
defer resp.Body.Close()
var comments map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&comments); err != nil {
fmt.Printf("Failed to decode JSON response\n")
return nil
}
var lines []string
if children, ok := comments["children"].([]interface{}); ok {
for _, child := range children {
childComment := child.(map[string]interface{})
appendComment(childComment, &lines, 0)
}
}
return lines
}
func appendComment(comment map[string]interface{}, lines *[]string, level int) {
indent := "" + strings.Repeat(" ", min(level, 4)*2) + "| "
if author, ok := comment["author"].(string); ok {
*lines = append(*lines, indent+sanitize(author)+" wrote:")
text := sanitize(comment["text"].(string))
paragraphs := strings.Split(text, "\n\n")
for _, paragraph := range paragraphs {
textLines := wrapText(paragraph, indent)
*lines = append(*lines, textLines...)
*lines = append(*lines, indent)
}
*lines = (*lines)[:len(*lines)-1] // Drop the blank line after the last paragraph
} else {
*lines = append(*lines, indent+"[deleted]")
}
*lines = append(*lines, " ")
if children, ok := comment["children"].([]interface{}); ok {
for _, child := range children {
appendComment(child.(map[string]interface{}), lines, level+1)
}
}
}
func wrapText(text, indent string) []string {
words := strings.Fields(text)
var lines []string
var sb strings.Builder
maxWidth := 60
for _, word := range words {
if sb.Len()+len(word)+1 > maxWidth {
lines = append(lines, indent+sb.String())
sb.Reset()
}
if sb.Len() > 0 {
sb.WriteString(" ")
}
sb.WriteString(word)
}
if sb.Len() > 0 {
lines = append(lines, indent+sb.String())
}
return lines
}