Store slug field then use for coin link

pull/275/head
Vuong 3 years ago
parent 8b8db3bd13
commit b2cfe9f614
No known key found for this signature in database
GPG Key ID: 9E32EEF440B0A5D4

@ -8,8 +8,10 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var coinslock sync.Mutex var (
var updatecoinsmux sync.Mutex coinslock sync.Mutex
updatecoinsmux sync.Mutex
)
// UpdateCoins updates coins view // UpdateCoins updates coins view
func (ct *Cointop) UpdateCoins() error { func (ct *Cointop) UpdateCoins() error {
@ -110,6 +112,7 @@ func (ct *Cointop) processCoins(coins []types.Coin) {
PercentChange30D: v.PercentChange30D, PercentChange30D: v.PercentChange30D,
PercentChange1Y: v.PercentChange1Y, PercentChange1Y: v.PercentChange1Y,
LastUpdated: v.LastUpdated, LastUpdated: v.LastUpdated,
Slug: v.Slug,
}) })
if ilast != nil { if ilast != nil {
last, _ := ilast.(*Coin) last, _ := ilast.(*Coin)

@ -197,7 +197,7 @@ func (ct *Cointop) RowLink() string {
return "" return ""
} }
return ct.api.CoinLink(coin.Name) return ct.api.CoinLink(coin.Slug)
} }
// RowLinkShort returns a shortened version of the row url link // RowLinkShort returns a shortened version of the row url link

@ -267,15 +267,13 @@ func (s *Service) Price(name string, convert string) (float64, error) {
return 0, ErrNotFound return 0, ErrNotFound
} }
// CoinLink returns the URL link for the coin func (s *Service) CoinLink(slug string) string {
func (s *Service) CoinLink(name string) string { // slug is API ID of coin
ID := s.coinNameToID(name) return fmt.Sprintf("https://www.coingecko.com/en/coins/%s", slug)
return fmt.Sprintf("https://www.coingecko.com/en/coins/%s", ID)
} }
// SupportedCurrencies returns a list of supported currencies // SupportedCurrencies returns a list of supported currencies
func (s *Service) SupportedCurrencies() []string { func (s *Service) SupportedCurrencies() []string {
// keep these in alphabetical order // keep these in alphabetical order
return []string{ return []string{
"AED", "AED",
@ -462,6 +460,7 @@ func (s *Service) getPaginatedCoinData(convert string, offset int, names []strin
PercentChange1Y: util.FormatPercentChange(percentChange1Y), PercentChange1Y: util.FormatPercentChange(percentChange1Y),
Volume24H: util.FormatVolume(item.TotalVolume), Volume24H: util.FormatVolume(item.TotalVolume),
LastUpdated: util.FormatLastUpdated(item.LastUpdated), LastUpdated: util.FormatLastUpdated(item.LastUpdated),
Slug: item.ID,
}) })
} }
} }

@ -90,6 +90,7 @@ func (s *Service) getPaginatedCoinData(convert string, offset int) ([]apitypes.C
PercentChange7D: util.FormatPercentChange(quote.PercentChange7D), PercentChange7D: util.FormatPercentChange(quote.PercentChange7D),
Volume24H: util.FormatVolume(v.Quote[convert].Volume24H), Volume24H: util.FormatVolume(v.Quote[convert].Volume24H),
LastUpdated: util.FormatLastUpdated(v.LastUpdated), LastUpdated: util.FormatLastUpdated(v.LastUpdated),
Slug: util.FormatSlug(v.Slug),
}) })
} }
return ret, nil return ret, nil
@ -297,7 +298,6 @@ func (s *Service) GetGlobalMarketData(convert string) (apitypes.GlobalMarketData
market, err := s.client.GlobalMetrics.LatestQuotes(&cmc.QuoteOptions{ market, err := s.client.GlobalMetrics.LatestQuotes(&cmc.QuoteOptions{
Convert: convert, Convert: convert,
}) })
if err != nil { if err != nil {
return ret, err return ret, err
} }
@ -332,8 +332,7 @@ func (s *Service) Price(name string, convert string) (float64, error) {
} }
// CoinLink returns the URL link for the coin // CoinLink returns the URL link for the coin
func (s *Service) CoinLink(name string) string { func (s *Service) CoinLink(slug string) string {
slug := util.NameToSlug(name)
return fmt.Sprintf("https://coinmarketcap.com/currencies/%s", slug) return fmt.Sprintf("https://coinmarketcap.com/currencies/%s", slug)
} }

@ -13,7 +13,7 @@ type Interface interface {
GetGlobalMarketData(convert string) (types.GlobalMarketData, error) GetGlobalMarketData(convert string) (types.GlobalMarketData, error)
GetCoinData(name string, convert string) (types.Coin, error) GetCoinData(name string, convert string) (types.Coin, error)
GetCoinDataBatch(names []string, convert string) ([]types.Coin, error) GetCoinDataBatch(names []string, convert string) ([]types.Coin, error)
CoinLink(name string) string CoinLink(slug string) string
SupportedCurrencies() []string SupportedCurrencies() []string
Price(name string, convert string) (float64, error) Price(name string, convert string) (float64, error)
GetExchangeRate(convertFrom, convertTo string, cached bool) (float64, error) // I don't love this caching GetExchangeRate(convertFrom, convertTo string, cached bool) (float64, error) // I don't love this caching

@ -17,6 +17,8 @@ type Coin struct {
PercentChange30D float64 `json:"percentChange30D"` PercentChange30D float64 `json:"percentChange30D"`
PercentChange1Y float64 `json:"percentChange1Y"` PercentChange1Y float64 `json:"percentChange1Y"`
LastUpdated string `json:"lastUpdated"` LastUpdated string `json:"lastUpdated"`
// Slug uses to access the coin's info web page
Slug string `json:"slug"`
} }
// GlobalMarketData struct // GlobalMarketData struct

@ -29,6 +29,10 @@ func FormatName(name string) string {
return name return name
} }
func FormatSlug(slug string) string {
return slug
}
// FormatRank formats the rank value // FormatRank formats the rank value
func FormatRank(rank interface{}) int { func FormatRank(rank interface{}) int {
switch v := rank.(type) { switch v := rank.(type) {

Loading…
Cancel
Save