-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.go
93 lines (79 loc) · 2.04 KB
/
solution.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
package main
import (
"fmt"
"log"
"strings"
"time"
swissknife "github.com/Sagleft/swiss-knife"
utopiago "github.com/Sagleft/utopialib-go/v2"
)
func main() {
cfg := config{}
if err := swissknife.ParseStructFromJSONFile(configFilePath, &cfg); err != nil {
log.Fatalln(err)
}
sol := newSolution(cfg)
if err := sol.utopiaConnect(); err != nil {
log.Fatalln(err)
}
if err := sol.makePost(); err != nil {
log.Fatalln(err)
}
if err := sol.createPostImage(time.Now()); err != nil {
log.Fatalln(err)
}
if err := sol.sendPostImage(); err != nil {
log.Fatalln(err)
}
}
func newSolution(cfg config) *solution {
return &solution{
Config: cfg,
Utopia: utopiago.NewUtopiaClient(cfg.Utopia),
}
}
func (sol *solution) isTimeVariantExists() bool {
_, exists := APITimeVariants[sol.Config.TimeVariant]
return exists
}
func (sol *solution) makePost() error {
var postText string = ""
if !sol.isTimeVariantExists() {
return fmt.Errorf("unknown time variant given: %s", sol.Config.TimeVariant)
}
switch sol.Config.TimeVariant {
case "today":
timeFormated := time.Now().Format(timeLayoutUS)
postText += "Horoscope for " + timeFormated + "\n\n"
case "month":
timeFormated := time.Now().Format(timeLayoutMonth)
postText += "Horoscope for " + timeFormated + "\n\n"
case "year":
timeFormated := time.Now().Format(timeLayoutYear)
postText += "Horoscope for " + timeFormated + "year\n\n"
}
for i, sunsignInfo := range sunSigns {
sunsign := sunsignInfo.Tag
forecastResponse, err := sol.getZodiacForecast(sunsign)
if err != nil {
return err
}
newPostPart := sunsignInfo.Icon + " " + strings.ToTitle(sunsign) + "\n✨ " +
forecastResponse.Text + "\n\n"
// check post length
if len(postText+newPostPart) > postMaxLength || i == len(sunSigns)-1 {
// send post part or full post (if it is last post part)
if i == len(sunSigns)-1 {
postText += sol.Config.ChannelID
}
err := sol.sendPost(postText)
if err != nil {
return err
}
postText = ""
} else {
postText += newPostPart
}
}
return nil
}