
    g2$                         d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZ dZ G d	 d
e      Z G d de      Zy)    N)partial)
quote_plus	urlencode)GeocoderAuthenticationFailureGeocoderQueryErrorGeocoderQuotaExceededGeocoderServiceError)DEFAULT_SENTINELGeocoder)Location)logger)BaiduBaiduV3c                   z     e Zd ZdZdZdZdeededdd fd
Zd Zdedd	Z	dedd
Z
ddZddZd Zd Z xZS )r   a  Geocoder using the Baidu Maps v2 API.

    Documentation at:
        http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding

    .. attention::
        Newly registered API keys will not work with v2 API,
        use :class:`.BaiduV3` instead.
    z/geocoder/v2/N)schemetimeoutproxies
user_agentssl_contextadapter_factorysecurity_keyc                    t         	|   ||||||       || _        | j                  d| j                  | _        | j                  d| j                  | _        || _        y)ag  

        :param str api_key: The API key (AK) required by Baidu Map to perform
            geocoding requests. API keys are managed through the Baidu APIs
            console (http://lbsyun.baidu.com/apiconsole/key).

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.

        :param int timeout:
            See :attr:`geopy.geocoders.options.default_timeout`.

        :param dict proxies:
            See :attr:`geopy.geocoders.options.default_proxies`.

        :param str user_agent:
            See :attr:`geopy.geocoders.options.default_user_agent`.

        :type ssl_context: :class:`ssl.SSLContext`
        :param ssl_context:
            See :attr:`geopy.geocoders.options.default_ssl_context`.

        :param callable adapter_factory:
            See :attr:`geopy.geocoders.options.default_adapter_factory`.

            .. versionadded:: 2.0

        :param str security_key: The security key (SK) to calculate
            the SN parameter in request if authentication setting requires
            (http://lbsyun.baidu.com/index.php?title=lbscloud/api/appendix).
        )r   r   r   r   r   r   z://api.map.baidu.comN)	super__init__api_keyr   api_pathapireverse_pathreverse_apir   )
selfr   r   r   r   r   r   r   r   	__class__s
            U/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/geopy/geocoders/baidu.pyr   zBaidu.__init__    sf    V 	!#+ 	 	
 15dmmL9=dFWFWX(    c                 N    dj                  d |j                         D              S )zL
        Format the components dict to something Baidu understands.
        |c              3   >   K   | ]  }d j                  |        yw):N)join).0items     r"   	<genexpr>z1Baidu._format_components_param.<locals>.<genexpr>]   s     ;(:SXXd^(:s   )r(   items)r    
componentss     r"   _format_components_paramzBaidu._format_components_paramX   s&     xx;
(8(8(:;
 	
r#   T)exactly_oner   c                   | j                   d|d}| j                  | j                  | j                  |      }t	        j
                  d| j                  j                  |       t        | j                  |      }| j                  |||      S )a  
        Return a location point by address.

        :param str query: The address or query you wish to geocode.

        :param bool exactly_one: Return one result or a list of results, if
            available.

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
            exception. Set this only if you wish to override, on this call
            only, the value set during the geocoder's initialization.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.

        json)akoutputaddressz%s.geocode: %sr/   r   )r   _construct_urlr   r   r   debugr!   __name__r   _parse_json_call_geocoderr    queryr/   r   paramsurlcallbacks          r"   geocodezBaidu.geocode`   s{    2 ,,
 !!$((DMM6B%t~~'>'>D4++E""3'"BBr#   c                :   | j                   d| j                  |      d}| j                  | j                  | j                  |      }t        j                  d| j                  j                  |       t        | j                  |      }| j                  |||      S )a  
        Return an address by location point.

        :param query: The coordinates for which you wish to obtain the
            closest human-readable addresses.
        :type query: :class:`geopy.point.Point`, list or tuple of ``(latitude,
            longitude)``, or string as ``"%(latitude)s, %(longitude)s"``.

        :param bool exactly_one: Return one result or a list of results, if
            available. Baidu's API will always return at most one result.

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
            exception. Set this only if you wish to override, on this call
            only, the value set during the geocoder's initialization.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.

        r1   )r2   r3   locationz%s.reverse: %sr5   r6   )r   _coerce_point_to_stringr7   r   r   r   r8   r!   r9   r   _parse_reverse_jsonr;   r<   s          r"   reversezBaidu.reverse   s    , ,,44U;
 !!$"2"2D4E4EvN%t~~'>'>D433M""3'"BBr#   c                     |j                  d      }|s!| j                  |j                  d             y|j                  d      j                  d      }|d   d   }|d   d   }t        |||f|      }|r|S |gS )	zJ
        Parses a location from a single-result reverse API call.
        resultstatusNformatted_addressutf-8rC   latlng)get_check_statusencoder   )r    pager/   placerC   latitude	longitudes          r"   rE   zBaidu._parse_reverse_json   s     "txx12990188A$U+*%e,	Hx&;UCO:r#   c                     |j                  d      }|s!| j                  |j                  d             yd }|r ||      S |D cg c]
  } ||       c}S c c}w )zI
        Returns location, (latitude, longitude) from JSON feed.
        rH   rI   Nc                 b    | j                  d      }| d   d   }| d   d   }t        |||f|       S )zN
            Get the location, lat, lng from a single JSON place.
            levelrC   rL   rM   )rN   r   )rR   rC   rS   rT   s       r"   parse_placez&Baidu._parse_json.<locals>.parse_place   sD     yy)HZ(/Hj)%0IHx&;UCCr#   )rN   rO   )r    rQ   r/   rR   rX   r*   s         r"   r:   zBaidu._parse_json   sb    
 "txx12	D u%%278%$K%%888s   Ac                    |dk(  ry|dk(  rt        d      |dk(  rt        d      |dk(  rt        d      |d	k(  rt        d
      |dk(  rt        d      |dk(  rt        d      |dk(  rt        d      |dk(  rt        d      |dk(  rt        d      d|cxk  rdk  rt        d       d|cxk  rdk  rt        d       t        d|z        )z+
        Validates error statuses.
        r   N   zInternal server error.   zInvalid request.   zAuthentication failure.   zQuota validate failure.   zAK Illegal or Not Exist.e   zNo AKf   zMCODE Error   z
Invalid AK   z
Invalid SNi,  zAuthentication Failurei  zQuota Error.zUnknown error. Status: %r)r	   r   r   r   )r    rI   s     r"   rO   zBaidu._check_status   sH    Q;Q;&(  q[$"  q[/)  q[')  q[$*  s]/  s]/  s]/  s]/  F S /(  ! F S '  !
 %%@6%IJJr#   c                     t        |      }| j                  |d|S |d|| j                  }t        j                  t	        |      j                  d            j                         }|d|d|S )N?rK   z&sn=)r   r   hashlibmd5r   rP   	hexdigest)r    r?   pathr>   query_stringrawsns          r"   r7   zBaidu._construct_url  sj     ($!<00  $\43D3DECZ_33G<=GGIB$'r::r#   )T)r9   
__module____qualname____doc__r   r   r
   r   r.   rA   rF   rE   r:   rO   r7   __classcell__)r!   s   @r"   r   r      sj     H"L $$( 6)p
 $"CH -1:J CB(924Kl;r#   r   c                       e Zd ZdZdZdZy)r   zGeocoder using the Baidu Maps v3 API.

    Documentation at:
        http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
    z/geocoding/v3/z/reverse_geocoding/v3/N)r9   rl   rm   rn   r   r    r#   r"   r   r     s      H+Lr#   r   )re   	functoolsr   urllib.parser   r   	geopy.excr   r   r   r	   geopy.geocoders.baser
   r   geopy.locationr   
geopy.utilr   __all__r   r   rq   r#   r"   <module>ry      sC      .  < # 
~;H ~;B,e ,r#   