Ejemplo n.º 1
0
 def test_geopulse(self):
     geopulse = self.factual.geopulse(point(34.06021, -118.41828))
     income_only = geopulse.select('income')
     all_results = geopulse.data()[0]
     income_results = income_only.data()[0]
     self.assertIn('commercial_density', all_results)
     self.assertTrue(0 <= all_results['commercial_density'] <= 1)
     self.assertEqual(1, len(income_results))
     self.assertIn('income', income_results)
Ejemplo n.º 2
0
 def test_geopulse(self):
     geopulse = self.factual.geopulse(point(34.06021, -118.41828))
     income_only = geopulse.select('income')
     all_results = geopulse.data()[0]
     income_results = income_only.data()[0]
     self.assertIn('commercial_density', all_results)
     self.assertTrue(0 <= all_results['commercial_density'] <= 1)
     self.assertEqual(1, len(income_results))
     self.assertIn('income', income_results)
Ejemplo n.º 3
0
 def test_geopulse(self):
     geopulse = self.factual.geopulse(point(34.06021, -118.41828))
     income_only = geopulse.select('income')
     all_results = geopulse.data()
     income_results = income_only.data()
     self.assertIn('demographics', all_results)
     demographics = all_results['demographics']
     self.assertIn('area_statistics', demographics)
     area_statistics = demographics['area_statistics']
     self.assertIn('commercial_residential', area_statistics)
     commercial_residential = area_statistics['commercial_residential']
     self.assertTrue(0 <= commercial_residential['business'] <= 100)
     income_demographics = income_results['demographics']
     self.assertEqual(1, len(income_demographics))
     self.assertIn('income', income_demographics)
Ejemplo n.º 4
0
 def test_geopulse(self):
     geopulse = self.factual.geopulse(point(34.06021, -118.41828))
     income_only = geopulse.select('income')
     all_results = geopulse.data()
     income_results = income_only.data()
     self.assertIn('demographics', all_results)
     demographics = all_results['demographics']
     self.assertIn('area_statistics', demographics)
     area_statistics = demographics['area_statistics']
     self.assertIn('commercial_residential', area_statistics)
     commercial_residential = area_statistics['commercial_residential']
     self.assertTrue(0 <= commercial_residential['business'] <= 100)
     income_demographics = income_results['demographics']
     self.assertEqual(1, len(income_demographics))
     self.assertIn('income', income_demographics)
Ejemplo n.º 5
0
 def test_geocode(self):
     geocode = self.factual.geocode(point(34.058744, -118.416937))
     result = geocode.data()[0]
     self.assertEqual('1999 Avenue Of The Stars', result['address'])
     self.assertLess(result['$distance'], 20)
Ejemplo n.º 6
0
 def test_geocode(self):
     geocode = self.factual.geocode(point(34.06021, -118.41828))
     result = geocode.data()[0]
     self.assertEqual('1801 Avenue Of The Stars', result['address'])
     self.assertLess(result['$distance'], 20)
Ejemplo n.º 7
0
    def fetch(self, **kwargs):
        """
        If you fetch
            then format and store in object.fetched 
            save 


        Get data from 
            redis      - cached by dp/di 
            mysql      - cached by dp/di 
            
            foursquare - place id search 
            google     - place id search 
            factual    - place id search 

            foursquare - place attrs search 
            google     - place attrs search 
            factual    - place attrs search 
        """
        results = {}
        # GET FROM CACHE: select whichever one is not None
        data_provider = kwargs.get("data_provider")
        data_id = kwargs.get("data_id")
        if not kwargs.get("skip_cache"):
            cached = self.get_cache(data_provider=data_provider, data_id=data_id)
            if cached:
                self.fetched[data_provider] = cached
                return self.fetched[data_provider]
        self.logger.debug("fetch: from data")
        # FOURSQUARE
        if data_id and data_provider == "foursquare":
            response = client_foursquare.venues(data_id)
            results = response.get("venue")
        # GOOGLE
        elif data_id and data_provider == "google":
            results = client_google.get_place(None, place_id=data_id)
            results.get_details()
        elif data_id and data_provider == "factual":
            results = client_factual.get_row("places", data_id)
        # FOURSQUARE MATCH: lat/lng, name, phone
        elif not data_id and data_provider == "foursquare":
            response = client_foursquare.venues.search(
                params={
                    "intent": "match",
                    "ll": "%s,%s" % (kwargs.get("latitude"), kwargs.get("longitude")),
                    "query": kwargs.get("name"),
                    "phone": kwargs.get("phone"),
                }
            )
            if response.get("venues"):
                results = response.get("venues")[0]
        # GOOGLE PLACES SEARCH
        elif not data_id and data_provider == "google":
            keywords = []
            # Get lat/lng
            lat_lng = {"lat": kwargs.get("latitude"), "lng": kwargs.get("longitude")}
            # Set of search terms
            if kwargs.get("phone"):
                keywords.append(kwargs.get("phone"))
            if kwargs.get("name"):
                keywords.append(kwargs.get("name"))
            if kwargs.get("phone") and kwargs.get("name"):
                keywords.insert(0, "{0} {1}".format(kwargs.get("name"), kwargs.get("phone")))
            # Crosswalk to Google: clean up
            for kw in keywords:
                if kwargs.get("longitude") and kwargs.get("latitude"):
                    query_result = client_google.nearby_search(lat_lng=lat_lng, keyword=kw)
                else:
                    query_result = client_google.text_search(query=kw)
                if len(query_result.places) >= 1:
                    # GET FROM GOOGLE AND FORMAT
                    results = query_result.places[0]
                    # STORE RESULT - THIS IS FIRST TIME WE SEE IT
                    break
        elif not data_id and data_provider == "factual":
            try:
                results = (
                    client_factual.table("places")
                    .search(kwargs.get("name"))
                    .geo(point(kwargs.get("latitude"), kwargs.get("longitude")))
                    .data()
                )
            except APIException, e:
                self.logger.exception(e)
                # CHECK IF IT HAS HAPPENED IN THE LAST 60 SECONDS
                if redis_server.get("crosswalk:error:factual:RateLimitExceeded"):
                    self.logger.error("Sleeping 1 hour: RateLimitExceeded\n%s" % time.ctime())
                    time.sleep(60 * 60 + 20)
                else:
                    self.logger.error("Sleeping 1 minute: RateLimitExceeded")
                    time.sleep(65)
                    redis_server.set("crosswalk:error:factual:RateLimitExceeded", 1)
                    redis_server.expire("crosswalk:error:factual:RateLimitExceeded", 65)
                # SET KEY TO NOTIFY THAT IT HAPPENED
                # SLEEP FOR 1 MINUTE
                results = {}
            if results:
                results = results[0]
            else:
                results = {}