o
    4c2$                     @   s   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
 d
eZG dd deZdS )    N)partial)
quote_plus	urlencode)GeocoderAuthenticationFailureGeocoderQueryErrorGeocoderQuotaExceededGeocoderServiceError)DEFAULT_SENTINELGeocoder)Location)logger)BaiduBaiduV3c                       s   e Zd ZdZdZdZdeededdd fdd
Zdd Zd	ed
ddZ	d	ed
ddZ
dddZdddZdd Zdd Z  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          	         sL   t  j||||||d || _d| j| jf | _d| j| jf | _|| _dS )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%s://api.map.baidu.com%sN)	super__init__api_keyr   api_pathapireverse_pathreverse_apir   )	selfr   r   r   r   r   r   r   r   	__class__ K/var/www/html/gps/gps/lib/python3.10/site-packages/geopy/geocoders/baidu.pyr       s   +
zBaidu.__init__c                 C   s   d dd | D S )zL
        Format the components dict to something Baidu understands.
        |c                 s   s    | ]}d  |V  qdS ):N)join.0itemr    r    r!   	<genexpr>]   s    z1Baidu._format_components_param.<locals>.<genexpr>)r$   items)r   
componentsr    r    r!   _format_components_paramX   s   zBaidu._format_components_paramT)exactly_oner   c                C   sP   | j d|d}| | j| j|}td| jj| t| j	|d}| j
|||d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callbackr    r    r!   geocode`   s   zBaidu.geocodec                C   sV   | j d| |d}| | j| j|}td| jj| t	| j
|d}| j|||d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``.

        r-   )r.   r/   locationz%s.reverse: %sr1   r2   )r   _coerce_point_to_stringr3   r   r   r   r4   r   r5   r   _parse_reverse_jsonr7   r8   r    r    r!   reverse   s   zBaidu.reversec                 C   sh   | d}|s| | d dS | dd}|d d }|d d }t|||f|}|r1|S |gS )	zJ
        Parses a location from a single-result reverse API call.
        resultstatusNformatted_addressutf-8r>   latlng)get_check_statusencoder   )r   pager,   placer>   latitude	longituder    r    r!   r@      s   
zBaidu._parse_reverse_jsonc                    sH   | d}|s| | d dS dd  |r |S  fdd|D S )zI
        Returns location, (latitude, longitude) from JSON feed.
        rB   rC   Nc                 S   s2   |  d}| d d }| d d }t|||f| S )zN
            Get the location, lat, lng from a single JSON place.
            levelr>   rF   rG   )rH   r   )rL   r>   rM   rN   r    r    r!   parse_place   s   
z&Baidu._parse_json.<locals>.parse_placec                    s   g | ]} |qS r    r    r%   rP   r    r!   
<listcomp>   s    z%Baidu._parse_json.<locals>.<listcomp>)rH   rI   )r   rK   r,   rL   r    rQ   r!   r6      s   
	zBaidu._parse_jsonc                 C   s   |dkrdS |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r6td|dkr>td|dkrFtd|dkrNtdd|  kr[dk r\td d|  kridk rjtd td| )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   rC   r    r    r!   rI      sb   zBaidu._check_statusc                 C   sT   t |}| jd u rd||f S d||| jf }tt|d }d|||f S )Nz%s?%sz%s?%s%srE   z%s?%s&sn=%s)r   r   hashlibmd5r   rJ   	hexdigest)r   r;   pathr:   query_stringrawsnr    r    r!   r3     s   
zBaidu._construct_url)T)r5   
__module____qualname____doc__r   r   r	   r   r+   r=   rA   r@   r6   rI   r3   __classcell__r    r    r   r!   r      s*    
8$
!
6r   c                   @   s   e Zd ZdZdZdZdS )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)r5   rc   rd   re   r   r   r    r    r    r!   r     s    r   )r\   	functoolsr   urllib.parser   r   	geopy.excr   r   r   r   geopy.geocoders.baser	   r
   geopy.locationr   
geopy.utilr   __all__r   r   r    r    r    r!   <module>   s      