1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Merge pull request #127 from jeangovil/cryptolive

Added cryptolive widget
This commit is contained in:
Chris Cummer 2018-06-03 18:29:06 -07:00 committed by GitHub
commit d1b05dded4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 454 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package cryptolive
type list struct {
items []*fromCurrency
}
type fromCurrency struct {
name string
displayName string
to []*toCurrency
}
type toCurrency struct {
name string
price float32
}
type cResponse map[string]float32
/* -------------------- Unexported Functions -------------------- */
func (l *list) addItem(name string, displayName string, to []*toCurrency) {
l.items = append(l.items, &fromCurrency{
name: name,
displayName: displayName,
to: to,
})
}

View File

@ -0,0 +1,180 @@
package cryptolive
import (
"encoding/json"
"fmt"
"time"
"net/http"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
var Config *config.Config
var started = false
var baseURL = "https://min-api.cryptocompare.com/data/price"
var ok = true
// Widget define wtf widget to register widget later
type Widget struct {
wtf.TextWidget
// time interval for send http request
updateInterval int
*list
}
// NewWidget Make new instance of widget
func NewWidget() *Widget {
started = false
widget := Widget{
TextWidget: wtf.NewTextWidget(" $ CryptoLive ", "cryptolive", false),
updateInterval: Config.UInt("wtf.mods.cryptolive.updateInterval", 10),
}
widget.setList()
return &widget
}
func (widget *Widget) setList() {
currenciesMap, _ := Config.Map("wtf.mods.cryptolive.currencies")
widget.list = &list{}
for currency := range currenciesMap {
displayName, _ := Config.String("wtf.mods.cryptolive.currencies." + currency + ".displayName")
toList := getToList(currency)
widget.list.addItem(currency, displayName, toList)
}
}
/* -------------------- Exported Functions -------------------- */
// Refresh & update after interval time
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
if started == false {
// this code should run once
go func() {
for {
widget.updateCurrencies()
time.Sleep(time.Duration(widget.updateInterval) * time.Second)
}
}()
}
started = true
widget.UpdateRefreshedAt()
widget.View.Clear()
if !ok {
widget.View.SetText(
fmt.Sprint("Please check your internet connection!"),
)
return
}
display(widget)
}
/* -------------------- Unexported Functions -------------------- */
func display(widget *Widget) {
str := ""
var (
fromNameColor = Config.UString("wtf.mods.cryptolive.colors.from.name", "coral")
fromDisplayNameColor = Config.UString("wtf.mods.cryptolive.colors.from.displayName", "grey")
toNameColor = Config.UString("wtf.mods.cryptolive.colors.to.name", "white")
toPriceColor = Config.UString("wtf.mods.cryptolive.colors.to.price", "green")
)
for _, item := range widget.list.items {
str += fmt.Sprintf("[%s]%s[%s](%s):\n", fromNameColor, item.displayName, fromDisplayNameColor, item.name)
for _, toItem := range item.to {
str += fmt.Sprintf("\t[%s]%s: [%s]%f\n", toNameColor, toItem.name, toPriceColor, toItem.price)
}
str += "\n"
}
widget.View.SetText(fmt.Sprintf("\n%s", str))
}
func getToList(fromName string) []*toCurrency {
toNames, _ := Config.List("wtf.mods.cryptolive.currencies." + fromName + ".to")
var toList []*toCurrency
for _, to := range toNames {
toList = append(toList, &toCurrency{
name: to.(string),
price: -1,
})
}
return toList
}
func (widget *Widget) updateCurrencies() {
defer func() {
recover()
}()
for _, fromCurrency := range widget.list.items {
var (
client http.Client
jsonResponse cResponse
)
client = http.Client{
Timeout: time.Duration(5 * time.Second),
}
request := makeRequest(fromCurrency)
response, err := client.Do(request)
if err != nil {
ok = false
} else {
ok = true
}
defer response.Body.Close()
_ = json.NewDecoder(response.Body).Decode(&jsonResponse)
setPrices(&jsonResponse, fromCurrency)
}
display(widget)
}
func makeRequest(currency *fromCurrency) *http.Request {
fsym := currency.name
tsyms := ""
for _, to := range currency.to {
tsyms += fmt.Sprintf("%s,", to.name)
}
url := fmt.Sprintf("%s?fsym=%s&tsyms=%s", baseURL, fsym, tsyms)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
}
return request
}
func setPrices(response *cResponse, currencry *fromCurrency) {
for idx, toCurrency := range currencry.to {
currencry.to[idx].price = (*response)[toCurrency.name]
}
}

View File

@ -0,0 +1,243 @@
<!DOCTYPE html>
<html lang="en-us" class="wf-firasans-n4-active wf-active">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Enable responsiveness on mobile devices -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<meta name="generator" content="Hugo 0.38.2" />
<title>Module: CryptoLive | WTF - A Terminal Dashboard</title>
<meta content="Module: CryptoLive - WTF - A Terminal Dashboard" property="og:title">
<meta content=" - " property="og:description">
<!-- CSS -->
<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
<link href="https://fonts.googleapis.com/css?family=Fira+Sans:300,300i,400,400i|Roboto+Mono:300,300i,400,400i" rel="stylesheet">
<link rel="stylesheet" href="https://wtfutil.com/css/print.css" media="print">
<link rel="stylesheet" href="https://wtfutil.com/css/poole.css">
<link rel="stylesheet" href="https://wtfutil.com/css/hyde.css">
<link rel="stylesheet" href="https://wtfutil.com/css/syntax.css">
<link rel="stylesheet" href="https://wtfutil.com/css/wtf.css">
<!-- Font-Awesome -->
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl"
crossorigin="anonymous"></script>
<!-- Customised CSS -->
<link rel="stylesheet" href="https://wtfutil.com/css/custom.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Icons -->
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
<link rel="shortcut icon" href="/favicon.png">
<script async defer src="https://buttons.github.io/buttons.js"></script>
</head>
<body>
<div class="sidebar">
<div class="container">
<div class="sidebar-about text-center">
<a href="https://wtfutil.com/">
<img src="/img/wtf.png" alt="WFT Logo" class="" width=""> </a>
<p class="lead">
</p>
</div>
<div>
<h3 style="color: white;">Content</h3>
<ul style="list-style-type: none;">
<li class="sidebar-list-item-1">
<a href="/posts/overview/">Overview</a>
</li>
<li class="sidebar-list-item-1">
<a href="/posts/installation/">Installation</a>
</li>
<li class="sidebar-list-item-1">
<a href="/posts/configuration/">Configuration</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/configuration/attributes/">Attributes</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/configuration/iterm2/">iTerm2</a>
</li>
<li class="sidebar-list-item-1">
<a href="https://github.com/senorprogrammer/wtf/releases">Releases</a>
</li>
</ul>
<ul style="list-style-type: none;">
<li class="sidebar-list-item-1">
<a href="/posts/modules/">Modules</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/bamboohr/">BambooHR</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/clocks/">Clocks</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/cmdrunner/">CmdRunner</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/cryptolive/">CryptoLive</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/git/">Git</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/github/">Github</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/gcal/">Google Calendar</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/ipinfo/">IPInfo</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/jira/">Jira</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/newrelic/">New Relic</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/opsgenie/">OpsGenie</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/power/">Power</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/security/">Security</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/textfile/">Text File</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/todo/">Todo</a>
</li>
<li class="sidebar-list-item-2">
<a href="/posts/modules/weather/">Weather</a>
</li>
</ul>
</div>
<p class="copyright">
&copy; 2018 Chris Cummer.
<br />
<a href="https://creativecommons.org/licenses/by/4.0">Some Rights Reserved</a>.
</p>
</div>
</div>
<div class="content container">
<div class="post">
<h1>Module: CryptoLive</h1>
<div class="col-sm-12 col-md-12">
<span class="text-left post-date meta">
Jun 02, 2018
<br/>
</span>
</div>
<p>Compare crypto currencies using cryptocompare.com</p>
<h2 id="source-code">Source Code</h2>
<div class="highlight">
<pre class="chroma"><code class="language-bash" data-lang="bash">wtf/cryptolive/</code></pre>
</div>
<h2 id="required-env-variables">Required ENV Variables</h2>
<p>None.</p>
<h2 id="keyboard-commands">Keyboard Commands</h2>
<p>None.</p>
<h2 id="configuration">Configuration</h2>
<div class="highlight">
<pre class="chroma"><code class="language-yaml" data-lang="yaml">cryptolive<span class="p">:</span><span class="w">
</span><span class="w"> </span>enabled<span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="w"> </span>position<span class="p">:</span><span class="w">
</span><span class="w"> </span>top<span class="p">:</span><span class="w"> </span><span class="m">1</span><span class="w">
</span><span class="w"> </span>left<span class="p">:</span><span class="w"> </span><span class="m">2</span><span class="w">
</span><span class="w"> </span>height<span class="p">:</span><span class="w"> </span><span class="m">1</span><span class="w">
</span><span class="w"> </span>width<span class="p">:</span><span class="w"> </span><span class="m">1</span><span class="w">
</span><span class="w"> </span>refreshInterval<span class="p">:</span><span class="w"> </span><span class="m">15</span><span class="w">
</span><span class="w"> </span>updateInterval<span class="p">:</span><span class="w"> </span><span class="m">15</span><span class="w">
</span><span class="w"> </span>currencies<span class="p">:</span><span class="w"> </span><span class="w">
</span><span class="w"> </span>BTC<span class="p">:</span><span class="w">
</span><span class="w"> </span>displayName<span class="p">:</span><span class="w"> </span><span class="m">Bitcoin</span><span class="w">
</span><span class="w"> </span>to<span class="p">:</span><span class="w"> </span><span class="w">
</span><span class="w"> - </span>USD<span class="p"></span><span class="w">
</span><span class="w"> - </span>EUR<span class="p"></span><span class="w">
</span><span class="w"> - </span>ETH<span class="p"></span><span class="w">
</span><span class="w"> </span>ETH<span class="p"></span>:<span class="w">
</span><span class="w"> </span>displayName<span class="p">: </span>Ethereum<span class="w">
</span><span class="w"> </span>to<span class="p">: </span><span class="w">
</span><span class="w"> - </span>USD<span class="p"></span><span class="w">
</span><span class="w"> - </span>EUR<span class="p"></span><span class="w">
</span><span class="w"> - </span>ETH<span class="p"></span><span class="w">
</span><span class="w"> </span>colors<span class="p"></span>: <span class="w">
</span><span class="w"> </span>from<span class="p"></span>: <span class="w">
</span><span class="w"> </span>name<span class="p"></span>: coral<span class="w">
</span><span class="w"> </span>displayName<span class="p"></span>: grey<span class="w">
</span><span class="w"> </span>to<span class="p"></span>:<span class="w">
</span><span class="w"> </span>name<span class="p"></span>: white<span class="w">
</span><span class="w"> </span>price<span class="p"></span>: green<span class="w">
</span>
</code></pre>
</div>
<h3 id="attributes">Attributes</h3>
<p>
<code>enabled</code>
<br /> Determines whether or not this module is executed and if its data displayed onscreen.
<br /> Values:
<code>true</code>,
<code>false</code>.</p>
<p>
<code>position</code>
<br /> Defines where in the grid this module&rsquo;s widget will be displayed.
<br />
</p>
<p>
<code>updateInterval</code>
<br /> How often, in seconds, this module will update its data.
<br /> Values: A positive integer
<br/> Default Value: 10</p>
<p>
<code>colors</code>
<br /> Sets color of texts.
<br /> Values: A valid color
</p>
</div>
<div class="footer">
</div>
</div>
</body>
</html>

3
wtf.go
View File

@ -13,6 +13,7 @@ import (
"github.com/senorprogrammer/wtf/bamboohr" "github.com/senorprogrammer/wtf/bamboohr"
"github.com/senorprogrammer/wtf/clocks" "github.com/senorprogrammer/wtf/clocks"
"github.com/senorprogrammer/wtf/cmdrunner" "github.com/senorprogrammer/wtf/cmdrunner"
"github.com/senorprogrammer/wtf/cryptoexchanges/cryptolive"
"github.com/senorprogrammer/wtf/gcal" "github.com/senorprogrammer/wtf/gcal"
"github.com/senorprogrammer/wtf/git" "github.com/senorprogrammer/wtf/git"
"github.com/senorprogrammer/wtf/github" "github.com/senorprogrammer/wtf/github"
@ -173,6 +174,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
weather.Config = Config weather.Config = Config
prettyweather.Config = Config prettyweather.Config = Config
wtf.Config = Config wtf.Config = Config
cryptolive.Config = Config
Widgets = []wtf.Wtfable{ Widgets = []wtf.Wtfable{
bamboohr.NewWidget(), bamboohr.NewWidget(),
@ -192,6 +194,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) {
textfile.NewWidget(app, pages), textfile.NewWidget(app, pages),
todo.NewWidget(app, pages), todo.NewWidget(app, pages),
weather.NewWidget(app, pages), weather.NewWidget(app, pages),
cryptolive.NewWidget(),
prettyweather.NewWidget(), prettyweather.NewWidget(),
} }