a
    }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__ Q/var/www/html/django/DPS/env/lib/python3.9/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>]       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/   r0   locationz%s.reverse: %sr2   r3   )r   Z_coerce_point_to_stringr4   r   r   r   r5   r   r6   r   _parse_reverse_jsonr8   r9   r    r    r!   reverse   s    zBaidu.reversec                 C   sl   | d}|s"| | d dS | dd}|d d }|d d }t|||f|}|rb|S |gS dS )	zJ
        Parses a location from a single-result reverse API call.
        resultstatusNZformatted_addressutf-8r?   latlng)get_check_statusencoder   )r   pager-   placer?   latitude	longituder    r    r!   r@      s    
zBaidu._parse_reverse_jsonc                    sL   | d}|s"| | d dS dd  |r6 |S  fdd|D S 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?   rE   rF   )rG   r   )rK   r?   rL   rM   r    r    r!   parse_place   s    
z&Baidu._parse_json.<locals>.parse_placec                    s   g | ]} |qS r    r    r%   rO   r    r!   
<listcomp>   r)   z%Baidu._parse_json.<locals>.<listcomp>)rG   rH   )r   rJ   r-   rK   r    rP   r!   r7      s    
	zBaidu._parse_jsonc                 C   s  |dkrdS |dkrt dn|dkr0tdn|dkrBtdn|d	krTtd
n|dkrftdn|dkrxtdn|dkrtdnt|dkrtdnb|dkrtdnPd|  krdk rn n
tdn.d|  krdk rn n
tdntd| dS )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!   rH      s^    zBaidu._check_statusc                 C   sX   t |}| jd u rd||f S d||| jf }tt|d }d|||f S d S )Nz%s?%sz%s?%s%srD   z%s?%s&sn=%s)r   r   hashlibmd5r   rI   	hexdigest)r   r<   pathr;   Zquery_stringrawZsnr    r    r!   r4     s    
zBaidu._construct_url)T)T)r6   
__module____qualname____doc__r   r   r	   r   r,   r>   rA   r@   r7   rH   r4   __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)r6   r`   ra   rb   r   r   r    r    r    r!   r     s   r   )r[   	functoolsr   urllib.parser   r   Z	geopy.excr   r   r   r   Zgeopy.geocoders.baser	   r
   Zgeopy.locationr   Z
geopy.utilr   __all__r   r   r    r    r    r!   <module>   s     