from geopy import Nominatim
from geopy.extra.rate_limiter import RateLimiter


class GeoCodePipeline(object):

    def open_spider(self, spider):
        geolocator = Nominatim(timeout=5)
        self.__geocodeFunc = RateLimiter(geolocator.geocode, min_delay_seconds=2)

    def process_item(self, item, spider):

        for crime in item["crimes"]:
            place = crime["place"]
            latitude, longitude = self.__geocode_address(place)
            crime["latitude"] = latitude
            crime["longitude"] = longitude
        return item

    def __geocode_address(self, place):
        if place is None:
            return None, None

        location = self.__geocodeFunc(place)
        if location is not None:
            return location.latitude, location.longitude
        else:
            return None, None

from geopy import Nominatim, Photon
from geopy.extra.rate_limiter import RateLimiter


class GeoCodePipeline(object):

    def open_spider(self, spider):
        geolocator = Photon(timeout=5)
        self.__geocodeFunc = RateLimiter(geolocator.geocode, min_delay_seconds=2)

    def process_item(self, item, spider):

        for crime in item["crimes"]:
            place = crime["place"]
            latitude, longitude = self.__geocode_address(place)
            crime["latitude"] = latitude
            crime["longitude"] = longitude
        return item

    def __geocode_address(self, place):
        if place is None:
            return None, None

        location = self.__geocodeFunc(place)
        if location is not None:
            return location.latitude, location.longitude
        else:
            return None, None

Use Phonon instead of Nominatim for geo coding
Use Phonon instead of Nominatim for geo coding

Phonon is more fault tolerant to spelling mistakes.

- from geopy import Nominatim
+ from geopy import Nominatim, Photon
  from geopy.extra.rate_limiter import RateLimiter
  
  
  class GeoCodePipeline(object):
  
      def open_spider(self, spider):
-         geolocator = Nominatim(timeout=5)
+         geolocator = Photon(timeout=5)
          self.__geocodeFunc = RateLimiter(geolocator.geocode, min_delay_seconds=2)
  
      def process_item(self, item, spider):
  
          for crime in item["crimes"]:
              place = crime["place"]
              latitude, longitude = self.__geocode_address(place)
              crime["latitude"] = latitude
              crime["longitude"] = longitude
          return item
  
      def __geocode_address(self, place):
          if place is None:
              return None, None
  
          location = self.__geocodeFunc(place)
          if location is not None:
              return location.latitude, location.longitude
          else:
              return None, None
  
Use Phonon instead of Nominatim for geo coding
## Code Before:
from geopy import Nominatim
from geopy.extra.rate_limiter import RateLimiter


class GeoCodePipeline(object):

    def open_spider(self, spider):
        geolocator = Nominatim(timeout=5)
        self.__geocodeFunc = RateLimiter(geolocator.geocode, min_delay_seconds=2)

    def process_item(self, item, spider):

        for crime in item["crimes"]:
            place = crime["place"]
            latitude, longitude = self.__geocode_address(place)
            crime["latitude"] = latitude
            crime["longitude"] = longitude
        return item

    def __geocode_address(self, place):
        if place is None:
            return None, None

        location = self.__geocodeFunc(place)
        if location is not None:
            return location.latitude, location.longitude
        else:
            return None, None

## Instruction:
Use Phonon instead of Nominatim for geo coding
## Code After:
from geopy import Nominatim, Photon
from geopy.extra.rate_limiter import RateLimiter


class GeoCodePipeline(object):

    def open_spider(self, spider):
        geolocator = Photon(timeout=5)
        self.__geocodeFunc = RateLimiter(geolocator.geocode, min_delay_seconds=2)

    def process_item(self, item, spider):

        for crime in item["crimes"]:
            place = crime["place"]
            latitude, longitude = self.__geocode_address(place)
            crime["latitude"] = latitude
            crime["longitude"] = longitude
        return item

    def __geocode_address(self, place):
        if place is None:
            return None, None

        location = self.__geocodeFunc(place)
        if location is not None:
            return location.latitude, location.longitude
        else:
            return None, None
