diff --git a/src/mwfin/__init__.py b/src/mwfin/__init__.py index e69de29..2c60e41 100644 --- a/src/mwfin/__init__.py +++ b/src/mwfin/__init__.py @@ -0,0 +1 @@ +from .functions import get_company_financials diff --git a/src/mwfin/__main__.py b/src/mwfin/__main__.py index e69de29..e9c41c5 100644 --- a/src/mwfin/__main__.py +++ b/src/mwfin/__main__.py @@ -0,0 +1,59 @@ +import asyncio +import sys +import csv +import json +from argparse import ArgumentParser +from pathlib import Path + +from aiohttp import ClientSession + +from . import get_company_financials + + +JSON_EXT, CSV_EXT = '.json', '.csv' + + +def write_to_csv(data, file_obj) -> None: + writer = csv.writer(file_obj) + for statement in data.values(): + for key, values in statement.items(): + writer.writerow([key] + list(values)) + + +async def main() -> None: + parser = ArgumentParser(description="Scrape company financials") + + parser.add_argument( + '-s', '--symbol', + type=str, + help="Stock ticker symbol of the company to be scraped the financials of" + ) + + parser.add_argument( + '-f', '--to-file', + type=Path, + help="Writes results to the specified destination file. If omitted results are printed to stdout." + ) + + args = parser.parse_args() + session = ClientSession() + try: + data = await get_company_financials(args.symbol, False, session) + finally: + await session.close() + path: Path = args.to_file + if path is None: + print(json.dumps(data, indent=2)) + return + if path.suffix.lower() == JSON_EXT: + with open(path, 'w') as f: + json.dump(data, f, indent=2) + elif path.suffix.lower() == CSV_EXT: + with open(path, 'w') as f: + write_to_csv(data, f) + else: + print('unknown extension') + + +if __name__ == '__main__': + asyncio.run(main())