Basic sources and source commands.

This commit is contained in:
Chris Berkhout 2021-04-11 17:51:15 +02:00
parent a1118db739
commit 2e6265e321
5 changed files with 62 additions and 19 deletions

View file

@ -2,12 +2,30 @@ import argparse
import sys
from datetime import datetime
from pricehist.location import greet
from pricehist import sources
def cli(args=None):
parser = build_parser()
args = parser.parse_args()
if (args.command == 'sources'):
cmd_sources(args)
elif (args.command == 'source'):
cmd_source(args)
else:
parser.print_help()
def cmd_sources(args):
width = max([len(identifier) for identifier in sources.by_id.keys()])
for identifier, source in sources.by_id.items():
print(f'{identifier.ljust(width)} {source.name()}')
def cmd_source(args):
source = sources.by_id[args.identifier]
print(f'ID : {source.id()}')
print(f'Name : {source.name()}')
pass
def build_parser():
def valid_date(s):
if s == 'today':
@ -22,21 +40,27 @@ def build_parser():
def today():
return str(datetime.now().date())
parser = argparse.ArgumentParser(description='Fetch historical price data from CoinMarketCap.com.')
parser = argparse.ArgumentParser(description='Fetch historical price data')
parser.add_argument('identifier', metavar='ID', type=str,
help='currency or coin identifier from URL (example: bitcoin-cash)')
subparsers = parser.add_subparsers(title='commands', dest='command')
parser.add_argument('--start', dest='start', type=valid_date,
default='2009-01-03',
help='start date (default: 2009-01-03)')
sources_parser = subparsers.add_parser('sources', help='list sources')
parser.add_argument('--end', dest='end', type=valid_date,
default=today(),
help='end date (default: today)')
source_parser = subparsers.add_parser('source', help='show source details')
source_parser.add_argument('identifier', metavar='ID', type=str,
choices=sources.by_id.keys(),
help='the source identifier')
parser.add_argument('--csv', dest='csv', action='store_true',
help='print full data as csv (instead of Ledger pricedb format)')
# parser.add_argument('--start', dest='start', type=valid_date,
# default='2009-01-03',
# help='start date (default: 2009-01-03)')
# parser.add_argument('--end', dest='end', type=valid_date,
# default=today(),
# help='end date (default: today)')
# parser.add_argument('--csv', dest='csv', action='store_true',
# help='print full data as csv (instead of Ledger pricedb format)')
return parser

View file

@ -1,7 +0,0 @@
"""Send greetings."""
def greet(tz):
"""Greet a location."""
friendly_time = "now"
location = tz.split("/")[-1].replace("_"," ")
return f"Hello, {location}! The time is {friendly_time}."

View file

@ -0,0 +1,9 @@
class CoinMarketCap():
@staticmethod
def id():
return 'coinmarketcap'
@staticmethod
def name():
return 'CoinMarketCap.com'

View file

@ -0,0 +1,10 @@
class ECB():
@staticmethod
def id():
return 'ecb'
@staticmethod
def name():
return 'European Central Bank'

View file

@ -0,0 +1,7 @@
from .CoinMarketCap import CoinMarketCap
from .ECB import ECB
by_id = {
CoinMarketCap.id(): CoinMarketCap,
ECB.id(): ECB
}