o
    sgJ                     @   s   d Z ddlZddlmZ ejd ddlZddlZddl	Z
ddlZddlmZ e  G dd dZedkrYeg d	ZddlZej D ]\ZZee eejed
 dd qEdS dS )z!Public comparable data for stocks    N)Literal.)load_dotenvc                   @   s   e Zd ZdZedZddee ddfddZ	defd	d
Z
dd Zdd Zded fddZdd Zdd Zdd Zdd ZdS )PublicComparablesz(public comparable data for stock tickersAlpha_VantageNpublic_comparablesreturnc                 C   s   |d u rg }i | _ |D ]<}| |}|std| d|i| j |< | || j | d< | || j | d< | || j | d || j | d< qd S )NzInvalid ticker: overview_dataisbs
comp_table)dataget_ticker_data
ValueErrorget_income_statementget_balance_sheetcreate_comp_table)selfr   tickerr    r   A/var/www/html/XCapMarket/services/stock_info/public_comparable.py__init__   s   

zPublicComparables.__init__r   c                 C   s@   d| d| j  }t|}| }t|dksd|v rdS |S )z?Gets the ticker data if the ticker is valid, else returns Falsez;https://www.alphavantage.co/query?function=OVERVIEW&symbol=&apikey=r   zError MessageF)alpha_vantage_api_keyrequestsgetjsonlenr   r   urlresponser   r   r   r   r   -   s   
z!PublicComparables.get_ticker_datac                 C   s@  t j j}|d | || t|d d| | | j| d |d dd| t|d d|| t|d d	 d
 d|d d|d did| t| 	|d dd|| t|d d|d d|d didd|t
|d |d d|d didd|t
|d |d d|d didd|t
|d |d d|d did	}|S )z3Create the necessary items for the comparable tableNameMarketCapitalizationbillionr   LTM
RevenueTTMmillionannualReportsr   totalRevenue   N   quarterlyReportsebitdaEBITDAEVToRevenue
EVToEBITDAPERatio)	r!   stock_pricezMkt CapEVRevenuer-   zEV/Revz	EV/EBITDAzP/E)pd	Timestampnowyearget_stock_pricenormalize_orders_of_magintcalculate_enterprise_valuer   calculate_ttm_datafloat)r   dincome_statementr   r7   r   r   r   r   r   ;   s`   	4z#PublicComparables.create_comp_tablec                 C   s@   d| d| j  }t|}| }t|d  d d }|S )z"get the stock price for the tickerzDhttps://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=r   zTime Series (Daily)r   z4. close)r   r   r   r   listvalues)r   r   r   r    r   close_pricer   r   r   r8   x   s
   
z!PublicComparables.get_stock_priceorder_of_mag)r#   r&   thousandc                 C   sF   |dkr|d ddS |dkr|d ddS |dkr!|d	 d
dS dS )znNormalize the orders of magnitude
        return a dict with normalized value as a float and the unit
        r#   i ʚ;B)valueunitr&   i@B MrD   i  KNr   )r   rF   rC   r   r   r   r9      s   z)PublicComparables.normalize_orders_of_magc                 C   (   d| d| j  }t|}| }|S )z'get the income statement for the tickerzChttps://www.alphavantage.co/query?function=INCOME_STATEMENT&symbol=r   r   r   r   r   r   r   r   r   r         
z&PublicComparables.get_income_statementc                 C   rJ   )z$get the balance sheet for the tickerz@https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=r   rK   r   r   r   r   r      rL   z#PublicComparables.get_balance_sheetc                 C   s   |d d d |d d d kr|d d }n|d d }t |d t |d  }t |d t |d d	kr9|d nd }t || | }|S )
zcalculate the enterprise valuer'   r   fiscalDateEndingr+   shortLongTermDebtTotallongTermDebtcashAndShortTermInvestmentslongTermInvestmentsNone)r:   )r   r   mkt_capbs_data
total_debt
total_cashenterprise_valuer   r   r   r;      s   z,PublicComparables.calculate_enterprise_valuec                 C   sD   t |d | t |d |  t |d |  t |d |  }|S )z)calculate the trailing twelve months datar   r)   r*      )r=   )r   r   keyttm_datar   r   r   r<      s   z$PublicComparables.calculate_ttm_data)N)__name__
__module____qualname____doc__osgetenvr   r@   strr   r   r   r8   r   r9   r   r   r;   r<   r   r   r   r   r      s    
=
r   __main__)AAPLGOOGLMSFTTSLAr   r*   )indent)r^   systypingr   pathappendr_   numpynppandasr4   r   dotenvr   r   r[   r   r   r   itemsr   printdumpsr   r   r   r   <module>   s(     *