#!/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 ### LuKePicci BTC address: 1MTc9fczdo1CXjvYHbPc987HoWCrzzr2gB # v=0001 : Papampi : Initial release # v=0002 : Papampi : Some changes with damNmad help # v=0003 : Papampi : More changes with hurvajs77 help # v=0004 : Papampi : Added NiceHash with Rumo and CryptAtomeTrader44 help # v=0005 : LuKePicci : Refactor, cleanup and minor modifications to switching … # v=0006 : papampi : Comment out some output logs to prevent conflicts with 8wtm_auto_switch from collections import OrderedDict import json import requests import sys import urllib import urllib2 #configFile = "/home/m1/WTM.json" topCoinLogFile = "/home/m1/WTM_top_coin" # load config #cfg = json.loads(open(configFile).read()) cfg=json.loads(open(sys.argv[1]).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"]) print exchrate 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" miningCoin = content.split(":") #print "Currently mining coin: %s, last profit: %s" % (miningCoin[0], miningCoin[1]) try: httpResponse = requests.get(requestUrl) except: print("Can not get data from WhatToMine.com.") sys.exit() raise try: data = httpResponse.json(object_pairs_hook=OrderedDict)['coins'] except: print "Invalid JSON" sys.exit() raise # filter WTM coins by user selection only for i in data: if data[i]["tag"] == "NICEHASH": data[i]["tag"] = "NICE-" + data[i]["algorithm"].upper() for i in filter(lambda x: data[x]["tag"] not in includedCoins, data): data.pop(i) #save current revenue RevLog = open("WTM_current_revenue", "w") for i in data: RevLog.write("%s:%02.2f\n" % (data[i]["tag"], float(data[i]["btc_revenue"])*exchrate) ) print '%s %02.2f %s' % (data[i]["tag"], float(data[i]["btc_revenue"])*exchrate, currency) RevLog.close() # save current profit print "New profits" profitLog = open("WTM_current_profit", "w") for i in data: profitLog.write("%s:%s\n" % (data[i]["tag"], data[i]["profitability"])) print "%s: %s %%" % (data[i]["tag"], data[i]["profitability"]) profitLog.close() oldTopCoin = { "tag": "-", "profitability": 0 } try: oldTopCoin = filter(lambda x: x["tag"] == miningCoin[0], data.values())[0] except: print "Currently mining coin was not found in inclusion list, will switch to the new best one" minimumDifference = 0 newTopCoin = oldTopCoin # try searching for a new top coin comparing its profit with current mining coin profit try: for coin in data: # is this the currently mining coin or a better one? if miningCoin[0] == data[coin]["tag"] or (float(data[coin]["profitability"]) - minimumDifference) >= float(oldTopCoin["profitability"]): # Switch / update profitability newTopCoin = data[coin] break saveTopCoin(str(newTopCoin["tag"]) + ":" + str(newTopCoin["profitability"])) except: # Some errors print "" sys.exit()