-
Notifications
You must be signed in to change notification settings - Fork 1
/
pricerequester.py
250 lines (208 loc) · 11.5 KB
/
pricerequester.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import json
import sys
import ssl
import urllib.request
from urllib.error import HTTPError, URLError
from bs4 import BeautifulSoup
from datetimeutil import DateTimeUtil
from resultdata import ResultData
MINUTE_PRICE_DAY_NUMBER_LIMIT = 7 # if the request date is older than current time - this value,
# the price returned is a day close price, not a minute price !
IDX_DATA_ENTRY_TO = 1
class PriceRequester:
"""
:seqdiag_note Obtains the RT or historical rates from the cryptocompare.com web site. For historical rates, determines if a minute or close rate is to be obtained.
"""
def __init__(self):
try:
#since ssl prevents requesting the data from CryptoCompare
#when run from Kivy GUI, it must be disabled
self.ctx = ssl.create_default_context()
self.ctx.check_hostname = False
self.ctx.verify_mode = ssl.CERT_NONE
except AttributeError:
#occurs when run in QPython under Python 3.2
self.ctx = None
def getHistoricalPriceAtUTCTimeStamp(self,
crypto,
unit,
timeStampLocalForHistoMinute,
localTz,
timeStampUTCNoHHMMForHistoDay,
exchange):
'''
Why do we pass two different time stamp to the method ?
When requesting a minute price the time stamp obtained
from a local arrow datetime object is used. If we
request a minute price for 15:18 Zurich time (+00.02)
time, the minute price returned is the one at UTC 13:18.
Asking the same price from Mumbay would require to
ask the price for 20:18 (+00.05).
Now, when asking an histo day price, the returned price
is a close price. But crypto trade 24 H a day. The "close"
price time stamp must not be dependant from the location
from which the request is sent. Instead, the "close" price
is the one at the passed date with hour and minute set to 0.
This date is not a localiszd date, but a UTC localization
independent date.
When you go on the Cryptocompare site and you search for
historical prices, the close price for a given date is
the same whatever the location of the user is !
:seqdiag_note Obtainins a minute price if request date < 7 days from now, else a day close price.
:seqdiag_return ResultData
'''
resultData = ResultData()
resultData.setValue(ResultData.RESULT_KEY_CRYPTO, crypto)
resultData.setValue(ResultData.RESULT_KEY_UNIT, unit)
resultData.setValue(ResultData.RESULT_KEY_EXCHANGE, exchange)
if DateTimeUtil.isTimeStampOlderThan(timeStampLocalForHistoMinute, localTz, dayNumberInt=MINUTE_PRICE_DAY_NUMBER_LIMIT):
return self._getHistoDayPriceAtUTCTimeStamp(crypto, unit, timeStampUTCNoHHMMForHistoDay, exchange, resultData)
else:
return self._getHistoMinutePriceAtUTCTimeStamp(crypto, unit, timeStampLocalForHistoMinute, exchange, resultData)
def _getHistoMinutePriceAtUTCTimeStamp(self, crypto, unit, timeStampUTC, exchange, resultData):
timeStampUTCStr = str(timeStampUTC)
url = "https://min-api.cryptocompare.com/data/histominute?fsym={}&tsym={}&limit=1&aggregate=1&toTs={}&e={}".format(crypto, unit, timeStampUTCStr, exchange)
resultData.setValue(ResultData.RESULT_KEY_PRICE_TYPE, resultData.PRICE_TYPE_HISTO_MINUTE)
try:
if self.ctx == None:
#here, run in QPython under Python 3.2
webURL = urllib.request.urlopen(url)
else:
webURL = urllib.request.urlopen(url, context=self.ctx)
except HTTPError as e:
resultData.setError('ERROR - could not complete request ' + url + '. Check your internet connection. Details: ' + str(e.reason) + '.')
except URLError as e:
resultData.setError('ERROR - No internet. Fix the problem and retry !')
except:
the_type, the_value, the_traceback = sys.exc_info()
resultData.setError('ERROR - could not complete request ' + url + '. Reason: ' + str(the_type) + '.')
else:
page = webURL.read()
soup = BeautifulSoup(page, 'html.parser')
dic = json.loads(soup.prettify())
dataListOrDic = dic['Data']
if dataListOrDic != []:
try:
dataEntryDic = dataListOrDic[IDX_DATA_ENTRY_TO]
resultData.setValue(ResultData.RESULT_KEY_PRICE_TIME_STAMP, dataEntryDic['time'])
resultData.setValue(ResultData.RESULT_KEY_PRICE, dataEntryDic['close'])
# except IndexError: # does not happen in any test case
# resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange, isRealTime=False)
except KeyError:
# happens when pair coupled to exchange do not return ay data.
# Either the exchange does not exist or the pair is not
# supported by the exchange.
resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange, isRealTime=False)
else:
resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange, isRealTime=False)
return resultData
def _getHistoDayPriceAtUTCTimeStamp(self, crypto, unit, timeStampUTC, exchange, resultData):
'''
:param crypto:
:param unit:
:param timeStampUTC:
:param exchange:
:param resultData:
:seqdiag_return ResultData
:return: resultData
'''
timeStampUTCStr = str(timeStampUTC)
url = "https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit=1&aggregate=1&toTs={}&e={}".format(crypto, unit, timeStampUTCStr, exchange)
resultData.setValue(ResultData.RESULT_KEY_PRICE_TYPE, resultData.PRICE_TYPE_HISTO_DAY)
try:
if self.ctx == None:
#here, run in QPython under Python 3.2
webURL = urllib.request.urlopen(url)
else:
webURL = urllib.request.urlopen(url, context=self.ctx)
except HTTPError as e:
resultData.setError('ERROR - could not complete request ' + url + '. Reason: ' + str(e.reason) + '.')
except URLError as e:
resultData.setError('ERROR - No internet. Fix the problem and retry !')
except:
the_type, the_value, the_traceback = sys.exc_info()
resultData.setError('ERROR - could not complete request ' + url + '. Reason: ' + str(the_type) + '.')
else:
page = webURL.read()
soup = BeautifulSoup(page, 'html.parser')
dic = json.loads(soup.prettify())
# if dic['Data'] != [] and unit in dic:
dataListOrDic = dic['Data']
if dataListOrDic != []:
try:
dataEntryDic = dataListOrDic[IDX_DATA_ENTRY_TO]
resultData.setValue(ResultData.RESULT_KEY_PRICE_TIME_STAMP, dataEntryDic['time'])
resultData.setValue(ResultData.RESULT_KEY_PRICE, dataEntryDic['close'])
except IndexError: # does not happen in any test case
resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange,isRealTime=False)
except KeyError:
# happens when pair coupled to exchange do not return ay data.
# Either the exchange does not exist or the pair is not
# supported by the exchange.
resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange, isRealTime=False)
else:
resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange, isRealTime=False)
from seqdiagbuilder import SeqDiagBuilder
SeqDiagBuilder.recordFlow()
return resultData
def getCurrentPrice(self, crypto, unit, exchange):
url = "https://min-api.cryptocompare.com/data/price?fsym={}&tsyms={}&e={}".format(crypto, unit, exchange)
resultData = ResultData()
resultData.setValue(ResultData.RESULT_KEY_CRYPTO, crypto)
resultData.setValue(ResultData.RESULT_KEY_UNIT, unit)
resultData.setValue(ResultData.RESULT_KEY_EXCHANGE, exchange)
resultData.setValue(ResultData.RESULT_KEY_PRICE_TYPE, resultData.PRICE_TYPE_RT)
try:
if self.ctx == None:
#here, run in QPython under Python 3.2
webURL = urllib.request.urlopen(url)
else:
webURL = urllib.request.urlopen(url, context=self.ctx)
except HTTPError as e:
resultData.setError('ERROR - could not complete request ' + url + '. Reason: ' + str(e.reason) + '.')
except URLError as e:
resultData.setError('ERROR - No internet. Fix the problem and retry !')
except:
the_type, the_value, the_traceback = sys.exc_info()
resultData.setError('ERROR - could not complete request ' + url + '. Reason: ' + str(the_type) + '.')
else:
page = webURL.read()
soup = BeautifulSoup(page, 'html.parser')
dic = json.loads(soup.prettify())
if unit in dic:
resultData.setValue(ResultData.RESULT_KEY_PRICE_TIME_STAMP, DateTimeUtil.utcNowTimeStamp())
resultData.setValue(ResultData.RESULT_KEY_PRICE, dic[unit]) #current price is indexed by unit symbol in returned dic
else:
resultData = self._handleProviderError(dic, resultData, url, crypto, unit, exchange, isRealTime=True)
from seqdiagbuilder import SeqDiagBuilder
SeqDiagBuilder.recordFlow()
return resultData
def _handleProviderError(self, dic, resultData, url, crypto, unit, exchange, isRealTime):
if 'Message' in dic.keys():
errorMessage = dic['Message']
errorMessage = errorMessage.replace('-', '/') # useful for msg containing a coin pair
if not isRealTime:
errorMessage = self._uniformiseErrorMessage(errorMessage, crypto, unit, exchange)
else:
errorMessage = errorMessage.rstrip(' .')
errorMessage = errorMessage.replace('fsym', 'Symbol')
resultData.setError('PROVIDER ERROR - ' + errorMessage + '.')
else:
resultData.setError('PROVIDER ERROR - ' + 'Request ' + url + ' did not return any data.')
return resultData
def _uniformiseErrorMessage(self, errorMessage, crypto, unit, exchange):
'''
this method transform the provider error msg returned by the historical price queries
(histo minute and histo day so they look identical to the error msg returned for the same
cause by the RT price request.
Histo error msg: e param is not valid the market does not exist for this coin pair
RT error msg ex: Binance market does not exist for this coin pair (BTC-ETH)
:param errorMessage:
:param crypto:
:param unit:
:param exchange:
:return: transformed errorMessage
'''
return errorMessage.replace('e param is not valid the', exchange) + ' ({}/{})'.format(crypto, unit)
if __name__ == '__main__':
pr = PriceRequester()