-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (133 loc) · 3.88 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
package main
import (
"context"
"flag"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"time"
mastodon "github.com/mattn/go-mastodon"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
cron "github.com/robfig/cron/v3"
)
type Configuration struct {
ServerUrl string
AccessToken string
MessageText string
MessageVisibility string
Schedule string
OneShot bool
MetricsPort string
}
var (
imgCount = promauto.NewGauge(prometheus.GaugeOpts{
Name: "radpanda_current_image_count",
Help: "The total number of images available to radpanda to post",
})
mediaUploadTime = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "radpanda_media_upload_time",
Help: "The time taken to upload media in seconds",
})
tootTime = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "radpanda_toot_time",
Help: "The time taken to toot in seconds",
})
)
func getConfig() Configuration {
config := Configuration{}
flag.StringVar(&config.ServerUrl, "server", "", "(Required) The Mastodon Server")
flag.StringVar(&config.AccessToken, "token", "", "(Required) The app access token")
flag.StringVar(&config.MessageText, "message", "", "The message to send with each toot")
flag.StringVar(&config.MessageVisibility, "visibility", "unlisted", "The visibility of the toot (public, unlisted, private, direct)")
flag.BoolVar(&config.OneShot, "one-shot", false, "Single shot message")
flag.StringVar(&config.Schedule, "schedule", "@every 1h", "A cron expression controlling when to send messages")
flag.StringVar(&config.MetricsPort, "metrics-address", "", "The address and port to listen on for prometheus metrics")
flag.Parse()
if config.AccessToken == "" {
config.AccessToken = os.Getenv("RADPANDA_TOKEN")
}
if config.ServerUrl == "" {
config.ServerUrl = os.Getenv("RADPANDA_SERVER")
}
if config.AccessToken == "" || config.ServerUrl == "" {
flag.PrintDefaults()
os.Exit(1)
}
if config.MessageText == "" {
config.MessageText = os.Getenv("RADPANDA_TEXT")
}
if config.MessageVisibility == "unlisted" {
config.MessageVisibility = os.Getenv("RADPANDA_VISIBILITY")
}
if config.Schedule == "@every 1h" {
config.Schedule = os.Getenv("RADPANDA_SCHEDULE")
}
if config.MetricsPort == "" {
config.MetricsPort = os.Getenv("RADPANDA_METRICS_PATH")
}
return config
}
func main() {
config := getConfig()
if config.MetricsPort != "" {
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(config.MetricsPort, nil)
}()
}
files, err := ioutil.ReadDir("img")
if err != nil {
log.Panic(err)
}
imgCount.Set(float64(len(files)))
rand.Seed(time.Now().UnixNano())
if config.OneShot {
process(config)
return
} else {
scheduler := cron.New()
_, err := scheduler.AddFunc(config.Schedule, func() { process(config) })
if err != nil {
log.Panic(err)
}
scheduler.Run()
}
}
func process(config Configuration) {
client := mastodon.NewClient(&mastodon.Config{
Server: config.ServerUrl,
AccessToken: config.AccessToken,
})
ctx := context.Background()
files, err := ioutil.ReadDir("img")
if err != nil {
log.Panic(err)
}
imgCount.Set(float64(len(files)))
fileIdx := rand.Intn(len(files))
mediaUploadStart := time.Now()
attachment, err := client.UploadMedia(ctx, "img/"+files[fileIdx].Name())
if err != nil {
log.Printf("Error Uploading Media, %s\n", files[fileIdx].Name())
log.Fatal(err)
}
mediaUploadTime.Observe(time.Since(mediaUploadStart).Seconds())
tootStart := time.Now()
toot := mastodon.Toot{
Status: config.MessageText,
Visibility: config.MessageVisibility,
MediaIDs: []mastodon.ID{
attachment.ID,
},
}
_, err = client.PostStatus(ctx, &toot)
if err != nil {
log.Println("Error Tooting")
log.Fatal(err)
}
tootTime.Observe(time.Since(tootStart).Seconds())
}