o
    ÔèFhm  ã                   @   s€   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZ d	ZG d
d„ de	ƒZdS )z"
:class:`GeocoderDotUS` geocoder.
é    N)Ú	b64encode)Ú	urlencodeÚpy3kÚRequest)ÚGeocoderÚDEFAULT_FORMAT_STRINGÚDEFAULT_TIMEOUT)ÚLocation)ÚConfigurationError)ÚloggerÚjoin_filter)ÚGeocoderDotUSc                       sL   e Zd ZdZddeeddf‡ fdd„	Zddd„Zedd	„ ƒZ	d
d„ Z
‡  ZS )r   z‚
    GeocoderDotUS geocoder, documentation at:
        http://geocoder.us/

    Note that GeocoderDotUS does not support SSL.
    Nc                    sZ   t t| ƒj||||d� |s|r|r|stdƒ‚d| _d| _nd| _d| _|| _|| _dS )a‹  
        :param str username:

        :param str password:

        :param str format_string: String containing '%s' where the
            string to geocode should be interpolated before querying the
            geocoder. For example: '%s, Mountain View, CA'. The default
            is just '%s'.

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising an :class:`geopy.exc.GeocoderTimedOut`
            exception.

            .. versionadded:: 0.97

        :param dict proxies: If specified, routes this geocoder's requests
            through the specified proxy. E.g., {"https": "192.0.2.0"}. For
            more information, see documentation on
            :class:`urllib2.ProxyHandler`.

            .. versionadded:: 0.96

        :param str user_agent: Use a custom User-Agent header.

            .. versionadded:: 1.12.0
        )Úformat_stringÚtimeoutÚproxiesÚ
user_agentz)Username and password must both specifiedTz*http://geocoder.us/member/service/namedcsvFz#http://geocoder.us/service/namedcsvN)Úsuperr   Ú__init__r
   ÚauthenticatedÚapiÚusernameÚpassword)Úselfr   r   r   r   r   r   ©Ú	__class__© úO/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/dot_us.pyr      s   
$ÿÿ
zGeocoderDotUS.__init__Tc           
         sÒ   ˆ j | }d ˆ jtd|iƒf¡}t dˆ jj|¡ t|ˆ  	¡ d�}ˆ j
||dd�}tr3| ¡  d¡n| ¡ }dd	„ t t|tƒsD|gn|¡D ƒ}t|ƒsOd
S |rXˆ  |d ¡S ‡ fdd	„|D ƒ}	d
|	v rgd
S |	S )a&  
        Geocode a location query.

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

            .. versionadded:: 0.97
        ú?Úaddressz%s.geocode: %s)ÚheadersT)r   Úrawúutf-8c                 S   s   g | ]}|‘qS r   r   )Ú.0Úrr   r   r   Ú
<listcomp>h   s    ÿz)GeocoderDotUS.geocode.<locals>.<listcomp>Nr   c                    s   g | ]}ˆ   |¡‘qS r   )Ú_parse_result)r"   Úres©r   r   r   r$   s   s    )r   Újoinr   r   r   Údebugr   Ú__name__r   Ú_get_headersÚ_call_geocoderr   ÚreadÚdecodeÚcsvÚreaderÚ
isinstanceÚlistÚlenr%   )
r   ÚqueryÚexactly_oner   Ú	query_strÚurlÚpageÚcontentÚplacesÚresultr   r'   r   ÚgeocodeQ   s(   
þÿzGeocoderDotUS.geocodec           
      C   sæ   t dd„ | D ƒƒ}d|v rd|d v rdS | dd¡| dd¡| dd¡| d	d¡| d
d¡g}| dd¡}| dd¡}| dd¡}tdtd|ƒ|td||gƒgƒ}| dd¡}| dd¡}|rk|rkt|ƒt|ƒf}	ndS t||	|ƒS )zS
        Parse individual results. Different, but lazy actually, so... ok.
        c                 S   s(   g | ]}t | d ¡ƒdkr| d ¡‘qS )ú=é   )r3   Úsplit)r"   Úxr   r   r   r$   €   s   ( z/GeocoderDotUS._parse_result.<locals>.<listcomp>Úerrorzcouldn't findNÚnumberÚprefixÚstreetÚtypeÚsuffixÚcityÚstateÚzipz, ú ÚlatÚlong)ÚdictÚgetr   Úfloatr	   )
r;   Úplacer   rG   rH   Úzip_codeÚnameÚlatitudeÚ	longitudeÚlatlonr   r   r   r%   x   s4   ÿ




ûýzGeocoderDotUS._parse_resultc                 C   sF   i }| j r!d | j| jf¡}d dt| d¡ƒ d¡f¡}||d< |S )Nú:rJ   ÚBasicr!   ÚAuthorization)r   r(   r   r   r   Úencoder.   )r   r   Úusername_passwordÚauthr   r   r   r+   Ÿ   s   þzGeocoderDotUS._get_headers)TN)r*   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r<   Ústaticmethodr%   r+   Ú__classcell__r   r   r   r   r      s    	ù
4'
&r   )r^   r/   Úbase64r   Úgeopy.compatr   r   r   Úgeopy.geocoders.baser   r   r   Úgeopy.locationr	   Ú	geopy.excr
   Ú
geopy.utilr   r   Ú__all__r   r   r   r   r   Ú<module>   s    