-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
reddit.go
65 lines (58 loc) · 2.06 KB
/
reddit.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
package filters
import (
"html"
"regexp"
"strings"
"github.com/buger/jsonparser"
"github.com/microcosm-cc/bluemonday"
"github.com/slurdge/goeland/internal/goeland"
"github.com/slurdge/goeland/internal/goeland/httpget"
"github.com/slurdge/goeland/log"
"github.com/spf13/viper"
)
func filterReddit(source *goeland.Source, params *filterParams) {
policy := bluemonday.NewPolicy()
policy.AllowImages()
policy.AllowStandardURLs()
policy.AllowAttrs("href").OnElements("a")
policy.AllowElements("p")
re := regexp.MustCompile(`\/comments\/([a-z0-9]+)\/`)
for i, entry := range source.Entries {
postId := re.FindStringSubmatch(entry.URL)[1]
if !viper.GetBool("unsafe-no-sanitize-filter") {
entry.Content = policy.Sanitize(entry.Content)
}
if strings.Contains(entry.Content, "b.thumbs.redditmedia.com") ||
strings.Contains(entry.Content, "external-preview.redd.it") {
//we consider this is only a picture post
image_url, err := getBetterPreview(postId)
if err != nil {
log.Warningf("Cannot get better preview picture %v, ignoring", err)
continue
}
ore := regexp.MustCompile(`<img\s+src="[^"]+"[^>]*>`)
entry.Content = ore.ReplaceAllString(entry.Content, `<img src="`+image_url+`">`)
}
source.Entries[i] = entry
}
}
func getBetterPreview(postId string) (string, error) {
jsonURL := "https://api.reddit.com/api/info/?id=t3_" + postId
body, err := httpget.GetHTTPRessource(jsonURL)
if err != nil {
return "", err
}
media_id, err := jsonparser.GetString(body, "data", "children", "[0]", "data", "gallery_data", "items", "[0]", "media_id")
if err == nil {
image_url, err := jsonparser.GetString(body, "data", "children", "[0]", "data", "media_metadata", media_id, "p", "[3]", "u")
if err == nil {
image_url = html.UnescapeString(image_url)
return image_url, nil
}
}
preview, err := jsonparser.GetString(body, "data", "children", "[0]", "data", "preview", "images", "[0]", "source", "url")
if err != nil {
return "", err
}
return html.UnescapeString(preview), nil
}