
    g@@                         d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
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 d
lmZ dZ G d de      Zy)    N)timegm)datetime)partial)	urlencode)ConfigurationErrorGeocoderQueryErrorGeocoderQuotaExceededGeocoderServiceErrorGeocoderUnavailable)DEFAULT_SENTINELGeocoder)Location)ensure_pytz_is_installedfrom_timezone_name)logger)GoogleV3c                        e Zd ZdZdZdZ	 dddddeededdd
 fdZd	 Zd
 Z		 ddeddddddddZ
deddddZdeddZd Zd ZddZd Z xZS )r   zGeocoder using the Google Maps v3 API.

    Documentation at:
        https://developers.google.com/maps/documentation/geocoding/

    Pricing details:
        https://developers.google.com/maps/documentation/geocoding/usage-and-billing
    z/maps/api/geocode/jsonz/maps/api/timezone/jsonNzmaps.googleapis.com )
domainscheme	client_id
secret_keytimeoutproxies
user_agentssl_contextadapter_factorychannelc       
            t         |   |||||	|
       |r|st        d      |r|st        d      t        |xr |      | _        || _        || _        | j                  s|st        d      || _        |j                  d      | _	        || _
        | j                  d| j                  | j                  | _        | j                  d| j                  | j                  | _        y)ag  

        :param str api_key: The API key required by Google to perform
            geocoding requests, mandatory (unless premier is used,
            then both ``client_id`` and ``secret_key`` must be specified
            instead).
            API keys are managed through
            the Google APIs console (https://code.google.com/apis/console).
            Make sure to have both ``Geocoding API`` and ``Time Zone API``
            services enabled for this API key.

            .. versionchanged:: 2.1
               Previously a warning has been emitted when neither ``api_key``
               nor premier were specified. Now a :class:`geopy.exc.ConfigurationError`
               is raised.

        :param str domain: Should be the localized Google Maps domain to
            connect to. The default is 'maps.googleapis.com', but if you're
            geocoding address in the UK (for example), you may want to set it
            to 'maps.google.co.uk' to properly bias results.

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.

        :param str client_id: If using premier, the account client id.

        :param str secret_key: If using premier, the account secret key.

        :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 channel: If using premier, the channel identifier.
        )r   r   r   r   r   r   z'Must provide secret_key with client_id.z'Must provide client_id with secret_key.zSince July 2018 Google requires each request to have an API key. Pass a valid `api_key` to GoogleV3 geocoder to fix this error. See https://developers.google.com/maps/documentation/geocoding/usage-and-billing/://N)super__init__r   boolpremierr   r   api_keystripr   r   r   api_pathapitimezone_pathtz_api)selfr&   r   r   r   r   r   r   r   r   r   r   	__class__s               V/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/geopy/geocoders/google.pyr#   zGoogleV3.__init__&   s    ~ 	!#+ 	 	
 Z$%NOOi$%NOOI4*5"$||G$c  ll3'"&++t{{DMMJ%)[[$++t?Q?QR    c                    | j                   |d<   | j                  r| j                  |d<   dj                  | j                  t	        |      f      }t        j                  t        j                  | j                        |j                  d      t        j                        }t        j                  |j                               j                  d      }| j                   d| j"                  |d|S )z
        Returns a Premier account signed url. Docs on signature:
            https://developers.google.com/maps/documentation/business/webservices/auth#digital_signatures
        clientr   ?zutf-8r!   z&signature=)r   r   joinr(   r   hmacnewbase64urlsafe_b64decoder   encodehashlibsha1urlsafe_b64encodedigestdecoder   r   )r,   paramspath	signatures       r.   _get_signed_urlzGoogleV3._get_signed_url   s    
  >>x<< $F9xx	&(9:;HH$$T__5KK LL
	
 ,,

&/ 	 KKdI
 	
r/   c                 0   g }t        |t        j                  j                        r|j	                         }nHt        |t        j                  j
                        rt        |t        t        f      s|}nt        d      dj                  d |D              S )Nz7`components` parameter must be of type `dict` or `list`|c              3   >   K   | ]  }d j                  |        yw):N)r3   ).0items     r.   	<genexpr>z4GoogleV3._format_components_param.<locals>.<genexpr>   s      
'6tCHHTNs   )

isinstancecollectionsabcMappingitemsSequencestrbytes
ValueErrorr3   )r,   
componentscomponent_itemss      r.   _format_components_paramz!GoogleV3._format_components_param   s    j+//"9"9:(..0Oz;??#;#;<zC<8(OIK K xx 
'6
 
 	
r/   TF)exactly_oner   boundsregionrR   place_idlanguagesensorc                x   dt        |	      j                         i}
|r|s|rt        d      |||
d<   |||
d<   |||st        d      | j                  r| j                  |
d<   |r| j	                  |d      |
d<   |r||
d	<   |r| j                  |      |
d
<   |r||
d<   | j                  r| j                  |
      }n&dj                  | j                  t        |
      f      }t        j                  d| j                  j                  |       t        | j                   |      }| j#                  |||      S )a=  
        Return a location point by address.

        :param str query: The address or query you wish to geocode. Optional,
            if ``components`` param is set::

                >>> g.geocode(components={"city": "Paris", "country": "FR"})
                Location(France, (46.227638, 2.213749, 0.0))

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

        :type bounds: list or tuple of 2 items of :class:`geopy.point.Point` or
            ``(latitude, longitude)`` or ``"%(latitude)s, %(longitude)s"``.
        :param bounds: The bounding box of the viewport within which
            to bias geocode results more prominently.
            Example: ``[Point(22, 180), Point(-22, -180)]``.

        :param str region: The region code, specified as a ccTLD
            ("top-level domain") two-character value.

        :type components: dict or list
        :param components: Restricts to an area. Can use any combination of:
            `route`, `locality`, `administrative_area`, `postal_code`,
            `country`.

            Pass a list of tuples if you want to specify multiple components of
            the same type, e.g.:

                >>> [('administrative_area', 'VA'), ('administrative_area', 'Arlington')]

        :param str place_id: Retrieve a Location using a Place ID.
            Cannot be not used with ``query`` or ``bounds`` parameters.

                >>> g.geocode(place_id='ChIJOcfP0Iq2j4ARDrXUa7ZWs34')

        :param str language: The language in which to return results.

        :param bool sensor: Whether the geocoding request comes from a
            device with a location sensor.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        rZ   zNOnly one of the `query` or `place id` or `bounds`  parameters must be entered.rX   addressz9Either `query` or `components` or `place_id` must be set.keyz#%(lat1)s,%(lon1)s|%(lat2)s,%(lon2)srV   rW   rR   rY   r2   z%s.geocode: %srU   r   )rO   lowerrQ   r&   _format_bounding_boxrT   r%   rA   r3   r)   r   r   debugr-   __name__r   _parse_json_call_geocoder)r,   queryrU   r   rV   rW   rR   rX   rY   rZ   r>   urlcallbacks                r.   geocodezGoogleV3.geocode   sW   ~ c&k'')
 5/0 0 !)F: %F9=X-j , - - << LLF5M#88= ?F8%F8#'#@#@#LF< !)F:<<&&v.C((DHHi&789C%t~~'>'>D4++E""3'"BBr/   )rU   r   rY   rZ   c                   | j                  |      t        |      j                         d}|r||d<   | j                  r| j                  |d<   | j                  s'dj                  | j                  t        |      f      }n| j                  |      }t        j                  d| j                  j                  |       t        | j                  |      }| j                  |||      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.

        :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 language: The language in which to return results.

        :param bool sensor: Whether the geocoding request comes from a
            device with a location sensor.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        )latlngrZ   rY   r]   r2   z%s.reverse: %sr^   r_   )_coerce_point_to_stringrO   r`   r&   r%   r3   r)   r   rA   r   rb   r-   rc   r   rd   re   )	r,   rf   rU   r   rY   rZ   r>   rg   rh   s	            r.   reversezGoogleV3.reverse  s    F 2259&k'')
 !)F:<< LLF5M||((DHHi&789C&&v.C%t~~'>'>D4++E""3'"BBr/   )at_timer   c                x   t                | j                  |      }| j                  |      }||d}| j                  r| j                  |d<   dj	                  | j
                  t        |      f      }t        j                  d| j                  j                  |       | j                  || j                  |      S )al  
        Find the timezone a point in `query` was in for a specified `at_time`.

        `None` will be returned for points without an assigned
        Olson timezone id (e.g. for Antarctica).

        :param query: The coordinates for which you want a timezone.
        :type query: :class:`geopy.point.Point`, list or tuple of (latitude,
            longitude), or string as "%(latitude)s, %(longitude)s"

        :param at_time: The time at which you want the timezone of this
            location. This is optional, and defaults to the time that the
            function is called in UTC. Timezone-aware datetimes are correctly
            handled and naive datetimes are silently treated as UTC.
        :type at_time: :class:`datetime.datetime` or None

        :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`` or :class:`geopy.timezone.Timezone`.
        )location	timestampr]   r2   z%s.reverse_timezone: %sr_   )r   rl   _normalize_timezone_at_timer&   r3   r+   r   r   rb   r-   rc   re   _parse_json_timezone)r,   rf   rn   r   rp   rq   r>   rg   s           r.   reverse_timezonezGoogleV3.reverse_timezoneG  s    0 	!"//644W=	 !"
 << LLF5MhhYv%678.0G0GM""3(A(A7"SSr/   c                 f    | j                  |       |j                  d      }|y t        ||      S )N
timeZoneId)raw)_check_statusgetr   )r,   responsetimezone_ids      r.   rs   zGoogleV3._parse_json_timezonep  s6    8$ll<0
 !+8<<r/   c                     |-t        t        j                         j                               }|S t	        |t              rt        |j                               }|S t        d      )Nz4`at_time` must be an instance of `datetime.datetime`)r   r   utcnowutctimetuplerI   r   )r,   rn   rq   s      r.   rr   z$GoogleV3._normalize_timezone_at_time|  sd    ?x0==?@I  * w3356I
  %F r/   c                     |j                  dg       }| j                  |       |sy d }|r ||d         S |D cg c]
  } ||       c}S c c}w )Nresultsc                 n    | j                  d      }| d   d   d   }| d   d   d   }t        |||f|       S )z4Get the location, lat, lng from a single json place.formatted_addressgeometryrp   latlng)ry   r   )placerp   latitude	longitudes       r.   parse_placez)GoogleV3._parse_json.<locals>.parse_place  sM    yy!45HZ(4U;Hj)*5e<IHx&;UCCr/   r   )ry   rx   )r,   pagerU   placesr   r   s         r.   rd   zGoogleV3._parse_json  s]    )R(4 	D vay))4:;F5K&F;;;s   Ac                    |j                  d      }|dk(  ry |dk(  ry |j                  d      }|dv rt        |xs d      |dk(  rt        |xs d      |d	k(  rt        |xs d
      |dk(  rt        |xs d      t	        |xs d      )NstatusOKZERO_RESULTSerror_message)OVER_QUERY_LIMITOVER_DAILY_LIMITzThe given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of timeREQUEST_DENIEDzYour request was deniedINVALID_REQUESTz"Probably missing address or latlngUNKNOWN_ERRORzServer errorzUnknown error)ry   r	   r   r   r
   )r,   rz   r   r   s       r.   rx   zGoogleV3._check_status  s    h'T>^# _5 ==' **  ''$]%O6OPP(($E!E  &%m&E~FF '}'GHHr/   )N)T)rc   
__module____qualname____doc__r(   r*   r   r#   rA   rT   ri   rm   rt   rs   rr   rd   rx   __classcell__)r-   s   @r.   r   r      s     (H-M ]S )$$( ]S~
.
( cC $cCR $2Ch 26?O 'TR
=<$Ir/   r   )r6   collections.abcrJ   r9   r4   calendarr   r   	functoolsr   urllib.parser   	geopy.excr   r   r	   r
   r   geopy.geocoders.baser   r   geopy.locationr   geopy.timezoner   r   
geopy.utilr   __all__r    r/   r.   <module>r      sJ           "  < # G 
aIx aIr/   