-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (141 loc) · 3.44 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 (
"bytes"
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"strings"
"syscall"
"math/rand"
"github.com/robfig/cron"
"github.com/zet4/catsbutnotreally/services"
_ "github.com/zet4/catsbutnotreally/services/ibsearch"
_ "github.com/zet4/catsbutnotreally/services/randomcat"
"fmt"
"net/http"
"time"
)
const (
// VERSION Version of the project
VERSION = "0.4"
)
var (
client = &http.Client{Timeout: 60 * time.Second}
runner *cron.Cron
webapp *WebApp
)
var (
// Flags
configFile = ""
)
func flags() {
flag.StringVar(&configFile, "config", "config.json", "Config file for catsbutnotreally")
flag.Parse()
}
func main() {
flags()
reloadChan, reloadedChan := WatchConfig(configFile)
go func() {
var err error
for {
<-reloadChan
if runner != nil {
runner.Stop()
runner = nil
}
if webapp != nil {
webapp.Close()
}
runner = cron.New()
for _, v := range config.Destinations {
err = runner.AddFunc(v.Cron, work(v))
if err != nil {
log.Println("Error occurred while trying to add destination:", err.Error())
}
}
runner.Start()
webapp = WebAppFromConfig(config)
reloadedChan <- struct{}{}
}
}()
reloadChan <- struct{}{}
<-reloadedChan
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}
func work(d *Destination) func() {
var pool []int
var total int
sourceServices := make(map[*Source]func() (image string, customfields services.CustomFields, err error), 0)
for idx, v := range d.Sources {
for x := 0; x < v.Chance; x++ {
pool = append(pool, idx)
}
total += v.Chance
service, ok := services.Index[v.Service]
if !ok {
log.Printf("Service '%s' is not registred/available.\n", v.Service)
continue
}
sourceServices[v] = service(v.OptionalArguments)
}
return func() {
source := d.Sources[pool[rand.Intn(total)]]
service, ok := sourceServices[source]
if !ok {
return
}
image, fields, err := service()
if err != nil {
log.Printf("Error occurred while trying to run '%s': %s\n", source.Service, err.Error())
return
}
message, err := createMessage(*d.Username, *d.Avatar, source.Service, image, source.Display, &fields)
if err != nil {
log.Printf("Error occurred while preparing message: %s", err.Error())
return
}
err = postWebhook(d.Webhook, message)
if err != nil {
log.Printf("Error occurred while trying to send image retrieved from %s: %s", source.Service, err.Error())
return
}
if webapp != nil {
webapp.Add(d, source, image)
}
log.Printf("Sending a picture from '%s': %s", source.Service, image)
}
}
func createMessage(username, avatar, service, image, display string, fields *services.CustomFields) (result services.Displayable, err error) {
if display == "simple" || display == "" {
resp, err := client.Get(image)
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
tokens := strings.Split(resp.Request.URL.String(), "/")
if err = resp.Body.Close(); err != nil {
return nil, err
}
return &services.Simple{
Username: username,
Avatar: avatar,
File: bytes.NewReader(data),
Filename: tokens[len(tokens)-1],
}, nil
} else if display == "embed" {
return &services.Embedded{
Username: username,
Avatar: avatar,
Image: image,
Fields: fields,
}, nil
}
return nil, fmt.Errorf("'%s' is not a valid display type", display)
}