aboutsummaryrefslogtreecommitdiff
path: root/discord/pdevbitcoinbot/main.go
blob: 4abe16259147e5b4f69603c45cc04ea6759c1937 (plain)
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
// Command pdevbitcoinbot queries the bitstamp API and stores prices in a HDR
// Histogram. This computes the p95 price of bitcoin.
package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"
	"strconv"

	"within.website/x/internal"
	"within.website/x/web"
	"within.website/x/web/discordwebhook"
	"github.com/codahale/hdrhistogram"
	experrors "golang.org/x/exp/errors"
	"within.website/ln"
	"within.website/ln/opname"
)

type BitstampReply struct {
	High      string  `json:"high"`
	Last      string  `json:"last"`
	Timestamp string  `json:"timestamp"`
	Bid       string  `json:"bid"`
	Vwap      string  `json:"vwap"`
	Volume    string  `json:"volume"`
	Low       string  `json:"low"`
	Ask       string  `json:"ask"`
	Open      float64 `json:"open"`
}

type data struct {
	Snapshot  *hdrhistogram.Snapshot
	LastValue *string
}

var (
	whURL            = flag.String("webhook-url", "", "Discord webhook URL")
	dataFileLocation = flag.String("data", "./data.json", "data file location")
)

func getBitstamp() (*BitstampReply, error) {
	resp, err := http.Get("https://www.bitstamp.net/api/ticker/")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	var bsr BitstampReply
	err = json.NewDecoder(resp.Body).Decode(&bsr)
	if err != nil {
		log.Fatal(err)
	}

	return &bsr, nil
}

func main() {
	internal.HandleStartup()

	ctx := opname.With(context.Background(), "main")

	fin, err := os.Open(*dataFileLocation)
	if err != nil {
		var pe *os.PathError
		if experrors.As(err, &pe) {
			ln.Fatal(ctx, ln.F{
				"err_op":   pe.Op,
				"err_path": pe.Path,
				"err_err":  pe.Err,
			})
		}

		ln.FatalErr(ctx, err)
	}

	var d data
	err = json.NewDecoder(fin).Decode(&d)
	if err != nil {
		ln.FatalErr(ctx, err)
	}
	fin.Close()

	var h *hdrhistogram.Histogram
	if d.Snapshot == nil {
		h = hdrhistogram.New(-400, 3000000000, 5)
	} else {
		h = hdrhistogram.Import(d.Snapshot)
	}

	bsr, err := getBitstamp()
	if err != nil {
		ln.FatalErr(ctx, err)
	}

	bcf, err := strconv.ParseFloat(bsr.Ask, 64)
	if err != nil {
		ln.FatalErr(ctx, err)
	}

	h.RecordValue(int64(bcf))

	var lv = "_shrug_"

	if d.LastValue != nil {
		lv = *d.LastValue
	}

	dw := discordwebhook.Webhook{
		Content:  "BITCOIN PRICE TIEM",
		Username: "Buttcoin",
		Embeds: []discordwebhook.Embeds{discordwebhook.Embeds{
			Footer: discordwebhook.EmbedFooter{
				Text: "powered by pdevbitcoinbot, made by Cadey~#1337",
			},
			Fields: []discordwebhook.EmbedField{
				{
					Name:   "Now",
					Value:  bsr.Ask,
					Inline: true,
				},
				{
					Name:   "Last",
					Value:  lv,
					Inline: true,
				},
				{
					Name:   "P95",
					Value:  fmt.Sprint(h.ValueAtQuantile(95)),
					Inline: true,
				},
			},
		}},
	}

	req := discordwebhook.Send(*whURL, dw)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		ln.FatalErr(ctx, err)
	}
	err = discordwebhook.Validate(resp)
	if err != nil {
		var werr *web.Error
		if experrors.As(err, &werr) {
			ln.Fatal(ctx, werr)
		}

		ln.FatalErr(ctx, err)
	}

	d.LastValue = &bsr.Ask
	d.Snapshot = h.Export()

	fout, err := os.Create("./data.json")
	if err != nil {
		ln.FatalErr(ctx, err)
	}

	err = json.NewEncoder(fout).Encode(&d)
	if err != nil {
		ln.FatalErr(ctx, err)
	}
}