cli for writing to json/csv

This commit is contained in:
Maximilian Fajnberg 2021-11-28 15:53:19 +01:00
parent 2d9119a4be
commit 6dd37a4aa2
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1 @@
from .functions import get_company_financials

View File

@ -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())