def handle(self, *args, **kwargs): url = base_url + urllib.parse.urlencode(url_parameters) self.stdout.write("Fetching from:\n" + url) fetched_data = requests.get(url).json() price_timestamp = {} for row in fetched_data: curr = None if Currency.objects.filter(name=row['id']).exists(): curr = Currency.objects.get(name=row['id']) curr.symbol = row['symbol'] curr.current_price = row['current_price'] curr.market_cap = row['market_cap'] curr.total_volume = row['total_volume'] curr.high_24h = row['high_24h'] curr.low_24h = row['low_24h'] curr.price_change_24h = row['price_change_24h'] curr.price_change_percentage_24h = row[ 'price_change_percentage_24h'] else: # creates new record curr = Currency(name=row['id'], symbol=row['symbol'], current_price=row['current_price'], market_cap=row['market_cap'], total_volume=row['total_volume'], high_24h=row['high_24h'], low_24h=row['low_24h'], price_change_24h=row['price_change_24h'], price_change_percentage_24h=row[ 'price_change_percentage_24h']) curr.save() price_timestamp[curr.name] = curr.current_price prices = json.dumps(price_timestamp) self.stdout.write(prices)
def set_default_currency(): try: currency = Currency(code='USD', name='US Dollar', symbol='$') except Exception as e: logging.debug('Exception occured in creating default currency', str(e)) else: currency.save()
def import_currencies(): currencies = get_currencies_from_xml() db_currencies = Currency.objects.all() for db_currency in db_currencies: if db_currency.name not in currencies: db_currency.delete() for name, rate in currencies.items(): try: db_currency = Currency.objects.get(name=name) db_currency.name = name db_currency.rate = rate except Currency.DoesNotExist: db_currency = Currency(name=name, rate=rate) db_currency.save()
def get(self, request): #TODO: remove hardcoding data = urllib.request.urlopen("url...").read() data = json.loads(data) for currency in data: try: cur = Currency.objects.get(currency_id=currency['id']) except Currency.DoesNotExist: cur = Currency() cur.populate_from_dict(currency) cur.save() history = CurrencyHistory() history.populate_from_dict(currency) history.currency = cur history.save() return HttpResponse(data, content_type="application/json")
client = ClientManager(**keys) for exchange in exchanges: print(exchange) exchange = Exchange.objects.get(name=exchange) currencies = client.get_currencies(exchange.name.lower()) for currency in currencies: if not Currency.objects.filter(symbol=currency, exchange=exchange).exists(): new_currency = Currency() new_currency.exchange = exchange new_currency.symbol = currency if currency == "USDT" or currency == "USDC": new_currency.name = "USD" else: new_currency.name = currency new_currency.save() for exchange in exchanges: exchange = Exchange.objects.get(name=exchange) currency_pairs = client.get_currency_pairs(exchange.name.lower()) for currency_pair in currency_pairs: base = Currency.objects.get(symbol=currency_pair['base'], exchange=exchange) quote = Currency.objects.get(symbol=currency_pair['quote'], exchange=exchange) if not CurrencyPair.objects.filter(base=base, quote=quote).exists(): if exchange.name == "Poloniex": print("adding poloniex pair") new_currency = CurrencyPair() new_currency.base = base new_currency.quote = quote