-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
131 lines (106 loc) · 2.63 KB
/
main.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
package main
import (
"fmt"
"github.com/gtuk/discordwebhook"
"github.com/r-darwish/freestuff/src"
"log"
"os"
"strings"
"time"
)
func main() {
os.Exit(run())
}
var author = fmt.Sprint("Reddit Comments")
var cache freestuff.RedisCache
func run() int {
exitCode := 0
links, err := freestuff.GetLinksFromSubreddit("apphookup")
if err != nil {
log.Fatalf("Fatal error: %v", err.Error())
}
cache = freestuff.NewRedisCache()
for _, link := range links {
if link.Title == "" {
continue
}
err := handleLink(link)
if err != nil {
exitCode = 1
log.Printf("Error in %v: %+v", link, err.Error())
}
}
return exitCode
}
func handleLink(link freestuff.RedditLink) error {
known, err := cache.IsKnown(link.Link)
if err != nil {
return err
}
if known {
return nil
}
defer func(cache *freestuff.RedisCache, title string) {
err := cache.SetKnown(title)
if err != nil {
log.Printf("Error storing a link in the cache: %v", err)
}
}(&cache, link.Link)
extraInfo, err, publish := shouldPublish(link)
if publish {
return nil
}
var fields []discordwebhook.Field
t := true
if extraInfo != nil {
for _, label := range extraInfo.GetLabels() {
label := label
fields = append(fields, discordwebhook.Field{
Name: &label.Key,
Value: &label.Value,
Inline: &t,
})
}
}
embed := discordwebhook.Embed{
Title: &link.Title,
Url: &link.Link,
Thumbnail: &discordwebhook.Thumbnail{Url: &link.Image},
Author: &discordwebhook.Author{
Name: &author,
Url: &link.RedditLink,
},
Fields: &fields,
}
message := discordwebhook.Message{
Embeds: &[]discordwebhook.Embed{embed},
}
time.Sleep(time.Second * 5)
err = discordwebhook.SendMessage(freestuff.Config.Webhook, message)
if err != nil && err.Error() != "" {
return fmt.Errorf("sending a Discord message: %w", err)
}
return nil
}
func shouldPublish(link freestuff.RedditLink) (freestuff.ExtraInfo, error, bool) {
price, err := freestuff.Price(link.Title)
if err != nil {
return nil, err, false
}
if !isFree(link.Title) || price < freestuff.Config.PriceThreshold {
return nil, nil, false
}
extraInfo, err := freestuff.GetExtraInfo(link.Link)
if err != nil {
log.Printf("Error getting extra info for %s: %v", link.Link, err.Error())
return nil, nil, false
}
if appStoreInfo, ok := extraInfo.(freestuff.AppstoreInfo); ok && appStoreInfo.Category != "Games" {
log.Printf("Skipping category %s for %s", appStoreInfo.Category, link)
return nil, nil, false
}
return extraInfo, nil, true
}
func isFree(title string) bool {
return strings.Contains(strings.ToLower(title), "free") || strings.Contains(title, "0.00")
}