diff --git a/finam/const.py b/finam/const.py index 4f8c2fe..188a036 100644 --- a/finam/const.py +++ b/finam/const.py @@ -2,6 +2,7 @@ __all__ = ['Market', 'Timeframe', + 'Fileformat' ] @@ -40,3 +41,10 @@ class Timeframe(IntEnum): DAILY = 8 WEEKLY = 9 MONTHLY = 10 + +class Fileformat(IntEnum): + + CSV = 1 + CSVGZ = 2 + PKL = 3 + PKLXZ = 4 diff --git a/scripts/finam-download.py b/scripts/finam-download.py index 031aff9..160cd2e 100755 --- a/scripts/finam-download.py +++ b/scripts/finam-download.py @@ -14,7 +14,8 @@ Timeframe, Market, FinamExportError, - FinamObjectNotFoundError) + FinamObjectNotFoundError, + Fileformat) from finam.utils import click_validate_enum @@ -79,8 +80,13 @@ def _arg_split(ctx, param, value): @click.option('--ext', help='Resulting file extension', default='csv') +@click.option('--format', + help='Format of output file with data', + default='CSV', + callback=partial(click_validate_enum, Fileformat), + required=False) def main(contracts, market, timeframe, destdir, lineterm, - delay, startdate, enddate, skiperr, ext): + delay, startdate, enddate, skiperr, ext, format): exporter = Exporter() if not any((contracts, market)): @@ -115,10 +121,23 @@ def main(contracts, market, timeframe, destdir, lineterm, continue else: raise - destpath = os.path.join(destdir, '{}-{}.{}' - .format(contract.code, timeframe, ext)) + destpath = os.path.join(destdir, '{}-{}' + .format(contract.code, timeframe)) + + # extention is taken from param if output file is in csv format + if format == 'CSV': + destpath += f'.{ext}' + data.to_csv(destpath, index=False, line_terminator=lineterm) + elif format == 'CSVGZ': + destpath += f'.csv.gz' + data.to_csv(destpath, index=False, line_terminator=lineterm, compression='gzip') + if format == 'PKL': + destpath += f'.pkl' + data.to_pickle(destpath) + if format == 'PKLXZ': + destpath += f'.pkl.xz' + data.to_pickle(destpath, compression='xz') - data.to_csv(destpath, index=False, line_terminator=lineterm) if delay > 0: logger.info('Sleeping for {} second(s)'.format(delay)) time.sleep(delay)