Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 KB | None | 0 0
  1. import requests
  2. import datetime
  3. import time
  4. import configparser
  5. from requests.adapters import HTTPAdapter
  6. from requests.packages.urllib3.util.retry import Retry
  7.  
  8.  
  9.  
  10. def block_number(): # ritorna l'altezza dell'ultimo blocco trovato
  11. t0 = time.time()
  12. max = 0
  13. while max == 0:
  14. try:
  15. time.sleep(3)
  16. response = requests_retry_session().get('https://slushpool.com/stats/json/', timeout=5)
  17. data = response.json()
  18. for key in data:
  19. if key == "blocks":
  20. value = data[key]
  21. for key2 in value:
  22. if str(key2) > str(max):
  23. max = key2
  24. t2 = time.time()
  25. print('It works - Status Code:', response.status_code, '- Took', t2 - t0, 'seconds')
  26. except Exception as x:
  27. t1 = time.time()
  28. print('It failed :(', x.__class__.__name__, '- Took', t1 - t0, 'seconds')
  29. pass
  30. return max
  31.  
  32. def last_block_found(): # ritorna la data dell'ultimo blocco slush trovato in formato YYYY-MM-DD HH:MM:SS
  33. t0 = time.time()
  34. while True:
  35. try:
  36. response = requests_retry_session().get('https://slushpool.com/stats/json/', timeout=5)
  37. except Exception as x:
  38. t1 = time.time()
  39. print('It failed :(', x.__class__.__name__, '- Took', t1 - t0, 'seconds')
  40. pass
  41. else:
  42. t2 = time.time()
  43. print('It works - Status Code:', response.status_code, '- Took', t2 - t0, 'seconds')
  44. data = response.json()
  45. round_started = data["round_started"]
  46. return round_started
  47.  
  48. def nicehash_total_sha256d_speed(): #ritorna il totale della potenza di calcolo sha256d su nicehash in TH/s
  49. t0 = time.time()
  50. while True:
  51. try:
  52. response = requests_retry_session().get('https://api.nicehash.com/api?method=stats.global.24h', timeout=5)
  53. except Exception as x:
  54. t1 = time.time()
  55. print('It failed :(', x.__class__.__name__, '- Took', t1 - t0, 'seconds')
  56. pass
  57. else:
  58. t2 = time.time()
  59. print('It works - Status Code:', response.status_code, '- Took', t2 - t0, 'seconds')
  60. data = response.json()
  61. result = data["result"]
  62. stats = result["stats"]
  63. sha256d = stats[1]
  64. speed = int(eval(sha256d["speed"]) / 1000)
  65. return speed
  66.  
  67. def nicehash_best_sha256d_price(): # ritorna il prezzo migliore d'acquisto per sha256d su nicehash in BTC/PH/DAY
  68. t0 = time.time()
  69. while True:
  70. try:
  71. response = requests_retry_session().get('https://api.nicehash.com/api?method=stats.global.24h', timeout=5)
  72. except Exception as x:
  73. t1 = time.time()
  74. print('It failed :(', x.__class__.__name__, '- Took', t1 - t0, 'seconds')
  75. pass
  76. else:
  77. t2 = time.time()
  78. print('It works - Status Code:', response.status_code, '- Took', t2 - t0, 'seconds')
  79. data = response.json()
  80. result = data["result"]
  81. stats = result["stats"]
  82. sha256d = stats[1]
  83. price = sha256d["price"]
  84. return price
  85.  
  86. def now(): # ritorna la data attuale in formato YYYY-MM-DD HH:MM:SS
  87. return datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
  88.  
  89. def nicehash_get_order_method(api, key):
  90. try:
  91. raw_data = requests.get("https://www.nicehash.com/api?method=orders.get&my&id={0}&key={1}&location=1&algo=1".format(api, key))
  92. data = raw_data.json()
  93. #print(data)
  94. try:
  95. if not data['result']['orders']:
  96. print("Nessun ordine trovato!")
  97. return False
  98. else:
  99. print("Ordine trovato!")
  100. return data['result']['orders'][0]['id']
  101. except:
  102. return print("Dati non codificabili!")
  103. except:
  104. return print("Connessione assente!")
  105.  
  106. def current_block_stats():
  107. t0 = time.time()
  108. while True:
  109. try:
  110. response = requests_retry_session().get('https://slushpool.com/stats/json/', timeout=5)
  111. except Exception as x:
  112. t1 = time.time()
  113. print('It failed :(', x.__class__.__name__, '- Took', t1 - t0, 'seconds')
  114. pass
  115. else:
  116. t2 = time.time()
  117. print('It works - Status Code:', response.status_code, '- Took', t2 - t0, 'seconds')
  118. data = response.json()
  119. active_workers = data["active_workers"]
  120. active_stratum = data["active_stratum"]
  121. round_started = data["round_started"]
  122. round_duration = data["round_duration"]
  123. ghashes_ps = int(eval(data["ghashes_ps"]) / 1000000000000000000000000)
  124. shares = data["shares"]
  125. score = int(eval(data["score"]) / 1000000000)
  126. shares_cdf = eval(data["shares_cdf"])
  127. luck_b10 = eval(data["luck_b10"])
  128. luck_b50 = eval(data["luck_b50"])
  129. luck_b250 = eval(data["luck_b250"])
  130. luck_1 = float(data["luck_1"])
  131. luck_7 = eval(data["luck_7"])
  132. luck_30 = eval(data["luck_30"])
  133. data = [active_workers, active_stratum, round_started, round_duration, ghashes_ps, shares, score, shares_cdf,
  134. luck_b10, luck_b50, luck_b250, luck_1, luck_7, luck_30]
  135. if data != None:
  136. return data
  137.  
  138. def requests_retry_session(
  139. retries=3,
  140. backoff_factor=0.3,
  141. status_forcelist=(500, 502, 504, 503),
  142. session=None,
  143. ):
  144. session = session or requests.Session()
  145. retry = Retry(
  146. total=retries,
  147. read=retries,
  148. connect=retries,
  149. backoff_factor=backoff_factor,
  150. status_forcelist=status_forcelist,
  151. )
  152. adapter = HTTPAdapter(max_retries=retry)
  153. session.mount('http://', adapter)
  154. session.mount('https://', adapter)
  155. return session
  156.  
  157. def start_orders(api, key):
  158. price = nicehash_best_sha256d_price()
  159. START_ORDER_EU = "https://www.nicehash.com/api?method=orders.create&id={0}&key={1}&location=0&algo=1&amount={2}&price={3}&limit=0.05&pool_host={4}&pool_port={5}&pool_user={6}&pool_pass={7}".format(
  160. api, key, amount, price, host, port, worker, password)
  161. START_ORDER_USA = "https://www.nicehash.com/api?method=orders.create&id={0}&key={1}&location=1&algo=1&amount={2}&price={3}&limit=0.05&pool_host={4}&pool_port={5}&pool_user={6}&pool_pass={7}".format(
  162. api, key, amount, price, host, port, worker, password)
  163.  
  164. eu = requests.get(START_ORDER_EU)
  165. print(eu.json())
  166. time.sleep(5)
  167. usa = requests.get(START_ORDER_USA)
  168. print(usa.json())
  169.  
  170. round = False
  171.  
  172. while True:
  173. config = configparser.ConfigParser()
  174. config.read("settings.txt")
  175. api = config.get('Nicehash', 'api')
  176. key = config.get('Nicehash', 'key')
  177. host = config.get('Nicehash', 'host')
  178. port = config.get('Nicehash', 'port')
  179. worker = config.get('Nicehash', 'worker')
  180. password = config.get('Nicehash', 'password')
  181. amount = config.get('Nicehash', 'amount')
  182. min_cdf = float(config.get('Nicehash', 'min_cdf'))
  183. max_luck = float(config.get('Nicehash', 'max_luck'))
  184. while round == False: # se il round non è iniziato
  185. current_block_stat = current_block_stats()
  186. share_cdf = current_block_stat[7]
  187. print(share_cdf, "attendendo", min_cdf)
  188. luck_today = current_block_stat[8]
  189. print(max_luck, "attendendo", luck_today)
  190. if share_cdf > min_cdf and luck_today < max_luck: # se le condizioni non sono più soddisfatte
  191. start_orders(api, key)
  192. round = True
  193. print("ordine")
  194. else:
  195. config.read("settings.txt")
  196. api = config.get('Nicehash', 'api')
  197. key = config.get('Nicehash', 'key')
  198. host = config.get('Nicehash', 'host')
  199. port = config.get('Nicehash', 'port')
  200. worker = config.get('Nicehash', 'worker')
  201. password = config.get('Nicehash', 'password')
  202. amount = config.get('Nicehash', 'amount')
  203. min_cdf = float(config.get('Nicehash', 'min_cdf'))
  204. max_luck = float(config.get('Nicehash', 'max_luck'))
  205. print("Non è il momento di iniziare")
  206.  
  207. if round == True: # se il round è iniziato
  208. config.read("settings.txt")
  209. api = config.get('Nicehash', 'api')
  210. key = config.get('Nicehash', 'key')
  211. host = config.get('Nicehash', 'host')
  212. port = config.get('Nicehash', 'port')
  213. worker = config.get('Nicehash', 'worker')
  214. password = config.get('Nicehash', 'password')
  215. amount = config.get('Nicehash', 'amount')
  216. min_cdf = float(config.get('Nicehash', 'min_cdf'))
  217. max_luck = float(config.get('Nicehash', 'max_luck'))
  218. share_cdf = current_block_stats()[7]
  219. print(share_cdf, min_cdf)
  220. luck_today = current_block_stats()[8]
  221. print(luck_today, max_luck)
  222. time.sleep(10)
  223. #if share_cdf < min_cdf or luck_today < max_luck: # se le condizioni non sono più soddisfatte
  224. print("fine")
  225. round = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement