o
    e$                     @   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mZ d dlmZmZ d dlmZ d dlmZmZ dZG d	d
 d
eZdS )    N)partial)quote	urlencode)GeocoderAuthenticationFailureGeocoderInsufficientPrivilegesGeocoderRateLimitedGeocoderServiceErrorGeocoderUnavailable)DEFAULT_SENTINELGeocoder)Location)join_filterlogger)Bingc                       sx   e Zd ZdZh dZdZdZdeededdd fdd	
Zd
deddddddZ	d
edddddZ
dddZ  ZS )r   zGeocoder using the Bing Maps Locations API.

    Documentation at:
        https://msdn.microsoft.com/en-us/library/ff701715.aspx
    >   locality
postalCodeaddressLineadminDistrictcountryRegionz/REST/v1/Locationsz/REST/v1/Locations/%(point)sNzdev.virtualearth.net)schemetimeoutproxies
user_agentssl_contextadapter_factorydomainc          	         sJ   t  j||||||d || _d| j|| jf | _d| j|| jf | _dS )a  

        :param str api_key: Should be a valid Bing Maps API key
            (https://www.microsoft.com/en-us/maps/create-a-bing-maps-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 domain: base api domain

            .. versionadded:: 2.4
        )r   r   r   r   r   r   z	%s://%s%sN)super__init__api_keyr   geocode_pathgeocode_apireverse_pathreverse_api)	selfr   r   r   r   r   r   r   r   	__class__ G/var/www/html/venv/lib/python3.10/site-packages/geopy/geocoders/bing.pyr   %   s   *zBing.__init__TF)exactly_oneuser_locationr   cultureinclude_neighborhoodinclude_country_codec                   s   t |tjjr fdd| D } j|d< n| jd}|r' ||d< |r-d|d< |r3||d< |d	ur;||d
< |rAd|d< d jt	|f}	t
d jj|	 t j|d}
 j|	|
|dS )a  
        Return a location point by address.

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

            For a structured query, provide a dictionary whose keys
            are one of: `addressLine`, `locality` (city),
            `adminDistrict` (state), `countryRegion`, or `postalCode`.
        :type query: str or dict

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

        :param user_location: Prioritize results closer to
            this location.
        :type user_location: :class:`geopy.point.Point`

        :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.

        :param str culture: Affects the language of the response,
            must be a two-letter country code.

        :param bool include_neighborhood: Sets whether to include the
            neighborhood field in the response.

        :param bool include_country_code: Sets whether to include the
            two-letter ISO code of the country in the response (field name
            'countryRegionIso2').

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        c                    s    i | ]\}}| j v r||qS r&   )structured_query_params).0keyvalr#   r&   r'   
<dictcomp>   s
    
z Bing.geocode.<locals>.<dictcomp>r/   )queryr/   userLocation   
maxResultsr*   NincludeNeighborhoodciso2include?z%s.geocode: %sr(   r   )
isinstancecollectionsabcMappingitemsr   _coerce_point_to_stringjoinr    r   r   debugr%   __name__r   _parse_json_call_geocoder)r#   r3   r(   r)   r   r*   r+   r,   paramsurlcallbackr&   r1   r'   geocode[   s,   .
zBing.geocode)r(   r   r*   r,   c                C   s   |  |}d| ji}|r||d< |rd|d< t|d}d| jt|d t|f}	t	d| j
j|	 t| j|d	}
| j|	|
|d
S )ar  
        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.

        :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.

        :param str culture: Affects the language of the response,
            must be a two-letter country code.

        :param bool include_country_code: Sets whether to include the
            two-letter ISO code of the country in the response (field name
            'countryRegionIso2').

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r/   r*   r8   r9   zutf-8r:   )pointz%s.reverse: %sr;   r<   )rB   r   r   encoderC   r"   dictr   r   rD   r%   rE   r   rF   rG   )r#   r3   r(   r   r*   r,   rL   rH   quoted_pointrI   rJ   r&   r&   r'   reverse   s   
#
zBing.reversec                    s   | dd}|dkr4| dd}|dkrt||dkr t||dkr(t||dkr0t|t||d	 d
 d }|du sDt|sFdS dd  |rR |d
 S  fdd|D S )zW
        Parse a location name, latitude, and longitude from an JSON response.
        
statusCode   errorDetails i  i  i  i  resourceSetsr   	resourcesNc                 S   s   d}| d }| dd|}| dd|}| dd|}| dd|}| dd|}td	||g}td
||g}	td	||	|g}
| d d d pRd}| d d d p\d}|ri|rit|}t|}t|
||f| S )z3
            Parse each return object.
            z, 
addressr   rT   r   r   r   r   z,  rL   coordinatesr   Nr5   )getstripr   floatr   )resource
stripcharsaddrrW   citystatezipcodecountry
city_stateplacelocationlatitude	longituder&   r&   r'   parse_resource   s    z(Bing._parse_json.<locals>.parse_resourcec                    s   g | ]} |qS r&   r&   )r.   r]   ri   r&   r'   
<listcomp>
  s    z$Bing._parse_json.<locals>.<listcomp>)rZ   r   r   r   r	   r   len)r#   docr(   status_codeerrrV   r&   rj   r'   rF      s&   zBing._parse_json)T)rE   
__module____qualname____doc__r-   r   r!   r
   r   rK   rP   rF   __classcell__r&   r&   r$   r'   r      s4    :O2r   )collections.abcr>   	functoolsr   urllib.parser   r   	geopy.excr   r   r   r   r	   geopy.geocoders.baser
   r   geopy.locationr   
geopy.utilr   r   __all__r   r&   r&   r&   r'   <module>   s    