def _parse_module_index_trend_estimates(self, modules_dict, module_name):
        """
        Method to parse the indexTrend module returned from the Yahoo Finance API call. The indexTrend module is
        uniquely parsed because it contains two separate pieces of data: The index trend's information and the index
        trend's estimates. This method will parse the index trend's estimates portion of the indexTrend.
        :param modules_dict: A dictionary containing all the modules.
        :type modules_dict: dict
        :param module_name: The name of the module to be parsed.
        :type module_name: str
        :return: A tuple containing a dataframe containing the company profile and None, or None and an error.
        :rtype: tuple
        """

        # Attempt to find the indexTrend module and the estimates submodule. If the module is not found, then it will
        # return None and a YahooModuleNotFoundError.
        try:
            index_trend_info = modules_dict[module_name]
            index_trend_estimates = index_trend_info['estimates']

        except Exception as e:
            return None, YahooExceptions.YahooModuleNotFoundError(e)

        # Attempt to get a formatted dataframe from the module. If there was an error formatting the dataframe, then it
        # will return None and a YahooModuleFormatError.
        try:
            index_trend_estimates = self._format_dataframe(
                index_trend_estimates)

        except Exception as e:
            return None, YahooExceptions.YahooModuleFormatError(e)

        # If the module was found and formatted, then it will return a dataframe containing the company officers
        # information and None since there was no error.
        return index_trend_estimates, None
    def _parse_module_asset_profile_profile(self, modules_dict, module_name):
        """
        Method to parse the assetProfile module returned from the Yahoo Finance API call. The assetProfile module is
        uniquely parsed because it contains two separate pieces of data: The company profile and the company officers.
        This method will parse the profile portion of the assetProfile.
        :param modules_dict: A dictionary containing all the modules.
        :type modules_dict: dict
        :param module_name: The name of the module to be parsed.
        :type module_name: str
        :return: A tuple containing a dataframe containing the company profile and None, or None and an error.
        :rtype: tuple
        """

        # Attempt to find the assetProfile module. If the module is not found, then it will return None and a
        # YahooModuleNotFoundError.
        try:
            profile_dict = modules_dict[module_name]

        except Exception as e:
            return None, YahooExceptions.YahooModuleNotFoundError(e)

        # Attempt to get a formatted dataframe from the module. If there was an error formatting the dataframe, then it
        # will return None and a YahooModuleFormatError.
        try:
            # We are going to remove the company officer's submodule to be parsed later.
            del profile_dict['companyOfficers']
            profile = self._format_dataframe(profile_dict)

        except Exception as e:
            return None, YahooExceptions.YahooModuleFormatError(e)

        # If the module was found and formatted, then it will return a dataframe containing the profile
        # information and None since there was no error.
        return profile, None
    def _parse_module_earnings_finance_quarterly(self, modules_dict,
                                                 module_name):
        """
        Method to parse the earnings module returned from the Yahoo Finance API call. The earnings module is uniquely
        parsed because it contains four separate pieces of data: The company earnings estimates, the company quarterly
        earnings estimates, the company's yearly financials, and the company's quarterly financials.
        This method will parse the company's quarterly financials.
        :param modules_dict: A dictionary containing all the modules.
        :type modules_dict: dict
        :param module_name: The name of the module to be parsed.
        :type module_name: str
        :return: A tuple containing a dataframe containing the company profile and None, or None and an error.
        :rtype: tuple
        """

        # Attempt to find the earnings, financialsChart, and quarterly submodule. If the module is not found, then it
        # will return None and a YahooModuleNotFoundError.
        try:
            module = modules_dict[module_name]
            financial_quarterly = module['financialsChart']['quarterly']

        except Exception as e:
            return None, YahooExceptions.YahooModuleNotFoundError(e)

        # Attempt to get a formatted dataframe from the module. If there was an error formatting the dataframe, then it
        # will return None and a YahooModuleFormatError.
        try:
            financial_quarterly = self._format_dataframe(financial_quarterly)

        except Exception as e:
            return None, YahooExceptions.YahooModuleFormatError(e)

        # If the module was found and formatted, then it will return a dataframe containing the company officers
        # information and None since there was no error.
        return financial_quarterly, None
Example #4
0
    def _parse_quote(self, quotes_dict):
        try:
            quotes = quotes_dict['indicators']['quote'][0]
            dates = quotes_dict['timestamp']
            adj_close = quotes_dict['indicators']['adjclose'][0]

        except Exception as e:
            return None, YahooExceptions.YahooIndicatorNotFoundError(e)

        try:
            # Combine the two dictionaries.
            quote_dictionary = {**quotes, **adj_close}

            # Turn the dictionary into a dataframe.
            quote_dataframe = pd.DataFrame.from_dict(quote_dictionary)

            # Get the timestamp (datetime) for each quote entry in the data and parse it.
            quote_dataframe['date'] = dates

            quote_dataframe.reindex(columns=[
                'date', 'open', 'high', 'low', 'close', 'adjclose', 'volume'
            ])

        except Exception as e:
            return None, YahooExceptions.YahooIndicatorFormatError(e)

        return quote_dataframe, None
Example #5
0
    def _parse_quote_splits(self, quotes_dict):
        try:
            # Get the splits dictionary from the JSON API output.
            splits_dict = quotes_dict['chart']['result'][0]['events']['splits']
        except Exception as e:
            return None, YahooExceptions.YahooIndicatorNotFoundError(e)

        try:
            # Convert the splits dictionary to a Dataframe.
            splits_dataframe = pd.DataFrame.from_dict(splits_dict,
                                                      orient='index')
            splits_dataframe.index = pd.Index(
                range(0, splits_dataframe.shape[0]))
        except Exception as e:
            return None, YahooExceptions.YahooIndicatorFormatError(e)

        return splits_dataframe, None
    def _parse_module_earnings_estimates(self, modules_dict, module_name):
        """
        Method to parse the earnings module returned from the Yahoo Finance API call. The earnings module is uniquely
        parsed because it contains four separate pieces of data: The company earnings estimates, the company quarterly
        earnings estimates, the company's yearly financials, and the company's quarterly financials.
        This method will parse the company's earnings estimates.
        :param modules_dict: A dictionary containing all the modules.
        :type modules_dict: dict
        :param module_name: The name of the module to be parsed.
        :type module_name: str
        :return: A tuple containing a dataframe containing the company profile and None, or None and an error.
        :rtype: tuple
        """

        # Attempt to find the earnings and the earningsChart submodule. If the module is not found, then it will
        # return None and a YahooModuleNotFoundError.
        try:
            earnings_estimates_dict = modules_dict[module_name][
                'earningsChart']
            del earnings_estimates_dict['quarterly']

        except Exception as e:
            return None, YahooExceptions.YahooModuleNotFoundError(e)

        # Attempt to get a formatted dataframe from the module. If there was an error formatting the dataframe, then it
        # will return None and a YahooModuleFormatError.
        try:
            # Sometimes the earnings data portion is returned as a list instead of a date.
            if isinstance(earnings_estimates_dict['earningsDate'], list):
                earnings_estimates_dict[
                    'earningsDate'] = earnings_estimates_dict['earningsDate'][
                        0]

            earnings_estimates = self._format_dataframe(
                earnings_estimates_dict)

        except Exception as e:
            return None, YahooExceptions.YahooModuleFormatError(e)

        # If the module was found and formatted, then it will return a dataframe containing the company officers
        # information and None since there was no error.
        return earnings_estimates, None
    def _parse_module_error(self, symbol, response_data):
        if 'Will be right back' in response_data.text:
            exception = str(
                YahooExceptions.YahooRuntimeError(
                    'Yahoo Finance is currently down.'))
            return YahooSummaryResponse(symbol, exception)

        else:
            if response_data['quoteSummary'][
                    'result'] is None and response_data['quoteSummary'][
                        'error'] is not None:
                exception = str(
                    YahooExceptions.YahooRequestError(
                        response_data['quoteSummary']['error']['description']))
                return YahooSummaryResponse(symbol, exception)

            else:
                exception = str(
                    YahooExceptions.YahooRequestError(response_data))
                return YahooSummaryResponse(symbol, exception)
    def _parse_module_calendar_events_earnings(self, modules_dict,
                                               module_name):
        """
        Method to parse the calendarEvents module returned from the Yahoo Finance API call. The calendarEvents module is
        uniquely parsed because it contains two separate pieces of data: The company's earnings events and the company's
        dividends dates. This method will parse the company's earnings dates portion of the calendarEvents.
        :param modules_dict: A dictionary containing all the modules.
        :type modules_dict: dict
        :param module_name: The name of the module to be parsed.
        :type module_name: str
        :return: A tuple containing a dataframe containing the company profile and None, or None and an error.
        :rtype: tuple
        """

        # Attempt to find the calendarEvents module and the earnings submodule. If the module is not found, then it will
        # return None and a YahooModuleNotFoundError.
        try:
            calender_events_earnings = modules_dict[module_name]['earnings']

        except Exception as e:
            return None, YahooExceptions.YahooModuleNotFoundError(e)

        # Attempt to get a formatted dataframe from the module. If there was an error formatting the dataframe, then it
        # will return None and a YahooModuleFormatError.
        try:
            if isinstance(calender_events_earnings['earningsDate'], list):
                calender_events_earnings[
                    'earningsDate'] = calender_events_earnings['earningsDate'][
                        0]

            calender_events_earnings = self._format_dataframe(
                calender_events_earnings)
        except Exception as e:
            return None, YahooExceptions.YahooModuleFormatError(e)

        # If the module was found and formatted, then it will return a dataframe containing the company officers
        # information and None since there was no error.
        return calender_events_earnings, None
    def _parse_module(self, modules_dict, module_name, submodule_name=None):
        """
        Method to parse an individual requested module returned from the Yahoo Finance API call.
        :param modules_dict: A dictionary containing all the modules.
        :type modules_dict: dict
        :param module_name: The name of the module to be parsed.
        :type module_name: str
        :param submodule_name: The name of the submodule to be parsed. Some modules are wrapped in another dictionary.
        :type submodule_name: str
        :return: A tuple containing a dataframe containing the information and None, or None and an error.
        :rtype: tuple
        """

        # Attempt to find the module to be parsed. If the module is not found, then it will return None and a
        # YahooModuleNotFoundError.
        try:
            # Checks to see if there is a submodule.
            if not submodule_name:
                module = modules_dict[module_name]
            else:
                module = modules_dict[module_name][submodule_name]

        except Exception as e:
            return None, YahooExceptions.YahooModuleNotFoundError(e)

        # Attempt to get a formatted dataframe from the module. If there was an error formatting the dataframe, then it
        # will return None and a YahooModuleFormatError.
        try:
            module_dataframe = self._format_dataframe(module)

        except Exception as e:
            return None, YahooExceptions.YahooModuleFormatError(e)

        # If the module was found and formatted, then it will return a dataframe containing the module information and
        # None since there was no error.
        return module_dataframe, None