def handle(self, *args, **options): # instance variable unique to each instance self.xchange = Xchange.objects.get(pk=XCHANGE['COINDESK']) self.comparison_currency = 'USD' start_date = datetime.date(datetime.utcnow()) end_date = start_date - timedelta(days=600) currency = Currency() prices = self.getPrice(currency.symbol, end_date=start_date, start_date=end_date) coins = Coins.Coins() if prices == 0 or prices == 'NoneType' or prices == []: logger.info(currency.symbol + "Not found") for key in prices['bpi']: coin = coins.get_coin_type( symbol=currency.symbol, time=int(time.mktime(start_date.timetuple())), exchange=self.xchange) coin.time = int(time.mktime(start_date.timetuple())) coin.close = prices['bpi'][key] coin.xchange = self.xchange coin.currency = currency coin.save()
def get_queryset(self): try: currency = Currency.objects.get(active=0, pk=self.kwargs['pk']) except ObjectDoesNotExist as error: logger.error("{0} Does not exist: {1}".format(self.kwargs['pk'], error)) return None coins = Coins.Coins(currency.symbol) coin = coins.getObject() xchange = Xchange.objects.get(pk=XCHANGE['COIN_MARKET_CAP']) return coin.objects.filter(xchange=xchange, time__gte=self.request.GET.get('time')).order_by('-time')
def handle(self, *args, **options): # instance variable unique to each instance xchange_coins = json.loads(self.getCoins()) for xchange_coin in xchange_coins: if xchange_coin['id'] in ['USD', 'EUR', 'GBP']: continue try: currency = Currency.objects.get(symbol=xchange_coin['id']) print(xchange_coin['id'] + " exists") except ObjectDoesNotExist as error: print(xchange_coin['id'] + " does not exist in our currency list..adding" + error) currency = Currency() symbol = SymbolName.SymbolName(xchange_coin['id']) currency.symbol = symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=symbol) print("added") except: print("failed adding {0}".format(xchange_coin['id'])) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=7) while end_date < start_date: sleep(2) prices = self.getPrice( currency.symbol, start_date=self.__date_to_iso8601( date_time=(start_date - timedelta(days=1)).timetuple()), end_date=self.__date_to_iso8601( date_time=start_date.timetuple()), granularity=86400) coins = Coins.Coins() if len(prices) > 0: for price in prices: coin = coins.get_coin_type(symbol=currency.symbol, time=int(price[0]), exchange=self.xchange) coin.time = int(price[0]) coin.low = float(price[1]) coin.high = float(price[2]) coin.open = float(price[3]) coin.close = float(price[4]) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1)
def getCoinList(self): for index in range(0, 1600, 100): url = 'https://api.coinmarketcap.com/v1/ticker/?start={0}&limit=100'.format( index) response = requests.get(url) if response.status_code != 200: logger.info("not found: {0}".format(response.url)) return False for coin in response.json(): try: currency = Currency.objects.get(symbol=coin['symbol']) logger.info("{0}: exists in Currencies, continuing".format( coin['symbol'])) coins = Coins.Coins(coin['symbol']) coins.createClass() currency.class_name = coins.class_name if currency.name is None: currency.name = coin['name'] if currency.coin_name is None: currency.coin_name = coin['name'] if currency.full_name is None: currency.full_name = coin['name'] currency.save() except ObjectDoesNotExist as error: logger.info( "{0}: not found, Adding it to Currencies: {1}".format( coin['symbol'], error)) coins = Coins.Coins(coin['symbol']) coins.createClass() currency = Currency(name=coin['name'], symbol=coin['symbol'], coin_name=coin['name'], full_name=coin['name'], coinmarketcap=coin['id'], class_name=coins.class_name) currency.save() return True
def handle(self, *args, **options): self.valid_periods = ("oneMin", "fiveMin", "thirtyMin", "hour", "day") self.SHOW_ENDPOINTS = False self.TIMEOUT = 30 self.comparison_currency = 'USD' self.xchange = Xchange.objects.get(pk=XCHANGE['BITTREX']) try: self.bittrex = Bittrex_mod(api_key=self.xchange.api_key, api_secret=self.xchange.api_secret, timeout=self.TIMEOUT, debug_endpoint=self.SHOW_ENDPOINTS, parse_float=Decimal) except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) xchange_coins = self.getCoins() for xchange_coin in xchange_coins['result']: coins = Coins.Coins(xchange_coin['Currency']) try: currency = Currency.objects.get( symbol=xchange_coin['Currency']) logger.info("{0} exists".format(xchange_coin['Currency'])) except ObjectDoesNotExist as error: logger.info( "{0} does not exist in our currency list..Adding".format( xchange_coin['Currency'], error)) coins.createClass() prices = self.getPrice(currency.symbol) if prices == 0: continue if prices == {} or prices is None: logger.info(currency.symbol + " No prices found") continue for price in prices: utc_dt = datetime.strptime(price['T'], '%Y-%m-%dT%H:%M:%S') timestamp = (utc_dt - datetime(1970, 1, 1)).total_seconds() coin = coins.getRecord(time=timestamp, xchange=self.xchange) coin.time = int(timestamp) coin.open = float(price['O']) coin.close = float(price['C']) coin.high = float(price['H']) coin.low = float(price['L']) coin.volume = float(price['V']) coin.xchange = self.xchange coin.currency = currency # coin.save() return
def handle(self, *args, **options): exchanges = json.loads(self.getExchanges()) for exchange in exchanges: try: xchange = Xchange.objects.get(name=exchange['name']) except ObjectDoesNotExist as error: xchange = Xchange() xchange.url = exchange['website'] xchange.name = exchange['name'] xchange.number_symbols = exchange['data_symbols_count'] xchange.start_date = int(time.mktime(datetime.strptime(exchange['data_start'], '%Y-%m-%d').timetuple())) xchange.end_date = int(time.mktime(datetime.strptime(exchange['data_end'], '%Y-%m-%d').timetuple())) xchange.symbols_count = exchange['data_symbols_count'] xchange.save() xchange_coins = json.loads(self.getCoins()) idx = 0 for xchange_coin in xchange_coins: try: currency = Currency.objects.get(symbol=xchange_coin['asset_id_base']) logger.info(xchange_coin['asset_id_base'] + " exists") except ObjectDoesNotExist as error: logger.info(xchange_coin['asset_id_base'] + " does not exist in our currency list") continue now = datetime.now() end_date = now.replace(second=0, minute=0, hour=0) start_date = end_date - timedelta(days=2) while end_date > start_date: prices = self.getPrice(xchange_coin['symbol_id'], start_date=start_date, end_date=end_date) coins = Coins.Coins() if len(prices) > 0: for price in prices: coin = coins.get_coin_type(symbol=currency.symbol, time=int(time.mktime(start_date.timetuple())), exchange=self.xchange) coin.time = int(time.mktime(start_date.timetuple())) coin.open = float(price['price_open']) coin.close = float(price['price_close']) coin.high = float(price['price_high']) coin.low = float(price['price_low']) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date + timedelta(days=1) idx = idx + 1 if idx > 5: return
def handle(self, *args, **options): self.xchange = Xchange.objects.get(pk=XCHANGE['KRAKEN']) try: self.kraken = krakenex.API() except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) self.comparison_currency = 'USD' xchange_coins = self.getCoins() for xchange_coin in xchange_coins: try: currency = Currency.objects.get( symbol=xchange_coins[xchange_coin]['altname']) print(xchange_coins[xchange_coin]['altname'] + " exists") except ObjectDoesNotExist as error: print(xchange_coins[xchange_coin]['altname'] + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName( xchange_coins[xchange_coin]['altname']) currency.symbol = symbol.parse_symbol() try: currency.save() currency = Currency.objects.get( symbol=xchange_coins[xchange_coin]['altname']) print("added") except: print("failed adding {0}".format( xchange_coins[xchange_coin]['altname'])) continue prices = self.getPrice(currency.symbol) if prices != 0: for price in prices: coins = Coins.Coins() coin = coins.get_coin_type( symbol=xchange_coins[xchange_coin]['altname'], time=int(price[0]), exchange=self.xchange) coin.time = int(price[0]) coin.open = float(price[1]) coin.close = float(price[4]) coin.high = float(price[2]) coin.low = float(price[3]) coin.volume = float(price[6]) coin.xchange = self.xchange coin.currency = currency coin.save()
def handle(self, *args, **options): # instance variable unique to each instance xchange_coins = json.loads(self.getCoins()) for xchange_coin in xchange_coins: if xchange_coin['id'] in ['USD', 'EUR', 'GBP']: continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=1000) coins = Coins.Coins(xchange_coin['id']) try: currency = Currency.objects.get(symbol=xchange_coin['id']) logger.info("{0} exists".format(xchange_coin['id'])) except ObjectDoesNotExist as error: logger.info( "{0} does not exist in our currency list..Adding".format( xchange_coin['id'], error)) coins.createClass() while end_date < start_date: sleep(2) prices = self.getPrice( currency.symbol, start_date=self.__date_to_iso8601( date_time=(start_date - timedelta(days=1)).timetuple()), end_date=self.__date_to_iso8601( date_time=start_date.timetuple()), granularity=86400) if len(prices) > 0: for price in prices: coin = coins.getRecord(time=int(price[0]), xchange=self.xchange) coin.time = int(price[0]) coin.low = float(price[1]) coin.high = float(price[2]) coin.open = float(price[3]) coin.close = float(price[4]) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1)
def handle(self, *args, **options): xchange_coins = self.getCoins() for xchange_coin in xchange_coins: try: currency = Currency.objects.get(symbol=xchange_coins[xchange_coin]['Symbol']) print(xchange_coins[xchange_coin]['Symbol'] + " exists") except ObjectDoesNotExist as error: print(xchange_coins[xchange_coin]['Symbol'] + " does not exist in our currency list..adding" + error) continue currency = Currency() symbol = SymbolName.SymbolName(xchange_coins[xchange_coin]['Symbol']) currency.symbol = symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=symbol) print("added") except: print("failed adding {0}".format(xchange_coins[xchange_coin]['Symbol'])) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=7) while end_date < start_date: start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(currency.symbol.replace('*', ''), start_date_ts) if len(prices) > 0: for price in prices: if prices[price] == 'Error': break coins = Coins.Coins() coin = coins.get_coin_type(symbol=currency.symbol.replace('*', ''), time=start_date_ts, exchange=self.xchange) coin.time = start_date_ts coin.close = float(prices[price]['USD']) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1) return 0
def handle(self, *args, **options): # instance variable unique to each instance try: self.client = Client(self.xchange.api_key, self.xchange.api_secret, api_version='2018-01-14') except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) xchange_coins = self.client.get_currencies() for xchange_coin in xchange_coins['data']: if xchange_coin is None: continue coins = Coins.Coins(xchange_coin['id']) try: currency = Currency.objects.get(symbol=xchange_coin['id']) logger.info("{0} exists".format(xchange_coin['id'])) except ObjectDoesNotExist as error: logger.info( "{0} does not exist in our currency list..Adding".format( xchange_coin['id'], error)) coins.createClass() now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=10) while end_date < start_date: start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(xchange_coin['id'], date=start_date.strftime('%Y-%m-%d')) if len(prices) != 0: if prices is None: break coin = coins.getRecord(time=start_date_ts, xchange=self.xchange) coin.time = int(calendar.timegm(start_date.timetuple())) coin.close = float(prices['amount']) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1)
def handle(self, *args, **options): # instance variable unique to each instance try: self.client = Client(self.xchange.api_key, self.xchange.api_secret, api_version='2018-01-14') except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format( error)) xchange_coins = json.loads(self.getCoins()) for xchange_coin in xchange_coins['data']: try: currency = Currency.objects.get(symbol=xchange_coin['id']) print(xchange_coin['id'] + " exists") except ObjectDoesNotExist as error: print(xchange_coin['id'] + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName(xchange_coin['id']) currency.symbol = symbol.parse_symbol() try: currency.save() print("added") except: print("failed adding {0}".format(xchange_coin['id'])) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=10) while end_date < start_date: prices = self.getPrice(xchange_coin['id'], date=start_date.strftime('%Y-%m-%d')) coins = Coins.Coins() if len(prices) != 0: coin = coins.get_coin_type(symbol=currency.symbol, time=int(calendar.timegm(start_date.timetuple())), exchange=self.xchange) coin.time = int(calendar.timegm(start_date.timetuple())) coin.close = float(prices.amount) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1)
def handle(self, *args, **options): exchange_coins = self.getCoins() for exchange_coin in exchange_coins: print(exchange_coin) try: currency = Currency.objects.get(symbol=exchange_coin) print(exchange_coin + " exists") except ObjectDoesNotExist as error: print(exchange_coin + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName(exchange_coin) currency.symbol = symbol.parse_symbol() try: currency.save() print("added") except: print("failed adding {0}".format(exchange_coin)) continue now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=5) start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(currency.symbol, start_date_ts, 9999999999, 14400) coins = Coins.Coin() for price in prices: print(price) utc_dt = datetime.strptime(price['T'], '%Y-%m-%dT%H:%M:%S') timestamp = (utc_dt - datetime(1970, 1, 1)).total_seconds() coin = coins.get_coin_type(symbol=currency.symbol, time=int(timestamp), exchange=self.xchange) coin.time = int(timestamp) coin.open = float(price['O']) coin.close = float(price['C']) coin.high = float(price['H']) coin.low = float(price['L']) coin.volume = float(price['V']) coin.xchange = self.xchange coin.currency = currency #coin.save() return
def handle(self, *args, **options): xchange_coins = self.getCoins() for xchange_coin in xchange_coins: if xchange_coin is None: continue coins = Coins.Coins(xchange_coins[xchange_coin]['Symbol']) try: currency = Currency.objects.get( symbol=xchange_coins[xchange_coin]['Symbol']) logger.info("{0} exists".format( xchange_coins[xchange_coin]['Symbol'])) except ObjectDoesNotExist as error: logger.info( "{0} does not exist in our currency list..Adding".format( xchange_coins[xchange_coin]['Symbol'], error)) coins.createClass() now = datetime.now() start_date = now.replace(second=0, minute=0, hour=0) end_date = start_date - timedelta(days=300) while end_date < start_date: start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(currency.symbol.replace('*', ''), start_date_ts) if len(prices) > 0: for price in prices: if prices[price] == 'Error': break coin = coins.getRecord(time=start_date_ts, xchange=self.xchange) coin.time = start_date_ts coin.close = float(prices[price]['USD']) coin.xchange = self.xchange coin.currency = currency coin.save() start_date = start_date - timedelta(days=1) return 0
def handle(self, *args, **options): self.xchange = Xchange.objects.get(pk=XCHANGE['CRYPTOINDEX']) self.comparison_currency = 'USD' self.symbol = 'CRIX' prices = self.getPrice(self.symbol) if prices == 0: return 0 coins = Coins.Coins(self.symbol) currency = Currency.objects.get(symbol=self.symbol) for price in prices: utc_dt = datetime.strptime(price['date'], '%Y-%M-%d') timestamp = (utc_dt - datetime(1970, 1, 1)).total_seconds() coin = coins.getRecord(time=int(timestamp), xchange=self.xchange) coin.time = int(timestamp) coin.close = float(price['price']) coin.xchange = self.xchange coin.currency = currency coin.save() return 0
def handle(self, *args, **options): xchange_coins = self.getCoins() for xchange_coin in xchange_coins: coins = Coins.Coins(xchange_coin) try: currency = Currency.objects.get(symbol=xchange_coin) logger.info("{0} exists".format(xchange_coin)) except ObjectDoesNotExist as error: logger.info( "{0} does not exist in our currency list..Adding".format( xchange_coin, error)) coins.createClass() start_date = datetime.now().replace(second=0, minute=0, hour=0) - timedelta(days=400) end_date = 9999999999 start_date_ts = calendar.timegm(start_date.timetuple()) prices = self.getPrice(currency.symbol, start_date_ts, end_date, 14400) for price in prices: if price == 'error': logger.info("error getting prices") continue utc_dt = datetime.strptime(price['T'], '%Y-%m-%dT%H:%M:%S') timestamp = (utc_dt - datetime(1970, 1, 1)).total_seconds() coin = coins.getRecord(time=timestamp, xchange=self.xchange) coin.time = int(timestamp) coin.open = float(price['O']) coin.close = float(price['C']) coin.high = float(price['H']) coin.low = float(price['L']) coin.volume = float(price['V']) coin.xchange = self.xchange coin.currency = currency coin.save() return
def handle(self, *args, **options): self.xchange = Xchange.objects.get(pk=XCHANGE['KRAKEN']) try: self.kraken = krakenex.API() except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) self.comparison_currency = 'USD' xchange_coins = self.getCoins() for xchange_coin in xchange_coins: coins = Coins.Coins(xchange_coins[xchange_coin]['altname']) try: currency = Currency.objects.get( symbol=xchange_coins[xchange_coin]['altname']) logger.info("{0} exists".format( xchange_coins[xchange_coin]['altname'])) except ObjectDoesNotExist as error: logger.info( "{0} does not exist in our currency list..Adding".format( xchange_coins[xchange_coin]['altname'], error)) coins.createClass() prices = self.getPrice(currency.symbol) if prices != 0: for price in prices: coin = coins.getRecord(time=int(price[0]), xchange=self.xchange) coin.time = int(price[0]) coin.open = float(price[1]) coin.close = float(price[4]) coin.high = float(price[2]) coin.low = float(price[3]) coin.volume = float(price[6]) coin.xchange = self.xchange coin.currency = currency coin.save()
def post(self, request): previewFund = json.loads(request.body.decode("utf-8")) end_date = datetime.utcnow().replace(hour=0, second=0, minute=0, microsecond=0) start_date = end_date - timedelta(days=30) previewBasket = [] while start_date < end_date: value = 0 for previewCurrency in previewFund['fundPreview']: try: currency = Currency.objects.get(active=0, pk=previewCurrency['currencyId']) except ObjectDoesNotExist as error: logger.error("{0} Does not exist: {1}".format(self.kwargs['pk'], error)) return None coins = Coins.Coins(currency.symbol) xchange = Xchange.objects.get(pk=XCHANGE['COIN_MARKET_CAP']) coin = coins.getRecord(xchange=xchange, time=calendar.timegm(start_date.timetuple())) value += (coin.close * previewCurrency['percent']) previewBasket.append({"name": start_date.timestamp(), "value": value}) start_date += timedelta(days=1) return JsonResponse(previewBasket, safe=False)
def parse(self, start_date): r = requests.get('https://coinmarketcap.com/historical/{0}/'.format( start_date.strftime('%Y%m%d'))) if r.status_code != 200: print("not found {0}".format(r.url)) return soup = BeautifulSoup(r.content, "html.parser") table = soup.find('tbody') for row in table.findAll('tr'): cells = row.findAll('td') symbol = cells[1].span.a.text symbol = cells[2].text.strip() market_cap = cells[3]['data-usd'] try: market_cap = float(market_cap) except: market_cap = 0 try: price = float(cells[4].a['data-usd']) except: price = 0 try: circulating_supply = cells[5].a['data-supply'] except: circulating_supply = cells[5].span['data-supply'] try: circulating_supply = int(float(circulating_supply)) except: circulating_supply = 0 new_symbol = SymbolName.SymbolName(symbol) try: currency = Currency.objects.get( symbol=new_symbol.parse_symbol()) except ObjectDoesNotExist as error: print(symbol + " does not exist in our currency list..continuing") continue currency = Currency() currency.symbol = new_symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=currency.symbol) print(symbol) except: print("failed adding {0}".format(currency.symbol)) continue coins = Coins.Coins() coin = coins.get_coin_type( symbol=symbol, time=int(calendar.timegm(start_date.timetuple())), exchange=self.xchange) if coin is not None: coin.xchange = self.xchange coin.close = price coin.currency = currency coin.time = int(calendar.timegm(start_date.timetuple())) coin.market_cap = market_cap coin.total_supply = circulating_supply coin.save() else: print("no class " + symbol) return
def parseHistoricalPage(self, currency_c, start_date, end_date): r = requests.get( 'https://coinmarketcap.com/currencies/{0}/historical-data/?start={1}&end={2}' .format(currency_c[2].lower(), start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d'))) if r.status_code != 200: print("not found {0}".format(r.url)) return soup = BeautifulSoup(r.content, "html.parser") table = soup.find('tbody') for row in table.findAll('tr'): cells = row.findAll('td') try: timestamp = datetime.datetime.strptime(cells[0].text.strip(), "%b %d, %Y").date() except: timestamp = 0 try: open = float(cells[1].text.strip()) except: open = 0 try: high = float(cells[2].text.strip()) except: high = 0 try: low = float(cells[3].text.strip()) except: low = 0 try: close = float(cells[4].text.strip()) except: close = 0 try: volume = int(cells[5].text.replace(',', '')) except: volume = 0 try: market_cap = int(cells[6].text.replace(',', '')) except: market_cap = 0 new_symbol = SymbolName.SymbolName(currency_c[1]) try: currency = Currency.objects.get( symbol=new_symbol.parse_symbol()) except ObjectDoesNotExist as error: print(symbol + " does not exist in our currency list..continuing") continue currency = Currency() currency.symbol = new_symbol.parse_symbol() try: currency.save() currency = Currency.objects.get(symbol=currency.symbol) print(symbol) except: print("failed adding {0}".format(currency.symbol)) continue coins = Coins.Coins() coin = coins.get_coin_type( symbol=new_symbol.symbol, time=int(calendar.timegm(timestamp.timetuple())), exchange=self.xchange) if coin is not None: coin.xchange = self.xchange coin.open = open coin.close = close coin.high = high coin.low = low coin.volume = volume coin.currency = currency coin.time = int(calendar.timegm(timestamp.timetuple())) coin.market_cap = market_cap coin.save() try: topCoin = TopCoins.objects.get( currency=currency, xchange=self.xchange, time=int(calendar.timegm(timestamp.timetuple()))) except ObjectDoesNotExist as error: topCoin = TopCoins() topCoin.currency = currency topCoin.market_cap = float(market_cap) topCoin.price = float(close) topCoin.xchange = self.xchange topCoin.time = int(calendar.timegm(timestamp.timetuple())) topCoin.save() else: print("no class " + symbol) return
def parseHistoricalPage(self, currency, start_date, end_date): try: url = 'https://coinmarketcap.com/currencies/{0}/historical-data/?start={1}&end={2}'\ .format(currency.coinmarketcap, start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d')) r = requests.get(url) except ConnectionError as error: logger.error("Connection Error: {0}".format(error)) return if r.status_code != 200: logger.error("not found {0}".format(r.url)) return soup = BeautifulSoup(r.content, "html.parser") table = soup.find('tbody') for row in table.findAll('tr'): cells = row.findAll('td') try: timestamp = datetime.datetime.strptime(cells[0].text.strip(), "%b %d, %Y").date() except Exception as error: logger.error("Error getting timestamp: {0}: {1}".format( cells[0], error)) continue try: open_price = float(cells[1].text.strip().replace(',', '')) except Exception as error: logger.info("Error getting open price:{0}: {1}".format( cells[1], error)) open_price = 0 try: high = float(cells[2].text.strip().replace(',', '')) except Exception as error: logger.info("Error getting high price:{0}: {1}".format( cells[2], error)) high = 0 try: low = float(cells[3].text.strip().replace(',', '')) except Exception as error: logger.info("Error getting low price: {0}: {1}".format( cells[3], error)) low = 0 try: close_price = float(cells[4].text.strip().replace(',', '')) except Exception as error: logger.info("Error getting close price:{0}: {1}".format( cells[4], error)) close_price = 0 try: volume = int(cells[5].text.replace(',', '')) except Exception as error: logger.info("Error getting volume {0}: {1}".format( cells[5], error)) volume = 0 try: if cells[6].text == '-': market_cap = 0 else: market_cap = int(cells[6].text.replace(',', '')) except Exception as error: logger.info("Error getting market cap{0}: {1}".format( cells[6], error)) market_cap = 0 coin = Coins.Coins(currency.symbol) coin_record = coin.getRecord(time=int( calendar.timegm(timestamp.timetuple())), xchange=self.xchange) if coin_record is not None: coin_record.xchange = self.xchange coin_record.open = open_price coin_record.close = close_price coin_record.high = high coin_record.low = low coin_record.volume = volume coin_record.currency = currency coin_record.time = int(calendar.timegm(timestamp.timetuple())) coin_record.market_cap = market_cap coin_record.save() try: top_coin = TopCoins.objects.get( currency=currency, xchange=self.xchange, time=int(calendar.timegm(timestamp.timetuple()))) except ObjectDoesNotExist as error: logger.error( "Top Coin does not exist: {0} {1} -> adding it: error:{2}" .format(currency.pk, currency.symbol, error)) top_coin = TopCoins() top_coin.currency = currency top_coin.market_cap = float(market_cap) top_coin.price = float(close_price) top_coin.xchange = self.xchange top_coin.time = int(calendar.timegm(timestamp.timetuple())) top_coin.save() else: logger.error("No class: {0} :not adding".format( currency.class_name)) return
def parse(self, start_date): r = requests.get('https://coinmarketcap.com/historical/{0}/'.format( start_date.strftime('%Y%m%d'))) if r.status_code != 200: logger.error("not found {0}".format(r.url)) return soup = BeautifulSoup(r.content, "html.parser") table = soup.find('tbody') for row in table.findAll('tr'): cells = row.findAll('td') symbol = cells[2].text.strip() market_cap = cells[3]['data-usd'] try: market_cap = float(market_cap) except Exception as error: logger.error("Failed Market Cap: {0}: {1}".format( market_cap, error)) market_cap = 0 try: p = cells[4].a['data-usd'] p = p.replace(',', '') price = float(p) except Exception as error: logger.error("Failed Price: {0}: {1}".format(price, error)) price = 0 try: circulating_supply = cells[5].a['data-supply'] except Exception as error: logger.error("Failed circulating supply: {0}:{1}".format( circulating_supply, error)) circulating_supply = cells[5].span['data-supply'] try: circulating_supply = int(float(circulating_supply)) except Exception as error: logger.error("Failedcirculating supply: {0}: {1}".format( circulating_supply, error)) circulating_supply = 0 try: currency = Currency.objects.get(symbol=symbol) except ObjectDoesNotExist as error: logger.error( "{0} does not exist in our currency list..continuing: error: {1}" .format(symbol, error)) continue coins = Coins.Coins(symbol) coin = coins.getRecord(time=int( calendar.timegm(start_date.timetuple())), xchange=self.xchange) if coin is not None: coins.createClass() coin.xchange = self.xchange coin.close = price coin.currency = currency coin.time = int(calendar.timegm(start_date.timetuple())) coin.market_cap = market_cap coin.total_supply = circulating_supply coin.save() else: logger.error("no class for symbol {0}:".format(symbol)) return
def handle(self, *args, **options): self.valid_periods = ("oneMin", "fiveMin", "thirtyMin", "hour", "day") self.SHOW_ENDPOINTS = False self.TIMEOUT = 30 self.comparison_currency = 'USD' self.xchange = Xchange.objects.get(pk=XCHANGE['BITTREX']) try: self.bittrex = Bittrex_mod(api_key=self.xchange.api_key, api_secret=self.xchange.api_secret, timeout=self.TIMEOUT, debug_endpoint=self.SHOW_ENDPOINTS, parse_float=Decimal) except ObjectDoesNotExist as error: logging.debug('Client does not exist:{0}'.format(error)) exchange_coins = self.getCoins() for exchange_coin in exchange_coins['result']: try: currency = Currency.objects.get( symbol=exchange_coin['Currency']) print(exchange_coin['Currency'] + " exists") except ObjectDoesNotExist as error: print(exchange_coin['Currency'] + " does not exist in our currency list..adding") currency = Currency() symbol = SymbolName.SymbolName(exchange_coin['Currency']) currency.symbol = symbol.parse_symbol() try: currency.save() print("added") except: print("failed adding {0}".format( exchange_coin['Currency'])) continue prices = self.getPrice(currency.symbol) if prices == 0: continue coins = Coins.Coins() if prices == {} or prices == None or type(prices) == 'None': print(currency.symbol + " No prices found") continue for price in prices: utc_dt = datetime.strptime(price['T'], '%Y-%m-%dT%H:%M:%S') timestamp = (utc_dt - datetime(1970, 1, 1)).total_seconds() coin = coins.get_coin_type(symbol=currency.symbol, time=int(timestamp), exchange=self.xchange) coin.time = int(timestamp) coin.open = float(price['O']) coin.close = float(price['C']) coin.high = float(price['H']) coin.low = float(price['L']) coin.volume = float(price['V']) coin.xchange = self.xchange coin.currency = currency coin.save() return