-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
180 lines (149 loc) · 4.2 KB
/
helpers.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
174
175
176
177
178
179
180
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path"
"slices"
"sort"
"strings"
"time"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)
var validSchemas = []string{"https", "http"}
func UrlJoin(baseUrl string, elem ...string) (result string, err error) {
u, err := url.Parse(baseUrl)
if err != nil {
return
}
if len(elem) > 0 {
elem = append([]string{u.Path}, elem...)
u.Path = path.Join(elem...)
}
return u.String(), nil
}
func IsValidHttpUrl(rawUrl string) bool {
parsedUrl, err := url.ParseRequestURI(rawUrl)
if parsedUrl == nil {
return false
}
if err != nil || !slices.Contains(validSchemas, parsedUrl.Scheme) {
return false
}
return true
}
func IconUrlExists(url string) bool {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("[DEBUG] %v", err)
return false
}
ctx, cancel := context.WithTimeout(req.Context(), 2*time.Second)
defer cancel()
req = req.WithContext(ctx)
client := http.DefaultClient
res, err := client.Do(req)
if err != nil {
log.Printf("[ERROR] %v", err)
return false
} else if res.StatusCode >= 300 {
log.Printf("[ERROR] status code: %d", res.StatusCode)
return false
}
return true
}
func GetConverterRules() []md.Rule {
return []md.Rule{
{
Filter: []string{"h1", "h2", "h3", "h4", "h5", "h6"},
Replacement: func(content string, selection *goquery.Selection, opt *md.Options) *string {
content = strings.TrimSpace(content)
return md.String(content)
},
},
{
Filter: []string{"img"},
AdvancedReplacement: func(content string, selec *goquery.Selection, opt *md.Options) (md.AdvancedResult, bool) {
src := selec.AttrOr("src", "")
src = strings.TrimSpace(src)
if src == "" {
return md.AdvancedResult{
Markdown: "",
}, false
}
src = opt.GetAbsoluteURL(selec, src, "")
text := fmt.Sprintf("\n%s\n", src)
return md.AdvancedResult{
Markdown: text,
}, false
},
},
{
Filter: []string{"a"},
AdvancedReplacement: func(content string, selec *goquery.Selection, opt *md.Options) (md.AdvancedResult, bool) {
// if there is no href, no link is used. So just return the content inside the link
href, ok := selec.Attr("href")
if !ok || strings.TrimSpace(href) == "" || strings.TrimSpace(href) == "#" {
return md.AdvancedResult{
Markdown: content,
}, false
}
href = opt.GetAbsoluteURL(selec, href, "")
// having multiline content inside a link is a bit tricky
content = md.EscapeMultiLine(content)
// if there is no link content (for example because it contains an svg)
// the 'title' or 'aria-label' attribute is used instead.
if strings.TrimSpace(content) == "" {
content = selec.AttrOr("title", selec.AttrOr("aria-label", ""))
}
// a link without text won't de displayed anyway
if content == "" {
return md.AdvancedResult{
Markdown: "",
}, false
}
replacement := fmt.Sprintf("%s (%s)", content, href)
return md.AdvancedResult{
Markdown: replacement,
}, false
},
},
}
}
func GetRelayListFromFile(filePath string) []string {
file, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Failed to read file: %s", err)
}
var relayList []string
if err := json.Unmarshal(file, &relayList); err != nil {
log.Fatalf("Failed to parse JSON: %s", err)
}
for i, relay := range relayList {
relayList[i] = "wss://" + strings.TrimSpace(relay)
}
return relayList
}
func CalcAvgPostTime(feedPostTimes []int64) int64 {
if len(feedPostTimes) < s.MinPostPeriodSamples {
return int64(s.MaxAvgPostPeriodHrs * 60 * 60)
}
sort.SliceStable(feedPostTimes, func(i, j int) bool {
return feedPostTimes[i] > feedPostTimes[j]
})
avgposttimesecs := (feedPostTimes[0] - feedPostTimes[len(feedPostTimes)-1]) / int64(len(feedPostTimes))
if avgposttimesecs < int64(s.MinAvgPostPeriodMins*60) {
return int64(s.MinAvgPostPeriodMins * 60)
} else if avgposttimesecs > int64(s.MaxAvgPostPeriodHrs*60*60) {
return int64(s.MaxAvgPostPeriodHrs * 60 * 60)
}
return avgposttimesecs
}
func TimetoUpdateFeed(rssfeed Entity) bool {
return time.Now().Unix()-rssfeed.LastCheckedTime >= rssfeed.AvgPostTime
}