示例#1
0
文件: ignfrance.py 项目: jmb/geopy
    def _request_raw_content(self, url, timeout):
        """
        Send the request to get raw content.
        """

        request = Request(url)

        if self.referer is not None:
            request.add_header("Referer", self.referer)

        raw_xml = self._call_geocoder(request, timeout=timeout, deserializer=None)

        return raw_xml
    def _request_raw_content(self, url, timeout):
        """
        Send the request to get raw content.
        """

        request = Request(url)

        if self.referer is not None:
            request.add_header('Referer', self.referer)

        raw_xml = self._call_geocoder(request,
                                      timeout=timeout,
                                      deserializer=None)

        return raw_xml
示例#3
0
    def _request_raw_content(self, url, timeout):
        """
        Send the request to get raw content.
        """

        request = Request(url)

        if self.referer is not None:
            request.add_header('Referer', self.referer)

        if self.username and self.password and self.referer is None:
            credentials = '{0}:{1}'.format(self.username, self.password).encode()
            auth_str = base64.standard_b64encode(credentials).decode()
            request.add_unredirected_header(
                'Authorization',
                'Basic {}'.format(auth_str.strip()))

        raw_xml = self._call_geocoder(
            request,
            timeout=timeout,
            deserializer=None
        )

        return raw_xml
示例#4
0
    def geocode(
        self,
        query,
        exactly_one=True,
        timeout=DEFAULT_SENTINEL,
        type=None,
        restrict_searchable_attributes=None,
        limit=None,
        language=None,
        countries=None,
        around=None,
        around_via_ip=None,
        around_radius=None,
        x_forwarded_for=None,
    ):
        """
        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.

        :param str type: Restrict the search results to a specific type.
            Available types are defined in documentation:
            https://community.algolia.com/places/api-clients.html#api-options-type

        :param str restrict_searchable_attributes: Restrict the fields in which
            the search is done.

        :param int limit: Limit the maximum number of items in the
            response. If not provided and there are multiple results
            Algolia API will return 20 results by default. This will be
            reset to one if ``exactly_one`` is True.

        :param str language: If specified, restrict the search results
            to a single language. You can pass two letters country
            codes (ISO 639-1).

        :param list countries: If specified, restrict the search results
            to a specific list of countries. You can pass two letters
            country codes (ISO 3166-1).

        :param around: Force to first search around a specific
            latitude longitude.
        :type around: :class:`geopy.point.Point`, list or tuple of
            ``(latitude, longitude)``, or string as ``"%(latitude)s,
            %(longitude)s"``.

        :param bool around_via_ip: Whether or not to first search
            around the geolocation of the user found via his IP address.
            This is true by default.

        :param around_radius: Radius in meters to search around the
            latitude/longitude. Otherwise a default radius is
            automatically computed given the area density.

        :param str x_forwarded_for: Override the HTTP header X-Forwarded-For.
            With this you can control the source IP address used to resolve
            the geo-location of the user. This is particularly useful when
            you want to use the API from your backend as if it was from your
            end-users' locations.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.

        """

        params = {
            'query': self.format_string % query,
        }

        if type is not None:
            params['type'] = type

        if restrict_searchable_attributes is not None:
            params[
                'restrictSearchableAttributes'] = restrict_searchable_attributes

        if limit is not None:
            params['hitsPerPage'] = limit

        if exactly_one:
            params["hitsPerPage"] = 1

        if language is not None:
            params['language'] = language.lower()

        if countries is not None:
            params['countries'] = ','.join([c.lower() for c in countries])

        if around is not None:
            p = Point(around)
            params['aroundLatLng'] = "%s,%s" % (p.latitude, p.longitude)

        if around_via_ip is not None:
            params['aroundLatLngViaIP'] = \
                'true' if around_via_ip else 'false'

        if around_radius is not None:
            params['aroundRadius'] = around_radius

        url = '?'.join((self.geocode_api, urlencode(params)))
        request = Request(url)

        if x_forwarded_for is not None:
            request.add_header('X-Forwarded-For', x_forwarded_for)

        if self.app_id is not None and self.api_key is not None:
            request.add_header('X-Algolia-Application-Id', self.app_id)
            request.add_header('X-Algolia-API-Key', self.api_key)

        logger.debug('%s.geocode: %s', self.__class__.__name__, url)
        return self._parse_json(
            self._call_geocoder(request, timeout=timeout),
            exactly_one,
            language=language,
        )
示例#5
0
    def reverse(
        self,
        query,
        exactly_one=True,
        timeout=DEFAULT_SENTINEL,
        limit=None,
        language=None,
    ):
        """
        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 int limit: Limit the maximum number of items in the
            response. If not provided and there are multiple results
            Algolia API will return 20 results by default. This will be
            reset to one if ``exactly_one`` is True.

        :param str language: If specified, restrict the search results
            to a single language. You can pass two letters country
            codes (ISO 639-1).

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        """
        location = self._coerce_point_to_string(query)

        params = {
            'aroundLatLng': location,
        }

        if limit is not None:
            params['hitsPerPage'] = limit

        if language is not None:
            params['language'] = language

        url = '?'.join((self.reverse_api, urlencode(params)))
        request = Request(url)

        if self.app_id is not None and self.api_key is not None:
            request.add_header('X-Algolia-Application-Id', self.app_id)
            request.add_header('X-Algolia-API-Key', self.api_key)

        logger.debug("%s.reverse: %s", self.__class__.__name__, url)
        return self._parse_json(
            self._call_geocoder(request, timeout=timeout),
            exactly_one,
            language=language,
        )