Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show daily scans #11

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func Start(screenApp *screen.ScreenApp) {
r.POST("/spotify", handlerWrapper(screenApp, spotifyGetMessage))

// Start Tap
go runTapRequests(screenApp)
go tapRunRequests(screenApp)

// Start Zess
go zessRunRequests(screenApp)

// Start API
r.Run()
Expand Down
40 changes: 40 additions & 0 deletions api/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package api

import (
"encoding/json"
"net/http"
)

type header struct {
Key string
Value string
}

var jsonHeader = header{
"Accept",
"application/json",
}

func makeGetRequest[T any](url string, headers []header, response *T) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}

for _, header := range headers {
req.Header.Set(header.Key, header.Value)
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return err
}

return nil
}
22 changes: 8 additions & 14 deletions api/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,16 @@ func spotifySetAccessToken() error {

func spotifyGetTrackTitle(trackID string) (string, error) {
url := fmt.Sprintf("https://api.spotify.com/v1/tracks/%s", trackID)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Bearer "+spotifyAccessToken)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
headers := []header{
jsonHeader,
{
"Authorization",
"Bearer " + spotifyAccessToken,
},
}
defer resp.Body.Close()

trackResponse := &spotifyTrackResponse{}
if err := json.NewDecoder(resp.Body).Decode(trackResponse); err != nil {

if err := makeGetRequest(url, headers, trackResponse); err != nil {
return "", err
}

Expand Down
56 changes: 17 additions & 39 deletions api/tap.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package api

import (
"encoding/json"
"log"
"net/http"
"scc/config"
"scc/screen"
"slices"
"time"
)

Expand All @@ -14,51 +13,30 @@ type tapReponse struct {
}

var (
tapURL = config.GetConfig().Tap.URL
timestampLayout = config.GetConfig().Tap.TimestampLayout
lastOrderTimestamp = time.Time{}
tapURL = config.GetConfig().Tap.URL
tapLastOrderTimestamp = time.Time{}
)

func runTapRequests(app *screen.ScreenApp) {
for true {
recentOrders, err := tapGetRecentOrders()
if err != nil {
log.Printf("Error: Unable to get recent order: %s\n", err)
func tapRunRequests(app *screen.ScreenApp) {
headers := []header{jsonHeader}

for {
recentOrders := &tapReponse{}
if err := makeGetRequest(tapURL, headers, recentOrders); err != nil {
log.Printf("Error: Unable to get recent orders: %s\n", err)
}
for _, order := range recentOrders.Orders {
timestamp, err := time.Parse(timestampLayout, order.OrderCreatedAt)
if err != nil {
log.Printf("Error: Unable to parse timestamp: %s\n", err)
}

if order.ProductCategory == "beverages" && timestamp.After(lastOrderTimestamp) {
slices.SortStableFunc(recentOrders.Orders, func(a, b screen.TapOrder) int {
return a.OrderCreatedAt.Compare(b.OrderCreatedAt)
})

for _, order := range recentOrders.Orders {
if order.OrderCreatedAt.After(tapLastOrderTimestamp) {
app.Tap.Update(&order)
lastOrderTimestamp = timestamp
tapLastOrderTimestamp = order.OrderCreatedAt
}
}

time.Sleep(1 * time.Minute)
}
}

func tapGetRecentOrders() (*tapReponse, error) {
req, err := http.NewRequest("GET", tapURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

response := &tapReponse{}
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return nil, err
}

return response, nil
}
42 changes: 42 additions & 0 deletions api/zess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api

import (
"log"
"scc/config"
"scc/screen"
"slices"
"time"
)

type zessResponse struct {
Scans []screen.ZessScan `json:"scans"`
}

var (
zessURL = config.GetConfig().Zess.URL
zessLastOrderTimestamp = time.Time{}
)

func zessRunRequests(app *screen.ScreenApp) {
headers := []header{jsonHeader}

for {
recentScans := &zessResponse{}
if err := makeGetRequest(zessURL, headers, recentScans); err != nil {
log.Printf("Error: Unable to get recent scans: %s\n", err)
}

slices.SortStableFunc(recentScans.Scans, func(a, b screen.ZessScan) int {
return a.ScanTime.Compare(b.ScanTime)
})

for _, order := range recentScans.Scans {
if order.ScanTime.After(zessLastOrderTimestamp) {
app.Zess.Update(&order)
zessLastOrderTimestamp = order.ScanTime
}
}

time.Sleep(1 * time.Minute)
}
}
27 changes: 27 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cammie:
blocked_names:
- "Name 1"
- "Name 2"
blocked_ips:
- "10.0.0.10"
max_message_length: 200
spotify:
client_id: "abcd"
client_secret: "dcba"
tap:
url: "https://tap.zeus.gent/recent"
beer:
- "Schelfaut"
- "Duvel"
- "Fourchette"
- "Jupiler"
- "Karmeliet"
- "Kriek"
- "Chouffe"
- "Maes"
- "Somersby"
- "Sportzot"
- "Stella"
zess:
url: "https://zess.zeus.gent/recent_scans"
day_amount: 40
16 changes: 11 additions & 5 deletions config.template.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
cammie:
blocked_names: []
blocked_ips: []
max_message_length:
blocked_names: [] # List of names that don't get shown
blocked_ips: [] # List of IP's that don't get shown
max_message_length: # Max message list
spotify:
client_id:
client_secret:
client_id: # Client ID of the Zeus spotify account
client_secret: # Client secret of the Zeus spotify account
tap:
url: # URL of tap
beer: [] # List of beers in tap, can be found in tap
zess:
url: # URL of zess
day_amount: # Amount of days to keep track, should be the same as is displayed which depends on the screen size
12 changes: 9 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ type spotifyConfig struct {
}

type tapConfig struct {
URL string `yaml:"url"`
TimestampLayout string `yaml:"timestamp_layout"`
Beers []string `yaml:"beer"`
URL string `yaml:"url"`
Beers []string `yaml:"beer"`
}

type zessConfig struct {
URL string `yaml:"url"`
DayAmount int `yaml:"day_amount"`
}

type Config struct {
Cammie cammieConfig `yaml:"cammie"`
Spotify spotifyConfig `yaml:"spotify"`
Tap tapConfig `yaml:"tap"`
Zess zessConfig `yaml:"zess"`
}

var (
Expand All @@ -47,5 +52,6 @@ func GetConfig() *Config {
log.Fatalf("Failed to unmarshal config: %v", err)
}
})

return configInstance
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ require (
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)

replace github.com/navidys/tvxwidgets => github.com/topvennie/tvxwidgets v0.7.2
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
Expand Down Expand Up @@ -56,12 +56,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/navidys/tvxwidgets v0.7.0 h1:ls5tikzqXnsHwAAV/8zwnRwx/DvSybepUih9txkwjwE=
github.com/navidys/tvxwidgets v0.7.0/go.mod h1:hzFnllDl4o2Ten/67T0F8ZgC1NiLrZYqWxLVjxWu+zo=
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
github.com/onsi/ginkgo/v2 v2.19.1 h1:QXgq3Z8Crl5EL1WBAC98A5sEBHARrAJNzAmMxzLcRF0=
github.com/onsi/ginkgo/v2 v2.19.1/go.mod h1:O3DtEWQkPa/F7fBMgmZQKKsluAy8pd3rEQdrjkPb9zA=
github.com/onsi/gomega v1.34.0 h1:eSSPsPNp6ZpsG8X1OVmOTxig+CblTc4AxpPBykhe2Os=
github.com/onsi/gomega v1.34.0/go.mod h1:MIKI8c+f+QLWk+hxbePD4i0LMJSExPaZOVfkoex4cAo=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -83,6 +81,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/topvennie/tvxwidgets v0.7.2 h1:pwO3zH7sAz423mNS3jN/5r0v6UCRlDeAnl+S50sZRVI=
github.com/topvennie/tvxwidgets v0.7.2/go.mod h1:/rHJ8G/5l5pF3QTHJhVAQoHlPjNSsd+eXeSA7Rg7rSs=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
Expand Down Expand Up @@ -115,8 +115,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
23 changes: 0 additions & 23 deletions screen/graph2.go

This file was deleted.

Loading
Loading