{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#
Synchronous Pymarketcap Interface
\n", "\n", "> See also: [Asynchronous Pymarketcap Scraper](async_live.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic usage\n", "\n", "#### `Pymarketcap([timeout=20, debug=False, cache=True])`\n", "\n", "The first response takes a long time because some data is cached at instantiation of the wrapper." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "No module named 'pymarketcap.core'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mpymarketcap\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mPymarketcap\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mcmc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mPymarketcap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mcmc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexchanges\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/Escritorio/code/pydocs/my_libraries/pymarketcap/pymarketcap/__init__.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0m__license__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"BSD License\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mpymarketcap\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mPymarketcap\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m from pymarketcap.errors import (\n\u001b[1;32m 16\u001b[0m \u001b[0mCoinmarketcapError\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mImportError\u001b[0m: No module named 'pymarketcap.core'" ] } ], "source": [ "from pymarketcap import Pymarketcap\n", "cmc = Pymarketcap(timeout=10)\n", "\n", "cmc.exchanges()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> All methods are self explained in docstrings:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(cmc.exchanges.__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "___________________________________\n", "\n", "## Coinmarketcap API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `listings()`\n", "List all criptocurrencies with their ids, names, symbol and website slug. You can use cached ``cryptocurrencies`` property also." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.listings()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `ticker(currency=None, limit=0, start=0, convert=\"USD\")`\n", "If `currency == None`, you will obtain data for all currencies in coinmarketcap:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.ticker()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can pass a currency through `name`, `symbol`, `website_slug` or `id`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data_from_symbol = cmc.ticker(\"BTC\")\n", "data_from_slug = cmc.ticker(\"bitcoin\")\n", "\n", "data_from_slug" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get all cryptocurrencies fields you can use the next property:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.cryptocurrencies" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other parameters:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(cmc.ticker.__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to get the valid badges that can be passed to `convert` param programatically, use this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(cmc.ticker_badges)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `ticker_all(convert=\"USD\")`\n", "Obtain all currencies from `ticker` method navigating through all API paginated responses." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.ticker_all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `stats(convert=\"USD\")`\n", "Get global criptocurrency statistics. The `convert` parameter works just like in previous method." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.stats()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_____________________________________________\n", "\n", "## Web scraper" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `currency(name, convert=\"USD\")`\n", "Get metadata from a cryptocurrency passed as first argument." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.currency(\"BTC\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `markets(name, convert=\"USD\")`\n", "Get all markets for a exchange in coinmarketcap. Pass a currency symbol or slug as first parameter. You can convert the response fields `\"volume_24h\"` and `\"price\"` between `\"USD\"` and `\"BTC\"` with `convert` parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.markets(\"BTC\", convert=\"BTC\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `ranks()`\n", "Get gainers and losers rankings:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.ranks()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `historical(currency, start=datetime(2008, 8, 18), end=datetime.now(), revert=False)`\n", "Get historical prices for a currency. You can filter by `start` and `end` datetimes provided in `datetime` objects. With parameter `revert` active, the response will be ordered in chronological order." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "\n", "cmc.historical(\"ETH\", start=datetime(2018, 2, 1), revert=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `recently(convert=\"USD\")`\n", "Get recently added coins in coinmarketcap along with other metadata." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list(cmc.recently())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `exchange(name, convert=\"USD\")`\n", "Get data about a exchange:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.exchange(\"okex\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to obtain all available values for the `name` parameter, use:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.exchange_slugs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to get all formatted exchange names:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.exchange_names" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `exchanges(convert=\"USD\")`\n", "Get all available exchanges in coinmarketcap ranked by volumes with some metadata:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.exchanges()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> If you want to connect with exchanges see [`ccxt` library](https://github.com/ccxt/ccxt)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `tokens(convert=\"USD\")`\n", "Get platforms tokens data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.tokens()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_________________________________\n", "\n", "## Graphs API\n", "The graphs API is used internally by coinmarketcap for display graphs info. The params `start` and `end` are passed as `datetime` objects.\n", "\n", "#### `graphs.currency(name, start=None, end=None)`\n", "\n", "Get graphs data for a currency:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.graphs.currency(\"BTC\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `graphs.global_cap([bitcoin=True, start=None, end=None])`\n", "Display data from [total market capitalization charts](https://coinmarketcap.com/charts/), including or excluding bitcoin:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.graphs.global_cap(bitcoin=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `graphs.dominance(start=None, end=None)`\n", "Get data from percentage of total market capitalization chart:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import datetime, timedelta\n", "\n", "ten_days_ago = datetime.now() - timedelta(days=10)\n", "cmc.graphs.dominance(start=ten_days_ago)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_________________________________\n", "\n", "## Utils\n", "#### `convert(value, currency_in, currency_out)`\n", "This method converts the value passed as first parameter as price of the `currency_in` currency to price of the `currency_out` parameter. Works as [coinmarketcap criptocurrencies calculator](https://coinmarketcap.com/calculator/). For example, if you want to converts $100€$ to dolars, use:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.convert(100, \"EUR\", \"USD\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For get all the currencies supported by this method (all currencies; fiat + crypto):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmc.currencies_to_convert" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `download_logo(name, size=64, filename=None)`\n", "Download a currency image logo in `.png` format. Valid sizes are: `[16, 32, 64, 128, 200]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from random import choice\n", "\n", "currency = choice([curr[\"name\"] for curr in cmc.cryptocurrencies])\n", "\n", "filename = cmc.download_logo(currency, size=200)\n", "\n", "from IPython.display import Image\n", "Image(filename)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Clean downloaded logo\n", "import os\n", "try:\n", " os.remove(filename)\n", "except FileNotFoundError:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### `download_exchange_logo(name, size=64, filename=None)`\n", "Download a exchange image logo in `.png` format. Valid sizes are: `[16, 32, 64, 128, 200]`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exchange = choice([str(exc[\"website_slug\"]) for exc in cmc.cryptoexchanges])\n", "\n", "filename = cmc.download_exchange_logo(exchange, size=128)\n", "Image(filename)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " os.remove(filename)\n", "except FileNotFoundError:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_____________________________\n", "\n", "## Catching HTTP errors\n", "You can use the nexts class in order to catch some HTTP error:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pymarketcap import (\n", " CoinmarketcapError, # This class is top hierarchy class errors\n", " CoinmarketcapHTTPError,\n", " CoinmarketcapTooManyRequestsError\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You could prevent errors like this\n", "\n", "\n", "![error](images/http_error.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This only happens with `Pymarketcap` class. In asynchronous interface, failed requests will be restarted in a queue. " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }