示例#1
0
def edit_channel():
    user_id = utils.get_user_id(app)
    check_status, check_response = utils.request_params_check(
        app.current_request.json_body,
        ('channelName', 'channelWebhook', 'channelId'))
    if not check_status:
        return check_response
    channel_name = app.current_request.json_body['channelName']
    channel_webhook = app.current_request.json_body['channelWebhook']
    channel_id = app.current_request.json_body['channelId']

    channel = dynamodb_utils.get_channels(user_id, channel_id=channel_id)

    if (len(channel) == 0):
        return utils.make_response(400, {"message": "Channel NOT found!"})

    filtered_channel = dynamodb_utils.get_channels(user_id,
                                                   channel_name=channel_name)

    if (len(filtered_channel) == 1) and dynamo_json.unmarshall(
            filtered_channel[0])['CHANNEL_ID'] != channel_id:
        return utils.make_response(
            409, {
                "message":
                "Channel with the same name already exists! Please give unique channel name to help differentiate channels."
            })

    response_code, res = dynamodb_utils.update_channel(user_id, channel_id,
                                                       channel_name,
                                                       channel_webhook)
    if (response_code == 200):
        return utils.make_response(response_code, res)
    else:
        return utils.make_response(response_code, res)
示例#2
0
def lambda_handler(event, context):
    if 'Records' not in event:
        logger.info('No records found')
        return

    comprehend = boto3.client(service_name='comprehend',
                              region_name=AWS_REGION)

    results = []
    for record in event['Records']:
        if 'NewImage' not in record['dynamodb']:
            logger.info(record['dynamodb'])
            continue

        tweet = unmarshall(record['dynamodb']['NewImage'])
        text = tweet['text']
        result = comprehend.detect_sentiment(Text=text, LanguageCode='en')
        logger.info(f'Analyzed tweet: "{text}"')
        logger.info(f'Analysis results: {result}')
        tweet['sentiment'] = result['Sentiment']
        results.append(tweet)

    if not results:
        logger.info('No records analyzed')
        return

    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(ANALYZED_TWEETS_TABLE)
    with table.batch_writer() as batch:
        for analyzed_tweet in results:
            batch.put_item(Item=analyzed_tweet)
示例#3
0
def dynamo_json(json_document):
    """Convert between Dynamo JSON and standard JSON"""
    document = json.loads(json_document)
    try:
        converted = dj.unmarshall(document)
    except:
        converted = dj.marshall(document)
    click.echo(json.dumps(converted))
示例#4
0
def getChannels():
    user_id = utils.get_user_id(app)
    channels = dynamodb_utils.get_channels(user_id)
    response = []
    for channel in channels:
        response.append(
            utils.dict_underscore_to_camelcase(
                dynamo_json.unmarshall(channel)))
    if len(response) > 0:
        return utils.make_response(200, {"channels": response})
    else:
        return utils.make_response(404, {
            "message":
            "No channels found for the you. Please add a channel."
        })
示例#5
0
 def test_marshall_unmarshall_identity(self):
     data = {
         "menu": {
             "id": "file",
             "value": "File",
             "popup": {
                 "menuitem": [{
                     "value": "New",
                     "onclick": "CreateNewDoc()"
                 }, {
                     "value": "Open",
                     "onclick": "OpenDoc()"
                 }, {
                     "value": "Close",
                     "onclick": "CloseDoc()"
                 }]
             }
         }
     }
     self.assertEqual(unmarshall(marshall(data)), data)
示例#6
0
 def test_unmarshall_marshall_identity(self):
     data = {
         "Age": {
             "N": "8"
         },
         "Colors": {
             "L": [{
                 "S": "White"
             }, {
                 "S": "Brown"
             }, {
                 "S": "Black"
             }]
         },
         "Name": {
             "S": "Fido"
         },
         "Vaccinations": {
             "M": {
                 "Rabies": {
                     "L": [{
                         "S": "2009-03-17"
                     }, {
                         "S": "2011-09-21"
                     }, {
                         "S": "2014-07-08"
                     }]
                 },
                 "Distemper": {
                     "S": "2015-10-13"
                 }
             }
         },
         "Breed": {
             "S": "Beagle"
         },
         "AnimalType": {
             "S": "Dog"
         }
     }
     self.assertEqual(unmarshall(marshall(data)), data)
示例#7
0
def update_channel(user_id, channel_id, channel_name, channel_webhook):
    try:
        dynamo_res = DYNAMO_CLIENT.update_item(
            TableName=TABLE_NAME,
            Key={
                'COGNITO_USERNAME': {
                    'S': user_id
                },
                'CHANNEL_ID': {
                    'S': channel_id
                }
            },
            UpdateExpression=
            'SET CHANNEL_NAME = :channel_name, CHANNEL_WEBHOOK = :channel_webhook',
            ConditionExpression=
            'COGNITO_USERNAME= :user_id AND CHANNEL_ID= :channel_id',
            ExpressionAttributeValues={
                ':user_id': {
                    'S': user_id
                },
                ':channel_id': {
                    'S': channel_id
                },
                ':channel_name': {
                    'S': channel_name
                },
                ':channel_webhook': {
                    'S': channel_webhook
                }
            },
            ReturnValues='ALL_NEW')
        dynamo_res = dynamo_json.unmarshall(dynamo_res['Attributes'])
        dynamo_res = utils.dict_underscore_to_camelcase(dynamo_res)
        return 200, dynamo_res
    except DYNAMO_CLIENT.exceptions.ConditionalCheckFailedException:
        return 400, {'message': 'The channel does not exist.'}
示例#8
0
 def test_unmarshalls_json(self):
     self.assertEqual(unmarshall({"foo": {"S": "bar"}}), {"foo": "bar"})