o
    ùT·jý  ã                   @   sn   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
„ d
eƒZdS )é    )ÚABCÚabstractmethodN)ÚDictÚListÚOptionalé   )Ú_QUERY1_URL_)ÚYfData)ÚTickerz/v1/financec                   @   s  e Zd ZdZd#dedefdd„Zedefd	d
„ƒZedefdd„ƒZedefdd„ƒZ	ede
fdd„ƒZedefdd„ƒZedeej fdd„ƒZedeeeef  fdd„ƒZdefdd„Zd$dd„Zdefdd„Zdeej fdd„Zed$dd „ƒZd$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ÚUSÚkeyÚregionc                 C   sH   || _ || _| ¡  ¡ | _t|d�| _d| _d| _d| _	d| _
d| _dS )aï  
        Initializes the Domain object with a key, session, and region.

        Args:
            key (str): Unique key identifying the domain entity.
            session (Optional[requests.Session]): Session object for HTTP requests. Defaults to None.
            region (str): Yahoo region (ISO 3166-1 alpha-2 country code, e.g.
                "US", "GB", "FR", "DE", "JP"). Determines the regional scope
                of returned data such as ``top_companies``. Defaults to "US".
        )ÚsessionN)Ú_keyr   ÚstripÚupperÚ_regionr	   Ú_dataÚ_nameÚ_symbolÚ	_overviewÚ_top_companiesÚ_research_reports)Úselfr   r   r   © r   úY/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/yfinance/domain/domain.pyÚ__init__   s   
zDomain.__init__Úreturnc                 C   s   | j S )z�
        Retrieves the key of the domain entity.

        Returns:
            str: The unique key of the domain entity.
        )r   ©r   r   r   r   r   '   s   z
Domain.keyc                 C   ó   |   | j¡ | jS )z|
        Retrieves the name of the domain entity.

        Returns:
            str: The name of the domain entity.
        )Ú_ensure_fetchedr   r   r   r   r   Úname1   ó   zDomain.namec                 C   r    )zŠ
        Retrieves the symbol of the domain entity.

        Returns:
            str: The symbol representing the domain entity.
        )r!   r   r   r   r   r   Úsymbol<   r#   zDomain.symbolc                 C   s   |   | 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   r   r   r   ÚtickerG   s   
zDomain.tickerc                 C   r    )z¨
        Retrieves the overview information of the domain entity.

        Returns:
            Dict: A dictionary containing an overview of the domain entity.
        )r!   r   r   r   r   r   ÚoverviewR   r#   zDomain.overviewc                 C   r    )z¯
        Retrieves the top companies within the domain entity.

        Returns:
            pandas.DataFrame: A DataFrame containing the top companies in the domain.
        )r!   r   r   r   r   r   Útop_companies]   r#   zDomain.top_companiesc                 C   r    )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   r   r   r   Úresearch_reportsh   r#   zDomain.research_reportsc                 C   s$   ddd| j dœ}| jj||d�}|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)Ú	formattedÚwithReturnsÚlangr   )Úparams)r   r   Úget_raw_json)r   Ú	query_urlÚparams_dictÚresultr   r   r   Ú_fetchs   s   
zDomain._fetchc                 C   sP   |  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   Údatar   r   r   Ú_parse_and_assign_common�   s
   zDomain._parse_and_assign_commonc              
   C   sd   |  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_idr>   Úindustries_countÚmarket_weightÚemployee_count©r5   )r   r&   r   r   r   r6   Ž   s   



ùzDomain._parse_overviewc                 C   s2   g d¢}dd„ |D ƒ}|sdS t j||d� 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              	   S   s:   g | ]}|  d ¡|  d¡|  d¡|  di ¡  dd¡f‘qS )r$   r"   rI   r@   r<   NrH   )Ú.0Úcr   r   r   Ú
<listcomp>­   s    ýýz/Domain._parse_top_companies.<locals>.<listcomp>N)Úcolumnsr$   )Ú_pdÚ	DataFrameÚ	set_index)r   r'   Útop_companies_columnÚtop_companies_valuesr   r   r   r7   ¢   s   
ýzDomain._parse_top_companiesc                 C   s   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   r   r   r   Ú_fetch_and_parse·   s   zDomain._fetch_and_parsec                 C   s   |du r
|   ¡  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)rT   )r   Ú	attributer   r   r   r!   ¿   s   ÿzDomain._ensure_fetched)Nr   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   Úpropertyr   r"   r$   r
   r%   r   r&   r   rN   rO   r'   r   r(   r2   r9   r6   r7   r   rT   r!   r   r   r   r   r      s0    	






r   )Úabcr   r   ÚpandasrN   Útypingr   r   r   Úconstr   r8   r	   r%   r
   Ú_QUERY_URL_r   r   r   r   r   Ú<module>   s    
