Esempio n. 1
0
    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. Please connect 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)

        return resultData
 def testGetMinuteHistoricalPriceForCryptoUnitPairNotSupportedByExchange(
         self):
     crypto = 'BTC'
     unit = 'USD'
     exchange = 'Binance'
     localTimeZone = 'Europe/Zurich'
     #time stamp is always UTC !
     now = DateTimeUtil.localNow(localTimeZone)
     timeStampLocalNow = now.timestamp()
     timeStampUtcNow = DateTimeUtil.utcNowTimeStamp()
     resultData = self.priceRequester.getHistoricalPriceAtUTCTimeStamp(
         crypto, unit, timeStampLocalNow, localTimeZone, timeStampUtcNow,
         exchange)
     self.assertEqual(resultData.getValue(resultData.RESULT_KEY_PRICE_TYPE),
                      resultData.PRICE_TYPE_HISTO_MINUTE)
     self.assertEqual(
         resultData.getValue(resultData.RESULT_KEY_ERROR_MSG),
         "PROVIDER ERROR - Binance market does not exist for this coin pair (BTC/USD)."
     )
     self.assertEqual(crypto,
                      resultData.getValue(resultData.RESULT_KEY_CRYPTO))
     self.assertEqual(unit, resultData.getValue(resultData.RESULT_KEY_UNIT))
     self.assertEqual(exchange,
                      resultData.getValue(resultData.RESULT_KEY_EXCHANGE))
Esempio n. 3
0
 def testUtcNowTimeStamp(self):
     nowZHts = arrow.utcnow().to('Europe/Zurich').timestamp
     utcTimeStamp = DateTimeUtil.utcNowTimeStamp()
     self.assertEqual(nowZHts, utcTimeStamp)