fixed and refactored cli
This commit is contained in:
parent
9186d8734f
commit
36226fc1be
@ -13,6 +13,30 @@ from .constants import END_DATE
|
|||||||
|
|
||||||
JSON_EXT, CSV_EXT = '.json', '.csv'
|
JSON_EXT, CSV_EXT = '.json', '.csv'
|
||||||
|
|
||||||
|
TICKER_SYMBOL = 'ticker_symbol'
|
||||||
|
QUARTERLY = 'quarterly'
|
||||||
|
TO_FILE = 'to_file'
|
||||||
|
|
||||||
|
|
||||||
|
def parse_cli() -> dict:
|
||||||
|
parser = ArgumentParser(description="Scrape company financials")
|
||||||
|
parser.add_argument(
|
||||||
|
TICKER_SYMBOL,
|
||||||
|
type=str,
|
||||||
|
help="Stock ticker symbol of the company to be scraped the financials of"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-Q', f'--{QUARTERLY}',
|
||||||
|
action='store_true',
|
||||||
|
help="If set, the financial data for the last quarters is returned; otherwise yearly data is returned."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-f', f'--{TO_FILE.replace("_", "-")}',
|
||||||
|
type=Path,
|
||||||
|
help="Writes results to the specified destination file. If omitted results are printed to stdout."
|
||||||
|
)
|
||||||
|
return vars(parser.parse_args())
|
||||||
|
|
||||||
|
|
||||||
def write_to_csv(data: Dict[str, ResultDict], file_obj) -> None:
|
def write_to_csv(data: Dict[str, ResultDict], file_obj) -> None:
|
||||||
writer = csv.writer(file_obj)
|
writer = csv.writer(file_obj)
|
||||||
@ -24,24 +48,13 @@ def write_to_csv(data: Dict[str, ResultDict], file_obj) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
async def main() -> None:
|
||||||
parser = ArgumentParser(description="Scrape company financials")
|
args = parse_cli()
|
||||||
parser.add_argument(
|
|
||||||
'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()
|
session = ClientSession()
|
||||||
try:
|
try:
|
||||||
data = await get_company_financials(args.symbol, False, session)
|
data = await get_company_financials(args[TICKER_SYMBOL], quarterly=args[QUARTERLY], session=session)
|
||||||
finally:
|
finally:
|
||||||
await session.close()
|
await session.close()
|
||||||
path: Path = args.to_file
|
path: Path = args[TO_FILE]
|
||||||
if path is None:
|
if path is None:
|
||||||
print(json.dumps(data, indent=2))
|
print(json.dumps(data, indent=2))
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user