def post(self): # Check if they gave a token if self.request.get('token'): # They did so now let's check the client client_obj = authenticate_client(str(self.request.get('token'))) # Check client if client_obj and client_obj is not False: # Found the client. Now check if they are still in their dails quota ! # We only allow as many results as assigned to the client. # We want the option to change this value for certain users that have # big volume sites. if the count is 0 that means we allow unlimited calls. # We allow 0 clients as our site uses this api too for the javascript calls # Get the current date and year current_date = int(time.strftime("%d")) current_month = int(time.strftime("%m")) current_year = int(time.strftime("%Y")) # get the calls search_apis_calls = dal.search_api_calls(client_obj, current_date, current_month, current_year) # Local Var with Limit daily_limit_local = search_apis_calls.count() # If the count of calls bigger than 0 if search_apis_calls is not False and ( client_obj.daily_limit == 0 or daily_limit_local < client_obj.daily_limit ): # Check if the Q parameter was given for a search if self.request.get('q') and len(self.request.get('q')) >= 3: # Well let's do some searches ! # Trim and check the search term. We want to avoid any errors and keep # it consistant for all the providers search_term = str(self.request.get('q')).strip().replace(' ', '').replace('\t', '').replace('\n', '').replace('\r', '') # Insert info about the call inserted_call = {} # Get all the searchable DAL's in # the order that we will search them. providers = dal.approved_providers() # Run the Search Runner to request from all providers search_raw_response = runner.search(self.request, search_term, providers) # Results # We just show quick info. Such as name, pic and some basic info. # If the user wants to send a E-Mail they can do so with the specified contact url. # This is to protected the E-Mail of address of the owner. (search_obj, search_responses) = dal.search_by_token(str(search_raw_response.token)) # Make the result text result_text = 'notfound' # Check if the response was a success if search_obj.provider_success_responses > 0: result_text = 'found' # Create the results success_results = [] failure_results = [] # Loop and add the diffrent results for response in search_responses: # Create the Provider Obj provider_obj = {} provider_obj['id'] = response.provider.key().id() provider_obj['name'] = response.provider.name provider_obj['website'] = response.provider.website provider_obj['logo'] = response.provider.logo_url(128) if response.status == runner.ProviderResponse.STATUS_FOUND: # Parse to get details data = res = json.loads(response.parsed_response) # Assign params res['owner_name'] = data['owner']['name'] res['contact_url'] = 'http://www.identichip.org/apis/v1/contact?token=' + str(self.request.get('token')) + "&key=" + str(search_obj.token) + "&provider=" + str(response.provider.key()) # Remove owner details del res['owner'] # Assign provider params res['provider'] = provider_obj # Add to list success_results.append(res) elif response.status not in [runner.ProviderResponse.STATUS_FOUND, runner.ProviderResponse.STATUS_NOTFOUND]: # Add the failed provider failure_results.append(provider_obj) # Well we just added a count daily_limit_local += 1 # Redirect to the Search's token so the user # can view the result. This also keeps them away from # executing this page multiple times as that would # be bad! self.response.out.write(json.dumps({ 'result': result_text, 'token': str(search_obj.token), 'url': 'http://www.identichip.org/view/' + str(search_obj.token), 'success': success_results, 'failed': failure_results, 'daily_limit': client_obj.daily_limit, 'remaining_limit': int(client_obj.daily_limit) - daily_limit_local })) # Save client call client_call = schemas.APICallCount() client_call.date = current_date client_call.month = current_month client_call.year = current_year client_call.uid = search_term client_call.client = client_obj client_call.search = search_obj db.put_async(client_call).get_result() # Save for Stats. This is done Async. This is the global search stat dal.update_or_add_search_counter(self.request, search_raw_response).get_result() else: # No UID to search ??? self.response.out.write(json.dumps({ 'error': 'No q parameter was given ! This parameter tells us what UID to search for. Which is quite imporant ... Please see the developer documentation for this at http://www.identichip.org/developer' })) else: # Inform them self.response.out.write(json.dumps({ 'error': 'This Token has exceeded it\'s daily call limit of ' + str(client_obj.daily_limit) + ". If your client requires more please get in contract with us as we can arrange custom plans." })) else: # Inform them self.response.out.write(json.dumps({ 'error': 'No such client found. Invalid Token !' })) else: # Inform them self.response.out.write(json.dumps({ 'error': 'No Client token was given. Please login and create a client to start searching from our providers' }))