diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-03-05 16:23:33 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-03-05 16:23:33 -0500 |
| commit | b29f4583327c98a18eaebd98ccd6d416504128e4 (patch) | |
| tree | 69daf2121b8ca2e70508c650aa74c78192648a60 | |
| parent | ea95496b1d9c9637bad0159b04e781daab359f1d (diff) | |
| download | x-b29f4583327c98a18eaebd98ccd6d416504128e4.tar.xz x-b29f4583327c98a18eaebd98ccd6d416504128e4.zip | |
add cryptocompare API bindings
Signed-off-by: Xe Iaso <me@xeiaso.net>
| -rw-r--r-- | cmd/xedn/stablediffusion.go | 10 | ||||
| -rw-r--r-- | web/chatgpt/chatgpt.go | 6 | ||||
| -rw-r--r-- | web/cryptocompare/cryptocompare.go | 44 |
3 files changed, 60 insertions, 0 deletions
diff --git a/cmd/xedn/stablediffusion.go b/cmd/xedn/stablediffusion.go index 6bc18c0..0b8c3cf 100644 --- a/cmd/xedn/stablediffusion.go +++ b/cmd/xedn/stablediffusion.go @@ -3,6 +3,7 @@ package main import ( "bytes" "context" + "expvar" "fmt" "image" "image/jpeg" @@ -20,6 +21,11 @@ import ( "within.website/x/internal/stablediffusion" ) +var ( + stableDiffusionHits = expvar.NewInt("xedn_avatar_hits") + stableDiffusionCreation = expvar.NewInt("xedn_avatar_creation") +) + type StableDiffusion struct { client *stablediffusion.Client db *bbolt.DB @@ -55,6 +61,8 @@ func (sd *StableDiffusion) RenderImage(ctx context.Context, w http.ResponseWrite return nil, err } + stableDiffusionCreation.Add(1) + img, _, err := image.Decode(bytes.NewBuffer(imgs.Images[0])) if err != nil { return nil, err @@ -140,6 +148,8 @@ func (sd *StableDiffusion) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write(data) } + stableDiffusionHits.Add(1) + return nil }) diff --git a/web/chatgpt/chatgpt.go b/web/chatgpt/chatgpt.go index ad1de30..d0b70dc 100644 --- a/web/chatgpt/chatgpt.go +++ b/web/chatgpt/chatgpt.go @@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "within.website/x/web" ) @@ -21,6 +22,10 @@ type Message struct { Content string `json:"content"` } +func (m Message) ProxyFormat() string { + return fmt.Sprintf("%s\\ %s", strings.Title(m.Role), m.Content) +} + type Usage struct { PromptTokens int `json:"prompt_tokens"` CompletionTokens int `json:"completion_tokens"` @@ -80,6 +85,7 @@ func (c Client) Complete(ctx context.Context, r Request) (*Response, error) { if resp.StatusCode != http.StatusOK { return nil, web.NewError(http.StatusOK, resp) } + defer resp.Body.Close() var result Response if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { diff --git a/web/cryptocompare/cryptocompare.go b/web/cryptocompare/cryptocompare.go new file mode 100644 index 0000000..6886f77 --- /dev/null +++ b/web/cryptocompare/cryptocompare.go @@ -0,0 +1,44 @@ +// Package cryptocompare fetches the latest USD price of a given crpytocurrency from CryptoCompare +package cryptocompare + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "within.website/x/web" + _ "within.website/x/web/useragent" +) + +func Get(symbol string, currencies []string) (map[string]float64, error) { + u, err := url.Parse("https://min-api.cryptocompare.com/data/price") + if err != nil { + return nil, err + } + + q := u.Query() + q.Set("fsym", symbol) + q.Set("tsyms", strings.Join(currencies, ",")) + + u.RawQuery = q.Encode() + + resp, err := http.Get(u.String()) + if err != nil { + return nil, fmt.Errorf("cryptocompare: can't fetch result: %w", err) + } + + if resp.StatusCode != http.StatusOK { + return nil, web.NewError(http.StatusOK, resp) + } + + defer resp.Body.Close() + + result := make(map[string]float64) + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("cryptocompare: can't decode result: %w") + } + + return result, nil +} |
