-
Notifications
You must be signed in to change notification settings - Fork 21
/
reddit.go
227 lines (189 loc) · 5.37 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
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
package client
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/cameronstanley/go-reddit"
"github.com/gorilla/feeds"
"github.com/graph-gophers/dataloader"
"golang.org/x/oauth2"
)
type linkListingChildren struct {
Kind string `json:"kind"`
Data reddit.Link `json:"data"`
}
type linkListingData struct {
Modhash string `json:"modhash"`
Children []linkListingChildren `json:"children"`
After string `json:"after"`
Before interface{} `json:"before"`
}
type linkListing struct {
Kind string `json:"kind"`
Data linkListingData `json:"data"`
}
type RedditClient struct {
HttpClient *http.Client
UserAgent string
Token *oauth2.Token
}
type GetArticleFn = func(client *RedditClient, link *reddit.Link) (*string, error)
type NowFn = func() time.Time
func RssHandler(redditURL string, now NowFn, client *RedditClient, getArticle GetArticleFn, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.URL.String() == "/" {
http.Redirect(w, r, "https://www.reddit.com/r/rss/comments/fvg3ed/i_built_a_better_rss_feed_for_reddit/", 301)
return
}
log.Println(r.URL)
url := fmt.Sprintf("%s%s", redditURL, r.URL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
req.Header.Add("User-Agent", client.UserAgent)
if client.Token != nil {
req.Header.Set("Authorization", fmt.Sprintf("bearer %s", client.Token.AccessToken))
}
resp, err := client.HttpClient.Do(req)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer resp.Body.Close()
var result linkListing
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
nowVal := now()
feed := &feeds.Feed{
Title: fmt.Sprintf("reddit-rss %s", r.URL),
Link: &feeds.Link{Href: "https://www.reddit.com/r/rss/comments/fvg3ed/i_built_a_better_rss_feed_for_reddit/"},
Description: "Reddit RSS feed that links directly to the content",
Author: &feeds.Author{Name: "Stephen Solka", Email: "stephen@solka.dev"},
Created: nowVal,
Updated: nowVal,
}
var limit int
limitStr, scoreLimit := r.URL.Query()["scoreLimit"]
if scoreLimit {
limit, err = strconv.Atoi(limitStr[0])
if err != nil {
scoreLimit = false
}
}
var safe bool
safeStr, hasSafe := r.URL.Query()["safe"]
if hasSafe {
safe = strings.ToLower(safeStr[0]) == "true"
}
var flair string
flairStr, hasFlair := r.URL.Query()["flair"]
if hasFlair {
flair = flairStr[0]
}
loader := articleLoader(client, getArticle)
var thunks []dataloader.Thunk
for _, link := range result.Data.Children {
if hasSafe && safe && (link.Data.Over18 || strings.ToLower(link.Data.LinkFlairText) == "nsfw") {
continue
}
if scoreLimit && limit > link.Data.Score {
continue
}
if hasFlair && flair != "" && link.Data.LinkFlairText != flair {
continue
}
thunks = append(thunks, loader.Load(ctx, dataKey(link.Data)))
}
for _, thunk := range thunks {
val, err := thunk()
if err != nil {
continue
}
item := val.(*feeds.Item)
feed.Items = append(feed.Items, item)
}
rss, err := feed.ToRss()
if err != nil {
http.Error(w, err.Error(), 500)
}
w.Header().Set("Content-Type", "application/rss+xml")
w.Header().Set("Cache-Control", "public, maxage=1800")
io.WriteString(w, rss)
}
func linkToFeed(client *RedditClient, getArticle GetArticleFn, link *reddit.Link) *feeds.Item {
var content string
c, _ := getArticle(client, link)
if c != nil {
content = *c
}
redditUrl := os.Getenv("REDDIT_URL")
if redditUrl == "" {
redditUrl = "https://old.reddit.com"
}
author := link.Author
authorLink := fmt.Sprintf("%s/u/%s", redditUrl, author)
content = fmt.Sprintf(`<p><a href="%s">u/%s</a> <a href="%s%s">comments</a></p> %s`, authorLink, author, redditUrl, link.Permalink, content)
u, err := url.Parse(link.URL)
if err == nil {
author = u.Host
}
t := time.Unix(int64(link.CreatedUtc), 0)
// if item link is to reddit, replace reddit with REDDIT_URL
itemLink := link.URL
if strings.HasPrefix(itemLink, "https://old.reddit.com") {
itemLink = fmt.Sprintf(`%s%s`, redditUrl, link.Permalink)
}
return &feeds.Item{
Title: link.Title,
Link: &feeds.Link{Href: itemLink},
Author: &feeds.Author{Name: author},
Created: t,
Id: link.ID,
Content: content,
}
}
type dataKey reddit.Link
func (k dataKey) String() string {
l := reddit.Link(k)
return l.ID
}
func (k dataKey) Raw() interface{} { return k }
func articleLoader(client *RedditClient, getArticle GetArticleFn) *dataloader.Loader {
return dataloader.NewBatchedLoader(func(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
wg := &sync.WaitGroup{}
lock := &sync.Mutex{}
resultMap := make(map[string]*dataloader.Result)
for _, key := range keys {
data := reddit.Link(key.(dataKey))
wg.Add(1)
go func(lock *sync.Mutex, wg *sync.WaitGroup, l reddit.Link) {
defer wg.Done()
item := linkToFeed(client, getArticle, &l)
lock.Lock()
defer lock.Unlock()
resultMap[l.ID] = &dataloader.Result{Data: item}
}(lock, wg, data)
}
wg.Wait()
var results []*dataloader.Result
for _, key := range keys {
data := reddit.Link(key.(dataKey))
results = append(results, resultMap[data.ID])
}
return results
}, dataloader.WithBatchCapacity(10))
}