
    Ej                     |    d dl mZmZ d dlZd dlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ e
 dZ G d	 d
e          ZdS )    )ABCabstractmethodN)DictListOptional   )_QUERY1_URL_)YfData)Tickerz/v1/financec                   x   e Zd ZdZddefdZedefd            Zedefd            Zedefd            Z	ede
fd	            Zedefd
            Zedeej                 fd            Zedeeeef                  fd            ZdefdZddZdefdZdeej                 fdZedd            ZddZdS )Domainz
    Abstract base class representing a domain entity in financial data, with key attributes 
    and methods for fetching and parsing data. Derived classes must implement the `_fetch_and_parse()` method.
    Nkeyc                     || _         || _        t          |          | _        d| _        d| _        d| _        d| _        d| _        dS )z
        Initializes the Domain object with a key, session.

        Args:
            key (str): Unique key identifying the domain entity.
            session (Optional[requests.Session]): Session object for HTTP requests. Defaults to None.
        )sessionN)	_keyr   r
   _data_name_symbol	_overview_top_companies_research_reports)selfr   r   s      R/var/www/html/bolsaweb/venv/lib/python3.11/site-packages/yfinance/domain/domain.py__init__zDomain.__init__   sO     	#G444
$(
&*)-7;AE    returnc                     | j         S )z
        Retrieves the key of the domain entity.

        Returns:
            str: The unique key of the domain entity.
        )r   r   s    r   r   z
Domain.key#   s     yr   c                 D    |                      | j                   | j        S )z|
        Retrieves the name of the domain entity.

        Returns:
            str: The name of the domain entity.
        )_ensure_fetchedr   r   s    r   namezDomain.name-   s"     	TZ(((zr   c                 D    |                      | j                   | j        S )z
        Retrieves the symbol of the domain entity.

        Returns:
            str: The symbol representing the domain entity.
        )r    r   r   s    r   symbolzDomain.symbol8   s"     	T\***|r   c                 ^    |                      | j                   t          | j                  S )z
        Retrieves a Ticker object based on the domain entity's symbol.

        Returns:
            Ticker: A Ticker object associated with the domain entity.
        )r    r   r   r   s    r   tickerzDomain.tickerC   s*     	T\***dl###r   c                 D    |                      | j                   | j        S )z
        Retrieves the overview information of the domain entity.

        Returns:
            Dict: A dictionary containing an overview of the domain entity.
        )r    r   r   s    r   overviewzDomain.overviewN   s"     	T^,,,~r   c                 D    |                      | j                   | j        S )z
        Retrieves the top companies within the domain entity.

        Returns:
            pandas.DataFrame: A DataFrame containing the top companies in the domain.
        )r    r   r   s    r   top_companieszDomain.top_companiesY   s$     	T0111""r   c                 D    |                      | j                   | j        S )z
        Retrieves research reports related to the domain entity.

        Returns:
            List[Dict[str, str]]: A list of research reports, where each report is a dictionary with metadata.
        )r    r   r   s    r   research_reportszDomain.research_reportsd   s$     	T3444%%r   c                 L    ddddd}| j                             ||          }|S )z
        Fetches data from the given query URL.

        Args:
            query_url (str): The URL used for the data query.

        Returns:
            Dict: The JSON response data from the request.
        truezen-USUS)	formattedwithReturnslangregion)params)r   get_raw_json)r   	query_urlparams_dictresults       r   _fetchzDomain._fetcho   s5     %+67^bcc((;(GGr   c                 Z   |                     d          | _        |                     d          | _        |                     |                     di                     | _        |                     |                     di                     | _        |                     d          | _        dS )z
        Parses and assigns common data fields such as name, symbol, overview, and top companies.

        Args:
            data (Dict): The raw data received from the API.
        r!   r#   r'   topCompaniesresearchReportsN)getr   r   _parse_overviewr   _parse_top_companiesr   r   )r   datas     r   _parse_and_assign_commonzDomain._parse_and_assign_common}   s     XXf%%
xx))--dhhz2.F.FGG"77QS8T8TUU!%*;!<!<r   c           
         |                     dd          |                     di                                dd          |                     dd          |                     dd          |                     dd          |                     di                                dd          |                     d	i                                dd          d
S )z
        Parses the overview data for the domain entity.

        Args:
            overview (Dict): The raw overview data.

        Returns:
            Dict: A dictionary containing parsed overview information.
        companiesCountN	marketCaprawmessageBoardIddescriptionindustriesCountmarketWeightemployeeCount)companies_count
market_capmessage_board_idrF   industries_countmarket_weightemployee_countr<   )r   r'   s     r   r=   zDomain._parse_overview   s      (||,<dCC",,{B77;;E4HH (-=t D D#<<t<< (-> E E%\\."==AA%NN&ll?B??CCE4PP
 
 	
r   c                 |    g d}d |D             }|sdS t          j        ||                              d          S )a)  
        Parses the top companies data and converts it into a pandas DataFrame.

        Args:
            top_companies (Dict): The raw top companies data.

        Returns:
            Optional[pandas.DataFrame]: A DataFrame containing top company data, or None if no data is available.
        )r#   r!   ratingzmarket weightc           	          g | ]i}|                     d           |                     d          |                     d          |                     di                                dd          fjS )r#   r!   rR   rH   rD   NrP   ).0cs     r   
<listcomp>z/Domain._parse_top_companies.<locals>.<listcomp>   s}      b  b  b OP "#x !f !h !nR 8 8 < <U4 H H!J  b  b  br   N)columnsr#   )_pd	DataFrame	set_index)r   r)   top_companies_columntop_companies_valuess       r   r>   zDomain._parse_top_companies   sn      MLL b  b Ta b  b  b
 $ 	4}1;OPPPZZ[cdddr   c                      t          d          )z
        Abstract method for fetching and parsing domain-specific data. 
        Must be implemented by derived classes.
        z>_fetch_and_parse() needs to be implemented by children classes)NotImplementedErrorr   s    r   _fetch_and_parsezDomain._fetch_and_parse   s     ""bcccr   c                 6    ||                                   dS dS )z
        Ensures that the given attribute is fetched by calling `_fetch_and_parse()` if the attribute is None.

        Args:
            attribute: The attribute to check and potentially fetch.
        N)r_   )r   	attributes     r   r    zDomain._ensure_fetched   s*     !!##### r   )N)r   N)__name__
__module____qualname____doc__strr   propertyr   r!   r#   r   r%   r   r'   r   rX   rY   r)   r   r+   r8   r@   r=   r>   r   r_   r     r   r   r   r      s        
F FC F F F F$ S    X c    X     X $ $ $ $ X$ $    X #x6 # # # X# &$tCH~"6 & & & X&4    = = = =
4 
 
 
 
(eXcm5L e e e e* d d d ^d$ $ $ $ $ $r   r   )abcr   r   pandasrX   typingr   r   r   constr	   r?   r
   r%   r   _QUERY_URL_r   rh   r   r   <module>rn      s    # # # # # # # #     ' ' ' ' ' ' ' ' ' '                        ***x$ x$ x$ x$ x$S x$ x$ x$ x$ x$r   