forked from damNmad/smartMiner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
WTM_SWITCHER
137 lines (117 loc) · 4.23 KB
/
WTM_SWITCHER
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python2.7
# _*_ coding: utf-8 _*_
#### Whattomine auto switch written by papampi + hurvajs77 + damNmad
### papampi BTC address: 1NsnsnqkkVuopTGvUSGrkMhhug8kg6zgP9
### damNmad BTC address: 1Mtf6K7c3ZhBDcPz91c4wcQ95DxLn88zC
### hurvajs77 BTC address: 3NRMRn3ZKrxwQqkwPfEsjb14dkVDWobyBC
import json
import requests
import sys
import urllib
import urllib2
configFile = "./WTM.json"
topCoinLogFile = "./WTM_top_coin"
# load config
cfg = json.loads(open(configFile).read())
requestUrl = urllib.unquote(urllib.unquote(cfg["WTM_URL"]))
minimumDifference = float(cfg["WTM_MIN_DIFFERENCE"])
includedCoins = cfg["WTM_COINS"].upper()
delimiter = ";"
currency=cfg["currency"]
# load included coins
includedCoins = includedCoins.strip(delimiter)
if not includedCoins:
print "No incluted coins. Please, check 1bash script for WTM settings."
sys.exit()
includedCoins = includedCoins.split(delimiter)
def saveTopCoin(data):
logFile = open(topCoinLogFile, "w")
logFile.write(data)
logFile.close()
return
#Get BTC exchange rate from blockchain.info
try:
exchrate=float(json.loads(urllib2.urlopen("https://blockchain.info/ticker").read())[currency]["last"])
except:
print("Can not get data from blockchain.info")
sys.exit()
raise
#BTC Rates for web, output, ...
print "BTC PRICE: " + str(exchrate) + " " + str(currency)
exchrateLog = open("WTM_BTC_EXCHANGE_RATE", "w")
exchrateLog.write(str(exchrate) + " " + str(currency))
exchrateLog.close()
# try load previous top coin
try:
with open(topCoinLogFile) as contentFile:
content = contentFile.read()
except:
content = "-:0"
topCoin = content.split(":")
print "Currently mining coin: %s, last profit: %s" % (topCoin[0], topCoin[1])
try:
httpResponse = requests.get(requestUrl)
except:
print("Can not get data from WhatToMine.com.")
sys.exit()
raise
try:
data = httpResponse.json()['coins']
data = data.values()
except:
print "Invalid JSON"
sys.exit()
raise
# filter WTM coins by user selection only
for i in reversed(data):
if i["tag"] not in includedCoins:
data.remove(i)
# calculate coin revenue
#print currency + " Revenue"
newRev = {}
for i in data:
newRev[i["tag"]] = float(i["btc_revenue"])
newRev = sorted(newRev.items(), key=lambda x: x[1], reverse=True)
#save current revenue
RevLog = open("WTM_current_revenue", "w")
for i, j in newRev:
RevLog.write("%s:%s\n" % (i, '%02.2f' %(j*exchrate) ))
print i, '%02.2f' %(j*exchrate) +" " + currency
RevLog.close()
# calculate coin profitability
newProfits = {}
for i in data:
newProfits[i["tag"]] = i["profitability"]
newProfits = sorted(newProfits.items(), key=lambda x: x[1], reverse=True)
# save current profit
print "New profits"
profitLog = open("WTM_current_profit", "w")
for i, j in newProfits:
profitLog.write("%s:%s\n" % (i, j))
print str(i) + ": " + str(j) + " %"
profitLog.close()
# is currently mining coin same as a new the most profitability coin?
if newProfits[0][0] == topCoin[0]:
print "Same coin"
saveTopCoin(str(newProfits[0][0]) + ":" + str(newProfits[0][1]))
sys.exit()
if (float(newProfits[0][1]) - minimumDifference) < float(topCoin[1]):
# try find actual top coin and compare their profit with maximum of current profits
try:
topCoinNewProfit = filter(lambda x: x["tag"] == topCoin[0], data)[0]
if (float(newProfits[0][1]) - minimumDifference) > float(topCoinNewProfit["profitability"]):
print "Currently mining %s coin is no longer more profitabile with %s" % (topCoin[0], topCoin[1])
print "Switching to new %s coin %s" % (newProfits[0][0], newProfits[0][1])
else:
print "Currently mining coin is still more profitabile (with subtracted difference) than new top coin"
print "Continuing mining %s " % topCoin[0]
saveTopCoin(topCoin[0] + ":" + topCoinNewProfit["profitability"])
sys.exit()
except:
# Some errors, blankedd this line "Top coin was not found in list of included coins"
print ""
sys.exit()
else:
# current profit is higher that currently mining
print "Found %s coin with higher profitability %s" % (newProfits[0][0], newProfits[0][1])
saveTopCoin(str(newProfits[0][0]) + ":" + str(newProfits[0][1]))