# Path: zaifbot/db/dao/candle_sticks.py
# class CandleSticksDao(DaoBase):
#     def __init__(self, currency_pair, period):
#         super().__init__()
#         self._currency_pair = str(currency_pair)
#         self._period = str(period)
# 
#     def _get_model(self):
#         return CandleSticks
# 
#     def get_by_duration(self, count, start_time, end_time, *, closed=False):
#         db_records = self._get_by_duration_db(start_time, end_time, closed=closed)
#         db_records_dict = self.rows2dicts(db_records)
#         if len(db_records_dict) >= count:
#             return db_records
# 
#         api_records = self._get_by_duration_web(count, start_time, end_time)
#         return api_records
# 
#     def _get_by_duration_web(self, count, start_time, to_epoch_time):
#         chart_api = BotChartApi()
#         records = chart_api.history(self._currency_pair, self._period, start_time, to_epoch_time)
# 
#         while len(records) < count:
#             random_sleep(1, 2)
#             records = chart_api.history(self._currency_pair, self._period, start_time, to_epoch_time)
# 
#         self._save_records(records)
#         return records
# 
#     def _get_by_duration_db(self, start_time, end_time, *, closed=False):
#         with self._session() as s:
#             result = s.query(self._Model).filter(
#                 and_(self._Model.time <= end_time,
#                      self._Model.time >= start_time,
#                      self._Model.currency_pair == self._currency_pair,
#                      self._Model.period == self._period,
#                      self._Model.closed == int(closed)
#                      )
#             ).order_by(self._Model.time).all()
#         return result
# 
#     def _save_records(self, records):
#         new_records = [
#             merge_dict(record, {'currency_pair': self._currency_pair,
#                                 'period': self._period,
#                                 'closed': True}
#                        )
#             for record in records
#         ]
#         self.create_multiple(new_records)
# 
# Path: zaifbot/exchange/period.py
# def Period(period):
#     for cls in _TradePeriod.__subclasses__():
#         if isinstance(period, str):
#             if cls.is_my_label(period):
#                 return cls(period)
#             continue
# 
#         if isinstance(period, int):
#             if cls.is_my_sec(period):
#                 return cls(period)
#             continue
# 
#         if isinstance(period, _TradePeriod):
#             return period
# 
#     raise ValueError('illegal argument received')
# 
# Path: zaifbot/exchange/currency_pairs.py
# class CurrencyPair:
#     _lock = Lock()
#     _instances = {}
# 
#     def __new__(cls, name):
#         with cls._lock:
#             name = str(name)
#             if cls._instances.get(name, None) is None:
#                 cls._instances[name] = super().__new__(cls)
#         return cls._instances[name]
# 
#     def __init__(self, name):
#         self._name = str(name)
#         self._info = _ZaifCurrencyPairsInfo()[self._name]
# 
#     def __str__(self):
#         return self._name
# 
#     @property
#     def name(self):
#         return self._name
# 
#     @property
#     def info(self):
#         return self._info
# 
#     @property
#     def is_token(self):
#         return self._info['is_token']
# 
# Path: zaifbot/utils/utils.py
# def int_epoch_time(t=None):
#     if not t:
#         return int(time.time())
#     return int(t)


from zaifbot.db.dao.candle_sticks import CandleSticksDao
from zaifbot.exchange.period import Period
from zaifbot.exchange.currency_pairs import CurrencyPair
from zaifbot.utils.utils import int_epoch_time


class CandleSticks:
    MAX_COUNT = 1500

    def __init__(self, currency_pair, period):
        self._currency_pair = CurrencyPair(currency_pair)

# Path: zaifbot/db/dao/candle_sticks.py
# class CandleSticksDao(DaoBase):
#     def __init__(self, currency_pair, period):
#         super().__init__()
#         self._currency_pair = str(currency_pair)
#         self._period = str(period)
# 
#     def _get_model(self):
#         return CandleSticks
# 
#     def get_by_duration(self, count, start_time, end_time, *, closed=False):
#         db_records = self._get_by_duration_db(start_time, end_time, closed=closed)
#         db_records_dict = self.rows2dicts(db_records)
#         if len(db_records_dict) >= count:
#             return db_records
# 
#         api_records = self._get_by_duration_web(count, start_time, end_time)
#         return api_records
# 
#     def _get_by_duration_web(self, count, start_time, to_epoch_time):
#         chart_api = BotChartApi()
#         records = chart_api.history(self._currency_pair, self._period, start_time, to_epoch_time)
# 
#         while len(records) < count:
#             random_sleep(1, 2)
#             records = chart_api.history(self._currency_pair, self._period, start_time, to_epoch_time)
# 
#         self._save_records(records)
#         return records
# 
#     def _get_by_duration_db(self, start_time, end_time, *, closed=False):
#         with self._session() as s:
#             result = s.query(self._Model).filter(
#                 and_(self._Model.time <= end_time,
#                      self._Model.time >= start_time,
#                      self._Model.currency_pair == self._currency_pair,
#                      self._Model.period == self._period,
#                      self._Model.closed == int(closed)
#                      )
#             ).order_by(self._Model.time).all()
#         return result
# 
#     def _save_records(self, records):
#         new_records = [
#             merge_dict(record, {'currency_pair': self._currency_pair,
#                                 'period': self._period,
#                                 'closed': True}
#                        )
#             for record in records
#         ]
#         self.create_multiple(new_records)
# 
# Path: zaifbot/exchange/period.py
# def Period(period):
#     for cls in _TradePeriod.__subclasses__():
#         if isinstance(period, str):
#             if cls.is_my_label(period):
#                 return cls(period)
#             continue
# 
#         if isinstance(period, int):
#             if cls.is_my_sec(period):
#                 return cls(period)
#             continue
# 
#         if isinstance(period, _TradePeriod):
#             return period
# 
#     raise ValueError('illegal argument received')
# 
# Path: zaifbot/exchange/currency_pairs.py
# class CurrencyPair:
#     _lock = Lock()
#     _instances = {}
# 
#     def __new__(cls, name):
#         with cls._lock:
#             name = str(name)
#             if cls._instances.get(name, None) is None:
#                 cls._instances[name] = super().__new__(cls)
#         return cls._instances[name]
# 
#     def __init__(self, name):
#         self._name = str(name)
#         self._info = _ZaifCurrencyPairsInfo()[self._name]
# 
#     def __str__(self):
#         return self._name
# 
#     @property
#     def name(self):
#         return self._name
# 
#     @property
#     def info(self):
#         return self._info
# 
#     @property
#     def is_token(self):
#         return self._info['is_token']
# 
# Path: zaifbot/utils/utils.py
# def int_epoch_time(t=None):
#     if not t:
#         return int(time.time())
#     return int(t)


# Path: zaifbot/exchange/candle_sticks.py
from zaifbot.db.dao.candle_sticks import CandleSticksDao
from zaifbot.exchange.period import Period
from zaifbot.exchange.currency_pairs import CurrencyPair
from zaifbot.utils.utils import int_epoch_time



class CandleSticks:
    MAX_COUNT = 1500

    def __init__(self, currency_pair, period):
        self._currency_pair = CurrencyPair(currency_pair)

        self._period = Period(period)