
    h".                     ~    d dl Zd dlmZmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ e	 dZ G d d	          ZdS )
    N)DictOptional)utils)
_BASE_URL_
_SENTINEL_)YfData)YFDataExceptionz/v10/finance/quoteSummary/c                      e Zd ZdZefdedefdZdefdZe	defd            Z
e	deeee         f         fd            Ze	dej        fd	            Ze	deeef         fd
            Ze	dej        fd            Ze	dej        fd            Ze	dej        fd            Ze	deeef         fd            Ze	deeef         fd            Zd ZddZedd            ZddZddZd ZdS )	FundsDataz
    ETF and Mutual Funds Data
    Queried Modules: quoteType, summaryProfile, fundProfile, topHoldings

    Notes: 
    - fundPerformance module is not implemented as better data is queryable using history
    datasymbolc                 ,   || _         || _        |t          ur6t          j        dt
          d           | j                             |           d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        dS )z
        Args:
            data (YfData): The YfData object for fetching data.
            symbol (str): The symbol of the fund.
        z=Set proxy via new config function: yf.set_config(proxy=proxy)   )
stacklevelN)_data_symbolr   warningswarnDeprecationWarning
_set_proxy_quote_type_description_fund_overview_fund_operations_asset_classes_top_holdings_equity_holdings_bond_holdings_bond_ratings_sector_weightings)selfr   r   proxys       U/var/www/html/marketdash/venv/lib/python3.11/site-packages/yfinance/scrapers/funds.py__init__zFundsData.__init__   s     

""MY[mz{||||J!!%(((   ! # $ #! $"!"&    returnc                 F    | j         |                                  | j         S )zh
        Returns the quote type of the fund.

        Returns:
            str: The quote type.
        )r   _fetch_and_parser!   s    r#   
quote_typezFundsData.quote_type2   s(     #!!###r%   c                 F    | j         |                                  | j         S )zj
        Returns the description of the fund.

        Returns:
            str: The description.
        )r   r(   r)   s    r#   descriptionzFundsData.description=   s(     $!!###  r%   c                 F    | j         |                                  | j         S )zw
        Returns the fund overview.

        Returns:
            Dict[str, Optional[str]]: The fund overview.
        )r   r(   r)   s    r#   fund_overviewzFundsData.fund_overviewI   (     &!!###""r%   c                 F    | j         |                                  | j         S )zo
        Returns the fund operations.

        Returns:
            pd.DataFrame: The fund operations.
        )r   r(   r)   s    r#   fund_operationszFundsData.fund_operationsU   (      (!!###$$r%   c                 F    | j         |                                  | j         S )z{
        Returns the asset classes of the fund.

        Returns:
            Dict[str, float]: The asset classes.
        )r   r(   r)   s    r#   asset_classeszFundsData.asset_classesa   r/   r%   c                 F    | j         |                                  | j         S )zu
        Returns the top holdings of the fund.

        Returns:
            pd.DataFrame: The top holdings.
        )r   r(   r)   s    r#   top_holdingszFundsData.top_holdingsm   (     %!!###!!r%   c                 F    | j         |                                  | j         S )z{
        Returns the equity holdings of the fund.

        Returns:
            pd.DataFrame: The equity holdings.
        )r   r(   r)   s    r#   equity_holdingszFundsData.equity_holdingsy   r2   r%   c                 F    | j         |                                  | j         S )zw
        Returns the bond holdings of the fund.

        Returns:
            pd.DataFrame: The bond holdings.
        )r   r(   r)   s    r#   bond_holdingszFundsData.bond_holdings   r/   r%   c                 F    | j         |                                  | j         S )zy
        Returns the bond ratings of the fund.

        Returns:
            Dict[str, float]: The bond ratings.
        )r   r(   r)   s    r#   bond_ratingszFundsData.bond_ratings   r7   r%   c                 F    | j         |                                  | j         S )z
        Returns the sector weightings of the fund.

        Returns:
            Dict[str, float]: The sector weightings.
        )r    r(   r)   s    r#   sector_weightingszFundsData.sector_weightings   s(     "*!!###&&r%   c                     d                     g d          }|d| j        dd}| j                            t          | j        z   |          }|S )zp
        Fetches the raw JSON data from the API.

        Returns:
            dict: The raw JSON data.
        ,)	quoteTypesummaryProfiletopHoldingsfundProfilezfinance.yahoo.comfalse)modules
corsDomainr   	formatted)params)joinr   r   get_raw_json_QUOTE_SUMMARY_URL_)r!   rG   params_dictresults       r#   _fetchzFundsData._fetch   s[     ((XXXYY")9LX\Xdsz{{(()<T\)IR](^^r%   Nc                    |                                  }	 |d         d         d         }|d         d         | _        |                     |d                    |                     |d                    |                     |d                    dS # t
          $ r t          d          t          $ r}t          j	                    }|
                    d	| j         d
|            |                    d           |                    d           |                    d|            |                    d           Y d}~dS d}~ww xY w)z;
        Fetches and parses the data from the API.
        quoteSummaryrO   r   rB   rC   rD   rE   zNo Fund data found.zFailed to get fund data for 'z
' reason: zGot response: z------------- N)rP   r   _parse_description_parse_top_holdings_parse_fund_profileKeyErrorr	   	Exceptionr   get_yf_loggererrorr   debug)r!   rO   r   eloggers        r#   r(   zFundsData._fetch_and_parse   se    	*.)(3A6D#K0=D ##D)9$:;;;$$T-%8999$$T-%899999 	9 	9 	9!"7888 	* 	* 	*(**FLLTTTQRTTUUULL)***LL)))LLT$$$LL)))))))))	*s   A8B "E2B
EEc                 \    t          | t                    s| S |                     d|          S )z
        Parses raw values from the data.

        Args:
            data: The data to parse.
            default: The default value if data is not a dictionary.

        Returns:
            The parsed value or the default value.
        raw)
isinstancedictget)r   defaults     r#   _parse_raw_valueszFundsData._parse_raw_values   s/     $%% 	Kxxw'''r%   c                 <    |                     dd          | _        dS )zk
        Parses the description from the data.

        Args:
            data: The data to parse.
        longBusinessSummary N)rb   r   )r!   r   s     r#   rT   zFundsData._parse_description   s!     !HH%:B??r%   c                 F   |                      |                    dd                    |                      |                    dd                    |                      |                    dd                    |                      |                    dd                    |                      |                    dd                    |                      |                    dd                    d| _        |                    d	g           }g g g }}}|D ]S}|                    |d
                    |                    |d                    |                    |d                    Tt	          j        |||d                              d          | _        |                    di           }t	          j        dg d| j        |                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            gd|                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            |                      |                    dt          j	                            gi                              d          | _
        |                    di           }t	          j        dg d | j        |                      |                    d!t          j	                            |                      |                    d"t          j	                            |                      |                    d#t          j	                            gd|                      |                    d$t          j	                            |                      |                    d%t          j	                            |                      |                    d&t          j	                            gi                              d          | _        t          d' |                    d(g           D                       | _        t          d) |                    d*g           D                       | _        dS )+zl
        Parses the top holdings from the data.

        Args:
            data: The data to parse.
        cashPositionNstockPositionbondPositionpreferredPositionconvertiblePositionotherPosition)ri   rj   rk   rl   rm   rn   holdingsr   holdingNameholdingPercent)SymbolNamezHolding Percentrr   equityHoldingsAverage)zPrice/Earningsz
Price/BookzPrice/SaleszPrice/CashflowzMedian Market Capz3 Year Earnings GrowthpriceToEarningspriceToBookpriceToSalespriceToCashflowmedianMarketCapthreeYearEarningsGrowthCategory AveragepriceToEarningsCatpriceToBookCatpriceToSalesCatpriceToCashflowCatmedianMarketCapCatthreeYearEarningsGrowthCatbondHoldings)DurationMaturityzCredit QualitydurationmaturitycreditQualitydurationCatmaturityCatcreditQualityCatc              3   4   K   | ]}|D ]}|||         fV  d S N .0dkeys      r#   	<genexpr>z0FundsData._parse_top_holdings.<locals>.<genexpr>.  s9      !b!bA`a!b!bY\3#-!b!b!b!b!b!b!br%   bondRatingsc              3   4   K   | ]}|D ]}|||         fV  d S r   r   r   s      r#   r   z0FundsData._parse_top_holdings.<locals>.<genexpr>1  s9      &l&ljk&l&lcfQsV}&l&l&l&l&l&l&lr%   sectorWeightings)rd   rb   r   appendpd	DataFrame	set_indexr   r   NAr   r   ra   r   r    )	r!   r   	_holdingsr   _name_holding_percentitemr   r   s	            r#   rU   zFundsData._parse_top_holdings   sO    !22488ND3Q3QRR!33DHH_d4S4STT 22488ND3Q3QRR!%!7!7ATVZ8[8[!\!\#'#9#9$((CXZ^:_:_#`#`!33DHH_d4S4STT
 
 HHZ,,	+-r2( 	< 	<DNN4>***LLm,---##D)9$:;;;;\/+
 +
   9X	 	  88$4b99 "  H  H  HL&&'7';';<Mru'U'UVV&&'7';';M25'Q'QRR&&'7';';NBE'R'RSS&&'7';';<Mru'U'UVV&&'7';';<Mru'U'UVV&&'7';';<UWYW\']']^^ &&'7';';<PRTRW'X'XYY&&'7';';<Lbe'T'TUU&&'7';';<Mru'U'UVV&&'7';';<PRTRW'X'XYY&&'7';';<PRTRW'X'XYY&&'7';';<XZ\Z_'`'`aa!.
 ! !$ 9Y% 	* ."55 lAAAL&&~'9'9*be'L'LMM&&~'9'9*be'L'LMM&&~'9'9/25'Q'QRR
 &&~'9'9-'O'OPP&&~'9'9-'O'OPP&&~'9'9:Lbe'T'TUU!,
   9Y 	 "!b!b-QS9T9T!b!b!bbb #'&l&ldhhGY[]>^>^&l&l&l"l"lr%   c                    |                     dd          |                     dd          |                     dd          d| _        |                     di           }|                     di           }t          j        dg d	| j        |                     |                     d
t          j                            |                     |                     dt          j                            |                     |                     dt          j                            gd|                     |                     d
t          j                            |                     |                     dt          j                            |                     |                     dt          j                            gi                              d          | _        dS )zl
        Parses the fund profile from the data.

        Args:
            data: The data to parse.
        categoryNameNfamily	legalType)r   r   r   feesExpensesInvestmentfeesExpensesInvestmentCat
Attributes)zAnnual Report Expense RatiozAnnual Holdings TurnoverzTotal Net AssetsannualReportExpenseRatioannualHoldingsTurnovertotalNetAssetsr|   )	rb   r   r   r   r   rd   r   r   r   )r!   r   r   _fund_operations_cats       r#   rV   zFundsData._parse_fund_profile3  s    !HH^T:: HHXt44 HH[$77
 
  88$<bAA#xx(CRHH "iiiL&&'7';';<VXZX]'^'^__&&'7';';<TVXV['\'\]]&&'7';';<Lbe'T'TUU
 &&';'?'?@Z\^\a'b'bcc&&';'?'?@XZ\Z_'`'`aa&&';'?'?@PRTRW'X'XYY!.
 ! ! 9\"" 	r%   )r&   Nr   )__name__
__module____qualname____doc__r   r   strr$   r*   propertyr,   r   r   r.   r   r   r1   floatr4   r6   r9   r;   r=   r?   rP   r(   staticmethodrd   rT   rU   rV   r   r%   r#   r   r      s|         9C ' 'V 'S ' ' ' '<	 C 	  	  	  	  	!S 	! 	! 	! X	! 	#tC#$67 	# 	# 	# X	# 	% 	% 	% 	% X	% 	#tCJ/ 	# 	# 	# X	# 	"bl 	" 	" 	" X	" 	% 	% 	% 	% X	% 	#r| 	# 	# 	# X	# 	"d3:. 	" 	" 	" X	" 	'4E	? 	' 	' 	' X	'
 
 
* * * *0 ( ( ( \( @ @ @ @Jm Jm Jm JmX# # # # #r%   r   )pandasr   typingr   r   r   yfinancer   yfinance.constr   r   yfinance.datar   yfinance.exceptionsr	   rM   r   r   r%   r#   <module>r      s        ! ! ! ! ! ! ! !        1 1 1 1 1 1 1 1             / / / / / /#??? C# C# C# C# C# C# C# C# C# C#r%   