-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKUPI.py
152 lines (98 loc) · 3.65 KB
/
KUPI.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# the inclusion of the tests module is not meant to offer best practices for
# testing in general, but rather to support the `find_packages` example in
# setup.py that excludes installing the "tests" package
import time
import requests
import json
import os
# GLOBAL_DOMAIN = 'http://127.0.0.1:8000'
GLOBAL_DOMAIN = 'http://kupi.net'
GLOBAL_API_KEY = False
def jprint(arr):
print(json.dumps(arr, indent=4, sort_keys=False))
def apiRoute(arr):
if not GLOBAL_API_KEY:
return {
'error': True,
'message': 'Api key not specified!'
}
conf_site = os.path.join(GLOBAL_DOMAIN,'api', 'v1', GLOBAL_API_KEY)
postfix = '/'.join(arr)
url = os.path.join(conf_site, postfix.lower())
try:
print(url)
r = requests.get(url)
response = r.content.decode('utf8')
if len(response) > 0:
data = json.loads(response)
return data
else:
print('Server error')
return False
except Exception as e:
print('Error >> '+str(e))
return False
class KUPINET:
def __init__(self, apikey):
global GLOBAL_API_KEY
GLOBAL_API_KEY = apikey
class Stocks:
def __init__(self, stock_name=False):
self.stock_name = stock_name.strip() if stock_name else False
def getOrders(self, coin_from, coin_to):
coin_from_ = coin_from.strip().lower()
coin_to_ = coin_to.strip().lower()
return apiRoute(['stocks',self.stock_name,'orders',coin_from_,coin_to_])
def getList(self):
return apiRoute(['stocks-list'])
def getAllPairs(self):
return apiRoute(['stocks', self.stock_name, 'all-pairs'])
class Pair:
def __init__(self, coin_from=False, coin_to=False):
self.coin_from_ = coin_from.strip().lower()
self.coin_to_ = coin_to.strip().lower()
def getBestPrices(self):
return apiRoute(['pairs', 'best-prices', self.coin_from_, self.coin_to_])
class BestPrices:
def __init__(self, coin):
self.coin_ = coin.strip().lower()
def Ask(self):
return apiRoute(['best-prices', 'ask', self.coin_])
def Bid(self):
return apiRoute(['best-prices', 'bid', self.coin_])
class Calc:
def __init__(self, coin_from=False, coin_to=False):
self.coin_from = False
self.coin_to = False
if coin_from and coin_to:
self.coin_from = coin_from.strip().lower()
self.coin_to = coin_to.strip().lower()
def Amount(self, amount):
self.amount = str(float(amount))
if not self.coin_from or not self.coin_to:
return {
'error': True,
'message': 'Coins not defined',
}
return apiRoute(['calc', 'math', self.coin_from, self.coin_to, self.amount])
def Data(self):
return apiRoute(['calc', 'data'])
if __name__ == "__main__":
pass
# orders = KUPINET('freeApi').Stocks('Binance').getOrders('ETH','BTC')
# jprint(orders)
# allStocks = KUPINET('freeApi').Stocks().getList()
# jprint(allStocks)
#
# pairs = KUPINET.Stocks('Binance').getAllPairs()
# jprint(pairs)
# prices = KUPINET.Pair('LTC','ETH').getBestPrices()
# jprint(prices)
# bestAsk = KUPINET.BestPrices('LTC').Ask()
# bestBid = KUPINET.BestPrices('LTC').Bid()
# jprint(bestAsk)
# jprint(bestBid)
# calcMath = KUPINET.Calc('LTC','ETH').Amount(10)
# jprint(calcMath)
# calcData = KUPINET.Calc().Data()
# jprint(calcData)