예제 #1
0
    def test_expired_key_before_expiration_time(self):
        #### Create mocked Bucket #####
        conn = boto3.resource('s3', region_name='us-east-1')
        conn.create_bucket(Bucket='mybucket')

        cache = Cache('mybucket', 'prefix1')
        cache.set('expiredkey2', 'stringtocache', 30)
        time.sleep(4)
        assert cache.get('expiredkey2') == 'stringtocache'
예제 #2
0
    def test_simple_cache(self):
        #### Create mocked Bucket #####
        conn = boto3.resource('s3', region_name='us-east-1')
        conn.create_bucket(Bucket='mybucket')

        cache = Cache('mybucket', 'prefix1')
        thing = {1: 2, 'a': 'b'}
        cache.set('key', thing)
        assert cache.get('key') == thing
예제 #3
0
    def test_space_in_key(self):
        #### Create mocked Bucket #####
        conn = boto3.resource('s3', region_name='us-east-1')
        conn.create_bucket(Bucket='mybucket')

        cache = Cache('mybucket', 'prefix1')
        thing = 'abcdefg'
        cache.set('ke y', thing)
        assert cache.get('ke y') == thing
예제 #4
0
def handler(event, context):

    # config values
    USERNAME = os.getenv('USERNAME')
    PASSWORD = os.getenv('PASSWORD')
    QUEUENAME = os.getenv('QUEUENAME')
    S3BUCKET = os.getenv('S3BUCKET')
    prefix = os.getenv('prefix')
    cacheprefix = prefix + 'cache/'

    cache = Cache(S3BUCKET, cacheprefix)

    sqs = boto3.client('sqs')

    print(event)
    print(context)
    print(USERNAME)
    print(PASSWORD)
    print(QUEUENAME)
    print(S3BUCKET)
    print(prefix)
    print(cache)

    # get list of recent arlo media objects from past 7 days
    arlo = Arlo(USERNAME, PASSWORD)
    today = (date.today() - timedelta(days=0)).strftime("%Y%m%d")
    seven_days_ago = (date.today() - timedelta(days=7)).strftime("%Y%m%d")
    library = arlo.GetLibrary(seven_days_ago, today)

    # only process library items that have arrived since our last processing time
    latest_saved_media_timestamp = cache.get('latest_saved_media_timestamp')
    library = [
        l for l in library if l['lastModified'] > latest_saved_media_timestamp
    ]

    if len(library) > 0:
        latest_saved_media_timestamp = max(
            [l['lastModified'] for l in library])

    cameras = arlo.GetDevices('camera')
    camera_id2name = {}
    for c in cameras:
        camera_id2name[c['deviceId']] = c['deviceName']

    for media in library:
        camera_name = camera_id2name.get(media['deviceId'], 'unknown_device')
        filename = camera_name + '_' + datetime.datetime.fromtimestamp(
            int(media['name']) // 1000).strftime('%Y-%m-%d %H-%M-%S')
        message = {
            "media": media,
            "metadata": {
                "s3bucket": S3BUCKET,
                "content_key": prefix + 'media/' + filename + '.mp4',
                "thumbnail_key":
                prefix + 'media/' + filename + '_thumbnail.jpg'
            }
        }
        print(message)

        response = sqs.send_message(QueueUrl=QUEUENAME,
                                    MessageBody=json.dumps(message))

    cache.set('latest_saved_media_timestamp', latest_saved_media_timestamp,
              3600 * 24 * 30)