o
    Zh:                     @   sX   d Z ddlmZmZmZmZ ddlmZmZ ddl	m
Z
mZmZmZ G dd de
ZdS )z&Util that calls Google Scholar Search.    )AnyDictOptionalcast)convert_to_secret_strget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrmodel_validatorc                   @   sj   e Zd ZU dZdZeed< dZee	 ed< e
ddZedded	ed
efddZded
efddZdS )GoogleTrendsAPIWrapperaa  Wrapper for SerpApi's Google Scholar API

    You can create SerpApi.com key by signing up at: https://serpapi.com/users/sign_up.

    The wrapper uses the SerpApi.com python package:
    https://serpapi.com/integrations/python

    To use, you should have the environment variable ``SERPAPI_API_KEY``
    set with your API key, or pass `serp_api_key` as a named parameter
    to the constructor.

     Example:
        .. code-block:: python

        from langchain_community.utilities import GoogleTrendsAPIWrapper
        google_trends = GoogleTrendsAPIWrapper()
        google_trends.run('langchain')
    Nserp_search_engineserp_api_keyZforbid)extrabefore)modevaluesreturnc                 C   sL   t t|dd|d< zddlm} W n ty   tdw |}||d< |S )z?Validate that api key and python package exists in environment.r   ZSERPAPI_API_KEYr   )SerpApiClientzigoogle-search-results is not installed. Please install it with `pip install google-search-results>=2.4.2`r   )r   r   Zserpapir   ImportError)clsr   r   r    r   b/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/utilities/google_trends.pyvalidate_environment$   s   
z+GoogleTrendsAPIWrapper.validate_environmentqueryc                 C   s  t t| j}d| |d}g }| |}| }d|v r#|d d nd}|s)dS |d d  }|d	 d  }d
d |D }	t|	}
t|	}t	|	t
|	 }|	d	 |	d  |	d dkra|	d nd |	d dkrkdnd }d| d|d}i }| |}| di }g }g }dd |dg D }dd |dg D }d| d|d  d|d  d|d	  d|d  d|d  d|d	  d|
 d| d| dt|d  d dd!d |	D  d"d| d#d| g}d$|S )%z,Run query through Google Trends with SerpapiZgoogle_trends)engineapi_keyqZinterest_over_timeZtimeline_dataNzNo good Trend Result was foundr   datec                 S   s    g | ]}| d d  dqS )r   r   Zextracted_valueget.0resultsr   r   r   
<listcomp>Q   s    z.GoogleTrendsAPIWrapper.run.<locals>.<listcomp>   d   ZRELATED_QUERIES)r   r   Z	data_typer   Zrelated_queriesc                 S      g | ]}| d qS r   r    r"   r   r   r   r%   j       risingc                 S   r(   r)   r    r"   r   r   r   r%   k   r*   topzQuery: z
Date From:  z, z

Date To:    z
Min Value: z
Max Value: z
Average Value: z
Percent Change: %z
Trend values: c                 S   s   g | ]}t |qS r   )str)r#   xr   r   r   r%   u   s    z
Rising Related Queries: z
Top Related Queries: z

)r   r
   r   Zget_secret_valuer   get_dictsplitminmaxsumlenr!   r0   join)selfr   Zserpapi_api_keyparamstotal_resultsclientZclient_dictZ
start_dateend_dater   Z	min_valueZ	max_valueZ	avg_valueZpercentage_changeZtotal_results2r+   r,   docr   r   r   run:   s   


	
zGoogleTrendsAPIWrapper.run)__name__
__module____qualname____doc__r   r   __annotations__r   r   r
   r	   Zmodel_configr   classmethodr   r   r0   r?   r   r   r   r   r   	   s   
 r   N)rC   typingr   r   r   r   Zlangchain_core.utilsr   r   Zpydanticr   r	   r
   r   r   r   r   r   r   <module>   s
    