Пример #1
0
    def on_get_search_by_url(self, req: Request, resp: Response):
        image_match_percent = req.get_param_as_int('image_match_percent', required=False, default=None)
        target_meme_match_percent = req.get_param_as_int('target_meme_match_percent', required=False, default=None)
        same_sub = req.get_param_as_bool('same_sub', required=False, default=False)
        only_older = req.get_param_as_bool('only_older', required=False, default=False)
        meme_filter = req.get_param_as_bool('meme_filter', required=False, default=False)
        filter_crossposts = req.get_param_as_bool('filter_crossposts', required=False, default=True)
        filter_author = req.get_param_as_bool('filter_author', required=False, default=True)
        url = req.get_param('url', required=True)
        filter_dead_matches = req.get_param_as_bool('filter_dead_matches', required=False, default=False)
        target_days_old = req.get_param_as_int('target_days_old', required=False, default=0)

        try:
            search_results = self.image_svc.check_image(
                url,
                target_match_percent=image_match_percent,
                target_meme_match_percent=target_meme_match_percent,
                meme_filter=meme_filter,
                same_sub=same_sub,
                date_cutoff=target_days_old,
                only_older_matches=only_older,
                filter_crossposts=filter_crossposts,
                filter_dead_matches=filter_dead_matches,
                filter_author=filter_author,
                max_matches=500,
                max_depth=-1,
                source='api'
            )
        except NoIndexException:
            log.error('No available index for image repost check.  Trying again later')
            raise HTTPServiceUnavailable('Search API is not available.', 'The search API is not currently available')

        print(search_results.search_times.to_dict())
        resp.body = json.dumps(search_results, cls=ImageRepostWrapperEncoder)
Пример #2
0
 def on_get(self, req, resp, username):
     try:
         redis_cli = super().redis_client
         if redis_cli is not None:
             user_info = redis_cli.hget('USERS', username)
             access_info = redis_cli.hget('USERS_APIKEY',
                                          user_info['api_key'])
             access_token = ''
             if access_info is not None and access_info['access_token']:
                 if redis_cli.exists(access_info['access_token']):
                     access_token = access_info['access_token']
             if user_info is not None:
                 user_info = json.loads(user_info)
                 resp.status = HTTP_200
                 resp.body = json.dumps(
                     dict(status='Success',
                          user=dict(username=user_info['username'],
                                    api_key=user_info['api_key'],
                                    access_token=access_token)))
             else:
                 raise HTTPNotFound(description='No user with username {}'.
                                    format(username))
         else:
             raise HTTPServiceUnavailable(
                 description='Data instances are not yet active')
     except (HTTPNotFound, HTTPServiceUnavailable) as e:
         raise e
     except Exception as e:
         print('Exception in getting user', e)
         raise HTTPInternalServerError(
             description='Something went wrong while getting user info')
Пример #3
0
 def raise_error(self,
                 error_code=None,
                 desc='Something went wrong in server'):
     if error_code and error_code != 500:
         if error_code == 401 or error_code == 403:
             redis_cli = Redis.get_redis_client()
             if redis_cli is not None:
                 access_token = redis_cli.delete('sheets_v4_access')
             else:
                 print('Redis client is not active')
         if error_code == 400:
             raise HTTPBadRequest(description=desc or '')
         elif error_code == 401:
             raise HTTPUnauthorized(description=desc or '')
         elif error_code == 403:
             raise HTTPForbidden(description=desc or '')
         elif error_code == 404:
             raise HTTPNotFound(description=desc or '')
         elif error_code == 409:
             raise HTTPConflict(description=desc or '')
         elif error_code == 412:
             raise HTTPPreconditionFailed(description=desc or '')
         elif error_code == 422:
             raise HTTPUnprocessableEntity(description=desc or '')
         elif error_code == 503:
             raise HTTPServiceUnavailable(description=desc or '')
         else:
             raise HTTPError(HTTP_400,
                             description=desc or '',
                             code=error_code)
     else:
         raise HTTPInternalServerError(description=desc or '')
Пример #4
0
    def on_get(self, req, resp, spreadsheet_id):
        sheet_range = req.get_param('range', required=True)
        data_format = req.get_param('format')
        data_fields = req.get_param('fields')
        row_info = req.get_param_as_bool('row_info')

        try:
            spreadsheets = DataStore.get_sheet_instance()
        except Exception as e:
            print('Datastore is not configured properly', e)
            raise HTTPServiceUnavailable(
                description='Datastore is not configured properly')

        def run():
            value = spreadsheets.values().batchGet(
                spreadsheetId=spreadsheet_id,
                ranges=json.loads(sheet_range)).execute()
            value = value['valueRanges']
            title_value = value[0]['values'] if 'values' in value[0] else []
            data_value = value[1]['values'] if 'values' in value[1] else []
            value = title_value + data_value
            response_data = []
            if len(value):
                if data_format == 'raw':
                    response_data = value
                else:
                    data_fields_list = list()
                    if data_fields:
                        data_fields_list = data_fields.split(',')
                    response_data = SheetValues.__construct_mapped_dict(
                        value, data_fields_list, row_info)
            resp.status = HTTP_200
            resp.body = json.dumps(response_data)

        super().main(run)
Пример #5
0
    def on_get(self, req: Request, resp: Response):

        post_id = req.get_param('post_id', required=False, default=None)
        url = req.get_param('url', required=False, default=None)

        if not post_id and not url:
            log.error('No post ID or URL provided')
            raise HTTPBadRequest("No Post ID or URL", "Please provide a post ID or url to search")

        search_settings = get_image_search_settings_from_request(req, self.config)
        search_settings.max_matches = 500

        post = None
        if post_id:
            with self.uowm.start() as uow:
                post = uow.posts.get_by_post_id(post_id)

        try:
            search_results = self.image_svc.check_image(
                url,
                post=post,
                search_settings=search_settings,
                source='api'
            )
        except NoIndexException:
            log.error('No available index for image repost check.  Trying again later')
            raise HTTPServiceUnavailable('Search API is not available.', 'The search API is not currently available')
        except ImageConversioinException as e:
            log.error('Problem hashing the provided url: %s', str(e))
            raise HTTPBadRequest('Invalid URL', 'The provided URL is not a valid image')

        print(search_results.search_times.to_dict())
        resp.body = json.dumps(search_results, cls=ImageRepostWrapperEncoder)
Пример #6
0
    def on_delete(self, req, resp, spreadsheet_id, sheet_id):
        if sheet_id:
            try:
                spreadsheets=DataStore.get_sheet_instance()
            except Exception as e:
                print('Datastore is not configured properly', e)
                raise HTTPServiceUnavailable(description='Datastore is not configured properly')
            def run():
                body_props={
                    "requests": [
                        {

                            "deleteSheet": {
                                "sheetId": sheet_id
                            }
                        }
                    ]
                }
                value=spreadsheets.batchUpdate(spreadsheetId=spreadsheet_id, body=body_props).execute()
                resp.body=json.dumps(dict(
                    status='Success',
                    message='Successfully deleted the sheet with id {}'.format(sheet_id),
                    spreadsheet_id=value['spreadsheetId']
                ))
                resp.status=HTTP_200
            super().main(run)
        else:
            raise HTTPBadRequest(description='Sheet identifier is mandatory for this request')
Пример #7
0
    def on_post(self, req, resp, spreadsheet_id):
        sheet_range = req.get_param('range', required=True)
        try:
            req_body = json.load(req.bounded_stream)
            if 'values' in req_body and isinstance(
                    req_body['values'], list) and len(req_body['values']):
                dimensions = 'dimensions' in req_body and req_body[
                    'dimensions'] or 'ROWS'
                value_input = 'value_input_option' in req_body and req_body[
                    'value_input_option'] or 'USER_ENTERED'
                insert_option = 'insert_option' in req_body and req_body[
                    'insert_option'] or 'INSERT_ROWS'
                try:
                    spreadsheets = DataStore.get_sheet_instance()
                except Exception as e:
                    print('Datastore is not configured properly', e)
                    raise HTTPServiceUnavailable(
                        description='Datastore is not configured properly')

                def run():
                    sheet_body = {
                        "values": req_body['values'],
                        "majorDimension": dimensions
                    }
                    response = spreadsheets.values().append(
                        spreadsheetId=spreadsheet_id,
                        body=sheet_body,
                        range=sheet_range,
                        valueInputOption=value_input,
                        insertDataOption=insert_option).execute()
                    resp.body = json.dumps(
                        dict(
                            status='Success',
                            message='Successfully Inserted data into sheet',
                            spreadsheet_id=response['spreadsheetId'],
                        ))
                    resp.status = HTTP_201

                super().main(run)
            else:
                raise HTTPPreconditionFailed(
                    description=
                    'Values is a mandatory and must be valid for this request')
        except JSONDecodeError as err:
            print('Request body received', req.bounded_stream.read())
            print('Error while processing request', err)
            raise HTTPUnprocessableEntity(
                description='Cannot parse the body from the request')
        except (HTTPServiceUnavailable, HTTPPreconditionFailed,
                HTTPError) as err:
            raise err
        except Exception as e:
            print('Exception in creating sheet', e)
            raise HTTPInternalServerError(
                description='Something went wrong while creating sheet')
Пример #8
0
 def process_request(self, req, resp):
     if req.path != '/ping':
         req.context['SPREADSHEET_ID'] = spreadsheet_id
         req.context['API_KEY'] = api_key
         access_token = SheetsV4.get_access_token()
         if access_token is None:
             raise HTTPServiceUnavailable(
                 description=
                 'Cannot get access token for accessing data store')
         else:
             req.context['ACCESS_TOKEN'] = access_token
Пример #9
0
    def on_put(self, req, resp, spreadsheet_id):
        value_input = req.get_param('value_input_option',
                                    default='USER_ENTERED')
        try:
            req_body = json.load(req.bounded_stream)
            if 'data' in req_body and isinstance(
                    req_body['data'], list) and len(req_body['data']):
                try:
                    spreadsheets = DataStore.get_sheet_instance()
                except Exception as e:
                    print('Datastore is not configured properly', e)
                    raise HTTPServiceUnavailable(
                        description='Datastore is not configured properly')

                def run():
                    sheet_body = {
                        "data": req_body['data'],
                        "valueInputOption": value_input
                    }
                    response = spreadsheets.values().batchUpdate(
                        spreadsheetId=spreadsheet_id,
                        body=sheet_body).execute()
                    resp.body = json.dumps(
                        dict(
                            status='Success',
                            message=
                            'Successfully updated the data in the sheet',
                            spreadsheet_id=response['spreadsheetId'],
                        ))
                    resp.status = HTTP_200

                super().main(run)
            else:
                raise HTTPPreconditionFailed(
                    description=
                    'Data is a mandatory and must be valid for this request')
        except JSONDecodeError as err:
            print('Request body received', req.bounded_stream.read())
            print('Error while processing request', err)
            raise HTTPUnprocessableEntity(
                description='Cannot parse the body from the request')
        except (HTTPServiceUnavailable, HTTPPreconditionFailed,
                HTTPError) as err:
            raise err
        except Exception as e:
            print('Exception in updating sheet info', e)
            raise HTTPInternalServerError(
                description='Something went wrong while updating sheet')
Пример #10
0
 def on_get(self, req, resp, spreadsheet_id, sheet_id):
     spreadsheet_id=spreadsheet_id
     try:
         spreadsheets=DataStore.get_sheet_instance()
     except Exception as e:
         print('Datastore is not configured properly', e)
         raise HTTPServiceUnavailable(description='Datastore is not configured properly')
     def run():
         value=spreadsheets.get(spreadsheetId=spreadsheet_id, fields='sheets.properties').execute()
         resp.body=json.dumps(dict(
             status='Success',
             spreadsheet_id=spreadsheet_id,
             data='sheets' in value and value['sheets'] or []
         ))
         resp.status=HTTP_200
     super().main(run)
Пример #11
0
 def on_delete(self, req, resp, username):
     try:
         redis_cli = super().redis_client
         if redis_cli is not None:
             user_info = redis_cli.hdel('USERS', username)
             if user_info:
                 resp.status = HTTP_200
                 resp.body = json.dumps(
                     dict(status='Success',
                          message='Successfully deleted the user'))
             else:
                 raise HTTPNotFound(description='No user with username {}'.
                                    format(username))
         else:
             raise HTTPServiceUnavailable(
                 description='Data instances are not yet active')
     except (HTTPNotFound, HTTPServiceUnavailable) as e:
         raise e
     except Exception as e:
         print('Exception in deleting user', e)
         raise HTTPInternalServerError(
             description='Something went wrong while deleting user info')
Пример #12
0
 def on_post(self, req, resp, spreadsheet_id, sheet_id):
     spreadsheet_id=spreadsheet_id
     title=sheet_id
     body_props={
         "requests": [
             {
                 "addSheet": {
                     "properties": {
                         "title": title
                     }
                 }
             }
         ]
     }
     try:
         spreadsheets=DataStore.get_sheet_instance()
     except Exception as e:
         print('Datastore is not configured properly', e)
         raise HTTPServiceUnavailable(description='Datastore is not configured properly')
     def run():
         value=spreadsheets.batchUpdate(spreadsheetId=spreadsheet_id, body=body_props).execute()
         sheet_info=dict()
         if 'replies' in value:
             sheet_props=dict()
             if value['replies'][0] and 'addSheet' in value['replies'][0] and 'properties' in value['replies'][0]['addSheet']:
                 sheet_props=value['replies'][0]['addSheet']['properties']
             if 'sheetId' in sheet_props:
                 sheet_info['id']=sheet_props['sheetId']
             if 'title' in sheet_props:
                 sheet_info['title']=sheet_props['title']
         resp.body=json.dumps(dict(
             status='Success',
             message='Successfully Created a new sheet',
             spreadsheet_id=value['spreadsheetId'],
             data=sheet_info
         ))
         resp.status=HTTP_201
     super().main(run)
Пример #13
0
 def on_put(self, req, resp, username):
     token = req.get_header('Authorization')
     is_active = req.get_param_as_bool('is_active', required=True)
     try:
         redis_cli = super().redis_client
         if redis_cli is not None and 'auth_info' in req.context:
             temp_auth_info = json.loads(req.context['auth_info'])
             temp_auth_info.is_active = is_active
             redis_cli.hset('USERS_APIKEY', token,
                            json.dumps(temp_auth_info))
             resp.status = HTTP_200
             resp.body = json.dumps(
                 dict(status='Success',
                      message='Successfully updated the staus of the user'))
         else:
             raise HTTPServiceUnavailable(
                 description='Data instances are not yet active')
     except (HTTPNotFound, HTTPServiceUnavailable) as e:
         raise e
     except Exception as e:
         print('Exception in deleting user', e)
         raise HTTPInternalServerError(
             description='Something went wrong while deleting user info')
Пример #14
0
    def on_delete(self, req, resp, spreadsheet_id):
        sheet_id = req.get_param('sheet_id', required=True)
        dimensions = req.get_param('dimensions', default='ROWS')
        start_index = req.get_param('start_index', required=True)
        end_index = req.get_param('end_index', required=True)
        try:
            spreadsheets = DataStore.get_sheet_instance()
        except Exception as e:
            print('Datastore is not configured properly', e)
            raise HTTPServiceUnavailable(
                description='Datastore is not configured properly')

        def run():
            sheet_body = {
                "requests": [{
                    "deleteDimension": {
                        "range": {
                            "sheetId": sheet_id,
                            "dimension": dimensions,
                            "startIndex": start_index,
                            "endIndex": end_index
                        }
                    }
                }]
            }
            response = spreadsheets.batchUpdate(spreadsheetId=spreadsheet_id,
                                                body=sheet_body).execute()
            resp.body = json.dumps(
                dict(
                    status='Success',
                    message=
                    'Successfully deleted {} from {} to {} in the sheet with id {}'
                    .format(dimensions, start_index, end_index, sheet_id),
                    spreadsheet_id=response['spreadsheetId']))
            resp.status = HTTP_200

        super().main(run)
Пример #15
0
 def process_request(self, req, resp):
     if req.path != '/ping':
         token=req.get_header('Authorization')
         if token:
             try:
                 auth_info = id_token.verify_oauth2_token(token, requests.Request(), CLIENT_ID)
                 if auth_info['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
                     raise ValueError('Wrong issuer.')
                 username = auth_info['name']
                 userid = auth_info['sub']
                 constructed_id=username + '_' + userid
                 try:
                     redis_cli=Redis.get_redis_client()
                     user_info=redis_cli.hget('task_manager_users', constructed_id)
                     if user_info is None:
                         resp.body=json.dumps(dict(
                             is_new_user=True,
                             user_id=userid,
                             username=username
                         ))
                         resp.status=HTTP_200
                         resp.complete=True
                         user_thread=Thread(target=configure_user, args=(constructed_id, username, userid))
                         print('configuring new user')
                         user_thread.start()
                         print('New user configuration initiated')
                     else:
                         user_info=json.loads(user_info)
                         req.context['SHEET_ID']=user_info['sheetid']
                         req.context['SHEET_NAME']=user_info['sheet_name']
                 except Exception as e:
                     print('Exception in authenticating user', e)
                     raise HTTPServiceUnavailable(description='Exception in authenticating user')
             except ValueError:
                 raise HTTPUnauthorized(description='Token passed in header either expired or is not valid')
         else:
             raise HTTPUnauthorized(description='Authorization header is mandatory for processing the request')
Пример #16
0
 def on_post(self, req, resp, username):
     try:
         req_body = json.load(req.bounded_stream)
         if username and 'password' in req_body and len(
                 req_body['password']) > 5:
             redis_cli = super().redis_client
             if redis_cli is not None:
                 password = req_body['password']
                 api_key = super().generate_token(32)
                 access_token = super().generate_token(32)
                 hash_password = hashlib.pbkdf2_hmac(
                     'sha256',
                     password.encode('utf-8'),
                     api_key[8:32].encode('utf-8'),
                     100000,
                     dklen=128)
                 user_info = dict(username=username,
                                  password=hash_password.hex(),
                                  api_key=api_key)
                 access_info = dict(username=username,
                                    is_active=True,
                                    access_token=access_token)
                 try:
                     if not redis_cli.hexists('USERS', username):
                         redis_cli.hset('USERS', username,
                                        json.dumps(user_info))
                         redis_cli.hset('USERS_APIKEY', api_key,
                                        json.dumps(access_info))
                         redis_cli.set(access_token, api_key, ex=28800)
                         resp.status = HTTP_201
                         resp.body = json.dumps(
                             dict(status='Succcess',
                                  message='Successfully created the user',
                                  user=dict(api_key=api_key,
                                            access_token=access_token,
                                            username=username)))
                     else:
                         raise HTTPConflict(
                             description=
                             'User already exists with username {}'.format(
                                 username))
                 except HTTPConflict as err:
                     raise err
                 except Exception as err:
                     raise Exception(
                         'Something went wrong while executing redis commands',
                         err)
             else:
                 raise HTTPServiceUnavailable(
                     description='Data instances are not yet active')
         else:
             raise HTTPPreconditionFailed(
                 description=
                 'Username and password are mandatory and must be valid for this request'
             )
     except JSONDecodeError as err:
         print('Request body received', req.bounded_stream.read())
         print('Error while processing request', err)
         raise HTTPUnprocessableEntity(
             description='Cannot parse the body from the request')
     except (HTTPPreconditionFailed, HTTPServiceUnavailable,
             HTTPConflict) as err:
         raise err
     except Exception as e:
         print('Exception in creating user', e)
         raise HTTPInternalServerError(
             description='Something went wrong while creating user info')
Пример #17
0
 def on_post(self, req, resp, auth_type):
     if auth_type == 'login':
         try:
             req_body = json.load(req.bounded_stream)
             if 'username' in req_body and req_body[
                     'username'] and 'password' in req_body and len(
                         req_body['password']) > 5:
                 username = req_body['username']
                 redis_cli = super().redis_client
                 if redis_cli is not None:
                     user_info = redis_cli.hget('USERS', username)
                     if user_info is not None:
                         user_info = json.loads(user_info)
                         api_key = user_info['api_key']
                         hash_password = hashlib.pbkdf2_hmac(
                             'sha256',
                             req_body['password'].encode('utf-8'),
                             api_key[8:32].encode('utf-8'),
                             100000,
                             dklen=128)
                         if user_info['password'] == hash_password.hex():
                             access_info = redis_cli.hget(
                                 'USERS_APIKEY', api_key)
                             access_info = json.loads(access_info)
                             if access_info is not None and access_info[
                                     'is_active']:
                                 access_token = access_info['access_token']
                                 if not redis_cli.exists(access_token):
                                     access_token = super().generate_token(
                                         32)
                                     access_info[
                                         'access_token'] = access_token
                                     access_info = redis_cli.hset(
                                         'USERS_APIKEY', api_key,
                                         json.dumps(access_info))
                                 redis_cli.set(access_token,
                                               api_key,
                                               ex=28800)
                                 resp.status = HTTP_200
                                 resp.body = json.dumps(
                                     dict(status='Success',
                                          user=dict(
                                              api_key=api_key,
                                              access_token=access_token,
                                              username=username)))
                             else:
                                 raise HTTPNotAcceptable(
                                     description='User is not active')
                         else:
                             raise HTTPUnauthorized(
                                 description=
                                 'Username and password doesnot match')
                     else:
                         raise HTTPNotFound(
                             description='No user with username {}'.format(
                                 username))
                 else:
                     raise HTTPServiceUnavailable(
                         description='Data instances are not yet active')
             else:
                 raise HTTPPreconditionFailed(
                     description=
                     'Username and password are mandatory and must be valid for this request'
                 )
         except JSONDecodeError as err:
             print('Request body received', req.bounded_stream.read())
             print('Error while processing request', err)
             raise HTTPUnprocessableEntity(
                 description='Cannot parse the body from the request')
         except (HTTPPreconditionFailed, HTTPServiceUnavailable,
                 HTTPNotFound, HTTPUnauthorized, HTTPNotAcceptable) as err:
             raise err
         except Exception as e:
             print('Exception in signing in user', e)
             raise HTTPInternalServerError(
                 description='Something went wrong while creating user info'
             )
     elif auth_type == 'logout':
         try:
             api_key = req.get_header('Authorization')
             redis_cli = super().redis_client
             if redis_cli is not None:
                 access_info = redis_cli.hget('USERS_APIKEY', api_key)
                 access_info = json.loads(access_info)
                 access_token = access_info['access_token']
                 redis_cli.delete(access_token)
                 resp.status = HTTP_200
                 resp.body = json.dumps(
                     dict(status='Succcess',
                          message='Successfully signed out'))
             else:
                 raise HTTPServiceUnavailable(
                     description='Data instances are not yet active')
         except Exception as err:
             print('Exception while signing out', err)
             raise HTTPInternalServerError(
                 description='Something went wrong while signing out')
     else:
         raise HTTPBadRequest(description='The request is not valid')