# Path: cryptofolio/models.py
# class Fiat(models.Model):
#     name = models.CharField(max_length=10, primary_key=True)
# 
#     def __str__(self):
#         return "%s" % (self.name)
# 
# Path: cryptofolio/models.py
# class Currency(models.Model):
#     name = models.CharField(max_length=10, primary_key=True)
# 
#     def __str__(self):
#         return "%s" % (self.name)
# 
# Path: cryptofolio/models.py
# class Exchange(models.Model):
#     name = models.CharField(max_length=100, primary_key=True)
#     label = models.CharField(max_length=100, null=True)
# 
#     def __str__(self):
#         return "%s %s" % (self.name, self.label)
# 
# Path: cryptofolio/api/Coinmarket.py
# class Coinmarket:
#     PRODUCTION_BASE_URL = 'https://pro-api.coinmarketcap.com/v1/'
#     TICKER_PATH = 'cryptocurrency/listings/latest'
#     LISTINGS_PATH = 'cryptocurrency/map'
# 
#     def __init__(self, is_sandbox=True):
#         self.logger = Logger(__name__)
#         self.session = Session()
#         headers = {
#                 'Accepts': 'application/json',
#                 'X-CMC_PRO_API_KEY': settings.COINMARKET_API_KEY
#         }
#         self.session.headers.update(headers)
# 
#     # Coinmarketcap API is limited:
#     # Please limit requests to no more than 30 per minute.
#     @sleep_and_retry
#     @limits(calls=27, period=60)
#     def fetch(self, path, params=None):
#         try:
#             response = self.session.get(
#                     '{}{}'.format(self.PRODUCTION_BASE_URL, path),
#                     params=params)
#             data = json.loads(response.text)
#             return data
#         except (ConnectionError, Timeout, TooManyRedirects) as e:
#             self.logger.log(e)
# 
#     def getRates(self, fiat):
#         market = {}
# 
#         parameters = {'start': '1', 'limit': '1000', 'convert': fiat}
#         ticker = self.fetch(self.TICKER_PATH, params=parameters)
# 
#         for t in ticker['data']:
#             name = t['name'].upper()
#             symbol = t['symbol'].upper()
#             price = t['quote'][fiat]['price']
#             if price:
#                 market[name] = float(price)
#                 market[symbol] = float(price)
# 
#         return market
# 
#     def getCoinNames(self):
#         parameters = {'start': '1', 'limit': '4710'}
#         listings = self.fetch(self.LISTINGS_PATH, params=parameters)
#         coins = []
#         for coin in listings['data']:
#             coins.append(coin['symbol'])
#         return coins
# 
# Path: cryptofolio/api/Config.py
# def get_configured_exchange_names():
#     return [Config.BINANCE, Config.BITTREX, Config.COINBASE, Config.POLONIEX,
#             Config.COINBASEPRO, Config.KRAKEN, Config.KUCOIN]
# 
# Path: cryptofolio/api/Config.py
# def get_configured_fiat_names():
#     return ["USD", "EUR", "GBP"]


from django.core.management.base import BaseCommand
from cryptofolio.models import Fiat
from cryptofolio.models import Currency
from cryptofolio.models import Exchange
from cryptofolio.api.Coinmarket import Coinmarket
from cryptofolio.api.Config import get_configured_exchange_names
from cryptofolio.api.Config import get_configured_fiat_names


class Command(BaseCommand):
    help = "Import coin names from Coinmarketcap"

    def handle(self, *args, **options):
        market = Coinmarket()

# Path: cryptofolio/models.py
# class Fiat(models.Model):
#     name = models.CharField(max_length=10, primary_key=True)
# 
#     def __str__(self):
#         return "%s" % (self.name)
# 
# Path: cryptofolio/models.py
# class Currency(models.Model):
#     name = models.CharField(max_length=10, primary_key=True)
# 
#     def __str__(self):
#         return "%s" % (self.name)
# 
# Path: cryptofolio/models.py
# class Exchange(models.Model):
#     name = models.CharField(max_length=100, primary_key=True)
#     label = models.CharField(max_length=100, null=True)
# 
#     def __str__(self):
#         return "%s %s" % (self.name, self.label)
# 
# Path: cryptofolio/api/Coinmarket.py
# class Coinmarket:
#     PRODUCTION_BASE_URL = 'https://pro-api.coinmarketcap.com/v1/'
#     TICKER_PATH = 'cryptocurrency/listings/latest'
#     LISTINGS_PATH = 'cryptocurrency/map'
# 
#     def __init__(self, is_sandbox=True):
#         self.logger = Logger(__name__)
#         self.session = Session()
#         headers = {
#                 'Accepts': 'application/json',
#                 'X-CMC_PRO_API_KEY': settings.COINMARKET_API_KEY
#         }
#         self.session.headers.update(headers)
# 
#     # Coinmarketcap API is limited:
#     # Please limit requests to no more than 30 per minute.
#     @sleep_and_retry
#     @limits(calls=27, period=60)
#     def fetch(self, path, params=None):
#         try:
#             response = self.session.get(
#                     '{}{}'.format(self.PRODUCTION_BASE_URL, path),
#                     params=params)
#             data = json.loads(response.text)
#             return data
#         except (ConnectionError, Timeout, TooManyRedirects) as e:
#             self.logger.log(e)
# 
#     def getRates(self, fiat):
#         market = {}
# 
#         parameters = {'start': '1', 'limit': '1000', 'convert': fiat}
#         ticker = self.fetch(self.TICKER_PATH, params=parameters)
# 
#         for t in ticker['data']:
#             name = t['name'].upper()
#             symbol = t['symbol'].upper()
#             price = t['quote'][fiat]['price']
#             if price:
#                 market[name] = float(price)
#                 market[symbol] = float(price)
# 
#         return market
# 
#     def getCoinNames(self):
#         parameters = {'start': '1', 'limit': '4710'}
#         listings = self.fetch(self.LISTINGS_PATH, params=parameters)
#         coins = []
#         for coin in listings['data']:
#             coins.append(coin['symbol'])
#         return coins
# 
# Path: cryptofolio/api/Config.py
# def get_configured_exchange_names():
#     return [Config.BINANCE, Config.BITTREX, Config.COINBASE, Config.POLONIEX,
#             Config.COINBASEPRO, Config.KRAKEN, Config.KUCOIN]
# 
# Path: cryptofolio/api/Config.py
# def get_configured_fiat_names():
#     return ["USD", "EUR", "GBP"]


# Path: cryptofolio/management/commands/initialize.py
from django.core.management.base import BaseCommand
from cryptofolio.models import Fiat
from cryptofolio.models import Currency
from cryptofolio.models import Exchange
from cryptofolio.api.Coinmarket import Coinmarket
from cryptofolio.api.Config import get_configured_exchange_names
from cryptofolio.api.Config import get_configured_fiat_names



class Command(BaseCommand):
    help = "Import coin names from Coinmarketcap"

    def handle(self, *args, **options):
        market = Coinmarket()

        for name in market.getCoinNames():