-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (58 loc) · 1.36 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
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
)
const quotepath = "https://raw.githubusercontent.com/Ilya-Meer/marcus/main/data/quotes.json"
type Quote struct {
Text []string `json:"quote"`
Book int `json:"book"`
Section int `json:"section"`
}
type Formatter struct {
strings.Builder
}
func main() {
var quotes []Quote
r, err := http.Get(quotepath)
if err != nil {
log.Fatal("Encountered an error fetching quotes...")
}
defer r.Body.Close()
json.NewDecoder(r.Body).Decode("es)
randomQuote := getRandomQuote(quotes)
formatted := formatQuote(randomQuote)
fmt.Println(formatted)
}
func getRandomQuote(quotes []Quote) (quote Quote) {
rand.Seed(time.Now().UnixNano())
randIndex := rand.Intn(len(quotes) - 1)
return quotes[randIndex]
}
func formatQuote(quote Quote) (formattedQuote string) {
var formatter Formatter
formatter.AddBreaks(2)
formatter.PadString("From 'Meditations' by Marcus Aurelius \n", 5)
formatter.AddBreaks(2)
for _, str := range quote.Text {
formatter.PadString(str, 5)
formatter.AddBreaks(1)
}
formatter.AddBreaks(2)
formatter.PadString(
fmt.Sprintf(
"💎 Book %s, Section %s 💎",
strconv.FormatInt(int64(quote.Book), 10),
strconv.FormatInt(int64(quote.Section), 10),
),
5,
)
formatter.AddBreaks(2)
return formatter.String()
}