diff options
| author | n1 <hrdina.pavel@gmail.com> | 2020-09-02 10:10:15 +0200 |
|---|---|---|
| committer | n1 <hrdina.pavel@gmail.com> | 2020-09-02 10:10:15 +0200 |
| commit | 23b46766429a6341a5e39060fc4e7e849bce518c (patch) | |
| tree | 81d9aea81d9d9ae905dd385f0fd3b7ec3362bf45 | |
| parent | 09d118eeac8b22a3fc9d67dfcee60799a26a12a9 (diff) | |
Added: coin ID support.0.3.2
| -rw-r--r-- | README.rst | 66 | ||||
| -rwxr-xr-x | build.sh | 1 | ||||
| -rw-r--r-- | karpet/core.py | 51 | ||||
| -rw-r--r-- | pyproject.toml | 2 | ||||
| -rw-r--r-- | test_karpet.py | 18 | ||||
| -rwxr-xr-x | test_upload.sh | 1 | ||||
| -rwxr-xr-x | upload.sh | 1 |
7 files changed, 97 insertions, 43 deletions
@@ -55,8 +55,8 @@ Usage from karpet import Karpet -``fetch_crypto_historical_data()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +fetch_crypto_historical_data() +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Retrieves historical data. .. code-block:: python @@ -74,8 +74,8 @@ Retrieves historical data. 2019-01-05 3855.39 direct 3926.92 3841.13 3874.06 35766.65 1.394385e+08 -``fetch_exchanges()`` -~~~~~~~~~~~~~~~~~~~~~ +fetch_exchanges() +~~~~~~~~~~~~~~~~~ Retrieves exchange list. .. code-block:: python @@ -84,8 +84,8 @@ Retrieves exchange list. k.fetch_exchanges("nrg") ['DigiFinex', 'KuCoin', 'CryptoBridge', 'Bitbns', 'CoinExchange'] -``fetch_tweets()`` -~~~~~~~~~~~~~~~~~~ +fetch_tweets() +~~~~~~~~~~~~~~ Retrieves twitter tweets. .. code-block:: python @@ -96,8 +96,8 @@ Retrieves twitter tweets. .. image:: https://raw.githubusercontent.com/im-n1/karpet/master/assets/tweets.png -``fetch_google_trends()`` -~~~~~~~~~~~~~~~~~~~~~~~~~ +fetch_google_trends() +~~~~~~~~~~~~~~~~~~~~~ Retrieves Google Trends - in percents for the given date range. .. code-block:: python @@ -117,8 +117,8 @@ And with a few lines of code you can get a chart .. image:: https://raw.githubusercontent.com/im-n1/karpet/master/assets/trends_chart.png -``fetch_news()`` -~~~~~~~~~~~~~~~~ +fetch_news() +~~~~~~~~~~~~ Retrieves crypto news. .. code-block:: python @@ -135,8 +135,8 @@ Retrieves crypto news. } news = k.fetch_news("btc", limit=30) # Gets 30 news. -``fetch_top_news()`` -~~~~~~~~~~~~~~~~~~~~ +fetch_top_news() +~~~~~~~~~~~~~~~~ Retrieves top crypto news in 2 categories: * Editor's choices - articles picked by editors @@ -167,48 +167,66 @@ Retrieves top crypto news in 2 categories: 'description': 'Stability around $10,600 for Bitcoin price is ...' # Truncated. } +get_coin_ids() +~~~~~~~~~~~~~~ +Resolves coin ID's based on the given symbol (there are coins out there with identical symbol). + +Use this to get distinctive coin ID which can be used as ``id`` param for +method ``fetch_crypto_historical_data()``. + +.. code-block:: python + + k = Karpet() + print(k.get_coin_ids("sta")) + ['statera'] + Changelog --------- +0.3.2 +~~~~~ +* new method ``get_coin_ids()`` +* method ``fetch_crypto_historical_data()`` has ``id`` param now + 0.3.1 ~~~~~ -* Migrated to coingecko.com API (no API key needed anymore). +* migrated to coingecko.com API (no API key needed anymore) 0.3 ~~~ -* Migrated to cryptocompare.com API (you need an API key now). -* Requirements are now managed by Poetry. +* migrated to cryptocompare.com API (you need an API key now) +* requirements are now managed by Poetry 0.2.5 ~~~~~ -* Added ``fetch_top_news()`` method for top crypto news separated in 2 categories. +* added ``fetch_top_news()`` method for top crypto news separated in 2 categories 0.2.4 ~~~~~ -* ``fetch_news()`` adds new "description" item and renames "image_url" to "image". -* All ``fetch_news()`` item properties are now presented even if they are ``None``. +* ``fetch_news()`` adds new "description" item and renames "image_url" to "image" +* all ``fetch_news()`` item properties are now presented even if they are ``None`` 0.2.3 ~~~~~ -* Simplified import from ``from karpet.karpet import Karpet`` to ``from karpet import Karpet``. +* simplified import from ``from karpet.karpet import Karpet`` to ``from karpet import Karpet`` 0.2.2 ~~~~~ -* Added ``fetch_news()`` method for retrieving crypto news. +* added ``fetch_news()`` method for retrieving crypto news 0.2.1 ~~~~~ -* Added ``fetch_exchanges()`` method for retrieving symbol exchange list. -* Removed obsolete library dependency. +* added ``fetch_exchanges()`` method for retrieving symbol exchange list +* removed obsolete library dependency 0.2 ~~~ -* Twitter scraping added. +* twitter scraping added 0.1 ~~~ -* Initial release. +* initial release Credits ------- diff --git a/build.sh b/build.sh deleted file mode 100755 index 5354521..0000000 --- a/build.sh +++ /dev/null @@ -1 +0,0 @@ -rm -rf dist ; ./setup.py sdist bdist_wheel diff --git a/karpet/core.py b/karpet/core.py index d6863da..7f264ab 100644 --- a/karpet/core.py +++ b/karpet/core.py @@ -6,17 +6,15 @@ except: import asyncio import time -import numpy as np from datetime import datetime, timedelta +import requests + import aiohttp import numpy as np import pandas as pd -import requests from bs4 import BeautifulSoup -from . import utils - class Karpet: @@ -76,9 +74,10 @@ class Karpet: except: raise Exception("Couldn't parse downloaded data from the internet.") - def fetch_crypto_historical_data(self, symbol): + def fetch_crypto_historical_data(self, symbol=None, id=None): """ Retrieve basic historical information for a specific cryptocurrency from coingecko.com. + Coin ID can be retreived by get_coin_ids() method. Output dataframe has following columns: @@ -94,15 +93,30 @@ class Karpet: Index is datetime64[ns]. :param str symbol: Coin symbol - i.e. BTC, ETH, ... + :param str id: Coin ID (baed on coingecko.com). :raises Exception: If data couldn't be download form the internet. :return: Dataframe with historical data. :rtype: pd.DataFrame """ + # Validate input params. + if (not symbol and not id) or (symbol and id): + raise AttributeError('Please hand "symbol" or "id" param.') + + if symbol: + ids = self.get_coin_ids(symbol) + + if 1 < len(ids): + raise Exception( + f'Symbol is common for {len(ids)} coins. Please specify "id" param instead.' + ) + + id = ids[0] + + # Fetch data. data = [] step = 1 - limit = 2000 - url = f"https://api.coingecko.com/api/v3/coins/{self._get_coin_id(symbol)}/market_chart?vs_currency=usd&days=max" + url = f"https://api.coingecko.com/api/v3/coins/{id}/market_chart?vs_currency=usd&days=max" # Fetch and check the response. data = self._get_json(url) @@ -140,7 +154,7 @@ class Karpet: return df - def fetch_crypto_exchanges(self, symbol): + def fetch_crypto_exchanges(self, symbol=None): """ Fetches all exchanges where the given symbol is listed. @@ -597,15 +611,28 @@ class Karpet: except: raise Exception("Couldn't parse downloaded data from the internet.") - def _get_coin_id(self, symbol): + def get_coin_ids(self, symbol): + """ + Returns coin ID's by coin symbol. There are some coins + with the same symbol. The only way to dostiguish between + them is to use ID's. These ID's are by coingecko.com. + + So this method return a list of ID's. If you get 2+ ID's + you probably want to use `id` param in one of the `fetch_*` + methods. + + :param str symbol: Symbol of the coin. + :return: List of ID's. + :rtype: list + """ url = "https://api.coingecko.com/api/v3/coins/list" response_data = self._get_json(url) symbol = symbol.upper() + found_ids = [] - # TODO: possible multiple equal symbols for coin in response_data: if coin["symbol"].upper() == symbol: - return coin["id"] + found_ids.append(coin["id"]) - raise Exception("Couldn't find coin ID.") + return found_ids diff --git a/pyproject.toml b/pyproject.toml index 88dc1ca..6d98e53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "karpet" -version = "0.3.1" +version = "0.3.2" description = "Library for fetching coin/token historical data, trends, tweets and more." authors = ["n1 <hrdina.pavel@gmail.com>"] readme = "README.rst" diff --git a/test_karpet.py b/test_karpet.py index 6ef1668..0623d4e 100644 --- a/test_karpet.py +++ b/test_karpet.py @@ -1,4 +1,5 @@ from datetime import date, datetime, timedelta +import pytest from karpet import Karpet @@ -12,16 +13,27 @@ def get_last_week(): def test_fetch_crypto_historical_data(): - c = Karpet(cryptocompare_api_key=CRYPTOCOMPARE_API_KEY) + c = Karpet() + + assert 1000 < len(c.fetch_crypto_historical_data(symbol="BTC")) + + +def test_fetch_crypto_historical_data_params(): + + c = Karpet() + + with pytest.raises(AttributeError): + c.fetch_crypto_historical_data() - assert 1000 < len(c.fetch_crypto_historical_data("BTC")) + with pytest.raises(AttributeError): + c.fetch_crypto_historical_data(symbol="a", id="b") def test_fetch_crypto_historical_data_limited(): c = Karpet(date(2019, 1, 1), date(2019, 1, 30)) - assert 30 == len(c.fetch_crypto_historical_data("BTC")) + assert 30 == len(c.fetch_crypto_historical_data(symbol="BTC")) def test_fetch_exchanges(): diff --git a/test_upload.sh b/test_upload.sh deleted file mode 100755 index 05824e0..0000000 --- a/test_upload.sh +++ /dev/null @@ -1 +0,0 @@ -twine upload -s --repository-url https://test.pypi.org/legacy/ dist/* diff --git a/upload.sh b/upload.sh deleted file mode 100755 index 8374f7a..0000000 --- a/upload.sh +++ /dev/null @@ -1 +0,0 @@ -twine upload -s dist/* |