moved to pkgs

This commit is contained in:
Seraphim Strub 2023-03-06 20:53:07 +01:00
parent 7ea6ba6809
commit 843a357428
8 changed files with 44 additions and 41 deletions

View file

@ -1,8 +1,8 @@
package main package api
type Api interface { type Api interface {
load() error Load() error
get() []Deal Get() []Deal
} }
type DealsMap map[string]Deal type DealsMap map[string]Deal

View file

@ -1,4 +1,4 @@
package main package api
import ( import (
"encoding/json" "encoding/json"
@ -17,7 +17,7 @@ type EpicStruct struct {
deals DealsMap deals DealsMap
} }
func newEpicApi() EpicStruct { func NewEpicApi() EpicStruct {
epic := EpicStruct{ epic := EpicStruct{
url: "https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions", url: "https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions",
baseUrl: "https://store.epicgames.com/p/", baseUrl: "https://store.epicgames.com/p/",
@ -75,7 +75,7 @@ type epicApiBody struct {
} `json:"data"` } `json:"data"`
} }
func (e EpicStruct) load() error { func (e EpicStruct) Load() error {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("GET", e.url, nil) req, err := http.NewRequest("GET", e.url, nil)
if err != nil { if err != nil {
@ -131,7 +131,7 @@ func (e EpicStruct) load() error {
return nil return nil
} }
func (e EpicStruct) get() []Deal { func (e EpicStruct) Get() []Deal {
var deals []Deal var deals []Deal
for _, deal := range e.deals { for _, deal := range e.deals {
deals = append(deals, deal) deals = append(deals, deal)

View file

@ -1,4 +1,4 @@
package main package api
import ( import (
"fmt" "fmt"
@ -15,7 +15,7 @@ type GogStruct struct {
deals DealsMap deals DealsMap
} }
func newGogApi() GogStruct { func NewGogApi() GogStruct {
gog := GogStruct{ gog := GogStruct{
url: "https://www.gog.com/", url: "https://www.gog.com/",
baseUrl: "https://www.gog.com/game/", baseUrl: "https://www.gog.com/game/",
@ -28,7 +28,7 @@ func newGogApi() GogStruct {
return gog return gog
} }
func (e GogStruct) load() error { func (e GogStruct) Load() error {
client := &http.Client{} client := &http.Client{}
// might have to add a cookie at a later time but currently works without // 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" // "Cookie", "gog_lc=GB_GBP_en-US" or "Accept-Language", "en"
@ -139,7 +139,7 @@ func (e GogStruct) load() error {
return nil return nil
} }
func (e GogStruct) get() []Deal { func (e GogStruct) Get() []Deal {
var deals []Deal var deals []Deal
for _, deal := range e.deals { for _, deal := range e.deals {
deals = append(deals, deal) deals = append(deals, deal)

View file

@ -1,4 +1,4 @@
package main package api
import ( import (
"encoding/json" "encoding/json"
@ -16,7 +16,7 @@ type HumbleBundleStruct struct {
deals DealsMap deals DealsMap
} }
func newHumbleBundleApi() HumbleBundleStruct { func NewHumbleBundleApi() HumbleBundleStruct {
humbleBundle := HumbleBundleStruct{ humbleBundle := HumbleBundleStruct{
url: "https://www.humblebundle.com/", url: "https://www.humblebundle.com/",
baseUrl: "https://www.humblebundle.com/store/", baseUrl: "https://www.humblebundle.com/store/",
@ -46,7 +46,7 @@ type humblebundleJsonBody struct {
} `json:"mosaic"` } `json:"mosaic"`
} }
func (e HumbleBundleStruct) load() error { func (e HumbleBundleStruct) Load() error {
client := &http.Client{} client := &http.Client{}
// might have to add a cookie at a later time but currently works without // 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" // "Cookie", "gog_lc=GB_GBP_en-US" or "Accept-Language", "en"
@ -138,7 +138,7 @@ func contains(s []string, str string) bool {
return false return false
} }
func (e HumbleBundleStruct) get() []Deal { func (e HumbleBundleStruct) Get() []Deal {
var deals []Deal var deals []Deal
for _, deal := range e.deals { for _, deal := range e.deals {
deals = append(deals, deal) deals = append(deals, deal)

View file

@ -1,4 +1,4 @@
package main package api
import ( import (
"encoding/json" "encoding/json"
@ -18,7 +18,7 @@ type SteamStruct struct {
deals DealsMap deals DealsMap
} }
func newSteamApi() SteamStruct { func NewSteamApi() SteamStruct {
steam := SteamStruct{ steam := SteamStruct{
url: "https://store.steampowered.com/search/results?force_infinite=1&maxprice=free&specials=1", url: "https://store.steampowered.com/search/results?force_infinite=1&maxprice=free&specials=1",
baseUrl: "https://store.steampowered.com/app/", baseUrl: "https://store.steampowered.com/app/",
@ -56,7 +56,7 @@ type steamApiBodyGame struct {
type steamApiBody map[string]steamApiBodyGame type steamApiBody map[string]steamApiBodyGame
func (e SteamStruct) load() error { func (e SteamStruct) Load() error {
client := &http.Client{} client := &http.Client{}
reqStore, err := http.NewRequest("GET", e.url, nil) reqStore, err := http.NewRequest("GET", e.url, nil)
if err != nil { if err != nil {
@ -150,7 +150,7 @@ func (e SteamStruct) load() error {
return nil return nil
} }
func (e SteamStruct) get() []Deal { func (e SteamStruct) Get() []Deal {
var deals []Deal var deals []Deal
for _, deal := range e.deals { for _, deal := range e.deals {
deals = append(deals, deal) deals = append(deals, deal)

View file

@ -1,4 +1,4 @@
package main package api
import ( import (
"encoding/json" "encoding/json"
@ -16,7 +16,7 @@ type UbisoftStruct struct {
deals DealsMap deals DealsMap
} }
func newUbsioftApi() UbisoftStruct { func NewUbsioftApi() UbisoftStruct {
ubisoft := UbisoftStruct{ ubisoft := UbisoftStruct{
url: "https://free.ubisoft.com/configuration.js", url: "https://free.ubisoft.com/configuration.js",
idPrefix: "ubisoft-", idPrefix: "ubisoft-",
@ -41,7 +41,7 @@ type ubisoftApiBody struct {
} `json:"news"` } `json:"news"`
} }
func (e UbisoftStruct) load() error { func (e UbisoftStruct) Load() error {
appId, prodUrl, err := func() (string, string, error) { appId, prodUrl, err := func() (string, string, error) {
client := &http.Client{} client := &http.Client{}
@ -135,7 +135,7 @@ func (e UbisoftStruct) load() error {
return nil return nil
} }
func (e UbisoftStruct) get() []Deal { func (e UbisoftStruct) Get() []Deal {
var deals []Deal var deals []Deal
for _, deal := range e.deals { for _, deal := range e.deals {
deals = append(deals, deal) deals = append(deals, deal)

View file

@ -11,6 +11,8 @@ import (
"github.com/disgoorg/disgo/webhook" "github.com/disgoorg/disgo/webhook"
"github.com/disgoorg/log" "github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2" "github.com/disgoorg/snowflake/v2"
"grow.rievo.dev/discordBots/cmd/dealsbot/api"
"grow.rievo.dev/discordBots/cmd/dealsbot/repository"
"os" "os"
"os/signal" "os/signal"
"reflect" "reflect"
@ -41,7 +43,7 @@ func main() {
client := webhook.New(webhookID, webhookToken) client := webhook.New(webhookID, webhookToken)
defer client.Close(context.TODO()) defer client.Close(context.TODO())
repo := InitDb() repo := repository.InitDb()
defer repo.Close() defer repo.Close()
ticker := time.NewTicker(10 * time.Minute) ticker := time.NewTicker(10 * time.Minute)
@ -52,17 +54,17 @@ func main() {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
var apis []Api var apis []api.Api
apis = append(apis, newUbsioftApi(), newEpicApi(), newSteamApi(), newGogApi(), newHumbleBundleApi()) apis = append(apis, api.NewUbsioftApi(), api.NewEpicApi(), api.NewSteamApi(), api.NewGogApi(), api.NewHumbleBundleApi())
for _, api := range apis { for _, api := range apis {
err := api.load() err := api.Load()
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
} }
var deals []Deal var deals []api.Deal
for _, api := range apis { for _, api := range apis {
apiDeals := api.get() apiDeals := api.Get()
deals = append(deals, apiDeals...) deals = append(deals, apiDeals...)
} }
@ -104,7 +106,7 @@ func main() {
<-s <-s
} }
func sendWebhook(client webhook.Client, deal Deal) { func sendWebhook(client webhook.Client, deal api.Deal) {
var status string var status string
status = fmt.Sprintf("currently free: %v\n", deal.Url) status = fmt.Sprintf("currently free: %v\n", deal.Url)

View file

@ -1,15 +1,16 @@
package main package repository
import ( import (
"encoding/json" "encoding/json"
"github.com/dgraph-io/badger/v4" "github.com/dgraph-io/badger/v4"
"github.com/disgoorg/log" "github.com/disgoorg/log"
"grow.rievo.dev/discordBots/cmd/dealsbot/api"
) )
type Repository interface { type Repository interface {
GetAll() ([]Deal, error) GetAll() ([]api.Deal, error)
GetValue(dealId string) Deal GetValue(dealId string) api.Deal
SetValue(deal Deal) error SetValue(deal api.Deal) error
DeleteValue(dealId string) error DeleteValue(dealId string) error
Close() error Close() error
} }
@ -36,8 +37,8 @@ func (d *DealRepository) RunGC() error {
return d.db.RunValueLogGC(0.7) return d.db.RunValueLogGC(0.7)
} }
func (d *DealRepository) GetAll() ([]Deal, error) { func (d *DealRepository) GetAll() ([]api.Deal, error) {
var deals []Deal var deals []api.Deal
err := d.db.View(func(txn *badger.Txn) error { err := d.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10 opts.PrefetchSize = 10
@ -46,7 +47,7 @@ func (d *DealRepository) GetAll() ([]Deal, error) {
for it.Rewind(); it.Valid(); it.Next() { for it.Rewind(); it.Valid(); it.Next() {
item := it.Item() item := it.Item()
err := item.Value(func(val []byte) error { err := item.Value(func(val []byte) error {
retrievedDeal := Deal{} retrievedDeal := api.Deal{}
err := json.Unmarshal(val, &retrievedDeal) err := json.Unmarshal(val, &retrievedDeal)
deals = append(deals, retrievedDeal) deals = append(deals, retrievedDeal)
return err return err
@ -60,8 +61,8 @@ func (d *DealRepository) GetAll() ([]Deal, error) {
return deals, err return deals, err
} }
func (d *DealRepository) GetValue(dealId string) (Deal, error) { func (d *DealRepository) GetValue(dealId string) (api.Deal, error) {
retrievedDeal := Deal{} retrievedDeal := api.Deal{}
err := d.db.View(func(txn *badger.Txn) error { err := d.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(dealId)) item, err := txn.Get([]byte(dealId))
if err != nil { if err != nil {
@ -74,12 +75,12 @@ func (d *DealRepository) GetValue(dealId string) (Deal, error) {
return err return err
}) })
if err != nil { if err != nil {
return Deal{}, err return api.Deal{}, err
} }
return retrievedDeal, nil return retrievedDeal, nil
} }
func (d *DealRepository) SetValue(deal Deal) error { func (d *DealRepository) SetValue(deal api.Deal) error {
jsonBytes, err := json.Marshal(deal) jsonBytes, err := json.Marshal(deal)
if err != nil { if err != nil {
return err return err