Skip to content
This repository has been archived by the owner on Feb 23, 2021. It is now read-only.

Commit

Permalink
Merge 174a68c into f745823
Browse files Browse the repository at this point in the history
  • Loading branch information
dylano committed Sep 22, 2018
2 parents f745823 + 174a68c commit 646240b
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 127 deletions.
87 changes: 30 additions & 57 deletions command/CommandController.js
Expand Up @@ -172,28 +172,28 @@ router.post('/articleservice', VerifyToken, async function(req, res) {
res.setHeader('Content-Type', 'application/json');

try {
// Set the version
let version = req.body.v ? req.body.v : 1;
console.log('Version is: ' + version);
let mobileMetadata;
if (req.body.article_id) {
// we have a pocket item. do we already have the audio file?
mobileMetadata = await audioHelper.getMobileFileMetadata(
req.body.article_id
);
logger.info('mobileMetadata: ' + JSON.stringify(mobileMetadata));
} else {
logger.info('error: missing article_id');
}
logger.debug('/articleservice version is: ' + version);

if (!mobileMetadata) {
throw 'No Metadata';
if (!req.body.article_id) {
logger.error('/articleservice error: missing article_id');
throw 'No article ID or metadata';
}

// if we didn't find it in the DB, create the audio file
if (!mobileMetadata.count) {
logger.info('Did not find the audio URL in DB: ' + req.body.article_id);
// Create the body as a local file.
let mobileMetadata = await audioHelper.getMobileFileMetadata(
req.body.article_id
);
if (mobileMetadata.count) {
// We have already processed this article
logger.debug(`Found file(s) in the database for ${req.body.article_id}`);
let response = await buildPocketResponseFromMetadata(
mobileMetadata,
version
);
res.status(200).send(JSON.stringify(response));
} else {
// Need to build the file(s)
logger.debug(`No file(s) in the database for ${req.body.article_id}`);
let article = await getPocketArticleTextFromUrl(req.body.url);
if (article) {
// Build the stitched file first
Expand All @@ -202,26 +202,29 @@ router.post('/articleservice', VerifyToken, async function(req, res) {
`${article.article}`,
voice
);

let introFile = await createAudioFileFromText(
buildIntro(article),
voice
);
let audioMetadata = await buildPocketAudio(introFile, articleFile);
// Add the correct voice:
audioMetadata['voice'] = voice;

logger.debug('Calling StoreMobileLocation: ' + audioMetadata.url);
await audioHelper.storeMobileLocation(
req.body.article_id,
article.lang,
voice,
audioMetadata
);
logger.debug('Before buildPocketResponse');
let response = buildPocketResponse(audioMetadata, version);

// Re-query the metadata for new file info
mobileMetadata = await audioHelper.getMobileFileMetadata(
req.body.article_id
);
let response = await buildPocketResponseFromMetadata(
mobileMetadata,
version
);

// Send it back to the mobile as quick as possible.
logger.info('POST article resp: ' + JSON.stringify(response));
res.status(200).send(JSON.stringify(response));

// Upload the individual parts for use by Alexa later & cleanup.
Expand All @@ -239,15 +242,9 @@ router.post('/articleservice', VerifyToken, async function(req, res) {
voice,
articleUrl
);
} else {
throw 'Unable to get article text.';
}
} else {
logger.debug('Found the file in the database');
let response = await buildPocketResponseFromMetadata(
mobileMetadata,
version
);
logger.info('POST article resp: ' + JSON.stringify(response));
res.status(200).send(JSON.stringify(response));
}
} catch (reason) {
logger.error('Error in /articleservice ' + reason);
Expand Down Expand Up @@ -913,30 +910,6 @@ async function buildPocketAudio(introFile, articleFile) {
return polly_tts.processPocketAudio(introFile, articleFile);
}

function buildPocketResponse(audioMetadata, version) {
logger.debug('Entering buildPocketResponse');
let response;
if (version == 2) {
let opus_metadata = {
format: 'opus',
url: audioMetadata.url.replace('.mp3', '.opus'),
status: 'processing',
voice: audioMetadata.voice,
sample_rate: 48000,
duration: audioMetadata.duration,
size: null
};

response = [audioMetadata, opus_metadata];
} else {
response = {
url: audioMetadata.url
};
}
logger.debug('JSON RESPONSE IS: ' + response);
return response;
}

async function buildPocketResponseFromMetadata(mmd, version) {
logger.debug('Calling buildPocketResponseFromMetatdata');
let v1Url;
Expand Down
46 changes: 27 additions & 19 deletions command/xcodeQueue.js
Expand Up @@ -18,28 +18,36 @@ AWS.config.update({ region: process.env.AWS_REGION });
var sqs = new AWS.SQS({ apiVersion: '2012-11-05' });

const xcodeQueue = {
useXcode: function() {
return !!process.env.SQS_QUEUE;
},

add: function(file) {
logger.debug('XCODE: filename: ' + file);
var jsonBody = {
filename: file,
targetCodec: 'opus 24'
};
if (this.useXcode()) {
logger.debug('XCODE: filename: ' + file);
var jsonBody = {
filename: file,
targetCodec: 'opus 24'
};

var params = {
MessageAttributes: {},
MessageGroupId: 'scout',
MessageDeduplicationId: uuidgen.generate(),
MessageBody: JSON.stringify(jsonBody),
QueueUrl: process.env.SQS_QUEUE
};
var params = {
MessageAttributes: {},
MessageGroupId: 'scout',
MessageDeduplicationId: uuidgen.generate(),
MessageBody: JSON.stringify(jsonBody),
QueueUrl: process.env.SQS_QUEUE
};

sqs.sendMessage(params, function(err, data) {
if (err) {
console.log('Error', err);
} else {
console.log('Success', data.MessageId);
}
});
sqs.sendMessage(params, function(err, data) {
if (err) {
logger.error('Error', err);
} else {
logger.debug('Success', data.MessageId);
}
});
} else {
logger.debug('No SQS queue defined, skipping XCode message.');
}
}
};

Expand Down
37 changes: 20 additions & 17 deletions data/database.js
Expand Up @@ -4,6 +4,7 @@ const Hostname = require('./models/Hostname');
const logger = require('../logger');
const uuidgen = require('node-uuid-generator');
const constants = require('../constants');
const xcodeQueue = require('../command/xcodeQueue');

class Database {
async processScoutUser(userid, access_token) {
Expand Down Expand Up @@ -208,23 +209,25 @@ class Database {
}
);
const mp3AudioFile = new AudioFiles(mp3FileInfo);
const mp3Promise = mp3AudioFile.save();

// save opus file data
let opusFileInfo = {};
Object.assign(
opusFileInfo,
commonFileInfo,
opusFileAttributes,
additionalOpusInfo,
{
uuid: uuidgen.generate(),
url: mp3FileUrl.replace('.mp3', '.opus')
}
);
const opusAudioFile = new AudioFiles(opusFileInfo);
const opusPromise = opusAudioFile.save();
await Promise.all([mp3Promise, opusPromise]);
const promiseArr = [mp3AudioFile.save()];

if (xcodeQueue.useXcode()) {
// save opus file data
let opusFileInfo = {};
Object.assign(
opusFileInfo,
commonFileInfo,
opusFileAttributes,
additionalOpusInfo,
{
uuid: uuidgen.generate(),
url: mp3FileUrl.replace('.mp3', '.opus')
}
);
const opusAudioFile = new AudioFiles(opusFileInfo);
promiseArr.push(opusAudioFile.save());
}
await Promise.all(promiseArr);
} catch (err) {
logger.error(`storeAudioFileLocation error: ${err}`);
}
Expand Down
43 changes: 28 additions & 15 deletions test/unit/CommandController.endpoints-test.js
Expand Up @@ -82,7 +82,10 @@ describe('CommandController - Endpoints', function() {
'getHostnameData',
sinon.fake(function() {
console.log('Calling fake getHostnameData');
return { publisher_name: 'publisher', favicon_url: 'favicon' };
return {
publisher_name: 'publisher',
favicon_url: 'http://favicon.url'
};
})
);
sinon.replace(
Expand Down Expand Up @@ -122,18 +125,20 @@ describe('CommandController - Endpoints', function() {
'checkFileExistence',
sinon.fake(function(url) {
console.log('Calling fake checkFileExistence');
return url == 'audio_file_url';
return url == 'http://audio_file.mp3';
})
);
sinon.replace(
AudioFileHelper.prototype,
'getMobileFileMetadata',
sinon.fake(function() {
console.log('Calling fake getMobileFileMetadata');
return {
fileUrl: 'audio_file_url',
duration: 300
};
return [
{
url: 'http://audio_file.mp3',
duration: 300
}
];
})
);
sinon.replace(
Expand All @@ -142,8 +147,8 @@ describe('CommandController - Endpoints', function() {
sinon.fake(function() {
console.log('Calling fake getMetaAudioLocation');
return {
intro: 'audio_file_url',
outro: 'audio_file_url'
intro: 'http://audio_file.mp3',
outro: 'http://audio_file.mp3'
};
})
);
Expand Down Expand Up @@ -182,15 +187,23 @@ describe('CommandController - Endpoints', function() {
'getSpeechSynthUrl',
sinon.fake(function() {
console.log('Calling fake getSpeechSynthUrl');
return 'audio_file_url';
return 'http://audio_file.mp3';
})
);
sinon.replace(
polly_tts,
'synthesizeSpeechFile',
sinon.fake(function() {
console.log('Calling fake synthesizeSpeechFile');
return 'audio_file_url';
return 'http://audio_file.mp3';
})
);
sinon.replace(
polly_tts,
'getFileSizeFromUrl',
sinon.fake(function() {
console.log('Calling fake getFileSizeFromUrl');
return 999;
})
);
sinon.replace(
Expand All @@ -200,7 +213,7 @@ describe('CommandController - Endpoints', function() {
console.log('Calling fake processPocketAudio');
return {
format: 'mp3',
url: 'audio_url',
url: 'http://audio_file.mp3',
status: 'available',
voice: 'Joanna',
sample_rate: '48000',
Expand All @@ -214,15 +227,15 @@ describe('CommandController - Endpoints', function() {
'uploadFile',
sinon.fake(function() {
console.log('Calling fake uploadFile');
return 'audio_file_url';
return 'http://audio_file.mp3';
})
);
sinon.replace(
polly_tts,
'postProcessPart',
sinon.fake(function() {
console.log('Calling fake uploadFile');
return 'audio_file_url';
return 'http://audio_file.mp3';
})
);
});
Expand Down Expand Up @@ -403,7 +416,7 @@ describe('CommandController - Endpoints', function() {
.end((err, res) => {
expect(res).have.status(200);
expect(res.body).be.a('object');
expect(res.body.url).be.equal('audio_file_url');
expect(res.body.url).be.equal('http://audio_file.mp3');
done();
});
});
Expand Down Expand Up @@ -593,7 +606,7 @@ describe('CommandController - Endpoints', function() {
delete userData.article_id;
});

it('should return metadata for the article', done => {
it.only('should return metadata for the article', done => {
chai
.request(app)
.post('/command/articleservice')
Expand Down
19 changes: 8 additions & 11 deletions test/unit/data/ArticleMetaAudio_firefox.json
@@ -1,21 +1,18 @@
{
"item_id": "2232154160",
"sort_id": 0,
"resolved_url":
"https://www.nytimes.com/2018/06/20/technology/personaltech/firefox-chrome-browser-privacy.html",
"resolved_url": "https://www.nytimes.com/2018/06/20/technology/personaltech/firefox-chrome-browser-privacy.html",
"title": "Firefox Is Back. It’s Time to Give It a Try.",
"author": "BRIAN X. CHEN",
"lengthMinutes": 8,
"length_minutes": 8,
"publisher": "publisher",
"icon_url": "favicon",
"imageURL":
"https://static01.nyt.com/images/2018/06/21/business/21Techfix/merlin_139815324_5d0f3563-0071-4a44-926f-3841b7e7fb44-facebookJumbo.jpg",
"image_url":
"https://static01.nyt.com/images/2018/06/21/business/21Techfix/merlin_139815324_5d0f3563-0071-4a44-926f-3841b7e7fb44-facebookJumbo.jpg",
"url": "audio_file_url",
"instructions_url": "audio_file_url",
"intro_url": "audio_file_url",
"outro_url": "audio_file_url",
"icon_url": "http://favicon.url",
"imageURL": "https://static01.nyt.com/images/2018/06/21/business/21Techfix/merlin_139815324_5d0f3563-0071-4a44-926f-3841b7e7fb44-facebookJumbo.jpg",
"image_url": "https://static01.nyt.com/images/2018/06/21/business/21Techfix/merlin_139815324_5d0f3563-0071-4a44-926f-3841b7e7fb44-facebookJumbo.jpg",
"url": "http://audio_file.mp3",
"instructions_url": "http://audio_file.mp3",
"intro_url": "http://audio_file.mp3",
"outro_url": "http://audio_file.mp3",
"offset_ms": 0
}

0 comments on commit 646240b

Please sign in to comment.