Ejemplo n.º 1
0
    def _get_data(self) -> dict:
        try:
            response = requests.get("https://api.tdax.com/api/orderbook-tickers/")
            response.raise_for_status()
            data = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            # TODO: [bid][price] [ask][price]
            schema = {
                "type": "object",
                "patternProperties": {
                    r"^.*_.*$": {
                        "type": "object",
                        "properties": {
                            "bid": {"type": "object"},
                            "ask": {"type": "object"},
                        },
                        "required": ["bid", "ask"],
                    },
                },
            }
            validate(data, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        result = {}
        for currencies, info in data.items():
            from_currency, to_currency = currencies.split("_")

            result[Pair(ECurrency(from_currency), ECurrency(to_currency))] = info

        return result
    def _get_data(self) -> dict:
        if not settings.OPENEXCHANGERATES_TOKEN:
            raise NoTokenException

        try:
            response = requests.get(
                f"http://openexchangerates.org/api/latest.json?app_id={settings.OPENEXCHANGERATES_TOKEN}"
            )
            response.raise_for_status()
            data = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            schema = {
                "type": "object",
                "properties": {
                    "base": {"type": "string"},
                    "timestamp": {"type": "number"},
                    "rates": {
                        "type": "object",
                        "patternProperties": {"^.*$": {"type": "number"}},
                        "not": {"required": [""]},
                    },
                },
                "required": ["base", "timestamp", "rates"],
            }
            validate(data, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        if data["base"] != "USD":
            raise APIChangedException("Base currency is not USD")

        return data
Ejemplo n.º 3
0
    def _get_data(self) -> dict:
        if not settings.FIXER_TOKEN:
            raise NoTokenException

        try:
            response = requests.get(
                f"http://data.fixer.io/api/latest?access_key={settings.FIXER_TOKEN}"
            )
            response.raise_for_status()
            data = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            schema = {
                "type": "object",
                "properties": {
                    "base": {"type": "string"},
                    "timestamp": {"type": "number"},
                    "rates": {
                        "type": "object",
                        "patternProperties": {"^.*$": {"type": "number"}},
                        "not": {"required": [""]},
                    },
                },
                "required": ["base", "timestamp", "rates"],
            }
            validate(data, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        if data["base"] != "EUR":
            raise APIChangedException("Base currency is not EUR")

        return data
Ejemplo n.º 4
0
    def get_pair_info(self, pair: Pair) -> PairData:
        if not self.is_pair_exists(pair):
            raise PairNotExistsException(pair)

        request_pair = f"{pair.from_currency}{pair.to_currency}".lower()

        try:
            response = requests.get(
                f"https://api.bitfinex.com/v1/pubticker/{request_pair}")
            response.raise_for_status()
            data = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            schema = {
                "type": "object",
                "properties": {
                    "mid": {
                        "type": "string"
                    },
                    "low": {
                        "type": "string"
                    },
                    "high": {
                        "type": "string"
                    },
                    "timestamp": {
                        "type": "string"
                    },
                },
                "required": ["mid", "low", "high", "timestamp"],
            }
            validate(data, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        try:
            rate = Decimal(data["mid"])
            low = Decimal(data["low"])
            high = Decimal(data["high"])
            last_trade_at = float(data["timestamp"])
        except (DecimalException, ValueError) as e:
            raise APIErrorException(e)

        return PairData(
            pair=pair,
            rate=rate,
            low24h=low,
            high24h=high,
            last_trade_at=datetime.fromtimestamp(last_trade_at),
        )
Ejemplo n.º 5
0
    def _get_pairs(self) -> tuple:
        try:
            response = requests.get("https://api.bitfinex.com/v1/symbols")
            response.raise_for_status()
            pairs = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            schema = {"type": "array", "items": {"type": "string"}}
            validate(pairs, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        return tuple(pairs)
Ejemplo n.º 6
0
    def _get_data(self) -> dict:
        try:
            response = requests.get("https://api.bitkub.com/api/market/ticker")
            response.raise_for_status()
            data = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            schema = {
                "type": "object",
                "patternProperties": {
                    r"^.*_.*$": {
                        "type":
                        "object",
                        "properties": {
                            "lowestAsk": {
                                "type": "number"
                            },
                            "highestBid": {
                                "type": "number"
                            },
                            "isFrozen": {
                                "type": "number"
                            },
                            "low24hr": {
                                "type": "number"
                            },
                            "high24hr": {
                                "type": "number"
                            },
                        },
                        "required": [
                            "lowestAsk",
                            "highestBid",
                            "isFrozen",
                            "low24hr",
                            "high24hr",
                        ],
                    },
                    "not": {
                        "required": ["error", ""]
                    },
                },
            }
            validate(data, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        result = {}
        all_currency_codes = get_all_currency_codes()
        for currencies, info in data.items():
            if info["isFrozen"]:
                logging.info("Bitkub isFrozen: %s", currencies)
                continue

            # reverse
            to_currency, from_currency = currencies.split("_")

            if not info["lowestAsk"] or not info["highestBid"]:
                if (to_currency in all_currency_codes
                        and from_currency in all_currency_codes):
                    logging.info("Bitkub no Bid Ask: %s", info)
                continue

            result[Pair(ECurrency(from_currency),
                        ECurrency(to_currency))] = info

        return result
Ejemplo n.º 7
0
    def _get_data(self) -> dict:
        try:
            response = requests.get(
                "https://api.bittrex.com/api/v1.1/public/getmarketsummaries")
            response.raise_for_status()
            data = response.json()
        except (requests.exceptions.RequestException, ValueError) as e:
            raise APIErrorException(e)

        try:
            schema = {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean"
                    },
                    "result": {
                        "type": "array",
                        "items": {
                            "type":
                            "object",
                            "properties": {
                                "MarketName": {
                                    "type": "string"
                                },
                                "High": {
                                    "type": ["number", "null"]
                                },
                                "Low": {
                                    "type": ["number", "null"]
                                },
                                "TimeStamp": {
                                    "type": "string"
                                },
                                "Bid": {
                                    "type": ["number", "null"]
                                },
                                "Ask": {
                                    "type": ["number", "null"]
                                },
                                "PrevDay": {
                                    "type": ["number", "null"]
                                },
                            },
                            "required": [
                                "MarketName",
                                "High",
                                "Low",
                                "TimeStamp",
                                "Bid",
                                "Ask",
                                "PrevDay",
                            ],
                        },
                    },
                },
                "required": ["success", "result"],
            }
            validate(data, schema)
        except ValidationError as e:
            raise APIErrorException(e)

        result = {}
        all_currency_codes = get_all_currency_codes()
        for x in data["result"]:
            # reverse
            to_currency, from_currency = x["MarketName"].upper().split("-")

            if not x["Bid"] or not x["Ask"]:
                if (to_currency in all_currency_codes
                        and from_currency in all_currency_codes):
                    logging.info("Bittrex no Bid Ask: %s", x)
                continue

            del x["MarketName"]
            result[Pair(ECurrency(from_currency), ECurrency(to_currency))] = x

        return result