From 238054065957e1581bc4c80a19210d0ff40090dd Mon Sep 17 00:00:00 2001 From: Maximilian Fajnberg Date: Sat, 27 Nov 2021 17:23:36 +0100 Subject: [PATCH] drafted remaining functions --- src/mwfin/constants.py | 2 +- src/mwfin/functions.py | 16 +++++++++++----- tests/test_functions.py | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/mwfin/constants.py b/src/mwfin/constants.py index 4f6468e..80c72ce 100644 --- a/src/mwfin/constants.py +++ b/src/mwfin/constants.py @@ -1,6 +1,6 @@ HTML_PARSER = 'html.parser' DOMAIN = 'www.marketwatch.com' -BASE_URL = f'https://{DOMAIN}/investing/stock' +BASE_URL = f'https://{DOMAIN}/investing/stock/' BS, IS, CF = 'bs', 'is', 'cf' FIN_STMT_URL_SUFFIX = { diff --git a/src/mwfin/functions.py b/src/mwfin/functions.py index 895923d..5ab711e 100644 --- a/src/mwfin/functions.py +++ b/src/mwfin/functions.py @@ -84,7 +84,10 @@ async def _get_financial_statement(statement: str, ticker_symbol: str, quarterly """ Returns data from the specified financial statement of the specified company. """ - pass + q = '/quarter' if quarterly else '' + url = constants.BASE_URL + ticker_symbol + '/financials' + constants.FIN_STMT_URL_SUFFIX[statement] + q + soup = await soup_from_url(url, session) + return extract_all_data(soup) async def get_balance_sheet(ticker_symbol: str, quarterly: bool = False, @@ -92,7 +95,7 @@ async def get_balance_sheet(ticker_symbol: str, quarterly: bool = False, """ Returns data from the balance sheet of the specified company. """ - pass + return await _get_financial_statement(constants.BS, ticker_symbol, quarterly, session) async def get_income_statement(ticker_symbol: str, quarterly: bool = False, @@ -100,7 +103,7 @@ async def get_income_statement(ticker_symbol: str, quarterly: bool = False, """ Returns data from the income statement of the specified company. """ - pass + return await _get_financial_statement(constants.IS, ticker_symbol, quarterly, session) async def get_cash_flow_statement(ticker_symbol: str, quarterly: bool = False, @@ -108,7 +111,7 @@ async def get_cash_flow_statement(ticker_symbol: str, quarterly: bool = False, """ Returns data from the cash flow statement of the specified company. """ - pass + return await _get_financial_statement(constants.CF, ticker_symbol, quarterly, session) async def get_company_financials(ticker_symbol: str, quarterly: bool = False, @@ -116,4 +119,7 @@ async def get_company_financials(ticker_symbol: str, quarterly: bool = False, """ Returns all fundamentals (balance sheet, income statement and cash flow statement) of the specified company. """ - pass + financials = await get_balance_sheet(ticker_symbol, quarterly, session) + financials.update(await get_income_statement(ticker_symbol, quarterly, session)) + financials.update(await get_cash_flow_statement(ticker_symbol, quarterly, session)) + return financials diff --git a/tests/test_functions.py b/tests/test_functions.py index 7e1ac79..aabbc56 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -104,7 +104,7 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): async def test__get_financial_statement(self, mock_soup_from_url, mock_extract_all_data): mock_session = MagicMock() test_ticker, statement = 'bar', BS - test_url = f'{BASE_URL}/{test_ticker}/financials{FIN_STMT_URL_SUFFIX[statement]}' + test_url = f'{BASE_URL}{test_ticker}/financials{FIN_STMT_URL_SUFFIX[statement]}' mock_soup_from_url.return_value = mock_soup = MagicMock() mock_extract_all_data.return_value = expected_output = {'foo': 'bar'}