
    2iW                     f    d dl Z d dlZd dlmZ d dlmZ dZdZdZdZ	dZ
d	Zd
ZdZdZ G d de      Zy)    N
Pagination)SerpApiClientExceptiongooglebingbaidugoogle_scholaryandexebayyahoo
home_depotyoutubec                       e Zd ZdZdZdZddZddZddZddZ	d Z
d	 Zd
 Zd Zd Zd Zd ZddZd ZddZddZy)SerpApiClienta  SerpApiClient enables to query any search engines supported by SerpApi and parse the results.
    ```python
    from serpapi import GoogleSearch
    search = SerpApiClient({
        "q": "Coffee", 
        "location": "Austin,Texas", 
        "engine": "google",
        "api_key": "<your private key>"
        })
	data = search.get_json()
    ```

    https://serpapi.com/search-api
    zhttps://serpapi.comNc                 .    || _         || _        || _        y N)params_dictenginetimeout)selfr   r   r   s       _/var/www/html/marco-python-backend/venv/lib/python3.12/site-packages/serpapi/serp_api_client.py__init__zSerpApiClient.__init__#   s    &    c                 8   d| j                   d<   | j                  r| j                  | j                   d<   | j                  r'd| j                   vr| j                  | j                   d<   d| j                   vrt        d      | j                  |z   | j                   fS )Npythonsourceserp_api_keyr   z/engine must be defined in params_dict or engine)r   SERP_API_KEYr   r   BACKENDr   paths     r   construct_urlzSerpApiClient.construct_url(   s    %-"/3/@/@D^,;;t///-1[[  *4+++()Z[[||d"D$4$444r   c                    d}	 | j                  |      \  }}t        j                  ||| j                        }|S # t        j                  $ r5}t        d|z          t        ||j                  j                         |d}~ww xY w)zFReturns:
            Response object provided by requests.get
        N)r   zfail: )r"   requestsgetr   	HTTPErrorprintresponsestatus_code)r   r!   url	parameterr(   es         r   get_responsezSerpApiClient.get_response3   sx     	!//5NC||CDLLIHO!! 	(S.!!QZZ++,G	s   7< B0A??Bc                 8    | j                  |      j                  S )z1Returns:
            Response text field
        )r-   textr    s     r   get_resultszSerpApiClient.get_resultsB   s       &+++r   c                 "    | j                         S )zAReturns:
            Raw HTML search result from Gooogle
        )r0   r   s    r   get_htmlzSerpApiClient.get_htmlH   s     !!r   c                 f    d| j                   d<   t        j                  | j                               S )zNReturns:
            Formatted JSON search results using json package
        jsonoutput)r   r5   loadsr0   r2   s    r   get_jsonzSerpApiClient.get_jsonN   s,     &,"zz$**,--r   c                 @    d| j                   d<   | j                         S )zDReturns:
            Formatted JSON search result as string
        r5   r6   )r   r0   r2   s    r   get_raw_jsonzSerpApiClient.get_raw_jsonU   s#     &,"!!r   c                 4    t        | j                               S )zFReturns:
            Dict with the formatted response content
        )dictr8   r2   s    r   get_dictionaryzSerpApiClient.get_dictionary\   s     DMMO$$r   c                 "    | j                         S )zmReturns:
            Dict with the formatted response content
            (alias for get_dictionary)
        )r=   r2   s    r   get_dictzSerpApiClient.get_dictb   s    
 ""$$r   c                 F    | j                         }| j                  d|      S )zcReturns: 
            Dynamically created python object wrapping the result data structure
        r(   )r=   
make_pyobj)r   nodes     r   
get_objectzSerpApiClient.get_objecti   s#    
 ""$z400r   c           	      ^   t        |t        fi       } |       }t        |t              rAt	        ||g        |D ]-  }t        ||      j                  | j                  ||             / |S t        |t              r|j                         D ]  \  }}t        |t              r@t	        ||g        |D ]-  }t        ||      j                  | j                  ||             / Vt        |t              rt	        ||| j                  ||             t	        |||        |S t	        |||       |S r   )
typeobject
isinstancelistsetattrgetattrappendrA   r<   items)r   namerB   pytypepyobjelchilds          r   rA   zSerpApiClient.make_pyobjr   s   dVJ+dD!E4$ Gt$++DOOD",EFGLd##zz| 0eeT*E4,# Ot,33DOOD"4MNOt,E4u)EFE4/0  E4&r   c                 |    | j                  dj                  ||            }|dk(  rt        j                  |      }|S )a3  Retrieve search result from the Search Archive API
        Parameters:
            search_id (int): unique identifier for the search provided by metadata.id 
            format (string): search format: json or html [optional]
        Returns:
            dict|string: search result from the archive
        z/searches/{0}.{1}r5   )r0   formatr5   r7   )r   	search_idrS   results       r   get_search_archivez SerpApiClient.get_search_archive   s=     !!"5"<"<Y"OPVZZ'Fr   c                 J    t        j                  | j                  d            S )ziGet account information using Account API
        Returns:
            dict: account information
        z/account)r5   r7   r0   r2   s    r   get_accountzSerpApiClient.get_account   s    
 zz$**:677r   c                     i | _         d| j                   d<   || j                   d<   || j                   d<   | j                  d      }t        j                  |      S )zGet location using Location API
        Parameters:
            q (string): location (like: city name..)
            limit (int): number of matches returned
        Returns:
            dict: Location matching q
        r5   r6   qlimitz/locations.json)r   r0   r5   r7   )r   rZ   r[   buffers       r   get_locationzSerpApiClient.get_location   s\     %+" !$)!!!"34zz&!!r   c                     t        | |||      S )zOReturn:
            Generator to iterate the search results pagination
        r   )r   startend	page_sizes       r   
paginationzSerpApiClient.pagination   s     $sI66r   )Ni`  )z/search)r5   )   )r   i ʚ;
   )__name__
__module____qualname____doc__r   r   r   r"   r-   r0   r3   r8   r:   r=   r?   rC   rA   rV   rX   r]   rb    r   r   r   r      s^     $GL
	5,"."%%108"7r   r   )r$   r5   serpapi.paginationr   !serpapi.serp_api_client_exceptionr   GOOGLE_ENGINEBING_ENGINEBAIDU_ENGINEGOOGLE_SCHOLAR_ENGINEYANDEX_ENGINEEBAY_ENGINEYAHOO_ENGINEHOME_DEPOT_ENGINEYOUTUBE_ENGINErF   r   ri   r   r   <module>ru      sN      ) D(   a7F a7r   