-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (177 loc) Β· 6.99 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
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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-resty/resty/v2"
"github.com/integrii/flaggy"
"github.com/madflojo/tasks"
"github.com/pterm/pterm"
"github.com/vartanbeno/go-reddit/v2/reddit"
)
var (
cfgIftttEventName string = os.Getenv("IFTTT_EVENT_NAME")
cfgIftttKey string = os.Getenv("IFTTT_KEY")
cfgRedditAppID string = os.Getenv("REDDIT_APP_ID")
cfgRedditAppSecret string = os.Getenv("REDDIT_APP_SECRET")
cfgRedditUsername string = os.Getenv("REDDIT_USERNAME")
cfgRedditPassword string = os.Getenv("REDDIT_PASSWORD")
lastPost *reddit.Post
sprintIDStyle = pterm.NewStyle(pterm.FgGray, pterm.BgLightCyan)
sprintTitleStyle = pterm.NewStyle(pterm.FgGray, pterm.BgLightMagenta)
version string = "unknown"
)
func main() {
mainScreen()
flaggy.SetName("TurnipMon")
flaggy.SetDescription("A little Animal Crossing NH turnip marketplace subreddit monitor, sending you phone notifications via IFTTT once a new turnip trade has been opened.")
flaggy.DefaultParser.ShowHelpOnUnexpected = true
flaggy.DefaultParser.AdditionalHelpPrepend = "http://github.com/klausklapper/turnipmon"
flaggy.String(&cfgIftttEventName, "n", "name", "[env: IFTTT_EVENT_NAME] Required. The IFTTT web hook event name to trigger.")
flaggy.String(&cfgIftttKey, "k", "key", "[env: IFTTT_KEY] Required. Your IFTTT web hook key (see https://ifttt.com/maker_webhooks).")
flaggy.String(&cfgRedditAppID, "i", "id", "[env: REDDIT_APP_ID] Required. Your Reddit app API ID credential.")
flaggy.String(&cfgRedditAppSecret, "s", "secret", "[env: REDDIT_APP_SECRET] Required. Your Reddit app API secret.")
flaggy.String(&cfgRedditUsername, "u", "username", "[env: REDDIT_USERNAME] Required. Your Reddit username.")
flaggy.String(&cfgRedditPassword, "p", "password", "[env: REDDIT_PASSWORD] Required. Your Reddit password.")
flaggy.SetVersion(version)
flaggy.Parse()
if len(cfgIftttEventName) < 1 {
pterm.Fatal.Println("Missing IFTTT web hook event name. Use the --help argument for more information.")
}
if len(cfgIftttKey) < 1 {
pterm.Fatal.Println("Missing IFTTT web hook key. Use the --help argument for more information.")
}
if len(cfgRedditAppID) < 1 {
pterm.Fatal.Println("Missing Reddit app ID. Use the --help argument for more information.")
}
if len(cfgRedditAppSecret) < 1 {
pterm.Fatal.Println("Missing Reddit app secret. Use the --help argument for more information.")
}
if len(cfgRedditUsername) < 1 {
pterm.Fatal.Println("Missing Reddit username. Use the --help argument for more information.")
}
if len(cfgRedditPassword) < 1 {
pterm.Fatal.Println("Missing Reddit password. Use the --help argument for more information.")
}
pterm.Success.Printfln("Configuration parsed! Event: %s, Key: [REDACTED]", cfgIftttEventName)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(quit)
pterm.Info.Println("Connecting to Reddit ...")
credentials := reddit.Credentials{ID: cfgRedditAppID, Secret: cfgRedditAppSecret, Username: cfgRedditUsername, Password: cfgRedditPassword}
client, err := reddit.NewClient(credentials)
pterm.Fatal.PrintOnError(err)
pterm.Info.Println("Fast forwarding to latest posts ...")
posts, _, err := client.Subreddit.NewPosts(ctx, "acturnips+ACNHTurnips", &reddit.ListOptions{
Limit: 5,
})
pterm.Fatal.PrintOnError(err)
if len(posts) < 1 {
pterm.Fatal.Println("No posts found. This should never happen?")
}
updateLastPost(posts[0])
pterm.Info.Printfln("Fast forwarded to %v%v", sprintID(lastPost.FullID), sprintTitle(lastPost.Title))
if time.Now().UTC().Sub(lastPost.Created.UTC()) < time.Minute*30 {
pterm.Success.Printfln("π Last post %v created within the last 30 minutes. Notifying your phone ... π π₯ ", sprintID(lastPost.FullID))
err = sendIftttRequest(ctx, lastPost.Title, lastPost.URL)
pterm.Fatal.PrintOnError(err)
} else {
pterm.Warning.Printfln("Last post %v is older than 30 minutes. Assuming it's already expired, no phone notification will be sent.", sprintID(lastPost.FullID))
}
pterm.Info.Println("All caught up.")
spinner := newSpinner()
scheduler := tasks.New()
defer scheduler.Stop()
_, err = scheduler.Add(&tasks.Task{
Interval: time.Duration(90 * time.Second),
ErrFunc: func(err error) {
pterm.Fatal.Println(err)
},
TaskFunc: func() error {
spinner.UpdateText("Checking ...")
posts, _, err := client.Subreddit.NewPosts(ctx, "acturnips+ACNHTurnips", &reddit.ListOptions{
Limit: 5,
Before: lastPost.FullID,
})
if err != nil {
return err
}
if len(posts) < 1 {
spinner.UpdateText("No new trades found. Next check in 90 seconds")
} else {
spinner.Success(fmt.Sprintf("Found %d new trades!", len(posts)))
for _, v := range posts {
pterm.Success.Printfln("π %v%v created within the last 30 minutes. Notifying your phone ... π π₯ ", sprintID(v.FullID), sprintTitle(v.Title))
err = sendIftttRequest(ctx, v.Title, v.URL)
if err != nil {
return err
}
}
updateLastPost(posts[0])
spinner = newSpinner()
}
return nil
},
})
pterm.Fatal.PrintOnError(err)
select {
case <-quit:
break
case <-ctx.Done():
break
}
pterm.Info.Println("Shutdown requested ...")
scheduler.Stop()
cancel()
pterm.Success.Println("Goodbye! π")
}
func newSpinner() *pterm.SpinnerPrinter {
spinner, err := pterm.DefaultSpinner.Start()
pterm.Fatal.PrintOnError(err)
spinner.UpdateText("No new trades found. Next check in 90 seconds")
return spinner
}
func updateLastPost(p *reddit.Post) {
lastPost = p
}
func sprintID(id string) string {
return sprintIDStyle.Sprintf("[%v]", id)
}
func sprintTitle(title string) string {
return sprintTitleStyle.Sprint(title)
}
func mainScreen() {
print("\033[H\033[2J")
ptermLogo, _ := pterm.DefaultBigText.WithLetters(
pterm.NewLettersFromStringWithStyle("Turnip", pterm.NewStyle(pterm.FgLightCyan)),
pterm.NewLettersFromStringWithStyle("Mon", pterm.NewStyle(pterm.FgLightMagenta))).
Srender()
pterm.DefaultCenter.Print(ptermLogo)
pterm.DefaultCenter.Print(pterm.DefaultHeader.WithFullWidth().WithBackgroundStyle(pterm.NewStyle(pterm.BgLightBlue)).WithMargin(10).Sprint("π₯ TurnipMon - Animal Crossing New Horizons Turnip Market Monitor"))
}
func sendIftttRequest(ctx context.Context, title string, uri string) error {
type payload struct {
Value1 string `json:"value1"`
Value2 string `json:"value2"`
Value3 string `json:"value3"`
}
resp, err := resty.New().R().
SetContext(ctx).
SetBody(payload{Value1: title, Value2: uri, Value3: "https://dodo.ac/np/images/8/86/Turnips_NH_Inv_Icon.png"}).
Post(fmt.Sprintf("https://maker.ifttt.com/trigger/%s/with/key/%s", cfgIftttEventName, cfgIftttKey))
if err != nil {
return err
}
if !resp.IsSuccess() {
pterm.Error.Printfln("IFTTT Request failed with [%v] %v: %v", resp.StatusCode(), resp.Status(), resp.Result())
return errors.New("failed to send IFTTT request")
}
return nil
}