
    Wg%.                     j    d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZ e dZ G d d	      Zy)
    N)YfData)
_BASE_URL_)YFDataException)utils)DictOptionalz/v10/finance/quoteSummary/c                      e Zd ZdZd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y)	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
    Ndatasymbolc                     || _         || _        || _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        y)z
        Args:
            data (YfData): The YfData object for fetching data.
            symbol (str): The symbol of the fund.
            proxy (optional): Proxy settings for fetching data.
        N)_data_symbolproxy_quote_type_description_fund_overview_fund_operations_asset_classes_top_holdings_equity_holdings_bond_holdings_bond_ratings_sector_weightings)selfr   r   r   s       J/var/www/html/venv/lib/python3.12/site-packages/yfinance/scrapers/funds.py__init__zFundsData.__init__   sq     

   ! # $ #! $"!"&    returnc                 R    | j                   | 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_type1   s(     #!!#r   c                 R    | j                   | 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                 R    | j                   | 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_overviewH   (     &!!#"""r   c                 R    | j                   | j                          | j                   S )zo
        Returns the fund operations.

        Returns:
            pd.DataFrame: The fund operations.
        )r   r!   r"   s    r   fund_operationszFundsData.fund_operationsT   (       (!!#$$$r   c                 R    | j                   | 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_classes`   r(   r   c                 R    | j                   | 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_holdingsl   (     %!!#!!!r   c                 R    | j                   | 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_holdingsx   r+   r   c                 R    | j                   | 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                 R    | j                   | 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   r0   r   c                 R    | j                   | 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j                  g d      }|d| j                  dd}| j                  j                  t        | j                  z   | j                  j
                  ||      }|S )z
        Fetches the raw JSON data from the API.

        Args:
            proxy: Proxy settings for fetching data.

        Returns:
            dict: The raw JSON data.
        ,)	quoteTypesummaryProfiletopHoldingsfundProfilezfinance.yahoo.comfalse)modules
corsDomainr   	formatted)user_agent_headersparamsr   )joinr   r   get_raw_json_QUOTE_SUMMARY_URL_rC   )r   r   r@   params_dictresults        r   _fetchzFundsData._fetch   sp     ((XY")9LX\XdXdsz{(()<T\\)I^b^h^h^{^{  EP  X](  ^r   c                 .   | j                  | j                        }	 |d   d   d   }|d   d   | _        | j                  |d          | j	                  |d          | j                  |d          y# t        $ r t        d      t        $ r}t        j                         }|j                  d	| j                   d
|        |j                  d       |j                  d       |j                  d        |j                  d       Y d}~yd}~ww xY w)z;
        Fetches and parses the data from the API.
        quoteSummaryrI   r   r;   r<   r=   r>   zNo Fund data found.zFailed to get fund data for 'z
' reason: zGot response: z------------- N)rJ   r   r   _parse_description_parse_top_holdings_parse_fund_profileKeyErrorr   	Exceptionr   get_yf_loggererrorr   debug)r   rI   r   eloggers        r   r!   zFundsData._fetch_and_parse   s	    TZZ(	*.)(3A6D#K0=D ##D)9$:;$$T-%89$$T-%89 	9!"788 	*((*FLL8jQRPSTULL)*LL)LL1TF$LL))	*s   AA2 2DA<DDc                 J    t        | t              s| S | j                  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                 2    |j                  dd      | _        y)zk
        Parses the description from the data.

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

        Args:
            data: The data to parse.
        cashPositionNstockPositionbondPositionpreferredPositionconvertiblePositionotherPosition)rc   rd   re   rf   rg   rh   holdingsr   holdingNameholdingPercent)SymbolNamezHolding Percentrl   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   8   K   | ]  }|D ]  }|||   f   y wN .0dkeys      r   	<genexpr>z0FundsData._parse_top_holdings.<locals>.<genexpr>0  s&     !bA`a!bY\3#-!b-!b   bondRatingsc              3   8   K   | ]  }|D ]  }|||   f   y wr   r   r   s      r   r   z0FundsData._parse_top_holdings.<locals>.<genexpr>3  s&     &ljk&lcfQsV}&l}&lr   sectorWeightings)r^   r\   r   appendpd	DataFrame	set_indexr   r   NAr   r   r[   r   r   )	r   r   	_holdingsr   _name_holding_percentitemr   r   s	            r   rO   zFundsData._parse_top_holdings   s~    !22488ND3QR!33DHH_d4ST 22488ND3QR!%!7!7ATVZ8[!\#'#9#9$((CXZ^:_#`!33DHH_d4ST
 HHZ,	+-r2( 	<DNN4>*LLm,-##D)9$:;	<
  \\/+
  9X	 	  88$4b9 "  HLL&&'7';';<Mruu'UV&&'7';';M255'QR&&'7';';NBEE'RS&&'7';';<Mruu'UV&&'7';';<Mruu'UV&&'7';';<UWYW\W\']^ &&'7';';<PRTRWRW'XY&&'7';';<Lbee'TU&&'7';';<Mruu'UV&&'7';';<PRTRWRW'XY&&'7';';<PRTRWRW'XY&&'7';';<XZ\Z_Z_'`a!.
 !$ 9Y% 	* ."5 llALL&&~'9'9*bee'LM&&~'9'9*bee'LM&&~'9'9/255'QR
 &&~'9'9-'OP&&~'9'9-'OP&&~'9'9:Lbee'TU!,
  9Y 	 "!b-QS9T!bb #'&ldhhGY[]>^&l"lr   c                 `   |j                  dd      |j                  dd      |j                  dd      d| _        |j                  di       }|j                  di       }t        j                  dg d	| j                  | j                  |j                  d
t        j                              | j                  |j                  dt        j                              | j                  |j                  dt        j                              gd| j                  |j                  d
t        j                              | j                  |j                  dt        j                              | j                  |j                  dt        j                              gi      j                  d      | _        y)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totalNetAssetsrv   )	r\   r   r   r   r   r^   r   r   r   )r   r   r   _fund_operations_cats       r   rP   zFundsData._parse_fund_profile5  st    !HH^T: HHXt4 HH[$7
  88$<bA#xx(CRH "iLL&&'7';';<VXZX]X]'^_&&'7';';<TVXV[V['\]&&'7';';<Lbee'TU
 &&';'?'?@Z\^\a\a'bc&&';'?'?@XZ\Z_Z_'`a&&';'?'?@PRTRWRW'XY!.
 ! 9\" 	r   r   )r   N)__name__
__module____qualname____doc__r   strr   r#   propertyr%   r   r   r'   r   r   r*   floatr-   r/   r2   r4   r6   r8   rJ   r!   staticmethodr^   rN   rO   rP   r   r   r   r
   r
      s   'V 'S ':	 C 	  	!S 	! 	! 	#tC#$67 	# 	# 	% 	% 	% 	#tCJ/ 	# 	# 	"bll 	" 	" 	% 	% 	% 	#r|| 	# 	# 	"d3:. 	" 	" 	'4E	? 	' 	'*0 ( ( @JmX#r   r
   )pandasr   yfinance.datar   yfinance.constr   yfinance.exceptionsr   yfinancer   typingr   r   rG   r
   r   r   r   <module>r      s1       % /  !#$>? E# E#r   