2021-02-23 18:29:27 +01:00
package main
import (
"encoding/json"
"io/ioutil"
"path/filepath"
)
// Config to store JSON configuration
type Config struct {
2021-04-06 10:33:24 +02:00
DatabaseConfig ` json:"database" `
2021-03-01 13:11:58 +01:00
TwitterConfig ` json:"twitter" `
2021-03-23 18:12:44 +01:00
TelegramConfig ` json:"telegram" `
2021-04-01 17:57:17 +02:00
APIConfig ` json:"api" `
2021-03-31 17:48:47 +02:00
AmazonConfig ` json:"amazon" `
2022-07-22 19:34:03 +02:00
NvidiaFEConfig ` json:"nvidia_fe" `
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
URLs [ ] string ` json:"urls" `
IncludeRegex string ` json:"include_regex" `
ExcludeRegex string ` json:"exclude_regex" `
PriceRanges [ ] PriceRange ` json:"price_ranges" `
BrowserAddress string ` json:"browser_address" `
2021-02-23 18:29:27 +01:00
}
2021-04-06 10:33:24 +02:00
// DatabaseConfig to store database configuration
type DatabaseConfig struct {
Type string ` json:"type" `
DSN string ` json:"dsn" `
}
2021-02-23 18:29:27 +01:00
// TwitterConfig to store Twitter API secrets
type TwitterConfig struct {
2021-02-27 18:31:11 +01:00
ConsumerKey string ` json:"consumer_key" `
ConsumerSecret string ` json:"consumer_secret" `
AccessToken string ` json:"access_token" `
AccessTokenSecret string ` json:"access_token_secret" `
Hashtags [ ] map [ string ] string ` json:"hashtags" `
2021-04-01 13:14:00 +02:00
EnableReplies bool ` json:"enable_replies" `
2021-04-15 15:13:00 +02:00
Retention int ` json:"retention" `
2021-02-23 18:29:27 +01:00
}
2021-03-23 18:12:44 +01:00
// TelegramConfig to store Telegram API key
type TelegramConfig struct {
2021-04-01 13:14:00 +02:00
Token string ` json:"token" `
ChatID int64 ` json:"chat_id" `
ChannelName string ` json:"channel_name" `
EnableReplies bool ` json:"enable_replies" `
2021-03-23 18:12:44 +01:00
}
2021-04-01 17:57:17 +02:00
// APIConfig to store HTTP API configuration
type APIConfig struct {
2021-03-25 18:00:29 +01:00
Address string ` json:"address" `
Certfile string ` json:"cert_file" `
Keyfile string ` json:"key_file" `
}
2021-03-31 17:48:47 +02:00
// AmazonConfig to store Amazon API secrets
type AmazonConfig struct {
Searches [ ] string ` json:"searches" `
AccessKey string ` json:"access_key" `
SecretKey string ` json:"secret_key" `
Marketplaces [ ] struct {
Name string ` json:"name" `
PartnerTag string ` json:"partner_tag" `
} ` json:"marketplaces" `
AmazonFulfilled bool ` json:"amazon_fulfilled" `
AmazonMerchant bool ` json:"amazon_merchant" `
AffiliateLinks bool ` json:"affiliate_links" `
}
2022-07-22 19:34:03 +02:00
// NvidiaFEConfig to store NVIDIA Founders Edition configuration
type NvidiaFEConfig struct {
Locations [ ] string ` json:"locations" `
GPUs [ ] string ` json:"gpus" `
UserAgent string ` json:"user_agent" `
Timeout int ` json:"timeout" `
}
feat: add price range filter (#26)
To avoid scalpers' price, the bot now understand filters on prices
using a minimum and maximum value, in a currency and a pattern to
detect the model.
Example:
```
"price_ranges": [
{"model": "3090", "max": 3000, "currency": "EUR"},
{"model": "3080", "max": 1600, "currency": "EUR"},
{"model": "3070", "max": 1200, "currency": "EUR"}
],
```
More details in README.md.
Signed-off-by: Julien Riou <julien@riou.xyz>
2021-05-23 02:32:30 +02:00
// PriceRange to store rules to filter products with price outside of the range
type PriceRange struct {
Model string ` json:"model" `
Min float64 ` json:"min" `
Max float64 ` json:"max" `
Currency string ` json:"currency" `
}
2021-02-23 18:29:27 +01:00
// NewConfig creates a Config struct
func NewConfig ( ) * Config {
return & Config { }
}
// Read Config from configuration file
func ( c * Config ) Read ( file string ) error {
file , err := filepath . Abs ( file )
if err != nil {
return err
}
jsonFile , err := ioutil . ReadFile ( file )
if err != nil {
return err
}
err = json . Unmarshal ( jsonFile , & c )
if err != nil {
return err
}
return nil
}
// HasTwitter returns true when Twitter has been configured
func ( c * Config ) HasTwitter ( ) bool {
return ( c . TwitterConfig . AccessToken != "" && c . TwitterConfig . AccessTokenSecret != "" && c . TwitterConfig . ConsumerKey != "" && c . TwitterConfig . ConsumerSecret != "" )
}
2021-03-23 18:12:44 +01:00
// HasTelegram returns true when Telegram has been configured
func ( c * Config ) HasTelegram ( ) bool {
return c . TelegramConfig . Token != "" && ( c . TelegramConfig . ChatID != 0 || c . TelegramConfig . ChannelName != "" )
}
2021-03-31 17:48:47 +02:00
2021-04-01 17:57:17 +02:00
// HasURLs returns true when list of URLS has been configured
2021-03-31 17:48:47 +02:00
func ( c * Config ) HasURLs ( ) bool {
return len ( c . URLs ) > 0
}
2021-04-06 10:33:24 +02:00
// HasDatabase returns true when database has been configured
func ( c * Config ) HasDatabase ( ) bool {
return c . DatabaseConfig . Type != "" && c . DatabaseConfig . DSN != ""
}
2022-07-22 19:34:03 +02:00
// HasNvidiaFE returns true when NVIDIA FE has been configured
func ( c * Config ) HasNvidiaFE ( ) bool {
return len ( c . NvidiaFEConfig . Locations ) > 0 && len ( c . NvidiaFEConfig . GPUs ) > 0
}
2021-03-31 17:48:47 +02:00
// HasAmazon returns true when Amazon has been configured
func ( c * Config ) HasAmazon ( ) bool {
var hasKeys , hasSearches , hasMarketplaces bool
hasKeys = c . AmazonConfig . AccessKey != "" && c . AmazonConfig . SecretKey != ""
hasSearches = len ( c . AmazonConfig . Searches ) > 0
for _ , marketplace := range c . AmazonConfig . Marketplaces {
if marketplace . PartnerTag != "" && marketplace . Name != "" {
hasMarketplaces = true
break
}
}
return hasKeys && hasSearches && hasMarketplaces
}