async def on_message(command): # Ignore string from self if command.author == client.user: return # 1. Handle Greeting: 'hi' -> 'hey' if command.content == 'hi': await command.channel.send('hey') return cmd_tokens = command.content.split() cmd_userid = command.author.id cmd_type = cmd_tokens[0] cmd_args = cmd_tokens[1:] # if command does not have the right number of tokens if len(cmd_args) < 1: await command.channel.send(ErrorMessage.INCORRECT_COMMAND) return # 2. Handle Google Search command. Response contains top 5 newline separated search results if cmd_type == '!google': query = ' '.join(cmd_args) if not save_search_query(conn, cmd_userid, query): print("ERROR") result = perform_search(query) await command.channel.send('\n'.join(result)) # 3. Handle Recent search command. # Response contains all the previous search queries containing the passed substring (newline separated) if cmd_type == '!recent': query = ' '.join(cmd_args) result = find_search_history(conn, cmd_userid, query) await command.channel.send('\n'.join(result))
async def get_product_by_image(request: web.Request) -> web.Response: data = await request.post() try: t1 = time.time() json_result = search.perform_search( image_bytes=data['image'].file.read()) t2 = time.time() print(t2 - t1) return web.json_response(json_result, dumps=json_dumps_utf) except Exception as ex: logging.error(ex)
def rest_search(request): results = {'query': '', 'results': []} if request.method == 'GET': form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'].strip() + '*' hits = search.perform_search(query) results['query'] = form.cleaned_data['query'] if hits: results['results'].extend(hits) return render_json(request, results['results'])
def rest_explore(request): results = { 'query': '', 'users': [] } if request.method == 'GET': form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['query'].strip() + '*' hits = search.perform_search(query + ' type:profile', limit=100, include_surrogates=True) results['query'] = form.cleaned_data['query'] if hits: results['users'].extend(hits) for user in results['users']: user['categories'] = [c.name for c in Category.objects.filter(person__pk=user['id'])] user['connections'] = Relation.objects.filter(Q(source=user['id'])|Q(target=user['id'])).count() return render_json(request, results)