From f9745ff46d66c6d10c0d9cc3af08901253443b10 Mon Sep 17 00:00:00 2001 From: Daniil Fajnberg Date: Sat, 27 Nov 2021 15:36:51 +0100 Subject: [PATCH] two more functions implemented --- src/mwfin/functions.py | 12 ++++++++++-- tests/test_functions.py | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/mwfin/functions.py b/src/mwfin/functions.py index a1a8827..895923d 100644 --- a/src/mwfin/functions.py +++ b/src/mwfin/functions.py @@ -61,14 +61,22 @@ def extract_row_data(tr: Tag) -> tuple[str, tuple[int]]: Returns the name of the item displayed in the table row (of a financial statement) as well as a number for each reporting period. """ - pass + item_name = str(tr.td.div.string).strip() + data_div = tr.find_all('td')[-1].div.div + values_str: str = data_div.attrs['data-chart-data'] + values = tuple(int(float(s)) for s in values_str.split(',')) + return item_name, values def extract_all_data(soup: BeautifulSoup) -> ResultDict: """ Extracts financials from the page. """ - pass + output = {constants.END_DATE: extract_end_dates(soup)} + for row in find_relevant_table_rows(soup): + row_data = extract_row_data(row) + output[row_data[0]] = row_data[1] + return output async def _get_financial_statement(statement: str, ticker_symbol: str, quarterly: bool = False, diff --git a/tests/test_functions.py b/tests/test_functions.py index 3a1221b..7e1ac79 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -73,9 +73,9 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): mock_is_relevant_table_row.assert_has_calls([call(expected_output[0]), call(expected_output[1])]) def test_extract_row_data(self): - test_table = self.test_soup.find('div', attrs={'class': 'financials'}).div.div.table + test_row = self.test_soup.find('div', attrs={'class': 'financials'}).tbody.tr expected_output = ('Item_1', (11000000, -22000000)) - output = functions.extract_row_data(test_table.tbody.tr) + output = functions.extract_row_data(test_row) self.assertTupleEqual(expected_output, output) @patch.object(functions, 'extract_row_data')