Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff #70

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ jobs:
with:
python-version: '${{ matrix.python-version }}'
architecture: '${{ matrix.arch }}'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install
- name: Install package
run: python setup.py install
- name: Run test script
run: python examples/get_csv.py && python examples/get_by_coin_name.py && python examples/get_by_id_number.py
run: poetry run python setup.py install
- name: Run linter
run: poetry run ruff check .
- name: Run formatter
run: poetry run ruff format . --check
- name: Run test scripts
run: |
poetry run python examples/get_csv.py
poetry run python examples/get_by_coin_name.py
poetry run python examples/get_by_id_number.py
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cryptoCMD: cryptoCurrency Market Data

[![PyPI Version][]][1] [![CI Status][]][2] [![License][]][3] [![Downloads][]][4] [![Code style: black][]][5] [![GitHub Sponsors][]][6]
[![PyPI Version][]][1] [![CI Status][]][2] [![License][]][3] [![Downloads][]][4] [![Ruff][]][5] [![GitHub Sponsors][]][6]


Cryptocurrency historical market price data scraper written in Python.
Expand Down Expand Up @@ -113,8 +113,8 @@ below. ✨☕
[3]: https://github.com/guptarohit/cryptoCMD/blob/master/LICENSE
[Downloads]: https://pepy.tech/badge/cryptoCMD
[4]: https://pepy.tech/project/cryptoCMD
[Code style: black]: https://img.shields.io/badge/code%20style-black-000000.svg
[5]: https://github.com/ambv/black
[Ruff]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
[5]: https://github.com/astral-sh/ruff
[GitHub Sponsors]: https://img.shields.io/github/sponsors/guptarohit?color=%23FF5733
[6]: https://github.com/sponsors/guptarohit
[![Buy Me A Coffee](https://img.buymeacoffee.com/button-api/?text=Buy%20me%20a%20coffee&emoji=&slug=rohitgupta&button_colour=5F7FFF&font_colour=ffffff&font_family=Lato&outline_colour=000000&coffee_colour=FFDD00)](https://www.buymeacoffee.com/rohitgupta)
4 changes: 2 additions & 2 deletions cryptocmd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .core import *
from .__version__ import __version__
from .core import * # noqa
from .__version__ import __version__ # noqa
28 changes: 22 additions & 6 deletions cryptocmd/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
order_ascending=False,
fiat="USD",
coin_name=None,
id_number=None
id_number=None,
):
"""
:param coin_code: coin code of cryptocurrency e.g. btc. Will be ignored if using id_number.
Expand All @@ -43,7 +43,7 @@ def __init__(
:param order_ascending: data ordered by 'Date' in ascending order (i.e. oldest first).
:param fiat: fiat code eg. USD, EUR
:param coin_name: coin name in case of many coins with same code e.g. sol -> solana, solcoin
:param id_number: id number for the a cryptocurrency on the coinmarketcap.com.
:param id_number: id number for the a cryptocurrency on the coinmarketcap.com.
Will override coin_code and coin_name when provided.
"""

Expand All @@ -54,7 +54,19 @@ def __init__(
self.order_ascending = order_ascending
self.fiat = fiat
self.coin_name = coin_name
self.headers = ["Date", "Open", "High", "Low", "Close", "Volume", "Market Cap", "Time Open", "Time High", "Time Low", "Time Close"]
self.headers = [
"Date",
"Open",
"High",
"Low",
"Close",
"Volume",
"Market Cap",
"Time Open",
"Time High",
"Time Low",
"Time Close",
]
self.rows = []
self.id_number = id_number

Expand Down Expand Up @@ -90,11 +102,15 @@ def _download_data(self, **kwargs):
self.start_date, self.end_date = None, None

coin_data = download_coin_data(
self.coin_code, self.start_date, self.end_date, self.fiat, self.coin_name, self.id_number
self.coin_code,
self.start_date,
self.end_date,
self.fiat,
self.coin_name,
self.id_number,
)

for _row in coin_data["data"]["quotes"]:

_row_quote = list(_row["quote"].values())[0]
date = datetime.strptime(
_row_quote["timestamp"], "%Y-%m-%dT%H:%M:%S.%fZ"
Expand Down Expand Up @@ -251,7 +267,7 @@ def export(self, format, name=None, path=None, **kwargs):

try:
with open(_file, "wb") as f:
if type(data) is str:
if isinstance(data, str):
f.write(data.encode("utf-8"))
else:
f.write(data)
Expand Down
41 changes: 27 additions & 14 deletions cryptocmd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def get_coin_id(coin_code, coin_name):
if coin_name is None:
return json_data["data"][0]["slug"]

return [data["slug"] for data in json_data["data"] if data["name"].lower() == coin_name.lower()][0]
return [
data["slug"]
for data in json_data["data"]
if data["name"].lower() == coin_name.lower()
][0]
if error_code == 400:
raise InvalidCoinCode(
"'{}' coin code is unavailable on coinmarketcap.com".format(coin_code)
Expand All @@ -61,7 +65,9 @@ def get_coin_id(coin_code, coin_name):
print("Error message:", e)


def download_coin_data(coin_code, start_date, end_date, fiat, coin_name, id_number=None):
def download_coin_data(
coin_code, start_date, end_date, fiat, coin_name, id_number=None
):
"""
Download HTML price history for the specified cryptocurrency and time range from CoinMarketCap.

Expand Down Expand Up @@ -117,25 +123,32 @@ def download_coin_data(coin_code, start_date, end_date, fiat, coin_name, id_numb
raise Exception(json_data["status"]["error_message"])
if id_number:
show_coin_info = False
if coin_code and coin_code != json_data['data']['symbol']:
print(f"INFO: Using 'id_number'! The 'coin_code' ({coin_code}) provided " + \
"is different from the symbol returned.")
if coin_code and coin_code != json_data["data"]["symbol"]:
print(
f"INFO: Using 'id_number'! The 'coin_code' ({coin_code}) provided "
+ "is different from the symbol returned."
)
show_coin_info = True
if coin_name and coin_name != json_data['data']['name']:
print(f"INFO: Using 'id_number'! The 'coin_name' ({coin_name}) provided " + \
"is different from the symbol returned.")
if coin_name and coin_name != json_data["data"]["name"]:
print(
f"INFO: Using 'id_number'! The 'coin_name' ({coin_name}) provided "
+ "is different from the symbol returned."
)
show_coin_info = True
if show_coin_info:
print(f"""The returned data belongs to coin "{json_data['data']['name']}", """ + \
f"""with symbol "{json_data['data']['symbol']}" """)
print(
f"""The returned data belongs to coin "{json_data['data']['name']}", """
+ f"""with symbol "{json_data['data']['symbol']}" """
)
return json_data
except Exception as e:
print(
"Error fetching price data for {} for interval '{}' and '{}'".format(
f"(id {id_number})" if id_number else coin_code,
start_date,
end_date,
))
f"(id {id_number})" if id_number else coin_code,
start_date,
end_date,
)
)

if hasattr(e, "message"):
print("Error message (download_data) :", e.message)
Expand Down
4 changes: 2 additions & 2 deletions examples/get_by_id_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from cryptocmd import CmcScraper

# Initialise scraper with 'id_number' of cryptocurrency.
# 'id_number' is unique for each cryptocurrency,
# 'id_number' is unique for each cryptocurrency,
# making it safer to retrieve data from tokens with duplicate names.
# 'id_number will override 'coin_code' and 'coin_name' when provided,
# a WARNING will be printed if 'id_number' is provided
# a WARNING will be printed if 'id_number' is provided
# and 'coin_code' or 'coin_name' provided are different from the coin returned.
# If time interval is not passed all time data will be scrapped
# 'id_number' can be retrieved from https://web-api.coinmarketcap.com/v1/cryptocurrency/map?symbol={SYMBOL}.
Expand Down
Loading
Loading