Beispiel #1
0
def parseRecord(encodedRec, outManager):
    """Parse an individual record. Verifies that an object was able to be
    decoded from the input base64 encoded string and if so, hands this to the
    enhancer method"""
    try:
        record = json.loads(encodedRec['body'])
    except json.decoder.JSONDecodeError as jsonErr:
        logger.error('Invalid JSON block received')
        logger.error(jsonErr)
        raise DataError('Malformed JSON block received from SQS')
    except KeyError as err:
        logger.error('Missing body attribute in SQS message')
        logger.debug(err)
        raise DataError('Body object missing from SQS message')

    logger.info('Storing cover from {}'.format(record['url']))

    coverParser = CoverParse(record)
    coverParser.storeCover()

    outManager.putKinesis(
        {
            'originalURL': coverParser.remoteURL.lower(),
            'storedURL': coverParser.s3CoverURL
        },
        os.environ['DB_UPDATE_STREAM'],
        recType='cover')

    return coverParser.s3CoverURL
Beispiel #2
0
 def test_storeCover_success_exists(self, mocker, testRecord, mockRequest):
     mockKey = mocker.patch.object(CoverParse, 'createKey')
     testParser = CoverParse(testRecord)
     mockS3 = mocker.patch('lib.covers.s3Client')()
     mockS3.checkForFile.return_value = 'existingImageURL'
     testParser.storeCover()
     mockKey.assert_called_once()
     mockS3.checkForFile.assert_called_once()
     assert testParser.s3CoverURL == 'existingImageURL'
Beispiel #3
0
 def test_storeCover_success_new(self, mocker, testRecord, mockRequest):
     mockKey = mocker.patch.object(CoverParse, 'createKey')
     testParser = CoverParse(testRecord)
     mockS3 = mocker.patch('lib.covers.s3Client')()
     mockS3.checkForFile.return_value = None
     mockS3.storeNewFile.return_value = 'newImageURL'
     testParser.storeCover()
     mockKey.assert_called_once()
     mockS3.checkForFile.assert_called_once()
     mockS3.storeNewFile.assert_called_once_with('image_binary')
     assert testParser.s3CoverURL == 'newImageURL'
Beispiel #4
0
 def test_storeCover_hathi(self, mocker, testRecord, mockRequest):
     mockKey = mocker.patch.object(CoverParse, 'createKey')
     mockMime = mocker.patch.object(CoverParse, 'getMimeType')
     testRecord['url'] = testRecord['url'].replace('ebooks', 'hathitrust')
     mockAuth = mocker.patch.object(CoverParse,
                                    'createAuth',
                                    return_value='auth')
     testParser = CoverParse(testRecord)
     mockS3 = mocker.patch('lib.covers.s3Client')()
     mockS3.checkForFile.return_value = 'existingImageURL'
     testParser.storeCover()
     mockKey.assert_called_once()
     mockMime.assert_called_once()
     mockAuth.assert_called_once()
     mockS3.checkForFile.assert_called_once()
     assert testParser.s3CoverURL == 'existingImageURL'
Beispiel #5
0
 def test_storeCover_success_new(self, mocker, testRecord, mockRequest):
     resizeMocks = mocker.patch.multiple(CoverResizer,
                                         loadOriginal=DEFAULT,
                                         loadImageData=DEFAULT,
                                         getNewDimensions=DEFAULT,
                                         resizeCover=DEFAULT,
                                         getCoverInBytes=DEFAULT)
     resizeMocks['getCoverInBytes'].return_value = 'image_binary'
     mockKey = mocker.patch.object(CoverParse, 'createKey')
     mockMime = mocker.patch.object(CoverParse,
                                    'getMimeType',
                                    return_value='testMime')
     testParser = CoverParse(testRecord)
     mockS3 = mocker.patch('lib.covers.s3Client')()
     mockS3.checkForFile.return_value = None
     mockS3.storeNewFile.return_value = 'newImageURL'
     testParser.storeCover()
     mockKey.assert_called_once()
     mockMime.assert_called_once()
     mockS3.checkForFile.assert_called_once()
     mockS3.storeNewFile.assert_called_once_with('image_binary', 'testMime')
     assert testParser.s3CoverURL == 'newImageURL'
Beispiel #6
0
 def test_storeCover_failure(self, testRecord, mockRequest):
     mockRequest.status_code = 500
     testParser = CoverParse(testRecord)
     with pytest.raises(URLFetchError):
         testParser.storeCover()
Beispiel #7
0
 def test_storeCover_failure_timeout(self, mocker, testRecord):
     mockRequest = mocker.patch('lib.covers.requests')
     mockRequest.get.side_effect = ReadTimeout
     testParser = CoverParse(testRecord)
     with pytest.raises(URLFetchError):
         testParser.storeCover()