o
    ÔèFhˆ  ã                   @   sl   d dl mZ d dlmZ d dl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d
S )é    )Úpartial)Ú	urlencode)ÚGeocoderAuthenticationFailureÚGeocoderQuotaExceededÚGeocoderServiceError)ÚDEFAULT_SENTINELÚGeocoder)ÚLocation)Úlogger)ÚGeocodeFarmc                       st   e Zd ZdZdZdZ	deededddœ‡ fdd„Zded	œd
d„Zded	œdd„Z	dd„ Z
dd„ Zdd„ Z‡  ZS )r   z‚Geocoder using the GeocodeFarm API.

    Documentation at:
        https://www.geocode.farm/geocoding/free-api-documentation/
    z/v3/json/forward/z/v3/json/reverse/N)ÚtimeoutÚproxiesÚ
user_agentÚssl_contextÚadapter_factoryÚschemec          	         sN   t ƒ j||||||d� || _d}d| j|| jf | _d| j|| jf | _dS )a(  

        :param str api_key: (optional) The API key required by GeocodeFarm
            to perform geocoding requests.

        :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 scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.
        )r   r   r   r   r   r   zwww.geocode.farmz	%s://%s%sN)ÚsuperÚ__init__Úapi_keyr   Úgeocode_pathÚapiÚreverse_pathÚreverse_api)	Úselfr   r   r   r   r   r   r   Údomain©Ú	__class__© úT/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/geocodefarm.pyr      s   %úÿÿzGeocodeFarm.__init__T)Úexactly_oner   c                C   s\   d|i}| j r| j |d< d | jt|ƒf¡}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``.
        ÚaddrÚkeyú?z%s.geocode: %s©r   ©r   )r   Újoinr   r   r
   Údebugr   Ú__name__r   Ú_parse_jsonÚ_call_geocoder)r   Úqueryr   r   ÚparamsÚurlÚcallbackr   r   r   ÚgeocodeP   s   ÿ
zGeocodeFarm.geocodec          	      C   sŽ   z|   |¡ d¡\}}W n ty   tdƒ‚w ||dœ}| jr%| j|d< d | jt|ƒf¡}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. GeocodeFarm'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``.
        ú,z"Must be a coordinate pair or Point)ÚlatÚlonr!   r"   z%s.reverse: %sr#   r$   )Ú_coerce_point_to_stringÚsplitÚ
ValueErrorr   r%   r   r   r
   r&   r   r'   r   r(   r)   )	r   r*   r   r   r0   r1   r+   r,   r-   r   r   r   Úreversek   s   ÿþ
zGeocodeFarm.reversec           	      C   sª   g }|  d¡D ]K}|  di ¡}|  di ¡}|  dd ¡}|  dd ¡}|  dd ¡}|d u r1|  dd ¡}|d u r;|  dd ¡}|rG|rGt|ƒ}t|ƒ}| t|||f|ƒ¡ q|S )	NÚRESULTSÚCOORDINATESÚADDRESSÚlatitudeÚ	longitudeÚaddress_returnedÚaddressÚformatted_address)ÚgetÚfloatÚappendr	   )	r   ÚresultsÚplacesÚresultÚcoordinatesr<   r9   r:   Ú	placenamer   r   r   Ú_parse_code�   s    zGeocodeFarm._parse_codec                 C   sT   |d u rd S |d }|   |¡ d| di ¡ dd¡v rd S |  |¡}|r(|d S |S )NÚgeocoding_resultsÚ
NO_RESULTSÚSTATUSÚstatusÚ r   )Ú_check_for_api_errorsr>   rF   )r   Ú
api_resultr   rG   rB   r   r   r   r(   ¢   s   

zGeocodeFarm._parse_jsonc                 C   s`   |  di ¡}d|  dd¡v rdS |  dd¡dk}|s.|  d¡}ttdœ}|  |t¡}||ƒ‚dS )	zc
        Raise any exceptions if there were problems reported
        in the api response.
        rI   rH   rJ   rK   NÚSUCCESSÚaccess)ÚAPI_KEY_INVALIDÚOVER_QUERY_LIMIT)r>   r   r   r   )r   rG   Ústatus_resultÚapi_call_successÚaccess_errorÚaccess_error_to_exceptionÚexception_clsr   r   r   rL   ±   s   
þÿ÷z!GeocodeFarm._check_for_api_errors)N)r'   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r.   r5   rF   r(   rL   Ú__classcell__r   r   r   r   r      s$    þ÷6$r   N)Ú	functoolsr   Úurllib.parser   Ú	geopy.excr   r   r   Úgeopy.geocoders.baser   r   Úgeopy.locationr	   Ú
geopy.utilr
   Ú__all__r   r   r   r   r   Ú<module>   s    