o
    sg                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 e  ejd Ze ZG dd dZedkrOe Zeed	d
Zee dS dS )+Class to search google using the Serper API    N)load_dotenv)
parsed_urlGOOGLE_SEARCH_APIc                   @   sd   e Zd ZdZddddZdd Z				
ddee eB fddZ				
dddZ	dee fddZ
d	S )GoogleSearchr   organicimagesnewssearchr   r	   c                 C   s   dS )r   N )selfr   r   //var/www/html/XCapMarket/utils/search_google.py__init__   s   zGoogleSearch.__init__FN
   domain_name_to_checkc              
      s  t |tr	|g}|dvrtd|dkr| d}d}d| }tdd}||d	}t 4 I d
H 7}	|	j||t|d4 I d
H }
|
 I d
H }W d
  I d
H  n1 I d
H s[w   Y  W d
  I d
H  n1 I d
H spw   Y  || j	|  }|r| 
||S |S )zGet the search results from google
        query is the search query
        type_of_search is 'search' or 'images'  or 'news' for web search or image search or news search
        r
   z5type_of_search must be 'search' or 'images' or 'news'r	   z( (financial, product, company, industry)   https://google.serper.dev/application/jsonz	X-API-KEYzContent-TypeqnumN)headersdata)
isinstancestr
ValueErrorr   aiohttpClientSessionpostjsondumpstype_of_search_outputcheck_search_results_for_domain)r   querytype_of_searchdomain_checkr   num_of_resultsgoogle_search_urlr   r   sessionresponseresultsr   r   r   search_google   s.   




*(zGoogleSearch.search_googlec              
   C   s   |dvrt dd| }tdd}||d}z(tj||t|dd}	|	 | j|  }
|r6| |
|W S |	 | j|  W S  ttj	j
fy` } ztjd	||d
d g W  Y d}~S d}~ww )zGet the search results from google
        query is the search query
        type_of_search is 'search' or 'images' for web search or image search
        )r   r   z+type_of_search must be 'search' or 'images'r   r   r   r   r   )r   r   timeoutzError with %s: %sT)exc_infoN)r   r   requestsr    r!   r"   r#   r$   TimeoutError
exceptionsConnectionError
XCM_loggererror)r   r%   r&   r'   r   r(   r)   r   r   r+   r,   er   r   r   _search_googleD   s.   


zGoogleSearch._search_googlec                 C   s4   g }|D ]}t |d d}|j|v r|| q|S )z,Check the search results for the domain namelinkF)r   domainappend)r   r,   r   new_resultsresult
result_urlr   r   r   r$   l   s   

z,GoogleSearch.check_search_results_for_domain)FNr   )__name__
__module____qualname____doc__r#   r   listr   r-   r7   r$   r   r   r   r   r      s    

+
(r   __main__	Stairworxr	   )rA   asyncior!   loggingosr   r0   dotenvr   utils.url_parserr   environr   	getLoggerr4   r   r>   r   runr-   r,   printr   r   r   r   <module>   s$    
c