-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit_subreddit_posts.go
177 lines (153 loc) · 5.49 KB
/
reddit_subreddit_posts.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
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"gorm.io/gorm"
)
func getRedditPosts(config *Config) ([]RedditPostResult, error) {
var results []RedditPostResult
var response RedditResponse
client := resty.New()
_, err := client.R().
SetHeaders(map[string]string{
"authority": "www.reddit.com",
"pragma": "no-cache",
"cache-control": "no-cache",
"sec-ch-ua": `"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"`,
"sec-ch-ua-mobile": "?0",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"dnt": "1",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"accept-language": "en-GB,en;q=0.9",
}).
SetResult(&response).
Get(fmt.Sprintf("https://www.reddit.com/r/%s/new.json?limit=100", config.Tag))
if err != nil {
return results, err
}
results = append(results, response.Data.Children...)
return results, nil
}
func getRedditSearchPosts(config *Config) ([]RedditPostResult, error) {
var results []RedditPostResult
var response RedditResponse
client := resty.New()
_, err := client.R().
SetHeaders(map[string]string{
"authority": "www.reddit.com",
"pragma": "no-cache",
"cache-control": "no-cache",
"sec-ch-ua": `"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"`,
"sec-ch-ua-mobile": "?0",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"dnt": "1",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"accept-language": "en-GB,en;q=0.9",
}).
SetResult(&response).
Get(fmt.Sprintf("https://www.reddit.com/search.json?q=%s&type=comment&sort=updated?limit=100", config.Tag))
if err != nil {
return results, err
}
results = append(results, response.Data.Children...)
return results, nil
}
func sendSlackNotificationForRedditPost(result RedditPostResult, config *Config) error {
if !config.NotifySlack {
return nil
}
logFields := log.Fields{
"article_id": result.Data.ID,
"title": result.Data.Title,
}
attachment := slack.Attachment{
Color: "#36a64f",
Fallback: "New post on Reddit!",
AuthorName: result.Data.Author,
AuthorLink: fmt.Sprintf("https://www.reddit.com/user/%s", result.Data.Author),
Title: result.Data.Title,
TitleLink: result.Data.URL,
Text: result.Data.Selftext,
Footer: "Reddit Post Notification",
FooterIcon: redditIconURL,
Ts: json.Number(strconv.FormatInt(int64(result.Data.CreatedUtc), 10)),
}
log.WithFields(logFields).Info("Notifying slack")
messageOpts := []slack.MsgOption{
slack.MsgOptionAsUser(false),
slack.MsgOptionAttachments(attachment),
slack.MsgOptionIconEmoji(":reddit:"),
slack.MsgOptionText("New post on <"+result.Data.URL+"|Reddit>", false),
slack.MsgOptionUsername("Reddit Post Notifications"),
slack.MsgOptionDisableLinkUnfurl(),
}
api := slack.New(config.SlackToken)
if _, _, err := api.PostMessage(config.SlackChannelID, messageOpts...); err != nil {
return err
}
return nil
}
func processRedditPosts(config *Config, db *gorm.DB) error {
if err := db.AutoMigrate(&RedditPost{}); err != nil {
return fmt.Errorf("error migrating RedditPost: %w", err)
}
log.Info("Fetching posts")
results, err := getRedditPosts(config)
if err != nil {
return err
}
searchResults, err := getRedditSearchPosts(config)
if err != nil {
return err
}
results = append(results, searchResults...)
inserted := 0
notified := 0
log.WithField("post_count", len(results)).Info("Processing posts")
for _, result := range results {
logFields := log.Fields{
"post_id": result.Data.ID,
"title": result.Data.Title,
}
var entity RedditPost
if dbResult := db.First(&entity, "post_id = ?", result.Data.ID); !errors.Is(dbResult.Error, gorm.ErrRecordNotFound) {
continue
}
log.WithFields(logFields).Info("Inserting new post")
entity = RedditPost{
PostID: result.Data.ID,
Title: result.Data.Title,
}
if dbResult := db.Create(&entity); dbResult.Error != nil {
log.WithError(dbResult.Error).WithFields(logFields).Fatal("error inserting post into database")
continue
}
inserted += 1
if err := sendSlackNotificationForRedditPost(result, config); err != nil {
log.WithError(err).WithFields(logFields).Fatal("error posting post to slack")
continue
}
notified += 1
}
log.WithFields(log.Fields{
"processed_post_count": len(results),
"inserted_post_count": inserted,
"notified_post_count": notified,
}).Info("Done with reddit posts")
return nil
}