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

allow to download price data in EUR and other fiat currencies #43

Merged
merged 5 commits into from
Jan 30, 2022
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
19 changes: 13 additions & 6 deletions cryptocmd/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ def __init__(
end_date=None,
all_time=False,
order_ascending=False,
fiat="USD",
):
"""
:param coin_code: coin code of cryptocurrency e.g. btc
:param start_date: date since when to scrape data (in the format of dd-mm-yyyy)
:param end_date: date to which scrape the data (in the format of dd-mm-yyyy)
:param all_time: 'True' if need data of all time for respective cryptocurrency
:param order_ascending: data ordered by 'Date' in ascending order (i.e. oldest first).
:param fiat: fiat code eg. USD, EUR

"""

Expand All @@ -46,6 +48,7 @@ def __init__(
self.end_date = end_date
self.all_time = bool(all_time)
self.order_ascending = order_ascending
self.fiat = fiat
self.headers = ["Date", "Open", "High", "Low", "Close", "Volume", "Market Cap"]
self.rows = []

Expand Down Expand Up @@ -80,7 +83,9 @@ def _download_data(self, **kwargs):
if self.all_time:
self.start_date, self.end_date = None, None

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

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

Expand Down Expand Up @@ -185,9 +190,9 @@ def export_csv(self, csv_name=None, csv_path=None, **kwargs):
csv_path = os.getcwd()

if csv_name is None:
# Make name fo file in format of {coin_code}_{start_date}_{end_date}.csv
csv_name = "{0}_{1}_{2}.csv".format(
self.coin_code, self.start_date, self.end_date
# Make name fo file in format of {coin_code}_{fiat}_{start_date}_{end_date}.csv
csv_name = "{0}_{1}_{2}_{3}.csv".format(
self.coin_code, self.fiat, self.start_date, self.end_date
)

if not csv_name.endswith(".csv"):
Expand Down Expand Up @@ -224,8 +229,10 @@ def export(self, format, name=None, path=None, **kwargs):
path = os.getcwd()

if name is None:
# Make name of file in format: {coin_code}_{start_date}_{end_date}.csv
name = "{0}_{1}_{2}".format(self.coin_code, self.start_date, self.end_date)
# Make name of file in format: {coin_code}_{fiat}_{start_date}_{end_date}.csv
name = "{0}_{1}-{2}_{3}".format(
self.coin_code, self.fiat, self.start_date, self.end_date
)

if not name.endswith(".{}".format(format)):
name += ".{}".format(format)
Expand Down
7 changes: 4 additions & 3 deletions cryptocmd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ def get_coin_id(coin_code):
print("Error message:", e)


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

:param coin_code: coin code of a cryptocurrency e.g. btc
:param start_date: date since when to scrape data (in the format of dd-mm-yyyy)
:param end_date: date to which scrape the data (in the format of dd-mm-yyyy)
:param fiat: fiat code eg. USD, EUR
:return: returns html of the webpage having historical data of cryptocurrency for certain duration
"""

Expand Down Expand Up @@ -93,8 +94,8 @@ def download_coin_data(coin_code, start_date, end_date):
.timestamp()
)

api_url = "https://web-api.coinmarketcap.com/v1/cryptocurrency/ohlcv/historical?convert=USD&slug={}&time_end={}&time_start={}".format(
coin_id, end_date_timestamp, start_date_timestamp
api_url = "https://web-api.coinmarketcap.com/v1/cryptocurrency/ohlcv/historical?convert={}&slug={}&time_end={}&time_start={}".format(
fiat,coin_id, end_date_timestamp, start_date_timestamp
)

try:
Expand Down