o
    ÔèFh2$  ã                   @   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__© úN/var/www/html/loop/nvenv/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 AKéf   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   z“Geocoder 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      