def _handle_get_node(self, result):
     venue = None
     for see_also in result.properties.get(str(RDFS.seeAlso), []):
         venue = get_foursquare_venue_from_url(see_also)
         if venue:
             break
     return venue
    def lookup_foursquare_content(self, node_uri, foursquare_identifier=None):
        """
        Looks up the foursquare details of a venue.
        :param node_uri: the uri of the node to look up.
        :param foursquare_identifier: the identifier of the foursquare data.  If this is not provided, the node will be
        fetched and the first seeAlso property from the node will be used as this parameter.
        :return:
        """
        def update_venue_details(venue):
            # No point in continuing this exercise if certain requirements are not resolved.
            if not venue:
                raise RuntimeError('Venue identifier is not defined')

            if not self._foursquare_client:
                raise RuntimeError('Foursquare client is not defined')

            # Finished checking requirements, fetch the details and update.
            logger.debug('Looking up venue: %s' % venue)
            venue_details = self._foursquare_client.venues(venue)

            # Translate the venue details into a rdf storage payload for sending to update.
            if 'venue' in venue_details:
                storage_payload = StoragePayload()
                foursquare_to_storage(venue_details['venue'], storage_payload)
                storage_payload.about = node_uri
                storage_payload.add_reference(DCTERMS.creator, self._representation_manager.representation_uri)

                return self._storage_client.update_node(storage_payload)

        # Attempt to look up the venue id from the details in the node.
        if foursquare_identifier is None:
            search_payload = StoragePayload()
            search_payload.about = node_uri
            promise = self._storage_client.get_node(search_payload).then(self._handle_get_node)
        else:
            promise = self._scheduler.promise()
            venue_identifier = get_foursquare_venue_from_url(foursquare_identifier)
            promise.resolved(venue_identifier)

        promise = promise.then(update_venue_details)

        return promise