Beispiel #1
0
def google_collector():
    """
    Return a GoogleCollector.

    A valid API key for the Google Maps client starts with "AIza".
    """
    c = GoogleCollector()
    c.authenticate('AIzaasdf')
    return c
Beispiel #2
0
def integration_test_google_place_details():
    """
    Use jq to parse the json output:

        cat google_collector_details_epoch_coffee.json | jq '.result | {name: .name, address: .formatted_address, phone: .formatted_phone_number, website: .website}'
    """
    places_api_key = os.environ['RYR_COLLECTOR_GOOGLE_PLACES_API_KEY']
    gmaps_places = GoogleCollector()
    gmaps_places.authenticate(api_key=places_api_key)
    details = gmaps_places.place_details('ChIJG-gJw2vKRIYROWi2uwOp8QE')
    print(json.dumps(details, indent=2))
Beispiel #3
0
    def get(self, request, latlong, format=None):
        """Return a list of all places nearby our coordinates."""
        # Define data.
        places_api_key = os.environ['RYR_COLLECTOR_GOOGLE_PLACES_API_KEY']

        # Prepare client.
        gmap = GoogleCollector()
        gmap.authenticate(api_key=places_api_key)

        # Retrieve nearby places.
        places_nearby = gmap.search_places_nearby(latlong)
        return Response(places_nearby)
Beispiel #4
0
    def authenticate(self):
        """Authenticate."""
        # Create collector.
        if self.provider.lower() == 'yelp':
            self.collector = YelpCollector()
            self.collector.authenticate(self.api_key)
        elif self.provider.lower() == 'google':
            self.collector = GoogleCollector()
            self.collector.authenticate(self.api_key)
        else:
            raise ValueError(
                f'The "{self.provider}" provider is not supported.')

        self.collector.weight = self.weight
Beispiel #5
0
def integration_test_google_search_place():
    """
    Use jq to parse the json output:

        cat google_collector_search_epoch_coffee.json | jq '.results[] | {name: .name, vicinity: .vicinity, place_id: .place_id}'
    """

    places_api_key = os.environ['RYR_COLLECTOR_GOOGLE_PLACES_API_KEY']

    epoch_latlong = (30.3186037, -97.72454019999999)
    gmaps_places = GoogleCollector()
    gmaps_places.authenticate(api_key=places_api_key)
    search_results = gmaps_places.search_places(epoch_latlong)
    print(json.dumps(search_results, indent=2))
Beispiel #6
0
def integration_test_full_google_yelp_workflow():
    """"""
    places_api_key = os.environ['RYR_COLLECTOR_GOOGLE_PLACES_API_KEY']
    yelp_api_key = os.environ['RYR_COLLECTOR_YELP_API_KEY']

    epoch_latlong = (30.3186037, -97.72454019999999)

    gmaps_places = GoogleCollector()
    gmaps_places.authenticate(api_key=places_api_key)
    s = gmaps_places.search_places(epoch_latlong)

    epoch_search_summary = gmaps_places.retrieve_search_summary(1)

    yelp = CollectorClient('yelp', api_key=yelp_api_key)
    yelp.authenticate()
    place_details = yelp.retrieve_place_details(
        epoch_search_summary.id,
        epoch_search_summary.name,
        epoch_search_summary.address,
    )
    print(json.dumps(place_details))
Beispiel #7
0
def integration_test_full_google_yelp_workflow_with_generic():
    """"""
    places_api_key = os.environ['RYR_COLLECTOR_GOOGLE_PLACES_API_KEY']
    yelp_api_key = os.environ['RYR_COLLECTOR_YELP_API_KEY']

    # Simulate a click Epoch Coffee Shop - Northloop on the map.
    epoch_latlong = (30.3186037, -97.72454019999999)
    epoch_latlong = (30.319136, -97.724303)
    # Retrieve the places nearby the location clicked on the map.
    gmap = GoogleCollector()
    gmap.authenticate(api_key=places_api_key)
    places_nearby = gmap.search_places_nearby(epoch_latlong)

    # Act like a user chose Epoch Coffee Shop - Northloop in the result list.
    epoch_search_summary = gmap.retrieve_search_summary(1)
    print(epoch_search_summary)

    # Lookup the place on Google Maps.
    google = CollectorClient('google', api_key=places_api_key)
    google.authenticate()
    google_place_details = google.lookup_place(epoch_search_summary.id)
    gb = google.to_business_info()
    print('*** Google:')
    print(gb)

    # Lookup the place on Yelp.
    yelp = CollectorClient('yelp', api_key=yelp_api_key, weight=10)
    yelp.authenticate()
    yelp_place_details = yelp.lookup_place(None, epoch_search_summary.name,
                                           epoch_search_summary.address)
    yb = yelp.to_business_info()
    print('*** Yelp:')
    print(yb)

    # Merge the results.
    print('*** Merged:')
    print(gb.merge(yb))
Beispiel #8
0
class CollectorClient:
    """
    Defines a generic client for the collectors.

    :param str provider: name of the provider to use

        The following providers are supported:
            * yelp
            * google

    :param tuple(str,str) oauth2: tuple containing 2 strings:
        0. the client id
        1. the client secret

    :param str api_key: API key

    If a client can take several types of authentication parameters, they are used in the following order.
        1. OAuth2
        2. API key

    Once one is found, the rest is ignored.
    """
    def __init__(self, provider, oauth2=None, api_key=None, weight=0):
        """Initialize the client."""
        # Authentication properties.
        self.provider = provider
        self.oauth2 = oauth2
        self.api_key = api_key

        # Collector.
        self.collector = None
        self.weight = weight

    def authenticate(self):
        """Authenticate."""
        # Create collector.
        if self.provider.lower() == 'yelp':
            self.collector = YelpCollector()
            self.collector.authenticate(self.api_key)
        elif self.provider.lower() == 'google':
            self.collector = GoogleCollector()
            self.collector.authenticate(self.api_key)
        else:
            raise ValueError(
                f'The "{self.provider}" provider is not supported.')

        self.collector.weight = self.weight

    def get_place_details(self, place_id):
        """
        Retrieve the details of a place.

        :param str place_id: the ID of a place
        :returns: A dictionnary containing the detailed information of the place matching the `place_id`.
        :rtype: dict
        """
        self.collector.get_place_details(place_id)
        b = self.collector.to_business_info()
        return b

    def retrieve_search_summary(self, index=0):
        """
        Retrieve the search information (ID, name and address) of a specific place.

        :param int index: position of the place to look for in the results.
        :return: the summary information of a specific place.
        :rtype: PlaceSearchSummary
        """
        return self.collector.retrieve_search_summary(index)

    def search_places(self, address, terms=None, **kwargs):
        """
        Search for a business based on the provided search criteria.

        The kwargs arguments are specific to the implementation of the collector.

        :param str address: business address
        :param str terms: search term (e.g. "food", "restaurants") or business names such as "Starbucks"
        :return: A dict representing the places matching the search criteria.
        :rtype: dict
        """
        return self.collector.search_places(address, terms=terms, **kwargs)

    def lookup_place(self, place_id=None, name=None, address=None):
        """
        Look up for a place.

        :param str place_id: ID of the place to look for. Dependent of the collector used to perform the lookup.
        :param str name: name of the place
        :param str address: adress of the place
        :return: A dictionary containing the business information.
        :rtype: dict
        """
        if not place_id:
            if not (name and address):
                raise ValueError('A name and a address must be provided.')
            self.search_places(
                address=address,
                terms=name,
                limit=1,
            )
            search_summary = self.retrieve_search_summary(0)
            lookup_id = search_summary.id
        else:
            lookup_id = place_id
        place_details = self.get_place_details(lookup_id)
        return place_details

    def to_business_info(self):
        """
        Convert the raw data to a BusinessInfo object.

        :rtype: BusinessInfo object
        """
        return self.collector.to_business_info()