fuzzyworld

quantum-inspiration / files / main.go

main.go

Blame: the commit that last changed each line · file · history

e58f6fff 2023-09-06 Pavel Safronov1package main
2
0c14bc4f 2023-09-06 Pavel Safronov3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "time"
8)
9
10type Results struct {
11 CreationDate string
12 RandomNumber uint8
13 Pages []WikiPage
14}
e58f6fff 2023-09-06 Pavel Safronov15
16func main() {
0c14bc4f 2023-09-06 Pavel Safronov17 random256Pages, err := getRandomWikipediaPages(256)
e58f6fff 2023-09-06 Pavel Safronov18 if err != nil {
0c14bc4f 2023-09-06 Pavel Safronov19 panic(fmt.Errorf("error getting random Wikipedia pages: %v", err))
20 }
21
22 randomNumbers, err := GetRandomNumbers(1)
23 if err != nil {
24 panic(fmt.Errorf("error getting random numbers: %v", err))
25 }
26
27 random := randomNumbers[0]
28
29 result := Results{
30 Pages: random256Pages,
31 RandomNumber: random,
32 CreationDate: time.Now().Format("2006-01-02"),
33 }
34 resultJson, err := json.MarshalIndent(result, "", " ")
35 if err != nil {
36 panic(fmt.Errorf("error marshalling results: %v", err))
37 }
38 completeJson := fmt.Sprintf("var results = %s;", resultJson)
39 outPath := "./public/results.js"
40 err = os.WriteFile(outPath, []byte(completeJson), 0644)
41 if err != nil {
42 panic(fmt.Errorf("error writing to %s: %v", outPath, err))
e58f6fff 2023-09-06 Pavel Safronov43 }
44
0c14bc4f 2023-09-06 Pavel Safronov45 page := random256Pages[random]
46 title := page.Title
47 url := page.Url
bc232b23 2026-10-27 Pavel Safronov48 message := fmt.Sprintf("Quantum inspiration for the day: <a href='%s'>%s</a>\n\n<a href='https://apps.fuzzyworld.net/quantum-inspiration/'>More...</a>", url, title)
e58f6fff 2023-09-06 Pavel Safronov49 err = SendPushoverMessage(message)
50 if err != nil {
51 panic(err)
52 }
53}