-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .core import * | ||
from .__version__ import __version__ | ||
from .cli import cli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import click | ||
from cryptocmd import CmcScraper, __version__ | ||
from datetime import datetime | ||
from halo import Halo | ||
from cryptocmd.utils import InvalidCoinCode | ||
|
||
|
||
def validate_date(ctx, param, value): | ||
if value is None: | ||
return value | ||
try: | ||
datetime.strptime(value, "%d-%m-%Y") | ||
except ValueError: | ||
raise click.BadParameter("Incorrect date format, should be like: dd-mm-YYYY.") | ||
return value | ||
|
||
|
||
spinner = Halo(spinner="dots") | ||
|
||
|
||
@click.command() | ||
@click.argument("coin_code") | ||
@click.option("--start_date", "-s", callback=validate_date, help="Start date of data.") | ||
@click.option("--end_date", "-e", callback=validate_date, help="End date of data.") | ||
@click.option("--csv_name", "-n", help="Name of csv.") | ||
@click.option( | ||
"--csv_path", | ||
"-p", | ||
default=".", | ||
type=click.Path(exists=True), | ||
help="Path to store csv.", | ||
) | ||
@click.version_option( | ||
__version__, "--version", "-v", prog_name=click.style("cryptocmd", fg="green") | ||
) | ||
def cli(coin_code, start_date, end_date, csv_name, csv_path): | ||
"""Cryptocurrency historical market price data scraper 🐍.\n | ||
Basic usage:\n | ||
>>> cryptocmd BTC -s 1-1-2017 -e 1-10-2018 | ||
""" | ||
|
||
# verify start date is less than equal to end date | ||
if start_date and end_date: | ||
if start_date.split("-")[::-1] > end_date.split("-")[::-1]: | ||
raise click.BadParameter( | ||
"start_date cannot be greater than end_date.", | ||
param_hint=["--start_date", "--end_date"], | ||
) | ||
|
||
try: | ||
cmcs = CmcScraper(coin_code, start_date=start_date, end_date=end_date) | ||
spinner.start("Downloading data of {} coin 📊".format(coin_code.upper())) | ||
cmcs.get_data() | ||
spinner.succeed("Downloaded data of {} coin 📊".format(coin_code.upper())) | ||
spinner.start("Exporting data to CSV file 📄") | ||
cmcs.export_csv(csv_name, csv_path) | ||
spinner.succeed("Exported data to CSV file 📄\n🍰 ✨".encode("utf-8")) | ||
except InvalidCoinCode: | ||
spinner.fail("Invalid Coin Code") | ||
except (KeyboardInterrupt, SystemExit): | ||
spinner.stop_and_persist(symbol="🍰".encode("utf-8"), text="bye.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters