Advertisement
papampi

WTM_AUTO_SWITCH2.py

Oct 11th, 2017 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. #!/usr/bin/env python2.7
  2. # _*_ coding: utf-8 _*_
  3. #### damNmad+papampi+hurvajs77 Whattomine auto switch written by papampi + hurvajs77, forked from smartminer by damNmad
  4.  
  5. import json
  6. import requests
  7. import sys
  8. import urllib
  9.  
  10. configFile = "./WTM.json"
  11. topCoinLogFile = "./WTM_top_coin"
  12.  
  13. # load config
  14. cfg = json.loads(open(configFile).read())
  15. requestUrl = urllib.unquote(urllib.unquote(cfg["WTM_URL"]))
  16. minimumDifference = float(cfg["WTM_MIN_DIFFERENCE"])
  17. includedCoins = cfg["WTM_COINS"].upper()
  18. delimiter = ";"
  19. # load included coins
  20. includedCoins = includedCoins.strip(delimiter)
  21.  
  22. if not includedCoins:
  23.     print "No incluted coins. Please, check 1bash script for WTM settings."
  24.     sys.exit()
  25.  
  26. includedCoins = includedCoins.split(delimiter)
  27.  
  28.  
  29. def saveTopCoin(data):
  30.     logFile = open(topCoinLogFile, "w")
  31.     logFile.write(data)
  32.     logFile.close()
  33.     return
  34.  
  35. # try load previous top coin
  36. try:
  37.     with open(topCoinLogFile) as contentFile:
  38.         content = contentFile.read()
  39. except:
  40.     content = "-:0"
  41.  
  42. topCoin = content.split(":")
  43. print "Currently mining coin: %s, profit: %s" % (topCoin[0], topCoin[1])
  44.  
  45. try:
  46.     httpResponse = requests.get(requestUrl)
  47. except:
  48.     print("Can not get data from WhatToMine.com.")
  49.     raise
  50.  
  51. try:
  52.     data = httpResponse.json()['coins']
  53.     data = data.values()
  54. except:
  55.     print "Invalid JSON"
  56.     raise
  57.  
  58. # filter WTM coins by user selection only
  59. for i in reversed(data):
  60.     if i["tag"] not in includedCoins:
  61.         data.remove(i)
  62.  
  63. # calculate coin profitability
  64. newProfits = {}
  65. for i in data:
  66.     newProfits[i["tag"]] = i["profitability"]
  67. newProfits = sorted(newProfits.items(), key=lambda x: x[1], reverse=True)
  68.  
  69. # save current profit
  70. print "New profits"
  71. profitLog = open("current-profit", "w")
  72. for i, j in newProfits:
  73.     profitLog.write("%s:%s\n" % (i, j))
  74.     print str(i) + ": " + str(j) + " %"
  75. profitLog.close()
  76.  
  77. # is currently mining coin same as a new the most profitability coin?
  78. if newProfits[0][0] == topCoin[0]:
  79.     print "Same coin"
  80.     saveTopCoin(newProfits[0][0] + " : " + newProfits[0][1])
  81.     sys.exit()
  82.  
  83. if (float(newProfits[0][1]) - minimumDifference) < float(topCoin[1]):
  84.     # try find actual top coin and compare their profit with maximum of current profits
  85.     try:
  86.         topCoinNewProfit = filter(lambda x: x["tag"] == topCoin[0], data)[0]
  87.         if (float(newProfits[0][1]) - minimumDifference) > float(topCoinNewProfit["profitability"]):
  88.             print "Currently mining %s coin is no longer profitability %s" % (topCoin[0], topCoin[1])
  89.             print "Switching to new %s coin %s" % (newProfits[0][0], newProfits[0][1])
  90.         else:
  91.             print "Currently mining coin is still more profitability (with subtracted difference) than new profit coin"
  92.             print "Continuing with mining %s coin" % topCoin[0]
  93.     except:
  94.         print "Top coin was not found in list of included coins"
  95.         sys.exit()
  96. else:
  97.     # current profit is higher that currently mining
  98.     print "Found %s coin with higher profitability %s" % (newProfits[0][0], newProfits[0][1])
  99.  
  100. saveTopCoin(newProfits[0][0] + " : " + newProfits[0][1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement