github.com/wtfutil/wtf@v0.43.0/modules/cryptocurrency/cryptolive/toplist/widget.go (about)

     1  package toplist
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"os"
     8  	"sync"
     9  	"time"
    10  )
    11  
    12  var baseURL = "https://min-api.cryptocompare.com/data/top/exchanges"
    13  
    14  // Widget Toplist Widget
    15  type Widget struct {
    16  	Result string
    17  
    18  	RefreshInterval time.Duration
    19  
    20  	list     *cList
    21  	settings *Settings
    22  }
    23  
    24  // NewWidget Make new toplist widget
    25  func NewWidget(settings *Settings) *Widget {
    26  	widget := Widget{
    27  		settings: settings,
    28  	}
    29  
    30  	widget.list = &cList{}
    31  	widget.setList()
    32  
    33  	return &widget
    34  }
    35  
    36  func (widget *Widget) setList() {
    37  	for symbol, currency := range widget.settings.top {
    38  		toList := widget.makeToList(symbol, currency.limit)
    39  		widget.list.addItem(symbol, currency.displayName, currency.limit, toList)
    40  	}
    41  }
    42  
    43  func (widget *Widget) makeToList(symbol string, limit int) (list []*tCurrency) {
    44  	for _, to := range widget.settings.top[symbol].to {
    45  		list = append(list, &tCurrency{
    46  			name: to.(string),
    47  			info: make([]tInfo, limit),
    48  		})
    49  	}
    50  
    51  	return
    52  }
    53  
    54  /* -------------------- Exported Functions -------------------- */
    55  
    56  // Refresh & update after interval time
    57  func (widget *Widget) Refresh(wg *sync.WaitGroup) {
    58  	if len(widget.list.items) != 0 {
    59  
    60  		widget.updateData()
    61  
    62  		widget.display()
    63  	}
    64  	wg.Done()
    65  }
    66  
    67  /* -------------------- Unexported Functions -------------------- */
    68  
    69  func (widget *Widget) updateData() {
    70  	defer func() {
    71  		if r := recover(); r != nil {
    72  			fmt.Println("recovered in updateSummary()", r)
    73  		}
    74  	}()
    75  
    76  	client := &http.Client{
    77  		Timeout: 5 * time.Second,
    78  	}
    79  
    80  	for _, fromCurrency := range widget.list.items {
    81  		for _, toCurrency := range fromCurrency.to {
    82  
    83  			request := makeRequest(fromCurrency.name, toCurrency.name, fromCurrency.limit)
    84  			response, _ := client.Do(request)
    85  
    86  			var jsonResponse responseInterface
    87  
    88  			err := json.NewDecoder(response.Body).Decode(&jsonResponse)
    89  
    90  			if err != nil {
    91  				os.Exit(1)
    92  			}
    93  
    94  			for idx, info := range jsonResponse.Data {
    95  				toCurrency.info[idx] = tInfo{
    96  					exchange:    info.Exchange,
    97  					volume24h:   info.Volume24h,
    98  					volume24hTo: info.Volume24hTo,
    99  				}
   100  			}
   101  
   102  		}
   103  	}
   104  }
   105  
   106  func makeRequest(fsym, tsym string, limit int) *http.Request {
   107  	url := fmt.Sprintf("%s?fsym=%s&tsym=%s&limit=%d", baseURL, fsym, tsym, limit)
   108  	request, _ := http.NewRequest("GET", url, http.NoBody)
   109  	return request
   110  }