Exemplo n.º 1
0
    def search_users_by_name(self, name):
        if self._enabled == False:
            return list()

        query = {
            'query': {
                'bool': {
                    'should': [{
                        'match': {
                            "name": {
                                'query': name,
                                'boost': 3
                            }
                        }
                    }, {
                        'match': {
                            "name.partial": name
                        }
                    }]
                }
            }
        }

        try:
            response = self._client.search(index=self._index,
                                           doc_type=self._doc_type,
                                           body=query)
            return self._parse_es_search_response(response)
        except Exception as ex:
            logging.error('search_users_by_name: {}'.format(ex))
            raise APIInternalServicesError(
                'Can\'t select data from Elasticsearch')
Exemplo n.º 2
0
 def remove(self, key):
     try:
         self._client.remove(key, policy=self._write_policy)
     except aerospike.exception.RecordNotFound:
         pass
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
Exemplo n.º 3
0
 def list_size(self, key, bin):
     try:
         return self._client.list_size(key, bin, policy=self._read_policy)
     except aerospike.exception.RecordNotFound:
         return 0
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
Exemplo n.º 4
0
 def check_exists(self, key):
     try:
         (_, meta) = self._client.exists(key, policy=self._read_policy)
         if meta == None:
             return False
         return True
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
Exemplo n.º 5
0
 def scan(self, namespace, set, bins):
     try:
         scan = self._client.scan(namespace, set)
         scan.select(*bins)
         response = scan.results(policy=self._scan_policy)
         return [entry[2] for entry in response]
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
Exemplo n.º 6
0
 def _put_one(self, key, bins, ttl):
     try:
         self._client.put(key,
                          bins,
                          meta={'ttl': ttl},
                          policy=self._write_policy)
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
Exemplo n.º 7
0
    def _parse_es_search_response(self, response):
        hits = response.get('hits')
        if hits == None:
            raise APIInternalServicesError(
                'Bad Elasticsearch response, no hits section')

        entries = hits.get('hits')
        if entries == None:
            raise APIInternalServicesError(
                'Bad Elasticsearch response, no hits.hits section')

        results = list()
        for entry in entries:
            rentry = SearchEntry()
            rentry.user_id = entry['_id']
            rentry.name = entry['_source']['name']
            rentry.rank = entry['_score']
            results.append(rentry)
        return results
Exemplo n.º 8
0
 def check_exists_many(self, keys):
     result = list()
     try:
         response = self._client.exists_many(keys, policy=self._read_policy)
         for entry in response:
             if entry[1] == None:
                 result.append(False)
             else:
                 result.append(True)
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
     return result
Exemplo n.º 9
0
 def _get_one(self, key, bins=None):
     try:
         if bins == None:
             record = self._client.get(key, policy=self._read_policy)
         else:
             record = self._client.select(key,
                                          bins,
                                          policy=self._read_policy)
     except aerospike.exception.RecordNotFound:
         return None
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
     return record[2]
Exemplo n.º 10
0
    def set_user_name(self, user_id, name):
        if self._enabled == False:
            return

        doc = {'name': name}

        try:
            self._client.index(index=self._index,
                               doc_type=self._doc_type,
                               id=user_id,
                               body=doc)
        except Exception as ex:
            logging.error('set_user_name: {}'.format(ex))
            raise APIInternalServicesError('Can\'t send data to Elasticsearch')
Exemplo n.º 11
0
 def _get_many(self, keys, bins=None):
     result = list()
     try:
         if bins == None:
             response = self._client.get_many(keys,
                                              policy=self._read_policy)
         else:
             response = self._client.select_many(keys,
                                                 bins,
                                                 policy=self._read_policy)
         for record in response:
             result.append(record[2])
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
     return result
Exemplo n.º 12
0
    def _send_code(self, user_email, auth_code):
        try:
            msg = MIMEMultipart('alternative')
            msg['Subject'] = "Subject: Quest authentication"
            msg.attach(MIMEText(self._get_message_body(auth_code), 'html'))

            server = smtplib.SMTP('smtp.gmail.com')
            server.ehlo()
            server.starttls()
            server.login(self._robot_address, self._robot_passwd)
            server.sendmail(self._robot_address, user_email, msg.as_string())
            server.quit()
        except Exception as ex:
            logging.error(ex)
            raise APIInternalServicesError(
                'Can\'t send email with auth code to {}'.format(user_email))
Exemplo n.º 13
0
    def create_channel(self, user_id1, user_id2):
        try:
            # Initialize the client
            client = Client(self._account_sid, self._auth_token)

            # Create the channel
            channel = client.chat \
                    .services(self._chat_service_sid) \
                    .channels \
                    .create(friendly_name="", type="private")

            member1 = channel.members.create(str(user_id1))
            member2 = channel.members.create(str(user_id2))

            return channel.sid

        except Exception as ex:
            logging.error('create_channel failed, {}'.format(ex))
            raise APIInternalServicesError('create_channel failed')
Exemplo n.º 14
0
    def make_messaging_token(self, user_id, uuid):
        try:
            identity = str(user_id)

            # Create access token with credentials
            token = AccessToken(self._account_sid,
                                self._api_key,
                                self._api_secret,
                                identity=identity)

            # Create an Chat grant and add to token
            chat_grant = ChatGrant(
                service_sid=self._chat_service_sid,
                push_credential_sid=self._push_credential_sid)
            token.add_grant(chat_grant)

            return token.to_jwt().decode('utf-8')
        except Exception as ex:
            logging.error('make_messaging_token failed, {}'.format(ex))
            raise APIInternalServicesError('make_messaging_token failed')
Exemplo n.º 15
0
class GetMatchingSession(POSTSession):
    def __init__(self, global_context):
        self._aerospike_connector = global_context.aerospike_connector
        self._namespace = Config.AEROSPIKE_NS_SIGNS
        self._image_set = 'sign_image'
        # TODO use all matchers
        self._matcher_host = Config.MATCHER_HOSTS[0]
        self._matcher_api_handler = '/api/matching/get'
        self._timeout_sec = Config.MATCHER_TIMEOUT_SEC
        self._params = Params()

    def _init_session_params(self, query):
        self._params.parse(query)

    def _run_session(self):
        logging.info('Get matchings for {} signs'.format(
            len(self._params.signs)))
        results = self._send_requests(self._gen_matching_requests(
            self._params))
        return {'matches': results}

    def _gen_matching_requests(self, params):
        image_screen_b64 = self._prepare_image_data(params.screen_blob)
        for sign_id in params.signs:
            yield (sign_id, image_screen_b64)

    def _prepare_image_data(self, data):
        buf = BytesIO()
        img = Image.open(BytesIO(data))

        logging.info('Prepare image, base size {}'.format(img.size))

        img.thumbnail((400, 400))
        img.save(buf, format='JPEG')
        return base64.b64encode(buf.getvalue())

    def _send_one_request(self, (sign_id, image_screen_b64)):
        # get
        image_background_data = self._aerospike_connector.get_data(
            (self._namespace, self._image_set, str(sign_id)))
        if image_background_data == None:
            logging.warning(
                'Background image not found, sing_id = {}'.format(sign_id))
            return {'error': 'Background image not found'}
        image_background_b64 = self._prepare_image_data(image_background_data)
        # send query
        try:
            request = json.dumps({
                'image_screen': image_screen_b64,
                'image_background': image_background_b64
            })

            url = 'https://' + self._matcher_host + self._matcher_api_handler
            resp = requests.post(url, data=request, timeout=self._timeout_sec)
        except Exception as ex:
            logging.error(ex)
            raise APIInternalServicesError('matching/get request failed')
        # parse response
        try:
            result = {
                'sign_id': sign_id,
                'transformation': json.loads(resp.text)
            }
            return result
        except Exception as ex:
            logging.error('{}\n{}'.format(ex, resp.text[:100]))
            raise APIInternalServicesError(
                'matching/get request failed, bad response')
Exemplo n.º 16
0
 def increment(self, key, bin, value=1):
     try:
         self._client.increment(key, bin, value, policy=self._write_policy)
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))
Exemplo n.º 17
0
 def list_append(self, key, bin, val):
     try:
         self._client.list_append(key, bin, val, policy=self._write_policy)
     except Exception as ex:
         logging.error('Database error: {}'.format(ex))
         raise APIInternalServicesError('Database error: {}'.format(ex))