-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
122 lines (108 loc) · 2.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"github.com/hugolgst/rich-go/client"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
)
var (
updateTime int
minerID string
clientID string
LargeImage string
LargeImageText string
)
func init() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Error reading config file, %s", err)
}
updateTime = viper.GetInt("config.updateTime")
minerID = viper.GetString("config.minerID")
clientID = viper.GetString("config.clientID")
LargeImage = viper.GetString("config.LargeImage")
LargeImageText = viper.GetString("config.LargeImageText")
fmt.Println("-- EthermineRPC By Dan (https://github.com/TheJaffaMeme) -- ")
}
func main() {
err := client.Login(clientID)
if err != nil {
panic(err)
}
ethAmount := getEthermineMonies()
usdAmount := getEthPricing(ethAmount)
ethAmountStr := fmt.Sprintf("%.5f", ethAmount)
usdAmountStr := fmt.Sprintf("%.2f", usdAmount)
fmt.Println("ETH = " + ethAmountStr)
fmt.Println("USD = " + usdAmountStr)
now := time.Now()
later := time.Now().Add(time.Minute * time.Duration(updateTime))
err = client.SetActivity(client.Activity{
State: usdAmountStr + " USD",
Details: ethAmountStr + " ETH",
LargeImage: LargeImage,
LargeText: LargeImageText,
Timestamps: &client.Timestamps{
Start: &now,
End: &later,
},
})
if err != nil {
panic(err)
}
fmt.Println("Sleeping for " + strconv.Itoa(updateTime) + " minutes...")
time.Sleep(time.Minute * time.Duration(updateTime))
main()
}
func owie() {
fmt.Println("Error! Check Network Or Try Again Later.")
time.Sleep(time.Second * time.Duration(10))
os.Exit(3)
}
func getEthermineMonies() float64 {
url := "https://api.ethermine.org/miner/" + minerID + "/dashboard"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
owie()
}
res, err := http.DefaultClient.Do(req)
if err != nil {
owie()
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
owie()
}
resp := string(body)
unpaidmoniesraw := gjson.Get(resp, "data.currentStatistics.unpaid").Num
var unpaidmonies float64 = unpaidmoniesraw / 1000000000000000000
return unpaidmonies
}
func getEthPricing(eth float64) float64 {
url := "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
owie()
}
res, err := http.DefaultClient.Do(req)
if err != nil {
owie()
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
owie()
}
resp := string(body)
ethtousd := gjson.Get(resp, "USD").Num
ethinusd := eth * ethtousd
return ethinusd
}