
    Wg                     t    d dl mZmZ ddlmZ ddlmZ ddlmZ d dl	m
Z
mZmZ d dlZe dZ G d	 d
e      Zy)    )ABCabstractmethod   )Ticker)_QUERY1_URL_)YfData)DictListOptionalNz/v1/financec                   B   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y)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| _	        y)aK  
        Initializes the Domain object with a key, session, and proxy.

        Args:
            key (str): Unique key identifying the domain entity.
            session (Optional[requests.Session]): Session object for HTTP requests. Defaults to None.
            proxy (Optional[Dict]): Proxy settings. Defaults to None.
        )sessionN)
_keyproxyr   r   _data_name_symbol	_overview_top_companies_research_reports)selfr   r   r   s       I/var/www/html/venv/lib/python3.12/site-packages/yfinance/domain/domain.py__init__zDomain.__init__   sJ     	
#G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     yyr   c                 P    | j                  | 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      	TZZ(zzr   c                 P    | j                  | 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.symbol9   s      	T\\*||r   c                 b    | j                  | 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.tickerD   s%     	T\\*dll##r   c                 P    | j                  | 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.overviewO   s      	T^^,~~r   c                 P    | j                  | 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_companiesZ   s$     	T001"""r   c                 P    | j                  | 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_reportse   s$     	T334%%%r   c                 z    ddddd}| j                   j                  || j                   j                  ||      }|S )a  
        Fetches data from the given query URL.

        Args:
            query_url (str): The URL used for the data query.
            proxy (Dict): Proxy settings for the request.

        Returns:
            Dict: The JSON response data from the request.
        truezen-USUS)	formattedwithReturnslangregion)user_agent_headersparamsr   )r   get_raw_jsonr4   )r   	query_urlr   params_dictresults        r   _fetchzDomain._fetchp   sH     %+67^bc((tzzGdGdmx  AF(  Gr   c                     |j                  d      | _        |j                  d      | _        | j                  |j                  di             | _        | j                  |j                  di             | _        |j                  d      | _        y)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   sr     XXf%
xx)--dhhz2.FG"77QS8TU!%*;!<r   c           
      V   |j                  dd      |j                  di       j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  di       j                  dd      |j                  d	i       j                  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_idrH   industries_countmarket_weightemployee_count)r>   )r   r(   s     r   r?   zDomain._parse_overview   s      (||,<dC",,{B7;;E4H (-=t D#<<t< (-> E%\\."=AA%N&ll?B?CCE4P
 	
r   c           
      &   g d}|D cg c]U  }|j                  d      |j                  d      |j                  d      |j                  di       j                  dd      fW }}|syt        j                  ||      j                  d      S c c}w )	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 weightr$   r"   rS   rJ   rF   N)columns)r>   _pd	DataFrame	set_index)r   r*   top_companies_columnctop_companies_valuess        r   r@   zDomain._parse_top_companies   s      M Ta b OP "#x !f !h !nR 8 < <U4 H!J  b  b
 $}}1;OPZZ[cdd bs   AB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cr   c                 *    || j                          yy)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   )NN)r   N)__name__
__module____qualname____doc__strr   propertyr   r"   r$   r   r&   r	   r(   r   rU   rV   r*   r
   r,   r:   rB   r?   r@   r   r]   r!    r   r   r   r   
   s7   
FC F( S   c      $ $ $ $   #x6 # # &$tCH~"6 & &$ =
4 
(eXcmm5L e* d d$r   r   )abcr   r   r&   r   constr   rA   r   typingr	   r
   r   pandasrU   _QUERY_URL_r   rf   r   r   <module>rl      s4    #     ' ' k*{$S {$r   