forked from kwikiel/swaping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaper.py
73 lines (57 loc) · 2.01 KB
/
swaper.py
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
import hmac
import time
import urllib
import hashlib
import requests
import config
class BitapiException(Exception):
pass
class Yolo():
def __init__(self, key=config.key, secret=config.secret):
self.key = key
self.secret = secret
def bitapi(self, method, **params):
"""Main method for bitmarket API"""
endpoint = "https://www.bitmarket.pl/api2/"
times = int(time.time())
params.update({
"method": method,
"tonce": times,
"currency": "BTC"
})
post = urllib.urlencode(params)
sign = hmac.HMAC(
str(self.secret), post, digestmod=hashlib.sha512).hexdigest()
headers = {"API-Key": str(self.key), "API-Hash": sign}
raw = requests.post(endpoint, data=post, headers=headers)
if 'error' in raw.json():
raise BitapiException(raw.json())
return raw.json()
def get_cutoff(self):
"""Returns max profit for swaps """
raw = requests.get("http://bitmarket.pl/json/swapBTC/swap.json")
return raw.json()["cutoff"]
def cancel_all(self):
"""Powerful: cancels all open swaps """
swap_list = self.bitapi("swapList")["data"]
for swap in swap_list:
self.bitapi("swapClose", id=swap["id"])
def make_best(self):
# Uses magic contant, remove later.
rclear = '%.4f' % float(self.get_cutoff()*0.95)
if self.get_balance()<0.01:
print "Nothing to do here. No money"
else:
print 'RATE: %s' % rclear
return self.bitapi(
'swapOpen',
amount=float(self.get_balance()),
rate=rclear)
def swap_list(self):
"""Listing all open offers with nice interface"""
swap_list_data = self.bitapi("swapList")["data"]
return swap_list_data
def get_info(self):
return self.bitapi('info')
def get_balance(self):
return self.bitapi('info')['data']['balances']['available']['BTC']