def _create_query_string(self, query): """ Creates and returns Companycheck API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Companycheck API request Raises: EngineException Usage: Private method. """ result_type = query.__dict__.get('result_type', DEFAULT_RESULT_TYPE) if result_type == '': result_type = DEFAULT_RESULT_TYPE postcode = query.__dict__.get('postcode', '') # Check to if the result type is valid if result_type: if result_type not in RESULT_TYPES: raise QueryParamException( self.name, "Engine doesn't support query result type '{0}'".format( query.result_type)) # Build the appropriate query string based on the result type if result_type == 'company': # Company search format: # https://companycheck.co.uk/api/json/ search?name=tesco&postcode=xxxx&apiKey=xxxxxx query_append = "search?name={}&postcode={}&apiKey={}".format\ (query.terms, postcode, self.api_key) elif result_type == 'director': # Director search format: # https://companycheck.co.uk/api/json/ directorSearch?name=branson&postcode=W11&apiKey=xxxxxx query_append = "directorSearch?name={}&postcode={}&apiKey={}".format\ (query.terms, postcode, self.api_key) else: raise QueryParamException( self.name, "No handler found for result type: {}".format( query.result_type)) print API_ENDPOINT + encode_symbols(query_append) return API_ENDPOINT + encode_symbols(query_append)
def _create_query_string(self, query): """ Creates and returns Facebook API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Facebook API request Raises: EngineException Usage: Private method. """ result_type = query.result_type if not result_type: result_type = DEFAULT_RESULT_TYPE if result_type not in RESULT_TYPES: raise QueryParamException( self.name, "Engine doesn't support query result type '{0}'".format( query.result_type)) search_params = {'result_type': result_type, 'q': query.terms} query_append = "search?q={}&type={}&access_token={}".format\ (search_params['q'], search_params['result_type'], self.api_key) return API_ENDPOINT + encode_symbols(query_append)
def _create_query_string(self, query): """ Creates and returns Neutrinogeoaddress API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Query Kwargs: address: (str) The address string. If this is not found terms will be used instead. Returns: str: query string for Neutrinogeoaddress API request Raises: EngineException Usage: Private method. """ address = query.__dict__.get('address', query.terms) # Throw an exception if we have no search terms. if not address: raise QueryParamException(self.name, "No address provided!") query_append = "?address={}&country-code={}&language-code={}&user-id={}&api-key={}".format\ (address, self.country_code, self.language_code, self.username, self.api_key) return API_ENDPOINT + encode_symbols(query_append)
def _build_iframe_url(self, address, trans_table): """ Builds the url to be used in an iframe for an embedded Google map of the address Args: address (str): The address to be used in the search on Google maps trans_table (str): A translation string to be used with the string.translate method. Can be generated using the maketrans function. Returns: iframe_url (str): The string to be used in the iframe to embed the map. """ google_endpoint = 'https://www.google.com/maps/embed/v1/search' iframe_url = '{}?q={}&key={}'.format(google_endpoint, encode_symbols(address.encode('utf-8').translate(trans_table)), encode_symbols(self.google_api_key)) return iframe_url
def _create_query_string(self, query): """ Creates and returns Googleplus API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Googleplus API request Raises: EngineException Usage: Private method. """ # Set the result type if query.result_type: result_type = query.result_type if result_type == 'people+': # If it's a people+ search, modifiy the result_type for the purposes of the API call. result_type = 'people' else: result_type = DEFAULT_RESULT_TYPE # Check to if the result type is valid if result_type not in RESULT_TYPES: raise QueryParamException( self.name, "Engine doesn't support query result type '{0}'".format( query.result_type)) # Set the number of results to get back, max value specified at the top of this file if query.top and query.top <= MAX_PAGE_SIZE[result_type]: top = query.top elif query.top and query.top > MAX_PAGE_SIZE[result_type]: top = MAX_PAGE_SIZE[result_type] else: top = MAX_PAGE_SIZE[result_type / 2] # Dictionary of search paramaters search_params = { 'result_type': result_type, 'q': query.terms, 'top': top, } # Craft the string to append to the endpoint url query_append = "{}?query='{}'&maxResults={}&key={}".format\ (search_params['result_type'], search_params['q'], search_params['top'] , self.api_key) return API_ENDPOINT + encode_symbols(query_append)
def _create_query_string(self, query): """ Creates and returns Googleplus API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Googleplus API request Raises: EngineException Usage: Private method. """ # Check for a result type, if none found, set it to default. result_type = query.result_type if not result_type: result_type = self.default_result_type # Check to if the result type is valid if result_type not in RESULT_TYPES: raise QueryParamException(self.name, "Engine doesn't support query result type '{0}'" .format(query.result_type)) # Set the number of results to get back, max value specified at the top of this file if query.top and query.top <= MAX_PAGE_SIZE[result_type]: top = query.top else: top = MAX_PAGE_SIZE[result_type] # Dictionary of search paramaters search_params = {'result_type': result_type, 'q': query.terms, 'top': top, 'page_token': query.__dict__.get('page_token', '') } # Craft the string to append to the endpoint url if result_type in ['people', 'activities']: query_append = "{}?query='{}'&maxResults={}&key={}&pageToken={}".format\ (search_params['result_type'], search_params['q'], search_params['top'], self.api_key, search_params['page_token']) elif result_type == 'person_lookup': query_append = "people/{}?key={}&pageToken={}".format\ (search_params['q'], self.api_key, search_params['page_token']) return API_ENDPOINT + encode_symbols(query_append)
def _parse_json_response(self, query, results): """ Parses Neutrinogeoaddress's JSON response and returns as an ifind Response. Args: query (ifind Query): object encapsulating details of a search query. results : requests library response object containing search results. Returns: ifind Response: object encapsulating a search request's results. Usage: Private method. """ response = Response(query.terms, query) content = json.loads(results.text) # Results aren't paginated, no more to get. response.no_more_results = True url_base = 'https://www.google.co.uk/maps/place/' trans_table = maketrans(u' ', u'+') # Switch spaces with + for the google maps url locations = content.get(u'locations') if locations: # There are results present, iterate over them. for loc in locations: # Kwargs below address = loc.get(u'address', '') latitude = loc.get(u'latitude', '') longitude = loc.get(u'longitude', '') country = loc.get(u'country', '') country_code = loc.get(u'country-code', '') city = loc.get(u'city', '') postcode = loc.get(u'postal-code', '') # The iframe_url must be placed in an iframe in order to render the map. if self.google_api_key: iframe_url = self._build_iframe_url(address, trans_table) else: iframe_url = None url = url_base + encode_symbols(address.encode('utf-8').translate(trans_table)) text = Neutrinogeoaddress._build_summary(address, city, country, postcode, latitude, longitude) response.add_result(title=address, url=url, summary=text, imageurl=None, address=address, latitude=latitude, longitude=longitude, country=country, country_code=country_code, city=city, postcode=postcode, iframe_url=iframe_url) return response
def _create_query_string(self, query): """ Creates and returns Googleplus API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Googleplus API request Raises: EngineException Usage: Private method. """ # Set the result type if query.result_type: result_type = query.result_type if result_type == 'people+': # If it's a people+ search, modifiy the result_type for the purposes of the API call. result_type = 'people' else: result_type = DEFAULT_RESULT_TYPE # Check to if the result type is valid if result_type not in RESULT_TYPES: raise QueryParamException(self.name, "Engine doesn't support query result type '{0}'" .format(query.result_type)) # Set the number of results to get back, max value specified at the top of this file if query.top and query.top <= MAX_PAGE_SIZE[result_type]: top = query.top elif query.top and query.top > MAX_PAGE_SIZE[result_type]: top = MAX_PAGE_SIZE[result_type] else: top = MAX_PAGE_SIZE[result_type/2] # Dictionary of search paramaters search_params = {'result_type': result_type, 'q': query.terms, 'top': top, } # Craft the string to append to the endpoint url query_append = "{}?query='{}'&maxResults={}&key={}".format\ (search_params['result_type'], search_params['q'], search_params['top'] , self.api_key) return API_ENDPOINT + encode_symbols(query_append)
def _create_query_string(self, query): """ Creates and returns Companycheck API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Companycheck API request Raises: EngineException Usage: Private method. """ result_type = query.result_type if not result_type: result_type = self.default_result_type # Check to if the result type is valid if result_type: if result_type not in RESULT_TYPES: raise QueryParamException(self.name, "Engine doesn't support query result type '{0}'" .format(query.result_type)) # Fetch the postcode keyword postcode = query.__dict__.get('postcode', '') # Build the appropriate query string based on the result type if result_type == 'company': # Company search format: # https://companycheck.co.uk/api/json/ search?name=tesco&postcode=xxxx&apiKey=xxxxxx query_append = "search?name={}&postcode={}&apiKey={}".format\ (query.terms, postcode, self.api_key) elif result_type == 'director': # Director search format: # https://companycheck.co.uk/api/json/ directorSearch?name=branson&postcode=W11&apiKey=xxxxxx query_append = "directorSearch?name={}&postcode={}&apiKey={}".format\ (query.terms, postcode, self.api_key) else: raise QueryParamException(self.name, "No handler found for result type: {}" .format(query.result_type)) return API_ENDPOINT + encode_symbols(query_append)
def _create_query_string(self, query): """ Creates and returns Pipl API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Pipl API request Raises: EngineException Usage: Private method. """ query_append = self._build_query_append(query) return API_ENDPOINT + unicode(encode_symbols(query_append), encoding='utf-8')
def _create_query_string(self, query): """ Creates and returns Bing API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Bing API request Raises: EngineException Usage: Private method. """ result_type = query.result_type if not result_type: result_type = DEFAULT_RESULT_TYPE if result_type not in RESULT_TYPES: raise QueryParamException( self.name, "Engine doesn't support query result type '{0}'".format( query.result_type)) params = {'$format': 'JSON', '$top': query.top, '$skip': query.skip} result_string = '?Sources="{}"'.format(result_type) query_string = 'Query="{}"'.format(str(query.terms)) for key, value in params.iteritems(): query_string += '&' + key + '=' + str(value) return API_ENDPOINT + encode_symbols(result_string + '&' + query_string)
def _create_query_string(self, query): """ Creates and returns Bing API query string with encoded query parameters. Args: query (ifind Query): object encapsulating details of a search query. Returns: str: query string for Bing API request Raises: EngineException Usage: Private method. """ result_type = query.result_type if not result_type: result_type = DEFAULT_RESULT_TYPE if result_type not in RESULT_TYPES: raise QueryParamException(self.name, "Engine doesn't support query result type '{0}'" .format(query.result_type)) params = {'$format': 'JSON', '$top': query.top, '$skip': query.skip} result_string = '?Sources="{}"'.format(result_type) query_string = 'Query="{}"'.format(str(query.terms)) for key, value in params.iteritems(): query_string += '&' + key + '=' + str(value) return API_ENDPOINT + encode_symbols(result_string + '&' + query_string)