diff --git a/cmd/dealsbot/api.go b/cmd/dealsbot/api/api.go similarity index 74% rename from cmd/dealsbot/api.go rename to cmd/dealsbot/api/api.go index f604689..dbddd27 100644 --- a/cmd/dealsbot/api.go +++ b/cmd/dealsbot/api/api.go @@ -1,8 +1,8 @@ -package main +package api type Api interface { - load() error - get() []Deal + Load() error + Get() []Deal } type DealsMap map[string]Deal diff --git a/cmd/dealsbot/epic.go b/cmd/dealsbot/api/epic.go similarity index 96% rename from cmd/dealsbot/epic.go rename to cmd/dealsbot/api/epic.go index 80cca2b..49dcf08 100644 --- a/cmd/dealsbot/epic.go +++ b/cmd/dealsbot/api/epic.go @@ -1,4 +1,4 @@ -package main +package api import ( "encoding/json" @@ -17,7 +17,7 @@ type EpicStruct struct { deals DealsMap } -func newEpicApi() EpicStruct { +func NewEpicApi() EpicStruct { epic := EpicStruct{ url: "https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions", baseUrl: "https://store.epicgames.com/p/", @@ -75,7 +75,7 @@ type epicApiBody struct { } `json:"data"` } -func (e EpicStruct) load() error { +func (e EpicStruct) Load() error { client := &http.Client{} req, err := http.NewRequest("GET", e.url, nil) if err != nil { @@ -131,7 +131,7 @@ func (e EpicStruct) load() error { return nil } -func (e EpicStruct) get() []Deal { +func (e EpicStruct) Get() []Deal { var deals []Deal for _, deal := range e.deals { deals = append(deals, deal) diff --git a/cmd/dealsbot/gog.go b/cmd/dealsbot/api/gog.go similarity index 96% rename from cmd/dealsbot/gog.go rename to cmd/dealsbot/api/gog.go index 4964cea..e40ef82 100644 --- a/cmd/dealsbot/gog.go +++ b/cmd/dealsbot/api/gog.go @@ -1,4 +1,4 @@ -package main +package api import ( "fmt" @@ -15,7 +15,7 @@ type GogStruct struct { deals DealsMap } -func newGogApi() GogStruct { +func NewGogApi() GogStruct { gog := GogStruct{ url: "https://www.gog.com/", baseUrl: "https://www.gog.com/game/", @@ -28,7 +28,7 @@ func newGogApi() GogStruct { return gog } -func (e GogStruct) load() error { +func (e GogStruct) Load() error { client := &http.Client{} // might have to add a cookie at a later time but currently works without // "Cookie", "gog_lc=GB_GBP_en-US" or "Accept-Language", "en" @@ -139,7 +139,7 @@ func (e GogStruct) load() error { return nil } -func (e GogStruct) get() []Deal { +func (e GogStruct) Get() []Deal { var deals []Deal for _, deal := range e.deals { deals = append(deals, deal) diff --git a/cmd/dealsbot/humblebundle.go b/cmd/dealsbot/api/humblebundle.go similarity index 95% rename from cmd/dealsbot/humblebundle.go rename to cmd/dealsbot/api/humblebundle.go index fbd060a..d5917f6 100644 --- a/cmd/dealsbot/humblebundle.go +++ b/cmd/dealsbot/api/humblebundle.go @@ -1,4 +1,4 @@ -package main +package api import ( "encoding/json" @@ -16,7 +16,7 @@ type HumbleBundleStruct struct { deals DealsMap } -func newHumbleBundleApi() HumbleBundleStruct { +func NewHumbleBundleApi() HumbleBundleStruct { humbleBundle := HumbleBundleStruct{ url: "https://www.humblebundle.com/", baseUrl: "https://www.humblebundle.com/store/", @@ -46,7 +46,7 @@ type humblebundleJsonBody struct { } `json:"mosaic"` } -func (e HumbleBundleStruct) load() error { +func (e HumbleBundleStruct) Load() error { client := &http.Client{} // might have to add a cookie at a later time but currently works without // "Cookie", "gog_lc=GB_GBP_en-US" or "Accept-Language", "en" @@ -138,7 +138,7 @@ func contains(s []string, str string) bool { return false } -func (e HumbleBundleStruct) get() []Deal { +func (e HumbleBundleStruct) Get() []Deal { var deals []Deal for _, deal := range e.deals { deals = append(deals, deal) diff --git a/cmd/dealsbot/steam.go b/cmd/dealsbot/api/steam.go similarity index 96% rename from cmd/dealsbot/steam.go rename to cmd/dealsbot/api/steam.go index 037a37e..9956a21 100644 --- a/cmd/dealsbot/steam.go +++ b/cmd/dealsbot/api/steam.go @@ -1,4 +1,4 @@ -package main +package api import ( "encoding/json" @@ -18,7 +18,7 @@ type SteamStruct struct { deals DealsMap } -func newSteamApi() SteamStruct { +func NewSteamApi() SteamStruct { steam := SteamStruct{ url: "https://store.steampowered.com/search/results?force_infinite=1&maxprice=free&specials=1", baseUrl: "https://store.steampowered.com/app/", @@ -56,7 +56,7 @@ type steamApiBodyGame struct { type steamApiBody map[string]steamApiBodyGame -func (e SteamStruct) load() error { +func (e SteamStruct) Load() error { client := &http.Client{} reqStore, err := http.NewRequest("GET", e.url, nil) if err != nil { @@ -150,7 +150,7 @@ func (e SteamStruct) load() error { return nil } -func (e SteamStruct) get() []Deal { +func (e SteamStruct) Get() []Deal { var deals []Deal for _, deal := range e.deals { deals = append(deals, deal) diff --git a/cmd/dealsbot/ubisoft.go b/cmd/dealsbot/api/ubisoft.go similarity index 95% rename from cmd/dealsbot/ubisoft.go rename to cmd/dealsbot/api/ubisoft.go index 07235be..4b5a887 100644 --- a/cmd/dealsbot/ubisoft.go +++ b/cmd/dealsbot/api/ubisoft.go @@ -1,4 +1,4 @@ -package main +package api import ( "encoding/json" @@ -16,7 +16,7 @@ type UbisoftStruct struct { deals DealsMap } -func newUbsioftApi() UbisoftStruct { +func NewUbsioftApi() UbisoftStruct { ubisoft := UbisoftStruct{ url: "https://free.ubisoft.com/configuration.js", idPrefix: "ubisoft-", @@ -41,7 +41,7 @@ type ubisoftApiBody struct { } `json:"news"` } -func (e UbisoftStruct) load() error { +func (e UbisoftStruct) Load() error { appId, prodUrl, err := func() (string, string, error) { client := &http.Client{} @@ -135,7 +135,7 @@ func (e UbisoftStruct) load() error { return nil } -func (e UbisoftStruct) get() []Deal { +func (e UbisoftStruct) Get() []Deal { var deals []Deal for _, deal := range e.deals { deals = append(deals, deal) diff --git a/cmd/dealsbot/main.go b/cmd/dealsbot/main.go index 7e02732..b294c85 100644 --- a/cmd/dealsbot/main.go +++ b/cmd/dealsbot/main.go @@ -11,6 +11,8 @@ import ( "github.com/disgoorg/disgo/webhook" "github.com/disgoorg/log" "github.com/disgoorg/snowflake/v2" + "grow.rievo.dev/discordBots/cmd/dealsbot/api" + "grow.rievo.dev/discordBots/cmd/dealsbot/repository" "os" "os/signal" "reflect" @@ -41,7 +43,7 @@ func main() { client := webhook.New(webhookID, webhookToken) defer client.Close(context.TODO()) - repo := InitDb() + repo := repository.InitDb() defer repo.Close() ticker := time.NewTicker(10 * time.Minute) @@ -52,17 +54,17 @@ func main() { for { select { case <-ticker.C: - var apis []Api - apis = append(apis, newUbsioftApi(), newEpicApi(), newSteamApi(), newGogApi(), newHumbleBundleApi()) + var apis []api.Api + apis = append(apis, api.NewUbsioftApi(), api.NewEpicApi(), api.NewSteamApi(), api.NewGogApi(), api.NewHumbleBundleApi()) for _, api := range apis { - err := api.load() + err := api.Load() if err != nil { log.Error(err) } } - var deals []Deal + var deals []api.Deal for _, api := range apis { - apiDeals := api.get() + apiDeals := api.Get() deals = append(deals, apiDeals...) } @@ -104,7 +106,7 @@ func main() { <-s } -func sendWebhook(client webhook.Client, deal Deal) { +func sendWebhook(client webhook.Client, deal api.Deal) { var status string status = fmt.Sprintf("currently free: %v\n", deal.Url) diff --git a/cmd/dealsbot/repository.go b/cmd/dealsbot/repository/repository.go similarity index 82% rename from cmd/dealsbot/repository.go rename to cmd/dealsbot/repository/repository.go index f6e15b1..f2ed638 100644 --- a/cmd/dealsbot/repository.go +++ b/cmd/dealsbot/repository/repository.go @@ -1,15 +1,16 @@ -package main +package repository import ( "encoding/json" "github.com/dgraph-io/badger/v4" "github.com/disgoorg/log" + "grow.rievo.dev/discordBots/cmd/dealsbot/api" ) type Repository interface { - GetAll() ([]Deal, error) - GetValue(dealId string) Deal - SetValue(deal Deal) error + GetAll() ([]api.Deal, error) + GetValue(dealId string) api.Deal + SetValue(deal api.Deal) error DeleteValue(dealId string) error Close() error } @@ -36,8 +37,8 @@ func (d *DealRepository) RunGC() error { return d.db.RunValueLogGC(0.7) } -func (d *DealRepository) GetAll() ([]Deal, error) { - var deals []Deal +func (d *DealRepository) GetAll() ([]api.Deal, error) { + var deals []api.Deal err := d.db.View(func(txn *badger.Txn) error { opts := badger.DefaultIteratorOptions opts.PrefetchSize = 10 @@ -46,7 +47,7 @@ func (d *DealRepository) GetAll() ([]Deal, error) { for it.Rewind(); it.Valid(); it.Next() { item := it.Item() err := item.Value(func(val []byte) error { - retrievedDeal := Deal{} + retrievedDeal := api.Deal{} err := json.Unmarshal(val, &retrievedDeal) deals = append(deals, retrievedDeal) return err @@ -60,8 +61,8 @@ func (d *DealRepository) GetAll() ([]Deal, error) { return deals, err } -func (d *DealRepository) GetValue(dealId string) (Deal, error) { - retrievedDeal := Deal{} +func (d *DealRepository) GetValue(dealId string) (api.Deal, error) { + retrievedDeal := api.Deal{} err := d.db.View(func(txn *badger.Txn) error { item, err := txn.Get([]byte(dealId)) if err != nil { @@ -74,12 +75,12 @@ func (d *DealRepository) GetValue(dealId string) (Deal, error) { return err }) if err != nil { - return Deal{}, err + return api.Deal{}, err } return retrievedDeal, nil } -func (d *DealRepository) SetValue(deal Deal) error { +func (d *DealRepository) SetValue(deal api.Deal) error { jsonBytes, err := json.Marshal(deal) if err != nil { return err