
    g                         d dl mZ d dlmZ d dlZddlmZmZmZ ddl	m	Z	  G d de      Z
 G d	 d
      Z G d de      Zy)    )PrettyPrinter)UserDictN   )APIKeyNotProvided	HTTPErrorHTTPConnectionError)__version__c                   ^     e Zd ZdZ fdZd Zed        Zd ZddZ	e
ddd	d
       Z xZS )SerpResultszKA dictionary-like object that represents the results of a SERP API request.c                N    t         |   |       || _        d | _        d | _        y N)super__init__clientrequestresponse)selfdatar   	__class__s      K/var/www/html/Manimaran/venv/lib/python3.12/site-packages/serpapi/client.pyr   zSerpResults.__init__   s%        c                 V    t        ddd      }|j                  | j                         S )N   TO   )indentcompactwidth)r   pprintr   )r   pps     r   __repr__zSerpResults.__repr__   s&    !T<))DII&'(r   c                 `    | j                   j                  d      }|r|j                  d      S y)z,The URL of the next page of results, if any.serpapi_pagination	next_linkN)r   get)r   r"   s     r   next_page_urlzSerpResults.next_page_url   s1     "YY]]+?@%))+66 r   c                     | j                   rad| j                  j                  i}| j                  j                  d| j                   |      }t        j                  || j                        S y)z(Return the next page of results, if any.api_keyGET)pathparamsr   N)r%   r   r'   r   r   from_http_response)r   r*   rs      r   	next_pagezSerpResults.next_page    sc       !4!45F##E0B0B6#RA11!DKK1HH r   c              #      K   d}| }|j                   r3||k  r-|j                         }|dz  }| |j                   r||k  r+yyyyw)z'Yield the next page of results, if any.r   r   N)r%   r.   )r   	max_pagescurrent_page_countcurrent_pages       r   yield_pageszSerpResults.yield_pages+   s_      ((-?)-K'113L!# ((-?)-K(-K(s   A AATN)
assert_200r   c                    |r	 |j                           | |j	                         |      } |j
                  | _        || _        | S # t        j                  j                  $ r}t        |      d}~ww xY w)zConstruct a SerpResults object from an HTTP response.

        Optionally (but default behavior), will raise an exception if the status code is not 200.
        This functionality can be disabled by setting `assert_200=False`.
        Nr+   )raise_for_statusrequests
exceptionsr   jsonr   r   )clsr-   r4   r   es        r   r,   zSerpResults.from_http_response6   sk     #""$ !&&(6*ii
 &&00 #l"#s   A A3#A..A3)i  )__name__
__module____qualname____doc__r   r    propertyr%   r.   r3   classmethodr,   __classcell__)r   s   @r   r   r   
   sH    U) 7 7	I	 15d  r   r   c                   L    e Zd ZdZdZdZde ZdddZe	d        Z
d	d
ddZy)SerpAPIHTTPz7A class that handles the HTTP requests to the SERP API.zhttps://serpapi.comzhttps://serpapi.com/dashboardzSerpApi Python Client, vN)r'   c                D    || _         t        j                         | _        y r   )r'   r7   Sessionsession)r   r'   s     r   r   zSerpAPIHTTP.__init__S   s     '')r   c                 >    |d   t        d| j                         y )Nr'   z'Please provide an API key, found here: )r   DASHBOARD_URL)r   r*   s     r   assert_api_keyzSerpAPIHTTP.assert_api_keyY   s0    )$#94;M;M9OP  %r   TF)rJ   r4   c                   d|vr| j                   |d<   |r| j                  | |       |j                  d      s| j                  |z   }n|}	 d| j                  i} | j
                  j                  d||||d|}	|r	 |	j                          |	S |	S # t        j                  j                  $ r}
t        |
      d }
~
ww xY w# t        j                  j                  $ r}
t        |
      d }
~
ww xY w)Nr'   httpz
User-Agent)methodurlr*   headers )r'   rJ   
startswithBASE_DOMAIN
USER_AGENTrG   r   r7   r8   ConnectionErrorr   r6   r   )r   rM   r)   r*   rJ   r4   kwargsrN   rO   r-   r;   s              r   r   zSerpAPIHTTP.request`   s    F" $F9 f- v&""T)CC	)#T__5G$$$ 3vwJPA #""$ q ""22 	)%a((	) &&00 #l"#s0   /B >C B?/B::B?C/C**C/)r<   r=   r>   r?   rR   rI   r	   rS   r   staticmethodrJ   r   rP   r   r   rD   rD   L   sC    A'K3M+K=9J"& *   7;u#r   rD   c                   (    e Zd ZdZd Zd Zd Zd Zy)SerpAPIz2A class that handles the HTTP requests to SerpAPI.c                 X     | j                   dd|i|}t        j                  ||       S )Nr*   r+   )r$   z/searchr   r   r,   r   r*   extrasr-   s       r   searchzSerpAPI.search   s0    DLLC&CFC--a-==r   c                      | j                   |fi |}|j                  di       j                  d      } | j                  d|fdi i|}|j                  S )Nsearch_metadataraw_html_filer(   r*   )r]   r$   r   text)r   r*   r\   r]   html_urlr-   s         r   search_htmlzSerpAPI.search_html   sX    V.v.::/488IDLL>>v>vvr   c                 X     | j                   dd|i|}t        j                  ||       S )Nr*   r+   )r(   z	/searchesrZ   r[   s       r   search_archivezSerpAPI.search_archive   s0    DLLEFEfE--a-==r   c                 X     | j                   dd|i|}t        j                  ||       S )Nr*   r+   )r(   z/locations.jsonrZ   r[   s       r   locationzSerpAPI.location   s0    DLLK&KFK--a-==r   N)r<   r=   r>   r?   r]   rc   re   rg   rP   r   r   rX   rX      s    <>>>r   rX   )r   r   collectionsr   r7   r8   r   r   r   r	   r   rD   rX   rP   r   r   <module>ri      s;         I I $?( ?D7 7t>k >r   