Add max pages flag

pull/94/head
Miguel Mota 3 years ago
parent 38b279083f
commit faabbfc8a6

@ -25,6 +25,7 @@ func RootCmd() *cobra.Command {
var apiChoice string var apiChoice string
var colorscheme string var colorscheme string
var perPage = cointop.DefaultPerPage var perPage = cointop.DefaultPerPage
var maxPages = cointop.DefaultMaxPages
var cacheDir string var cacheDir string
var colorsDir string var colorsDir string
@ -91,6 +92,7 @@ See git.io/cointop for more info.`,
OnlyTable: onlyTable, OnlyTable: onlyTable,
RefreshRate: refreshRateP, RefreshRate: refreshRateP,
PerPage: perPage, PerPage: perPage,
MaxPages: maxPages,
}) })
if err != nil { if err != nil {
return err return err
@ -112,6 +114,7 @@ See git.io/cointop for more info.`,
rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "No cache") rootCmd.Flags().BoolVarP(&noCache, "no-cache", "", false, "No cache")
rootCmd.Flags().UintVarP(&refreshRate, "refresh-rate", "r", 60, "Refresh rate in seconds. Set to 0 to not auto-refresh") rootCmd.Flags().UintVarP(&refreshRate, "refresh-rate", "r", 60, "Refresh rate in seconds. Set to 0 to not auto-refresh")
rootCmd.Flags().UintVarP(&perPage, "per-page", "", perPage, "Per page") rootCmd.Flags().UintVarP(&perPage, "per-page", "", perPage, "Per page")
rootCmd.Flags().UintVarP(&maxPages, "max-pages", "", maxPages, "Max number of pages")
rootCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath)) rootCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key") rootCmd.Flags().StringVarP(&cmcAPIKey, "coinmarketcap-api-key", "", "", "Set the CoinMarketCap API key")
rootCmd.Flags().StringVarP(&apiChoice, "api", "", "", "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"") rootCmd.Flags().StringVarP(&apiChoice, "api", "", "", "API choice. Available choices are \"coinmarketcap\" and \"coingecko\"")

@ -56,6 +56,7 @@ type State struct {
keepRowFocusOnSort bool keepRowFocusOnSort bool
lastSelectedRowIndex int lastSelectedRowIndex int
marketBarHeight int marketBarHeight int
maxPages int
page int page int
perPage int perPage int
portfolio *Portfolio portfolio *Portfolio
@ -154,6 +155,7 @@ type Config struct {
OnlyTable bool OnlyTable bool
RefreshRate *uint RefreshRate *uint
PerPage uint PerPage uint
MaxPages uint
} }
// APIKeys is api keys structure // APIKeys is api keys structure
@ -161,9 +163,21 @@ type APIKeys struct {
cmc string cmc string
} }
// DefaultCurrency ...
var DefaultCurrency = "USD"
// DefaultChartRange ...
var DefaultChartRange = "1Y"
// DefaultSortBy ...
var DefaultSortBy = "rank"
// DefaultPerPage ... // DefaultPerPage ...
var DefaultPerPage uint = 100 var DefaultPerPage uint = 100
// MaxPages
var DefaultMaxPages uint = 35
// DefaultColorscheme ... // DefaultColorscheme ...
var DefaultColorscheme = "cointop" var DefaultColorscheme = "cointop"
@ -193,10 +207,15 @@ func NewCointop(config *Config) (*Cointop, error) {
} }
perPage := DefaultPerPage perPage := DefaultPerPage
if config.PerPage != 0 { if config.PerPage > 0 {
perPage = config.PerPage perPage = config.PerPage
} }
maxPages := DefaultMaxPages
if config.MaxPages > 0 {
maxPages = config.MaxPages
}
ct := &Cointop{ ct := &Cointop{
// defaults // defaults
apiChoice: CoinGecko, apiChoice: CoinGecko,
@ -210,13 +229,13 @@ func NewCointop(config *Config) (*Cointop, error) {
chartRanges: ChartRanges(), chartRanges: ChartRanges(),
debug: debug, debug: debug,
chartRangesMap: ChartRangesMap(), chartRangesMap: ChartRangesMap(),
limiter: time.Tick(2 * time.Second), limiter: time.NewTicker(2 * time.Second).C,
filecache: nil, filecache: nil,
State: &State{ State: &State{
allCoins: []*Coin{}, allCoins: []*Coin{},
cacheDir: DefaultCacheDir, cacheDir: DefaultCacheDir,
coinsTableColumns: DefaultCoinTableHeaders, coinsTableColumns: DefaultCoinTableHeaders,
currencyConversion: "USD", currencyConversion: DefaultCurrency,
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility. // DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility.
favoritesBySymbol: make(map[string]bool), favoritesBySymbol: make(map[string]bool),
favorites: make(map[string]bool), favorites: make(map[string]bool),
@ -226,11 +245,12 @@ func NewCointop(config *Config) (*Cointop, error) {
hideStatusbar: config.HideStatusbar, hideStatusbar: config.HideStatusbar,
keepRowFocusOnSort: false, keepRowFocusOnSort: false,
marketBarHeight: 1, marketBarHeight: 1,
maxPages: int(maxPages),
onlyTable: config.OnlyTable, onlyTable: config.OnlyTable,
refreshRate: 60 * time.Second, refreshRate: 60 * time.Second,
selectedChartRange: "1Y", selectedChartRange: DefaultChartRange,
shortcutKeys: DefaultShortcuts(), shortcutKeys: DefaultShortcuts(),
sortBy: "rank", sortBy: DefaultSortBy,
page: 0, page: 0,
perPage: int(perPage), perPage: int(perPage),
portfolio: &Portfolio{ portfolio: &Portfolio{
@ -346,7 +366,7 @@ func NewCointop(config *Config) (*Cointop, error) {
if ct.apiChoice == CoinMarketCap { if ct.apiChoice == CoinMarketCap {
ct.api = api.NewCMC(ct.apiKeys.cmc) ct.api = api.NewCMC(ct.apiKeys.cmc)
} else if ct.apiChoice == CoinGecko { } else if ct.apiChoice == CoinGecko {
ct.api = api.NewCG() ct.api = api.NewCG(perPage, maxPages)
} else { } else {
return nil, ErrInvalidAPIChoice return nil, ErrInvalidAPIChoice
} }

@ -22,7 +22,7 @@ func PrintBitcoinDominance(config *DominanceConfig) error {
if config.APIChoice == CoinMarketCap { if config.APIChoice == CoinMarketCap {
coinAPI = api.NewCMC("") coinAPI = api.NewCMC("")
} else if config.APIChoice == CoinGecko { } else if config.APIChoice == CoinGecko {
coinAPI = api.NewCG() coinAPI = api.NewCG(0, 0)
} else { } else {
return ErrInvalidAPIChoice return ErrInvalidAPIChoice
} }

@ -56,7 +56,7 @@ func GetCoinPrices(config *PricesConfig) ([]string, error) {
if config.APIChoice == CoinMarketCap { if config.APIChoice == CoinMarketCap {
priceAPI = api.NewCMC("") priceAPI = api.NewCMC("")
} else if config.APIChoice == CoinGecko { } else if config.APIChoice == CoinGecko {
priceAPI = api.NewCG() priceAPI = api.NewCG(0, 0)
} else { } else {
return nil, ErrInvalidAPIChoice return nil, ErrInvalidAPIChoice
} }

@ -16,6 +16,9 @@ func NewCC() {
} }
// NewCG new CoinGecko API // NewCG new CoinGecko API
func NewCG() Interface { func NewCG(perPage, maxPages uint) Interface {
return cg.NewCoinGecko() return cg.NewCoinGecko(&cg.Config{
PerPage: perPage,
MaxPages: maxPages,
})
} }

@ -3,6 +3,7 @@ package coingecko
import ( import (
"errors" "errors"
"fmt" "fmt"
"math"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -20,21 +21,40 @@ var ErrPingFailed = errors.New("failed to ping")
// ErrNotFound is the error when the target is not found // ErrNotFound is the error when the target is not found
var ErrNotFound = errors.New("not found") var ErrNotFound = errors.New("not found")
// Config config
type Config struct {
PerPage uint
MaxPages uint
}
// Service service // Service service
type Service struct { type Service struct {
client *gecko.Client client *gecko.Client
maxResultsPerPage int maxResultsPerPage uint
maxPages int maxPages uint
cacheMap sync.Map cacheMap sync.Map
} }
// NewCoinGecko new service // NewCoinGecko new service
func NewCoinGecko() *Service { func NewCoinGecko(config *Config) *Service {
var maxResultsPerPage uint = 250 // absolute max
var maxResults uint = 0
var maxPages uint = 10
var perPage uint = 100
if config.PerPage > 0 {
perPage = config.PerPage
}
if config.MaxPages > 0 {
maxPages = config.MaxPages
maxResults = perPage * maxPages
maxPages = uint(math.Ceil(math.Max(float64(maxResults)/float64(maxResultsPerPage), 1)))
}
client := gecko.NewClient(nil) client := gecko.NewClient(nil)
svc := &Service{ svc := &Service{
client: client, client: client,
maxResultsPerPage: 250, // max is 250 maxResultsPerPage: uint(math.Min(float64(maxResults), float64(maxResultsPerPage))),
maxPages: 10, maxPages: maxPages,
cacheMap: sync.Map{}, cacheMap: sync.Map{},
} }
svc.cacheCoinsIDList() svc.cacheCoinsIDList()
@ -55,7 +75,7 @@ func (s *Service) GetAllCoinData(convert string, ch chan []apitypes.Coin) error
go func() { go func() {
defer close(ch) defer close(ch)
for i := 0; i < s.maxPages; i++ { for i := 0; i < int(s.maxPages); i++ {
if i > 0 { if i > 0 {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }
@ -334,7 +354,7 @@ func (s *Service) getPaginatedCoinData(convert string, offset int, names []strin
for i, name := range names { for i, name := range names {
ids[i] = s.coinNameToID(name) ids[i] = s.coinNameToID(name)
} }
list, err := s.client.CoinsMarket(convertTo, ids, order, s.maxResultsPerPage, page, sparkline, priceChangePercentage) list, err := s.client.CoinsMarket(convertTo, ids, order, int(s.maxResultsPerPage), page, sparkline, priceChangePercentage)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save