Skip to content
This repository has been archived by the owner on Aug 26, 2023. It is now read-only.

Updated functions for v1 Pro API #51

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 104 additions & 104 deletions coinmarketcap/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,113 @@
# -*- coding: utf-8 -*-

import os
import sys
import json
import requests
import tempfile
import requests_cache


class Market(object):

_session = None
__DEFAULT_BASE_URL = 'https://api.coinmarketcap.com/v2/'
__DEFAULT_TIMEOUT = 30
__TEMPDIR_CACHE = True

def __init__(self, base_url = __DEFAULT_BASE_URL, request_timeout = __DEFAULT_TIMEOUT, tempdir_cache = __TEMPDIR_CACHE):
self.base_url = base_url
self.request_timeout = request_timeout
self.cache_filename = 'coinmarketcap_cache'
self.cache_name = os.path.join(tempfile.gettempdir(), self.cache_filename) if tempdir_cache else self.cache_filename

@property
def session(self):
if not self._session:
self._session = requests_cache.core.CachedSession(cache_name=self.cache_name, backend='sqlite', expire_after=120)
self._session.headers.update({'Content-Type': 'application/json'})
self._session.headers.update({'User-agent': 'coinmarketcap - python wrapper around \
coinmarketcap.com (github.com/barnumbirr/coinmarketcap)'})
return self._session

def __request(self, endpoint, params):
response_object = self.session.get(self.base_url + endpoint, params = params, timeout = self.request_timeout)

try:
response = json.loads(response_object.text)

if isinstance(response, list) and response_object.status_code == 200:
response = [dict(item, **{u'cached':response_object.from_cache}) for item in response]
if isinstance(response, dict) and response_object.status_code == 200:
response[u'cached'] = response_object.from_cache

except Exception as e:
return e

return response

def listings(self):
"""
This endpoint displays all active cryptocurrency listings in one call. Use the
"id" field on the Ticker endpoint to query more information on a specific
cryptocurrency.
"""

response = self.__request('listings/', params=None)
return response

def ticker(self, currency="", **kwargs):
"""
This endpoint displays cryptocurrency ticker data in order of rank. The maximum
number of results per call is 100. Pagination is possible by using the
start and limit parameters.

GET /ticker/

Optional parameters:
(int) start - return results from rank [start] and above (default is 1)
(int) limit - return a maximum of [limit] results (default is 100; max is 100)
(string) convert - return pricing info in terms of another currency.
Valid fiat currency values are: "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK",
"DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN",
"MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY",
"TWD", "ZAR"
Valid cryptocurrency values are: "BTC", "ETH" "XRP", "LTC", and "BCH"

GET /ticker/{id}

Optional parameters:
(string) convert - return pricing info in terms of another currency.
Valid fiat currency values are: "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK",
"DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN",
"MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY",
"TWD", "ZAR"
Valid cryptocurrency values are: "BTC", "ETH" "XRP", "LTC", and "BCH"
"""

params = {}
params.update(kwargs)

# see https://github.com/barnumbirr/coinmarketcap/pull/28
if currency:
currency = str(currency) + '/'

response = self.__request('ticker/' + currency, params)
return response

def stats(self, **kwargs):
"""
This endpoint displays the global data found at the top of coinmarketcap.com.

Optional parameters:
(string) convert - return pricing info in terms of another currency.
Valid fiat currency values are: "AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK",
"DKK", "EUR", "GBP", "HKD", "HUF", "IDR", "ILS", "INR", "JPY", "KRW", "MXN",
"MYR", "NOK", "NZD", "PHP", "PKR", "PLN", "RUB", "SEK", "SGD", "THB", "TRY",
"TWD", "ZAR"
Valid cryptocurrency values are: "BTC", "ETH" "XRP", "LTC", and "BCH"
"""

params = {}
params.update(kwargs)
response = self.__request('global/', params)
return response
_session = None
__DEFAULT_BASE_URL = 'https://pro-api.coinmarketcap.com/v1/'
__DEFAULT_TIMEOUT = 30
__TEMPDIR_CACHE = True

def __init__(self, api_key, base_url=__DEFAULT_BASE_URL, request_timeout=__DEFAULT_TIMEOUT, tempdir_cache=__TEMPDIR_CACHE):
self.api_key = api_key
self.base_url = base_url
self.request_timeout = request_timeout
self.cache_filename = 'coinmarketcap_cache'
self.cache_name = os.path.join(tempfile.gettempdir(), self.cache_filename) if tempdir_cache else self.cache_filename

@property
def session(self):
if not self._session:
self._session = requests_cache.core.CachedSession(cache_name=self.cache_name, backend='sqlite', expire_after=120)
self._session.headers.update({'Content-Type': 'application/json'})
self._session.headers.update({'User-agent': 'coinmarketcap - python wrapper around \
coinmarketcap.com (github.com/barnumbirr/coinmarketcap)'})
self._session.headers.update({'X-CMC_PRO_API_KEY': self.api_key})
return self._session

def __request(self, endpoint, params):
response_object = self.session.get(self.base_url + endpoint, params=params, timeout=self.request_timeout)

try:
response = json.loads(response_object.text)

if isinstance(response, list) and response_object.status_code == 200:
response = [dict(item, **{u'cached': response_object.from_cache}) for item in response]
if isinstance(response, dict) and response_object.status_code == 200:
response[u'cached'] = response_object.from_cache

except Exception as e:
return e

return response

def listings(self, **kwargs):
"""
This endpoint displays all active cryptocurrency listings in one call. The maximum
number of results per call is 5000 (there's yet to be 5000 active cryptocurrencies
in coinmarketcap so it'll display whatever much there are). For every 200
cryptocurrencies, a credit count will be charged.

Optional parameters:
(int) start - return results from rank [start] and above (default is 1)
(int) limit - return a maximum of [limit] results (default is 100; max is 5000)
(string) convert - return pricing info in terms of another currency.
See available conversions here:
https://coinmarketcap.com/api/v1/#section/Standards-and-Conventions
(string) sort - sorts the list of cryptocurrencies by the following options:
"market_cap", "name", "symbol", "date_added", "market_cap", "price",
"circulating_supply", "total_supply", "max_supply", "num_market_pairs",
"volume_24h", "percent_change_1h", "percent_change_24h", "percent_change_7d"
(string) sort_dir - direction in which to order cryptocurrencies against the
specified sort. Options are "asc" or "desc".
(string) cryptocurrency_type - type of cryptocurrency to include. By default the type is
"all", but other valid options are "coins" and "tokens".
"""
params = {}
params.update(kwargs)
response = self.__request('cryptocurrency/listings/latest', params=params)
return response

def ticker(self, **kwargs):
"""
This endpoint displays cryptocurrency ticker data in order of rank. 1 credit
per use.

REQUIRED parameters:
(string) id - ID of the cryptocurrency on coinmarketcap (i.e. 1)

or

(string) symbol - the symbol of the cryptocurrency (i.e. BTC, ETH)

Optional parameters:
(string) convert - return pricing info in terms of another currency.
See available conversions here:
https://coinmarketcap.com/api/v1/#section/Standards-and-Conventions
"""

params = {}
params.update(kwargs)
response = self.__request('cryptocurrency/quotes/latest', params=params)
return response

def stats(self, **kwargs):
"""
This endpoint displays the global data found at the top of coinmarketcap.com.
1 credit per use.

Optional parameters:
(string) convert - return pricing info in terms of another currency.
See available conversions here:
https://coinmarketcap.com/api/v1/#section/Standards-and-Conventions
"""

params = {}
params.update(kwargs)
response = self.__request('global-metrics/quotes/latest', params=params)
return response