From 9d61a6fb33d487420b2b1b25fb06ac3754884862 Mon Sep 17 00:00:00 2001 From: Dave Date: Mon, 4 Jun 2018 13:40:34 -0400 Subject: [PATCH 01/27] Added wayback-machine action --- functions/src/actions/wayback-machine.js | 172 +++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 functions/src/actions/wayback-machine.js diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js new file mode 100644 index 00000000..03cd7426 --- /dev/null +++ b/functions/src/actions/wayback-machine.js @@ -0,0 +1,172 @@ +const request = require('request'); +const traverse = require('traverse'); +const xml2js = require('xml2js'); +const selectors = require('../configurator/selectors'); +const query = require('../state/query'); +const availableSchemes = require('../strings').intents.waybackMachine; +const {debug, warning} = require('../utils/logger')('ia:actions:music-query'); + +/** + * Handle wayback query action + * - fill slots of wayback query + * + * @param app + */ +function handler (app) { + debug('Start wayback query handler'); + + const slots = query.getSlots(app); + debug('We have slots:', Object.keys(slots)); + if (slots) { + waybackEngine(slots); + } +} + +/** + * Wayback Engine + * - makes requests to Archive and Alexa Rankings for given URL + * + * @param url + * @returns earliestYear, latestYear, totalUniqueURLs, alexaWorldRank, alexaUSRank; + */ +function waybackEngine (slots) { + if (!slots.includes('wayback')) { + return debug('wayback action called by mistake'); + } + var slotURL = slots[1]; + debug('slotURL: ' + slotURL); + // var slotURL = 'cnn.com'; + var archiveFirstPartURL = 'http://web.archive.org/__wb/search/metadata?q='; + var queryURL = archiveFirstPartURL + slotURL; + + var alexaFirstPartURL = 'http://data.alexa.com/data?cli=10&url='; + var alexaQueryURL = alexaFirstPartURL + slotURL; + + // Variables to be returned + var earliestYear; + var latestYear; + var totalUniqueURLs = 0; + var alexaWorldRank; + var alexaUSRank; + + function getRequest (url) { + return new Promise(function (resolve, reject) { + request(url, function (error, response, body) { + if (!error && response.statusCode === 200) { + resolve(body); + } else { + reject(error); + } + }); + }); + } + + // REQUEST to get data from Archive and Alexa Rankings + getRequest(queryURL).then(function (body1) { + debug('ARCHIVE'); + // Parse data from request + var importedJSON = JSON.parse(body1); + + // Create array of capture years and then find earliest year + // and most recent year. + var yearsArray = Object.keys(importedJSON.captures); + earliestYear = yearsArray[0]; + latestYear = yearsArray[yearsArray.length - 1]; + + // Traverse URL category to find baseline of URL count + traverse(importedJSON.urls[earliestYear]).forEach(function (node) { + if (typeof node === 'number') { + totalUniqueURLs += node; + } + }); + debug('Beginning value for total unique urls: ' + totalUniqueURLs); + + traverse(importedJSON.new_urls).forEach(function (node) { + if (typeof node === 'number') { + totalUniqueURLs += node; + } + }); + + debug('Date of first archive: ' + earliestYear); + debug('Date of last archive: ' + latestYear); + debug('Total Unique URLs: ' + totalUniqueURLs); + + return getRequest(alexaQueryURL); + }).then(function (body2) { + debug('ALEXA RANKINGS'); + var parser = new xml2js.Parser(); + parser.parseString(body2, function (err, result) { + if (err) { + console.log("Uh-oh, the XML parser didn't work"); + } else { + var convertedJSON = JSON.parse(JSON.stringify(result)); + alexaWorldRank = convertedJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; + alexaUSRank = convertedJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; + + debug('Alexa World Ranking: ' + alexaWorldRank); + debug('Alexa US Ranking: ' + alexaUSRank); + } + }); + }); // End of request +} + +function populateSlots (app) { + let slotScheme = selectors.find(availableSchemes, query.getSlots(app)); + checkSlotScheme(slotScheme); + let newValues = fillSlots(app, slotScheme); + // applyDefaultSlots(app, slotScheme.defaults); + + // new values could change actual slot scheme + const newScheme = selectors.find(availableSchemes, query.getSlots(app)); + if (slotScheme !== newScheme) { + slotScheme = newScheme; + // update slots for new scheme + checkSlotScheme(slotScheme); + newValues = Object.assign({}, newValues, fillSlots(app, slotScheme)); + // applyDefaultSlots(app, slotScheme.defaults); + } + return {slotScheme, newValues}; +} + +/** + * + * @param slotScheme + */ +function checkSlotScheme (slotScheme) { + if (!slotScheme) { + throw new Error('There are no valid slot scheme. Need at least default'); + } + + if (slotScheme && slotScheme.name) { + debug(`we are going with "${slotScheme.name}" slot scheme`); + } +} +/** + * Put all received values to slots + * and return list of new values + * + * @param app + * @returns {{}} + */ +function fillSlots (app, slotScheme) { + return slotScheme.slots + .reduce((newValues, slotName) => { + let value; + if (app.getArgument) { + // @deprecated + value = app.getArgument(slotName); + } else { + value = app.params.getByName(slotName); + } + if (value) { + query.setSlot(app, slotName, value); + newValues[slotName] = value; + } + return newValues; + }, {}); +} + +module.exports = { + handler, + populateSlots, +}; From d10b1ea67fd7e766562c54588f7ab7916559bbb0 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Wed, 13 Jun 2018 16:52:31 -0400 Subject: [PATCH 02/27] Further modifications to Wayback code. Fixed slots --- functions/src/actions/wayback-machine.js | 171 +++++++---------------- functions/src/strings.js | 3 + 2 files changed, 55 insertions(+), 119 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 03cd7426..0691fbea 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -1,68 +1,45 @@ const request = require('request'); const traverse = require('traverse'); const xml2js = require('xml2js'); -const selectors = require('../configurator/selectors'); -const query = require('../state/query'); -const availableSchemes = require('../strings').intents.waybackMachine; -const {debug, warning} = require('../utils/logger')('ia:actions:music-query'); +const dialog = require('../dialog'); +const mustache = require('mustache'); +const waybackStrings = require('../strings').intents.wayback; +const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); /** * Handle wayback query action * - fill slots of wayback query + * - perform data REQUEST + * - construct response speech for action * * @param app */ function handler (app) { - debug('Start wayback query handler'); - - const slots = query.getSlots(app); - debug('We have slots:', Object.keys(slots)); - if (slots) { - waybackEngine(slots); - } -} - -/** - * Wayback Engine - * - makes requests to Archive and Alexa Rankings for given URL - * - * @param url - * @returns earliestYear, latestYear, totalUniqueURLs, alexaWorldRank, alexaUSRank; - */ -function waybackEngine (slots) { - if (!slots.includes('wayback')) { - return debug('wayback action called by mistake'); + // Check parameter for Wayback qualifier + let wayback = app.params.getByName('wayback'); + if (wayback.includes('wayback') === false) { + debug('wayback action called by mistake'); } - var slotURL = slots[1]; - debug('slotURL: ' + slotURL); - // var slotURL = 'cnn.com'; - var archiveFirstPartURL = 'http://web.archive.org/__wb/search/metadata?q='; - var queryURL = archiveFirstPartURL + slotURL; - - var alexaFirstPartURL = 'http://data.alexa.com/data?cli=10&url='; - var alexaQueryURL = alexaFirstPartURL + slotURL; - // Variables to be returned - var earliestYear; - var latestYear; - var totalUniqueURLs = 0; - var alexaWorldRank; - var alexaUSRank; + var url = app.params.getByName('url'); + + // Create wayback object with properties to be filled in request + var waybackObject = { + url: '', + earliestYear: 0, + latestYear: 0, + totalUniqueURLs: 0, + alexaWorldRank: 0, + alexaUSRank: 0, + speech: 'default speech' +}; - function getRequest (url) { - return new Promise(function (resolve, reject) { - request(url, function (error, response, body) { - if (!error && response.statusCode === 200) { - resolve(body); - } else { - reject(error); - } - }); - }); - } + waybackObject.url = url; + var archiveQueryURL = 'http://web.archive.org/__wb/search/metadata?q=' + url; + var alexaQueryURL = 'http://data.alexa.com/data?cli=10&url=' + url; // REQUEST to get data from Archive and Alexa Rankings - getRequest(queryURL).then(function (body1) { + getRequest(archiveQueryURL).then(function (body1) { debug('ARCHIVE'); // Parse data from request var importedJSON = JSON.parse(body1); @@ -70,103 +47,59 @@ function waybackEngine (slots) { // Create array of capture years and then find earliest year // and most recent year. var yearsArray = Object.keys(importedJSON.captures); - earliestYear = yearsArray[0]; - latestYear = yearsArray[yearsArray.length - 1]; + waybackObject.earliestYear = yearsArray[0]; + waybackObject.latestYear = yearsArray[yearsArray.length - 1]; // Traverse URL category to find baseline of URL count - traverse(importedJSON.urls[earliestYear]).forEach(function (node) { + traverse(importedJSON.urls[waybackObject.earliestYear]).forEach(function (node) { if (typeof node === 'number') { - totalUniqueURLs += node; + waybackObject.totalUniqueURLs += node; } }); - debug('Beginning value for total unique urls: ' + totalUniqueURLs); + // debug('Beginning value for total unique urls: ' + totalUniqueURLs); traverse(importedJSON.new_urls).forEach(function (node) { if (typeof node === 'number') { - totalUniqueURLs += node; + waybackObject.totalUniqueURLs += node; } }); - debug('Date of first archive: ' + earliestYear); - debug('Date of last archive: ' + latestYear); - debug('Total Unique URLs: ' + totalUniqueURLs); - + // debug('Date of first archive: ' + earliestYear); + // debug('Date of last archive: ' + latestYear); + // debug('Total Unique URLs: ' + totalUniqueURLs); return getRequest(alexaQueryURL); }).then(function (body2) { debug('ALEXA RANKINGS'); var parser = new xml2js.Parser(); parser.parseString(body2, function (err, result) { if (err) { - console.log("Uh-oh, the XML parser didn't work"); + debug('Uh-oh, the XML parser didn\'t work'); } else { var convertedJSON = JSON.parse(JSON.stringify(result)); - alexaWorldRank = convertedJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; - alexaUSRank = convertedJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; + waybackObject.alexaWorldRank = convertedJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; + waybackObject.alexaUSRank = convertedJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; - debug('Alexa World Ranking: ' + alexaWorldRank); - debug('Alexa US Ranking: ' + alexaUSRank); + waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); + //debug('speech:' + waybackObject.speech); + dialog.close(app, waybackObject); } }); - }); // End of request -} + }); +} // End of handler -function populateSlots (app) { - let slotScheme = selectors.find(availableSchemes, query.getSlots(app)); - checkSlotScheme(slotScheme); - let newValues = fillSlots(app, slotScheme); - // applyDefaultSlots(app, slotScheme.defaults); - // new values could change actual slot scheme - const newScheme = selectors.find(availableSchemes, query.getSlots(app)); - if (slotScheme !== newScheme) { - slotScheme = newScheme; - // update slots for new scheme - checkSlotScheme(slotScheme); - newValues = Object.assign({}, newValues, fillSlots(app, slotScheme)); - // applyDefaultSlots(app, slotScheme.defaults); - } - return {slotScheme, newValues}; -} - -/** - * - * @param slotScheme - */ -function checkSlotScheme (slotScheme) { - if (!slotScheme) { - throw new Error('There are no valid slot scheme. Need at least default'); - } - - if (slotScheme && slotScheme.name) { - debug(`we are going with "${slotScheme.name}" slot scheme`); - } -} -/** - * Put all received values to slots - * and return list of new values - * - * @param app - * @returns {{}} - */ -function fillSlots (app, slotScheme) { - return slotScheme.slots - .reduce((newValues, slotName) => { - let value; - if (app.getArgument) { - // @deprecated - value = app.getArgument(slotName); +function getRequest (url) { + return new Promise(function (resolve, reject) { + request(url, function (error, response, body) { + if (!error && response.statusCode === 200) { + resolve(body); } else { - value = app.params.getByName(slotName); - } - if (value) { - query.setSlot(app, slotName, value); - newValues[slotName] = value; + reject(error); } - return newValues; - }, {}); + }); + }); } module.exports = { - handler, - populateSlots, + handler }; diff --git a/functions/src/strings.js b/functions/src/strings.js index a74629d6..cfc738ad 100644 --- a/functions/src/strings.js +++ b/functions/src/strings.js @@ -444,6 +444,9 @@ module.exports = { speech: 'Version is {{version}}.', }, + wayback: { + speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity and {{alexaUSRank}} in the United States.', + }, welcome: { acknowledges: [ 'Welcome to music at the Internet Archive.' From 92c7c8e3fa414346b808d6ca003f398141127f65 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Thu, 14 Jun 2018 08:58:07 -0400 Subject: [PATCH 03/27] Run eslint and local testing on wayback code --- functions/src/actions/wayback-machine.js | 28 ++++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 0691fbea..43903888 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -1,7 +1,7 @@ const request = require('request'); const traverse = require('traverse'); const xml2js = require('xml2js'); -const dialog = require('../dialog'); +// const dialog = require('../dialog'); const mustache = require('mustache'); const waybackStrings = require('../strings').intents.wayback; const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); @@ -25,14 +25,14 @@ function handler (app) { // Create wayback object with properties to be filled in request var waybackObject = { - url: '', - earliestYear: 0, - latestYear: 0, - totalUniqueURLs: 0, - alexaWorldRank: 0, - alexaUSRank: 0, - speech: 'default speech' -}; + url: '', + earliestYear: 0, + latestYear: 0, + totalUniqueURLs: 0, + alexaWorldRank: 0, + alexaUSRank: 0, + speech: 'default speech' + }; waybackObject.url = url; var archiveQueryURL = 'http://web.archive.org/__wb/search/metadata?q=' + url; @@ -80,14 +80,14 @@ function handler (app) { waybackObject.alexaUSRank = convertedJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - //debug('speech:' + waybackObject.speech); - dialog.close(app, waybackObject); + // debug('speech:' + waybackObject.speech); + // dialog.close(app, waybackObject); } }); - }); -} // End of handler - + }); // End of request + // dialog.close(app, ) +} // End of handler function getRequest (url) { return new Promise(function (resolve, reject) { request(url, function (error, response, body) { From c2b2cae413806985031bf4bf22abc47c2d3aa380 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Fri, 15 Jun 2018 20:00:24 -0400 Subject: [PATCH 04/27] Rewrote wayback machine to return promise --- functions/src/actions/wayback-machine.js | 120 ++++++++++++++++++++++- functions/src/strings.js | 2 +- 2 files changed, 118 insertions(+), 4 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 43903888..09aa8018 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -1,11 +1,122 @@ const request = require('request'); const traverse = require('traverse'); const xml2js = require('xml2js'); -// const dialog = require('../dialog'); +const dialog = require('../dialog'); const mustache = require('mustache'); const waybackStrings = require('../strings').intents.wayback; const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); +/** + * Handle wayback query action + * - fill slots of wayback query + * - perform data requests to archive and alexa rankings + * - construct response speech for action + * + * @param app + */ +function handler (app) { + + // Check parameter for Wayback qualifier + let wayback = app.params.getByName('wayback'); + if (wayback.includes('wayback') === false) { + debug('wayback action called by mistake'); + } + + //Get url parameter + var url = app.params.getByName('url'); + + var waybackObject = { + url: '', + earliestYear: 0, + latestYear: 0, + totalUniqueURLs: 0, + alexaWorldRank: 0, + alexaUSRank: 0, + speech: 'default speech' + }; + + waybackObject.url = url; + var archiveQueryURL = 'http://web.archive.org/__wb/search/metadata?q=' + url; + var alexaQueryURL = 'http://data.alexa.com/data?cli=10&url=' + url; + + +return Promise.all([getRequest(archiveQueryURL), getRequest(alexaQueryURL)]) + .then(function(allData) { + // All data available here in the order it was called. + + // Parse data from archive request + var archiveJSON = JSON.parse(allData[0]); + archiveEngine(archiveJSON, waybackObject); + + // Parse data from alexa request + var alexaJSON; + var XMLparser = new xml2js.Parser(); + XMLparser.parseString(allData[1], function (err, result) { + if (err) { + debug('Uh-oh, the XML parser didn\'t work'); + } else { + alexaJSON = JSON.parse(JSON.stringify(result)); + } + }); + alexaEngine(alexaJSON, waybackObject); + + /* + debug('ARCHIVE'); + debug('Date of first archive: ' + waybackObject.earliestYear); + debug('Date of last archive: ' + waybackObject.latestYear); + debug('Total Unique URLs: ' + waybackObject.totalUniqueURLs); + debug('ALEXA'); + debug('Alexa World Rank: ' + waybackObject.alexaWorldRank); + debug('Alexa US Rank: ' + waybackObject.alexaUSRank); + */ + + // Construct response dialog for action + waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); + //debug('speech:' + waybackObject.speech); + + dialog.close(app, waybackObject); + }); +} //End of handler + +function getRequest (url) { + return new Promise(function (resolve, reject) { + request(url, function (error, response, body) { + if (!error && response.statusCode === 200) { + resolve(body); + } else { + reject(error); + } + }); + }); +} + +function archiveEngine(archiveJSON, waybackObject) { + // Create array of capture years and then find earliest year + // and most recent year. + var yearsArray = Object.keys(archiveJSON.captures); + waybackObject.earliestYear = yearsArray[0]; + waybackObject.latestYear = yearsArray[yearsArray.length - 1]; + + // Traverse URL category to find baseline of URL count + traverse(archiveJSON.urls[waybackObject.earliestYear]).forEach(function (node) { + if (typeof node === 'number') { + waybackObject.totalUniqueURLs += node; + } + }); + // debug('Beginning value for total unique urls: ' + totalUniqueURLs); + + traverse(archiveJSON.new_urls).forEach(function (node) { + if (typeof node === 'number') { + waybackObject.totalUniqueURLs += node; + } + }); +} + +function alexaEngine(alexaJSON, waybackObject) { + waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; + waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; +} + /** * Handle wayback query action * - fill slots of wayback query @@ -14,6 +125,7 @@ const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); * * @param app */ + /* function handler (app) { // Check parameter for Wayback qualifier let wayback = app.params.getByName('wayback'); @@ -80,13 +192,14 @@ function handler (app) { waybackObject.alexaUSRank = convertedJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - // debug('speech:' + waybackObject.speech); + debug('speech:' + waybackObject.speech); // dialog.close(app, waybackObject); } }); }); // End of request - // dialog.close(app, ) + // Give wayback object to dialog function + //dialog.close(app, waybackObject); } // End of handler function getRequest (url) { return new Promise(function (resolve, reject) { @@ -99,6 +212,7 @@ function getRequest (url) { }); }); } +*/ module.exports = { handler diff --git a/functions/src/strings.js b/functions/src/strings.js index cfc738ad..2a335af8 100644 --- a/functions/src/strings.js +++ b/functions/src/strings.js @@ -445,7 +445,7 @@ module.exports = { }, wayback: { - speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity and {{alexaUSRank}} in the United States.', + speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity and {{alexaUSRank}} in the United States.', }, welcome: { acknowledges: [ From 9bc0f6942cd4b1fb40ac13890e64bbd28bfd6d90 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 19 Jun 2018 16:58:50 -0400 Subject: [PATCH 05/27] Created fallback for speech if country rank missing from alexa ranking parsing --- functions/src/actions/wayback-machine.js | 169 ++++++----------------- functions/src/strings.js | 2 +- 2 files changed, 43 insertions(+), 128 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 09aa8018..20283ed7 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -15,16 +15,16 @@ const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); * @param app */ function handler (app) { - // Check parameter for Wayback qualifier let wayback = app.params.getByName('wayback'); if (wayback.includes('wayback') === false) { debug('wayback action called by mistake'); } - //Get url parameter + // Get url parameter var url = app.params.getByName('url'); + // Create wayback object var waybackObject = { url: '', earliestYear: 0, @@ -39,28 +39,27 @@ function handler (app) { var archiveQueryURL = 'http://web.archive.org/__wb/search/metadata?q=' + url; var alexaQueryURL = 'http://data.alexa.com/data?cli=10&url=' + url; - -return Promise.all([getRequest(archiveQueryURL), getRequest(alexaQueryURL)]) - .then(function(allData) { - // All data available here in the order it was called. - - // Parse data from archive request - var archiveJSON = JSON.parse(allData[0]); - archiveEngine(archiveJSON, waybackObject); - - // Parse data from alexa request - var alexaJSON; - var XMLparser = new xml2js.Parser(); - XMLparser.parseString(allData[1], function (err, result) { - if (err) { - debug('Uh-oh, the XML parser didn\'t work'); - } else { - alexaJSON = JSON.parse(JSON.stringify(result)); - } - }); - alexaEngine(alexaJSON, waybackObject); - - /* + return Promise.all([getRequest(archiveQueryURL), getRequest(alexaQueryURL)]) + .then(function (allData) { + // All data available here in the order it was called. + + // Parse data from archive request + var archiveJSON = JSON.parse(allData[0]); + archiveEngine(archiveJSON, waybackObject); + + // Parse data from alexa request + var alexaJSON; + var XMLparser = new xml2js.Parser(); + XMLparser.parseString(allData[1], function (err, result) { + if (err) { + debug('Uh-oh, the XML parser didn\'t work'); + } else { + alexaJSON = JSON.parse(JSON.stringify(result)); + } + }); + alexaEngine(alexaJSON, waybackObject); + + /* debug('ARCHIVE'); debug('Date of first archive: ' + waybackObject.earliestYear); debug('Date of last archive: ' + waybackObject.latestYear); @@ -70,13 +69,19 @@ return Promise.all([getRequest(archiveQueryURL), getRequest(alexaQueryURL)]) debug('Alexa US Rank: ' + waybackObject.alexaUSRank); */ - // Construct response dialog for action + // Construct response dialog for action + if (waybackObject.alexaUSRank !== 0) { + waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); + waybackObject.speech += ' and ' + waybackObject.alexaUSRank + ' in the United States'; + } else { waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - //debug('speech:' + waybackObject.speech); + waybackObject.speech += '.'; + } + // debug('speech:' + waybackObject.speech); - dialog.close(app, waybackObject); + dialog.close(app, waybackObject); }); -} //End of handler +} // End of handler function getRequest (url) { return new Promise(function (resolve, reject) { @@ -90,7 +95,7 @@ function getRequest (url) { }); } -function archiveEngine(archiveJSON, waybackObject) { +function archiveEngine (archiveJSON, waybackObject) { // Create array of capture years and then find earliest year // and most recent year. var yearsArray = Object.keys(archiveJSON.captures); @@ -112,107 +117,17 @@ function archiveEngine(archiveJSON, waybackObject) { }); } -function alexaEngine(alexaJSON, waybackObject) { +function alexaEngine (alexaJSON, waybackObject) { waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; - waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; -} - -/** - * Handle wayback query action - * - fill slots of wayback query - * - perform data REQUEST - * - construct response speech for action - * - * @param app - */ - /* -function handler (app) { - // Check parameter for Wayback qualifier - let wayback = app.params.getByName('wayback'); - if (wayback.includes('wayback') === false) { - debug('wayback action called by mistake'); + var countryID = alexaJSON['ALEXA']['SD']; + debug('country id: ' + countryID); + if (countryID.includes('COUNTRY')) { + debug('country exists'); + waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; + } else { + debug('country doesn\'t exist'); } - - var url = app.params.getByName('url'); - - // Create wayback object with properties to be filled in request - var waybackObject = { - url: '', - earliestYear: 0, - latestYear: 0, - totalUniqueURLs: 0, - alexaWorldRank: 0, - alexaUSRank: 0, - speech: 'default speech' - }; - - waybackObject.url = url; - var archiveQueryURL = 'http://web.archive.org/__wb/search/metadata?q=' + url; - var alexaQueryURL = 'http://data.alexa.com/data?cli=10&url=' + url; - - // REQUEST to get data from Archive and Alexa Rankings - getRequest(archiveQueryURL).then(function (body1) { - debug('ARCHIVE'); - // Parse data from request - var importedJSON = JSON.parse(body1); - - // Create array of capture years and then find earliest year - // and most recent year. - var yearsArray = Object.keys(importedJSON.captures); - waybackObject.earliestYear = yearsArray[0]; - waybackObject.latestYear = yearsArray[yearsArray.length - 1]; - - // Traverse URL category to find baseline of URL count - traverse(importedJSON.urls[waybackObject.earliestYear]).forEach(function (node) { - if (typeof node === 'number') { - waybackObject.totalUniqueURLs += node; - } - }); - // debug('Beginning value for total unique urls: ' + totalUniqueURLs); - - traverse(importedJSON.new_urls).forEach(function (node) { - if (typeof node === 'number') { - waybackObject.totalUniqueURLs += node; - } - }); - - // debug('Date of first archive: ' + earliestYear); - // debug('Date of last archive: ' + latestYear); - // debug('Total Unique URLs: ' + totalUniqueURLs); - return getRequest(alexaQueryURL); - }).then(function (body2) { - debug('ALEXA RANKINGS'); - var parser = new xml2js.Parser(); - parser.parseString(body2, function (err, result) { - if (err) { - debug('Uh-oh, the XML parser didn\'t work'); - } else { - var convertedJSON = JSON.parse(JSON.stringify(result)); - waybackObject.alexaWorldRank = convertedJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; - waybackObject.alexaUSRank = convertedJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; - - waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - debug('speech:' + waybackObject.speech); - // dialog.close(app, waybackObject); - } - }); - }); // End of request - - // Give wayback object to dialog function - //dialog.close(app, waybackObject); -} // End of handler -function getRequest (url) { - return new Promise(function (resolve, reject) { - request(url, function (error, response, body) { - if (!error && response.statusCode === 200) { - resolve(body); - } else { - reject(error); - } - }); - }); } -*/ module.exports = { handler diff --git a/functions/src/strings.js b/functions/src/strings.js index 2a335af8..35b7ebba 100644 --- a/functions/src/strings.js +++ b/functions/src/strings.js @@ -445,7 +445,7 @@ module.exports = { }, wayback: { - speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity and {{alexaUSRank}} in the United States.', + speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity', }, welcome: { acknowledges: [ From efc09fe0645923090e17ea0a9a75fda61068f931 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 19 Jun 2018 19:00:13 -0400 Subject: [PATCH 06/27] Reverted strings.js back to master to pass tests --- functions/src/strings.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/functions/src/strings.js b/functions/src/strings.js index 35b7ebba..c498a91e 100644 --- a/functions/src/strings.js +++ b/functions/src/strings.js @@ -132,7 +132,7 @@ module.exports = { `Let's play music from {{creator}}.`, `Let's play music from {{coverage}}.`, `Let's dive into {{year}}.`, - `I have {{total}} albums from {{year}}. Let's dive into them.`, + `I have {{total}} albums from {{year}}. Let's dive into it.`, ], }, @@ -144,7 +144,7 @@ module.exports = { speech: [ `I don’t have anything for {{year}}. Try {{suggestions.0}}, for example.`, `I don't have {{creator}} albums for {{year}}. Try {{suggestions.0}}, for example.`, - `I don't have any albums for {{year}}. Try {{suggestions.0}}, for example.`, + `I don't have any albums of {{year}}. Try {{suggestions.0}}, for example.`, `I don't have that. Try {{suggestions.0}}, for example.`, ], }, @@ -189,7 +189,7 @@ module.exports = { * to check our undestanding */ acknowledges: [ - 'Ok! Lets go with the artist {{creator}}!', + 'Ok! Lets go with {{creator}} performer!', `You've selected {{alias.collectionId}}.`, ], @@ -205,7 +205,7 @@ module.exports = { * slots which we need for fulfillement */ speech: [ - 'What genre of music would you like to listen to? Please select a genre like {{short-options.suggestions}}?', + 'What genre of music would you like to listen to? Please select a topic like {{short-options.suggestions}}?', ], /** @@ -270,7 +270,7 @@ module.exports = { acknowledges: [ '{{coverage}} - good place!', '{{coverage}} {{year}} - great choice!', - '{{year}} - it was an excellent year!', + '{{year}} - it was excellent year!', 'Ok! Lets go with {{creator}}!', `You've selected {{alias.collectionId}}.`, ], @@ -320,7 +320,7 @@ module.exports = { */ repair: { speech: [ - `We don't have concerts by {{creator}}. Maybe you would like to listen to {{short-options.suggestions}}?`, + `We don't have concerts of {{creator}}. Maybe you would like to listent {{short-options.suggestions}}?`, ], }, }, { @@ -361,7 +361,7 @@ module.exports = { ], speech: [ - 'Ok, {{creator}} has played in {{coverage}} sometime in {{years-interval.suggestions}}. Do you have a particular year in mind?', + 'Ok, {{creator}} has played in {{coverage}} sometime {{years-interval.suggestions}}. Do you have a particular year in mind?', ], /** @@ -425,7 +425,7 @@ module.exports = { titleOption: { false: { - speech: `Excellent! I'll be saying the title to each song.`, + speech: `Excellent! I'll be saying title to each song.`, }, true: { speech: `Ok, muting song titles.`, @@ -444,9 +444,6 @@ module.exports = { speech: 'Version is {{version}}.', }, - wayback: { - speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity', - }, welcome: { acknowledges: [ 'Welcome to music at the Internet Archive.' @@ -468,7 +465,7 @@ module.exports = { events: { playlistIsEnded: { - speech: 'Playlist has ended. Would you like to listen to something more?', + speech: 'Playlist is ended. Do you want to listen something more?', } }, From 5514439504cfbe8facc9137ca7c1900a15769b8d Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 19 Jun 2018 19:05:04 -0400 Subject: [PATCH 07/27] Added Wayback dialog to strings.js --- functions/src/strings.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/functions/src/strings.js b/functions/src/strings.js index c498a91e..603745c8 100644 --- a/functions/src/strings.js +++ b/functions/src/strings.js @@ -444,6 +444,10 @@ module.exports = { speech: 'Version is {{version}}.', }, + wayback: { + speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity', + }, + welcome: { acknowledges: [ 'Welcome to music at the Internet Archive.' From d4184a2db2231b9b58d5f81cdbce06f33dc18d64 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Mon, 25 Jun 2018 14:38:26 -0400 Subject: [PATCH 08/27] Wayback machine revisions for code review + changes to config.js file --- functions/src/actions/wayback-machine.js | 127 +++++++++++------------ functions/src/config.js | 5 + 2 files changed, 64 insertions(+), 68 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 20283ed7..ed04694a 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -1,10 +1,15 @@ -const request = require('request'); -const traverse = require('traverse'); +// Third party imports +const _ = require('lodash'); +const axios = require('axios'); +const mustache = require('mustache'); const xml2js = require('xml2js'); + +// Local imports +const config = require('../config'); +const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); const dialog = require('../dialog'); -const mustache = require('mustache'); +const endpointProcessor = require('../network/endpoint-processor'); const waybackStrings = require('../strings').intents.wayback; -const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); /** * Handle wayback query action @@ -15,120 +20,106 @@ const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); * @param app */ function handler (app) { - // Check parameter for Wayback qualifier - let wayback = app.params.getByName('wayback'); - if (wayback.includes('wayback') === false) { - debug('wayback action called by mistake'); - } - - // Get url parameter - var url = app.params.getByName('url'); - // Create wayback object - var waybackObject = { + const waybackObject = { url: '', earliestYear: 0, latestYear: 0, totalUniqueURLs: 0, alexaWorldRank: 0, alexaUSRank: 0, - speech: 'default speech' + speech: waybackStrings.default, }; - waybackObject.url = url; - var archiveQueryURL = 'http://web.archive.org/__wb/search/metadata?q=' + url; - var alexaQueryURL = 'http://data.alexa.com/data?cli=10&url=' + url; + // Check parameter for Wayback qualifier + let wayback = app.params.getByName('wayback'); + if (!wayback.includes('wayback')) { + debug('wayback action called by mistake'); + dialog.ask(app, waybackObject); + } + + // Get url parameter and make url queries + waybackObject.url = app.params.getByName('url'); + const archiveQueryURL = endpointProcessor.preprocess( + config.wayback.ARCHIVE, app, waybackObject + ); + const alexaQueryURL = endpointProcessor.preprocess( + config.wayback.ALEXA, app, waybackObject + ); - return Promise.all([getRequest(archiveQueryURL), getRequest(alexaQueryURL)]) + return Promise.all([axios.get(archiveQueryURL), axios.get(alexaQueryURL)]) .then(function (allData) { // All data available here in the order it was called. // Parse data from archive request - var archiveJSON = JSON.parse(allData[0]); + let archiveJSON = allData[0].data; archiveEngine(archiveJSON, waybackObject); // Parse data from alexa request - var alexaJSON; - var XMLparser = new xml2js.Parser(); - XMLparser.parseString(allData[1], function (err, result) { + let alexaJSON; + let XMLparser = new xml2js.Parser(); + XMLparser.parseString(allData[1].data, function (err, result) { if (err) { - debug('Uh-oh, the XML parser didn\'t work'); + debug('The XML parser didn\'t work'); + waybackObject.speech = waybackStrings.error; + dialog.ask(app, waybackObject); } else { alexaJSON = JSON.parse(JSON.stringify(result)); } }); alexaEngine(alexaJSON, waybackObject); - /* - debug('ARCHIVE'); - debug('Date of first archive: ' + waybackObject.earliestYear); - debug('Date of last archive: ' + waybackObject.latestYear); - debug('Total Unique URLs: ' + waybackObject.totalUniqueURLs); - debug('ALEXA'); - debug('Alexa World Rank: ' + waybackObject.alexaWorldRank); - debug('Alexa US Rank: ' + waybackObject.alexaUSRank); - */ - // Construct response dialog for action if (waybackObject.alexaUSRank !== 0) { waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - waybackObject.speech += ' and ' + waybackObject.alexaUSRank + ' in the United States'; + waybackObject.speech += mustache.render(waybackStrings.additionalSpeech, waybackObject); } else { waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); waybackObject.speech += '.'; } - // debug('speech:' + waybackObject.speech); dialog.close(app, waybackObject); }); } // End of handler -function getRequest (url) { - return new Promise(function (resolve, reject) { - request(url, function (error, response, body) { - if (!error && response.statusCode === 200) { - resolve(body); - } else { - reject(error); - } - }); - }); -} - function archiveEngine (archiveJSON, waybackObject) { // Create array of capture years and then find earliest year // and most recent year. - var yearsArray = Object.keys(archiveJSON.captures); + let yearsArray = Object.keys(archiveJSON.captures); waybackObject.earliestYear = yearsArray[0]; waybackObject.latestYear = yearsArray[yearsArray.length - 1]; - // Traverse URL category to find baseline of URL count - traverse(archiveJSON.urls[waybackObject.earliestYear]).forEach(function (node) { - if (typeof node === 'number') { - waybackObject.totalUniqueURLs += node; - } - }); - // debug('Beginning value for total unique urls: ' + totalUniqueURLs); + // Traverse URL category + const traverse = obj => { + _.forOwn(obj, (val, key) => { + if (_.isArray(val)) { + val.forEach(el => { + traverse(el); + }); + } else if (_.isObject(val)) { + traverse(val); + } else { + waybackObject.totalUniqueURLs += val; + } + }); + }; - traverse(archiveJSON.new_urls).forEach(function (node) { - if (typeof node === 'number') { - waybackObject.totalUniqueURLs += node; - } - }); + // Find baseline of URL count + traverse(archiveJSON.urls[waybackObject.earliestYear]); + // Find final count of unique urls + traverse(archiveJSON.new_urls); } function alexaEngine (alexaJSON, waybackObject) { waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; - var countryID = alexaJSON['ALEXA']['SD']; - debug('country id: ' + countryID); - if (countryID.includes('COUNTRY')) { - debug('country exists'); + try { waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; - } else { - debug('country doesn\'t exist'); + } catch (e) { + debug('Country not found'); + debug(e); } } module.exports = { - handler + handler, }; diff --git a/functions/src/config.js b/functions/src/config.js index 6bcd9205..78d76f77 100644 --- a/functions/src/config.js +++ b/functions/src/config.js @@ -31,6 +31,11 @@ module.exports = { DEFAULT_SONG_IMAGE: 'http://archive.org/images/notfound.png', }, + wayback: { + ARCHIVE: 'http://web.archive.org/__wb/search/metadata?q={{url}}', + ALEXA: 'http://data.alexa.com/data?cli=10&url={{url}}', + }, + /** * settings specific for supported platforms */ From 565491b4f9f08479d352d5fd2f73c630573b2acc Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 26 Jun 2018 14:52:48 -0400 Subject: [PATCH 09/27] Changes to string.js with additional dialog --- functions/src/strings.js | 4 ++++ .../tests/actions/wayback-machine.spec.js | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 functions/tests/actions/wayback-machine.spec.js diff --git a/functions/src/strings.js b/functions/src/strings.js index 603745c8..b831be6a 100644 --- a/functions/src/strings.js +++ b/functions/src/strings.js @@ -446,6 +446,10 @@ module.exports = { wayback: { speech: '{{url}} was first captured by the Internet Archive in {{earliestYear}} and most recently in {{latestYear}}. The archive has {{totalUniqueURLs}} unique url\'s for this website. {{url}} is ranked {{alexaWorldRank}} in the world in popularity', + additionalSpeech: ' and {{alexaUSRank}} in the United States.', + default: 'Would you like to use the wayback machine to hear the history of a website? Simply say Wayback Machine and the name of the website you\'d like to hear.', + error: "I'm sorry I'm having trouble here. Maybe we should try this again later.", + suggestions: ['wayback machine google.com', 'wayback machine archive.org'] }, welcome: { diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js new file mode 100644 index 00000000..a0afecf9 --- /dev/null +++ b/functions/tests/actions/wayback-machine.spec.js @@ -0,0 +1,24 @@ +const {expect} = require('chai'); +const rewire = require('rewire'); + +const wayback = rewire('../../src/actions/wayback-machine'); + +const mockApp = require('../_utils/mocking/platforms/app'); +const mockDialog = require('../_utils/mocking/dialog'); + +describe('actions', () => { + let dialog; + beforeEach(() => { + dialog = mockDialog(); + wayback.__set__('dialog', dialog); + wayback.__set__('packageJSON.version', '1.2.3'); + }); + + describe('wayback', () => { + it('should tell archive.org and alexa ranking data about a url', () => { + let app = mockApp(); + wayback.handler(app); + expect(dialog.close.args[0][1]).to.have.property('speech'); + }); + }); +}); From 734ec8116049ea47dd8b4a9638f543095c30dbc0 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 26 Jun 2018 23:57:36 -0400 Subject: [PATCH 10/27] Changes to wayback spec file --- functions/src/actions/wayback-machine.js | 14 +++++++--- .../tests/actions/wayback-machine.spec.js | 26 ++++++++++--------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index ed04694a..f7d82ab9 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -32,12 +32,20 @@ function handler (app) { }; // Check parameter for Wayback qualifier - let wayback = app.params.getByName('wayback'); - if (!wayback.includes('wayback')) { + // let wayback = app.params.getByName('wayback'); + // if (!wayback.includes('wayback')) { + if (!app.params.getByName('wayback') && !app.params.getByName('url')) { debug('wayback action called by mistake'); dialog.ask(app, waybackObject); } - + debug('Wayback exists: ' + !app.params.getByName('wayback')); + debug('URL exists: ' + app.params.getByName('url') !== undefined); + /* + if (!app.params.getByName('wayback') && app.params.getByName('url')) { + debug('wayback action called by mistake'); + dialog.ask(app, waybackObject); + } + */ // Get url parameter and make url queries waybackObject.url = app.params.getByName('url'); const archiveQueryURL = endpointProcessor.preprocess( diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index a0afecf9..445bfc6c 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -1,24 +1,26 @@ const {expect} = require('chai'); const rewire = require('rewire'); -const wayback = rewire('../../src/actions/wayback-machine'); +const action = rewire('../../src/actions/wayback-machine'); +// const strings = require('../../src/strings').intents.wayback.speech; const mockApp = require('../_utils/mocking/platforms/app'); const mockDialog = require('../_utils/mocking/dialog'); +// const util = require('util'); describe('actions', () => { - let dialog; - beforeEach(() => { - dialog = mockDialog(); - wayback.__set__('dialog', dialog); - wayback.__set__('packageJSON.version', '1.2.3'); - }); + describe('wayback machine', () => { + let app; + let dialog; - describe('wayback', () => { - it('should tell archive.org and alexa ranking data about a url', () => { - let app = mockApp(); - wayback.handler(app); - expect(dialog.close.args[0][1]).to.have.property('speech'); + beforeEach(() => { + app = mockApp(); + dialog = mockDialog(); + action.__set__('dialog', dialog); + }); + it('check to see that a promise is returned with network requests', () => { + action.handler(app); + expect(Promise.resolve()).to.be.a('promise'); }); }); }); From 4ace6fc5e4ad57c55f28081830cdfd28b951779a Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 3 Jul 2018 22:10:03 -0400 Subject: [PATCH 11/27] Break traverse off into separate util function and created unit test for it --- functions/package.json | 3 +- functions/src/actions/wayback-machine.js | 61 ++++++++++-------------- functions/src/utils/traverse.js | 31 ++++++++++++ functions/tests/utils/traverse.spec.js | 35 ++++++++++++++ 4 files changed, 93 insertions(+), 37 deletions(-) create mode 100644 functions/src/utils/traverse.js create mode 100644 functions/tests/utils/traverse.spec.js diff --git a/functions/package.json b/functions/package.json index 94686c70..1dddb61b 100644 --- a/functions/package.json +++ b/functions/package.json @@ -37,7 +37,8 @@ "object.entries": "^1.0.4", "raven": "^2.6.0", "replaceall": "^0.1.6", - "supports-color": "^5.4.0" + "supports-color": "^5.4.0", + "xml2js": "^0.4.19" }, "devDependencies": { "axios-mock-adapter": "^1.15.0", diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index f7d82ab9..b29d8c0d 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -1,5 +1,4 @@ // Third party imports -const _ = require('lodash'); const axios = require('axios'); const mustache = require('mustache'); const xml2js = require('xml2js'); @@ -9,6 +8,7 @@ const config = require('../config'); const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); const dialog = require('../dialog'); const endpointProcessor = require('../network/endpoint-processor'); +const traverse = require('../utils/traverse'); const waybackStrings = require('../strings').intents.wayback; /** @@ -31,21 +31,12 @@ function handler (app) { speech: waybackStrings.default, }; - // Check parameter for Wayback qualifier - // let wayback = app.params.getByName('wayback'); - // if (!wayback.includes('wayback')) { + // Check to see that both parameters have content if (!app.params.getByName('wayback') && !app.params.getByName('url')) { debug('wayback action called by mistake'); dialog.ask(app, waybackObject); } - debug('Wayback exists: ' + !app.params.getByName('wayback')); - debug('URL exists: ' + app.params.getByName('url') !== undefined); - /* - if (!app.params.getByName('wayback') && app.params.getByName('url')) { - debug('wayback action called by mistake'); - dialog.ask(app, waybackObject); - } - */ + // Get url parameter and make url queries waybackObject.url = app.params.getByName('url'); const archiveQueryURL = endpointProcessor.preprocess( @@ -66,15 +57,24 @@ function handler (app) { // Parse data from alexa request let alexaJSON; let XMLparser = new xml2js.Parser(); - XMLparser.parseString(allData[1].data, function (err, result) { - if (err) { - debug('The XML parser didn\'t work'); - waybackObject.speech = waybackStrings.error; - dialog.ask(app, waybackObject); - } else { - alexaJSON = JSON.parse(JSON.stringify(result)); - } + let convertXML = new Promise((resolve, reject) => { + XMLparser.parseString(allData[1].data, function (err, result) { + if (err) { + debug('The XML parser didn\'t work'); + waybackObject.speech = waybackStrings.error; + dialog.ask(app, waybackObject); + } else { + alexaJSON = JSON.parse(JSON.stringify(result)); + } + }); }); + convertXML + .then(function (fulfilled) { + debug(fulfilled); + }) + .catch(function (error) { + debug(error.message); + }); alexaEngine(alexaJSON, waybackObject); // Construct response dialog for action @@ -98,24 +98,13 @@ function archiveEngine (archiveJSON, waybackObject) { waybackObject.latestYear = yearsArray[yearsArray.length - 1]; // Traverse URL category - const traverse = obj => { - _.forOwn(obj, (val, key) => { - if (_.isArray(val)) { - val.forEach(el => { - traverse(el); - }); - } else if (_.isObject(val)) { - traverse(val); - } else { - waybackObject.totalUniqueURLs += val; - } - }); - }; // Find baseline of URL count - traverse(archiveJSON.urls[waybackObject.earliestYear]); - // Find final count of unique urls - traverse(archiveJSON.new_urls); + waybackObject.totalUniqueURLs += traverse(archiveJSON.urls[waybackObject.earliestYear]); + debug('Baseline url count: ' + waybackObject.totalUniqueURLs); + + waybackObject.totalUniqueURLs += traverse(archiveJSON.new_urls); + debug('Final url count: ' + waybackObject.totalUniqueURLs); } function alexaEngine (alexaJSON, waybackObject) { diff --git a/functions/src/utils/traverse.js b/functions/src/utils/traverse.js new file mode 100644 index 00000000..55b8a19e --- /dev/null +++ b/functions/src/utils/traverse.js @@ -0,0 +1,31 @@ +const _ = require('lodash'); +const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); + +/** + * Traverse a given object + * + * @param {Object} obj + */ +module.exports = function (obj) { + let results = []; + function traverse (obj) { + _.forOwn(obj, (val, key) => { + if (_.isArray(val)) { + val.forEach(el => { + traverse(el); + }); + } else if (_.isObject(val)) { + traverse(val); + } else { + results.push(val); + } + }); + } + traverse(obj); + let count = 0; + while (results.length !== 0) { + count += results.pop(); + } + debug('final count inside traverse = ' + count); + return count; +}; diff --git a/functions/tests/utils/traverse.spec.js b/functions/tests/utils/traverse.spec.js new file mode 100644 index 00000000..01d9a03b --- /dev/null +++ b/functions/tests/utils/traverse.spec.js @@ -0,0 +1,35 @@ +const {expect} = require('chai'); +const traverse = require('../../src/utils/traverse'); + +let testJSON = {'captures': { + '1999': { + 'text/html': 18360 + }, + '2000': { + 'application/x-director': 19, + 'video/quicktime': 1584, + 'application/x-troff-man': 1, + 'x-world/x-vrml': 1, + 'audio/x-pn-realaudio': 176, + 'audio/mpeg': 195, + 'audio/x-wav': 3098, + 'image/png': 97, + 'text/html': 901401, + 'video/x-ms-asf': 142, + 'image/gif': 17388, + 'text/plain': 394428, + 'image/jpeg': 82903, + 'application/x-shockwave-flash': 39, + 'application/zip': 108, + 'audio/x-aiff': 2767, + 'text/css': 55, + 'application/pdf': 291 + }}}; + +describe('utils', () => { + describe('traverse', () => { + it('should traverse a given object to return the sum of it\'s leaf nodes', () => { + expect(traverse(testJSON)).to.be.equal(1423053); + }); + }); +}); From 3cbd15296d3162b46eea1c34c1bfe3b89d6c9821 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Fri, 6 Jul 2018 12:03:15 -0400 Subject: [PATCH 12/27] Reworked XML parser promise to include resolve and reject --- functions/package.json | 1 - functions/src/actions/wayback-machine.js | 18 +++++++++--------- functions/src/utils/traverse.js | 2 +- .../tests/actions/wayback-machine.spec.js | 3 +-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/functions/package.json b/functions/package.json index 1dddb61b..3a33cacb 100644 --- a/functions/package.json +++ b/functions/package.json @@ -37,7 +37,6 @@ "object.entries": "^1.0.4", "raven": "^2.6.0", "replaceall": "^0.1.6", - "supports-color": "^5.4.0", "xml2js": "^0.4.19" }, "devDependencies": { diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index b29d8c0d..41a566f8 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -55,27 +55,27 @@ function handler (app) { archiveEngine(archiveJSON, waybackObject); // Parse data from alexa request - let alexaJSON; let XMLparser = new xml2js.Parser(); let convertXML = new Promise((resolve, reject) => { XMLparser.parseString(allData[1].data, function (err, result) { if (err) { - debug('The XML parser didn\'t work'); - waybackObject.speech = waybackStrings.error; - dialog.ask(app, waybackObject); + let error = new Error('The XML parser didn\'t work. Error message: ' + err); + reject(error); } else { - alexaJSON = JSON.parse(JSON.stringify(result)); + resolve(result); } }); }); convertXML .then(function (fulfilled) { - debug(fulfilled); + debug('XML parse successful!'); + alexaEngine(JSON.parse(JSON.stringify(fulfilled)), waybackObject); }) .catch(function (error) { debug(error.message); + waybackObject.speech = waybackStrings.error; + dialog.ask(app, waybackObject); }); - alexaEngine(alexaJSON, waybackObject); // Construct response dialog for action if (waybackObject.alexaUSRank !== 0) { @@ -101,10 +101,10 @@ function archiveEngine (archiveJSON, waybackObject) { // Find baseline of URL count waybackObject.totalUniqueURLs += traverse(archiveJSON.urls[waybackObject.earliestYear]); - debug('Baseline url count: ' + waybackObject.totalUniqueURLs); + // debug('Baseline url count: ' + waybackObject.totalUniqueURLs); waybackObject.totalUniqueURLs += traverse(archiveJSON.new_urls); - debug('Final url count: ' + waybackObject.totalUniqueURLs); + // debug('Final url count: ' + waybackObject.totalUniqueURLs); } function alexaEngine (alexaJSON, waybackObject) { diff --git a/functions/src/utils/traverse.js b/functions/src/utils/traverse.js index 55b8a19e..c734b1b8 100644 --- a/functions/src/utils/traverse.js +++ b/functions/src/utils/traverse.js @@ -1,5 +1,5 @@ const _ = require('lodash'); -const {debug} = require('../utils/logger')('ia:actions:wayback-machine'); +const {debug} = require('../utils/logger')('ia:actions:utils:traverse'); /** * Traverse a given object diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index 445bfc6c..516a3a0c 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -2,11 +2,9 @@ const {expect} = require('chai'); const rewire = require('rewire'); const action = rewire('../../src/actions/wayback-machine'); -// const strings = require('../../src/strings').intents.wayback.speech; const mockApp = require('../_utils/mocking/platforms/app'); const mockDialog = require('../_utils/mocking/dialog'); -// const util = require('util'); describe('actions', () => { describe('wayback machine', () => { @@ -18,6 +16,7 @@ describe('actions', () => { dialog = mockDialog(); action.__set__('dialog', dialog); }); + it('check to see that a promise is returned with network requests', () => { action.handler(app); expect(Promise.resolve()).to.be.a('promise'); From 22c601573ba06a49612fd4cb571ce8d27d884d4a Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 10 Jul 2018 00:57:02 -0400 Subject: [PATCH 13/27] Reworked all functions to return promises and then created promises pipeline --- functions/src/actions/wayback-machine.js | 123 ++++++++++++----------- 1 file changed, 65 insertions(+), 58 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 41a566f8..2ce34466 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -47,74 +47,81 @@ function handler (app) { ); return Promise.all([axios.get(archiveQueryURL), axios.get(alexaQueryURL)]) - .then(function (allData) { - // All data available here in the order it was called. - - // Parse data from archive request - let archiveJSON = allData[0].data; - archiveEngine(archiveJSON, waybackObject); - - // Parse data from alexa request - let XMLparser = new xml2js.Parser(); - let convertXML = new Promise((resolve, reject) => { - XMLparser.parseString(allData[1].data, function (err, result) { - if (err) { - let error = new Error('The XML parser didn\'t work. Error message: ' + err); - reject(error); - } else { - resolve(result); - } + .then(function (requestData) { + return Promise.all([archiveEngine(requestData[0].data, waybackObject), xmlConverter(requestData[1].data)]) + .then(function (response) { + return Promise.all([alexaEngine(response[1], waybackObject)]) + .then(function () { + // Construct response dialog for action + if (waybackObject.alexaUSRank !== 0) { + waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); + waybackObject.speech += mustache.render(waybackStrings.additionalSpeech, waybackObject); + } else { + waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); + waybackObject.speech += '.'; + } + dialog.close(app, waybackObject); + }); }); - }); - convertXML - .then(function (fulfilled) { - debug('XML parse successful!'); - alexaEngine(JSON.parse(JSON.stringify(fulfilled)), waybackObject); - }) - .catch(function (error) { - debug(error.message); - waybackObject.speech = waybackStrings.error; - dialog.ask(app, waybackObject); - }); - - // Construct response dialog for action - if (waybackObject.alexaUSRank !== 0) { - waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - waybackObject.speech += mustache.render(waybackStrings.additionalSpeech, waybackObject); - } else { - waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - waybackObject.speech += '.'; - } - - dialog.close(app, waybackObject); - }); + }); // End of REQUEST promise } // End of handler function archiveEngine (archiveJSON, waybackObject) { - // Create array of capture years and then find earliest year - // and most recent year. - let yearsArray = Object.keys(archiveJSON.captures); - waybackObject.earliestYear = yearsArray[0]; - waybackObject.latestYear = yearsArray[yearsArray.length - 1]; + return new Promise(function (resolve, reject) { + debug('Inside archiveEngine promise...'); + // Create array of capture years and then find earliest year + // and most recent year. + let yearsArray = Object.keys(archiveJSON.captures); + waybackObject.earliestYear = yearsArray[0]; + waybackObject.latestYear = yearsArray[yearsArray.length - 1]; - // Traverse URL category + // Traverse URL category - // Find baseline of URL count - waybackObject.totalUniqueURLs += traverse(archiveJSON.urls[waybackObject.earliestYear]); - // debug('Baseline url count: ' + waybackObject.totalUniqueURLs); + // Find baseline of URL count + waybackObject.totalUniqueURLs += traverse(archiveJSON.urls[waybackObject.earliestYear]); + // debug('Baseline url count: ' + waybackObject.totalUniqueURLs); - waybackObject.totalUniqueURLs += traverse(archiveJSON.new_urls); - // debug('Final url count: ' + waybackObject.totalUniqueURLs); + waybackObject.totalUniqueURLs += traverse(archiveJSON.new_urls); + // debug('Final url count: ' + waybackObject.totalUniqueURLs); + + if (waybackObject.totalUniqueURLs > 0) { + debug('archiveEngine promise successful!'); + resolve(); + } + }); } function alexaEngine (alexaJSON, waybackObject) { - waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; - try { - waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; - } catch (e) { - debug('Country not found'); - debug(e); - } + return new Promise(function (resolve, reject) { + debug('Inside alexaEngine promise...'); + waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; + try { + waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; + } catch (e) { + debug('Country not found'); + debug(e); + } + if (waybackObject.alexaWorldRank > 0) { + debug('alexaEngine promise successful!'); + resolve(); + } + }); +} + +function xmlConverter (data) { + return new Promise(function (resolve, reject) { + debug('Inside xmlConverter promise...'); + let XMLparser = new xml2js.Parser(); + XMLparser.parseString(data, function (err, result) { + if (err) { + let error = new Error('The XML parser didn\'t work. Error message: ' + err); + reject(error); + } else { + debug('xmlConverter promise successful!'); + resolve(JSON.parse(JSON.stringify(result))); + } + }); + }); } module.exports = { From 0c7c183928d2cc2fded4155bd3bbfd3935252e35 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 10 Jul 2018 13:35:52 -0400 Subject: [PATCH 14/27] Added unit tests for axios requests in wayback feature --- functions/tests/actions/wayback-machine.spec.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index 516a3a0c..598fb785 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -1,3 +1,4 @@ +const axios = require('axios'); const {expect} = require('chai'); const rewire = require('rewire'); @@ -17,9 +18,17 @@ describe('actions', () => { action.__set__('dialog', dialog); }); - it('check to see that a promise is returned with network requests', () => { + it('check to see that overall action eventually returns a promise', () => { action.handler(app); expect(Promise.resolve()).to.be.a('promise'); }); + + it('check to see that axios request promises are working', () => { + let request = axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com'); + console.log('REQUEST VARIABLE = ' + request); + expect(request).to.not.be.undefined; + // expect(action.axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com')).to.not.be.undefined; + // expect(action.axios.get('http://data.alexa.com/data?cli=10&url=cnn.com')).to.not.be.undefined; + }); }); }); From bcb2da71a7cd963e3dee15ee048df4b5566e9ace Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 10 Jul 2018 14:03:33 -0400 Subject: [PATCH 15/27] Added more unit tests for axios requests in wayback feature --- functions/tests/actions/wayback-machine.spec.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index 598fb785..82f6c6f9 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -24,9 +24,10 @@ describe('actions', () => { }); it('check to see that axios request promises are working', () => { - let request = axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com'); - console.log('REQUEST VARIABLE = ' + request); - expect(request).to.not.be.undefined; + let archiveRequest = axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com'); + let alexaRequest = axios.get('http://data.alexa.com/data?cli=10&url=cnn.com'); + expect(archiveRequest).to.not.be.undefined; + expect(alexaRequest).to.not.be.undefined; // expect(action.axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com')).to.not.be.undefined; // expect(action.axios.get('http://data.alexa.com/data?cli=10&url=cnn.com')).to.not.be.undefined; }); From ced199ee57b97479c174df03f062cb134d27eab0 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Tue, 10 Jul 2018 14:11:34 -0400 Subject: [PATCH 16/27] Took some debug messages out of WB code and traverse util --- functions/src/actions/wayback-machine.js | 12 ++++++------ functions/src/utils/traverse.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 2ce34466..354c082e 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -68,7 +68,7 @@ function handler (app) { function archiveEngine (archiveJSON, waybackObject) { return new Promise(function (resolve, reject) { - debug('Inside archiveEngine promise...'); + // debug('Inside archiveEngine promise...'); // Create array of capture years and then find earliest year // and most recent year. let yearsArray = Object.keys(archiveJSON.captures); @@ -85,7 +85,7 @@ function archiveEngine (archiveJSON, waybackObject) { // debug('Final url count: ' + waybackObject.totalUniqueURLs); if (waybackObject.totalUniqueURLs > 0) { - debug('archiveEngine promise successful!'); + // debug('archiveEngine promise successful!'); resolve(); } }); @@ -93,7 +93,7 @@ function archiveEngine (archiveJSON, waybackObject) { function alexaEngine (alexaJSON, waybackObject) { return new Promise(function (resolve, reject) { - debug('Inside alexaEngine promise...'); + // debug('Inside alexaEngine promise...'); waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; try { waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; @@ -102,7 +102,7 @@ function alexaEngine (alexaJSON, waybackObject) { debug(e); } if (waybackObject.alexaWorldRank > 0) { - debug('alexaEngine promise successful!'); + // debug('alexaEngine promise successful!'); resolve(); } }); @@ -110,14 +110,14 @@ function alexaEngine (alexaJSON, waybackObject) { function xmlConverter (data) { return new Promise(function (resolve, reject) { - debug('Inside xmlConverter promise...'); + // debug('Inside xmlConverter promise...'); let XMLparser = new xml2js.Parser(); XMLparser.parseString(data, function (err, result) { if (err) { let error = new Error('The XML parser didn\'t work. Error message: ' + err); reject(error); } else { - debug('xmlConverter promise successful!'); + // debug('xmlConverter promise successful!'); resolve(JSON.parse(JSON.stringify(result))); } }); diff --git a/functions/src/utils/traverse.js b/functions/src/utils/traverse.js index c734b1b8..0cf415d8 100644 --- a/functions/src/utils/traverse.js +++ b/functions/src/utils/traverse.js @@ -1,5 +1,5 @@ const _ = require('lodash'); -const {debug} = require('../utils/logger')('ia:actions:utils:traverse'); +// const {debug} = require('../utils/logger')('ia:actions:utils:traverse'); /** * Traverse a given object @@ -26,6 +26,6 @@ module.exports = function (obj) { while (results.length !== 0) { count += results.pop(); } - debug('final count inside traverse = ' + count); + // debug('final count inside traverse = ' + count); return count; }; From d7b796c29e85734791edaf58a2812a5c4fee4ddb Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Wed, 11 Jul 2018 12:33:50 -0400 Subject: [PATCH 17/27] Added unit tests for archiveEngine and alexaEngine --- .../tests/actions/wayback-machine.spec.js | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index 82f6c6f9..e2bf4347 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -1,6 +1,7 @@ const axios = require('axios'); const {expect} = require('chai'); const rewire = require('rewire'); +// const sinon = require('sinon'); const action = rewire('../../src/actions/wayback-machine'); @@ -11,6 +12,8 @@ describe('actions', () => { describe('wayback machine', () => { let app; let dialog; + let archiveRequest = axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com'); + let alexaRequest = axios.get('http://data.alexa.com/data?cli=10&url=cnn.com'); beforeEach(() => { app = mockApp(); @@ -24,12 +27,32 @@ describe('actions', () => { }); it('check to see that axios request promises are working', () => { - let archiveRequest = axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com'); - let alexaRequest = axios.get('http://data.alexa.com/data?cli=10&url=cnn.com'); expect(archiveRequest).to.not.be.undefined; expect(alexaRequest).to.not.be.undefined; - // expect(action.axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com')).to.not.be.undefined; - // expect(action.axios.get('http://data.alexa.com/data?cli=10&url=cnn.com')).to.not.be.undefined; + }); + + it('check to see that archiveEngine is working', () => { + // let obj = { earliestYear: 0, latestYear: 0, totalUniqueURLs: 0 }; + // action.archiveEngine(archiveRequest, obj); + + // action.__set__('archiveEngine', sinon.spy()); + + action.__set__('archiveEngine', function (archiveRequest, obj) { + return 'Something Else'; + }); + // let func = action.__get__('archiveEngine'); + // let result = app.archiveEngine(archiveRequest, obj); + /* + expect(result).to.change(obj, 'earliestYear'); + expect(result).to.change(obj, 'latestYear'); + expect(result).to.change(obj, 'totalUniqueURLs'); + */ + }); + + it('check to see that alexaEngine is working', () => { + let obj = { alexaWorldRank: 0 }; + let result = action.alexaEngine(alexaRequest, obj); + expect(result).to.change(obj, 'alexaWorldRank'); }); }); }); From 167d2879d9d652b365a86ac0e1bd4fd4479737f5 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Wed, 11 Jul 2018 13:15:20 -0400 Subject: [PATCH 18/27] Added unit test for xml converter in wb machine --- functions/src/actions/wayback-machine.js | 2 +- .../tests/actions/wayback-machine.spec.js | 38 +++++++++++-------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 354c082e..666ce3d7 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -114,7 +114,7 @@ function xmlConverter (data) { let XMLparser = new xml2js.Parser(); XMLparser.parseString(data, function (err, result) { if (err) { - let error = new Error('The XML parser didn\'t work. Error message: ' + err); + let error = new Error('The XML parser didn\'t work.'); reject(error); } else { // debug('xmlConverter promise successful!'); diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index e2bf4347..9ab1e399 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -1,7 +1,6 @@ const axios = require('axios'); const {expect} = require('chai'); const rewire = require('rewire'); -// const sinon = require('sinon'); const action = rewire('../../src/actions/wayback-machine'); @@ -32,27 +31,34 @@ describe('actions', () => { }); it('check to see that archiveEngine is working', () => { - // let obj = { earliestYear: 0, latestYear: 0, totalUniqueURLs: 0 }; - // action.archiveEngine(archiveRequest, obj); + let obj = { earliestYear: 0, latestYear: 0, totalUniqueURLs: 0 }; + let result; - // action.__set__('archiveEngine', sinon.spy()); - - action.__set__('archiveEngine', function (archiveRequest, obj) { - return 'Something Else'; + action.__set__('archiveEngine', function (a, b) { + result = action.archiveEngine(archiveRequest, obj); + expect(result).to.change(obj, 'earliestYear'); + expect(result).to.change(obj, 'latestYear'); + expect(result).to.change(obj, 'totalUniqueURLs'); }); - // let func = action.__get__('archiveEngine'); - // let result = app.archiveEngine(archiveRequest, obj); - /* - expect(result).to.change(obj, 'earliestYear'); - expect(result).to.change(obj, 'latestYear'); - expect(result).to.change(obj, 'totalUniqueURLs'); - */ }); it('check to see that alexaEngine is working', () => { let obj = { alexaWorldRank: 0 }; - let result = action.alexaEngine(alexaRequest, obj); - expect(result).to.change(obj, 'alexaWorldRank'); + let result; + + action.__set__('alexaEngine', function (a, b) { + result = action.alexaEngine(alexaRequest, obj); + expect(result).to.change(obj, 'alexaWorldRank'); + }); + }); + + it('check to see that xmlConverter is working', () => { + let result; + + action.__set__('xmlConverter', function (a) { + result = action.xmlConverter(alexaRequest); + expect(result).to.not.throw('The XML parser didn\'t work.'); + }); }); }); }); From abb91d7118a214104065a73916e4dd4112adc316 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Fri, 13 Jul 2018 13:42:55 -0400 Subject: [PATCH 19/27] Took out axios calls for wayback.spec and added them into fixtures --- functions/tests/actions/wayback-machine.spec.js | 11 ++++------- functions/tests/fixtures/wayback-alexa.xml | 1 + functions/tests/fixtures/wayback-archive.json | 1 + 3 files changed, 6 insertions(+), 7 deletions(-) create mode 100644 functions/tests/fixtures/wayback-alexa.xml create mode 100644 functions/tests/fixtures/wayback-archive.json diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index 9ab1e399..f12a43bc 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -4,6 +4,8 @@ const rewire = require('rewire'); const action = rewire('../../src/actions/wayback-machine'); +const archiveRequest = require('../../fixtures/wayback-archive.json'); +const alexaRequest = require('../../fixtures/wayback-alexa.xml'); const mockApp = require('../_utils/mocking/platforms/app'); const mockDialog = require('../_utils/mocking/dialog'); @@ -11,8 +13,8 @@ describe('actions', () => { describe('wayback machine', () => { let app; let dialog; - let archiveRequest = axios.get('http://web.archive.org/__wb/search/metadata?q=cnn.com'); - let alexaRequest = axios.get('http://data.alexa.com/data?cli=10&url=cnn.com'); + console.log('ARCHIVE REQUEST: ' + archiveRequest); + console.log('ALEXA REQUEST: ' + alexaRequest); beforeEach(() => { app = mockApp(); @@ -25,11 +27,6 @@ describe('actions', () => { expect(Promise.resolve()).to.be.a('promise'); }); - it('check to see that axios request promises are working', () => { - expect(archiveRequest).to.not.be.undefined; - expect(alexaRequest).to.not.be.undefined; - }); - it('check to see that archiveEngine is working', () => { let obj = { earliestYear: 0, latestYear: 0, totalUniqueURLs: 0 }; let result; diff --git a/functions/tests/fixtures/wayback-alexa.xml b/functions/tests/fixtures/wayback-alexa.xml new file mode 100644 index 00000000..31dd0d76 --- /dev/null +++ b/functions/tests/fixtures/wayback-alexa.xml @@ -0,0 +1 @@ + diff --git a/functions/tests/fixtures/wayback-archive.json b/functions/tests/fixtures/wayback-archive.json new file mode 100644 index 00000000..b85f6dcd --- /dev/null +++ b/functions/tests/fixtures/wayback-archive.json @@ -0,0 +1 @@ +{"captures":{"2009":{"application/x-director":18,"image/jpeg":68178,"x-world/x-vrml":2,"audio/x-pn-realaudio":10927,"audio/mpeg":3,"audio/x-wav":1599,"image/png":176,"application/xml":2173,"video/x-flv":95,"text/html":950328,"video/x-ms-asf":11024,"image/gif":22489,"text/plain":8266,"application/octet-stream":9,"application/x-shockwave-flash":2157,"audio/x-aiff":1032,"application/zip":271,"text/css":1347,"application/pdf":258},"2017":{"text/css":77623,"application/xml":27424,"text/html":16804082,"image/jpeg":98030,"audio/x-pn-realaudio":1,"audio/mpeg":1903,"audio/x-wav":72,"image/png":63863,"image/svg+xml":2484,"video/x-ms-asf":27,"application/x-redhat-package-manager":2,"image/gif":17876,"application/javascript":871,"text/plain":9572,"text/x-component":568,"application/octet-stream":1489,"application/x-shockwave-flash":1019,"audio/x-aiff":33,"application/json":9677,"application/vnd.google-earth.kml+xml":4,"application/pdf":10},"1999":{"text/html":18360},"2018":{"application/x-director":1,"text/html":2600524,"image/jpeg":16321,"image/svg+xml":1349,"audio/mpeg":33,"audio/x-wav":6,"image/png":9256,"application/xml":1440,"video/x-ms-asf":5,"image/gif":5444,"application/javascript":1854,"text/plain":1059,"text/x-component":85,"application/octet-stream":110,"application/x-shockwave-flash":61,"audio/x-aiff":6,"application/json":3017,"text/css":18020,"application/pdf":4},"2002":{"application/java-vm":14,"video/quicktime":29,"image/jpeg":4100,"x-world/x-vrml":7,"audio/x-pn-realaudio":241,"audio/mpeg":1,"audio/x-wav":82,"image/png":64,"text/html":629274,"video/x-ms-asf":14,"image/gif":10045,"application/x-director":97,"text/plain":899,"application/x-shockwave-flash":20,"application/zip":352,"audio/x-aiff":31222,"text/css":1091,"application/pdf":63},"2003":{"application/x-director":140,"video/quicktime":39,"image/jpeg":62309,"x-world/x-vrml":26,"audio/x-pn-realaudio":175,"audio/mpeg":2,"audio/x-wav":47,"image/png":27,"text/html":1003577,"video/x-ms-asf":9,"image/gif":14642,"text/plain":1533,"application/x-shockwave-flash":111,"application/zip":490,"audio/x-aiff":47671,"text/css":1997,"application/pdf":224,"application/msword":2},"2000":{"application/x-director":19,"video/quicktime":1584,"application/x-troff-man":1,"x-world/x-vrml":1,"audio/x-pn-realaudio":176,"audio/mpeg":195,"audio/x-wav":3098,"image/png":97,"text/html":901401,"video/x-ms-asf":142,"image/gif":17388,"text/plain":394428,"image/jpeg":82903,"application/x-shockwave-flash":39,"application/zip":108,"audio/x-aiff":2767,"text/css":55,"application/pdf":291},"2001":{"image/jpeg":163326,"video/quicktime":1412,"image/png":6724,"application/pdf":280,"application/x-director":35,"application/x-shockwave-flash":298,"image/gif":225325,"application/java-vm":45,"x-world/x-vrml":1,"audio/x-pn-realaudio":1465,"audio/mpeg":55,"audio/x-wav":2442,"video/x-ms-asf":347,"application/x-maker":1,"application/octet-stream":10,"text/plain":148101,"application/x-tar":3,"text/html":1161047,"application/java-archive":7,"video/mpeg":4,"application/zip":287,"audio/x-aiff":19457,"text/css":5708},"2006":{"text/plain":4378,"application/x-director":17,"video/quicktime":3829,"application/x-troff-man":5,"image/jpeg":83619,"audio/x-pn-realaudio":1488,"audio/mpeg":33,"application/x-shockwave-flash":755,"image/png":140,"application/xml":182,"text/html":546180,"text/css":3802,"image/gif":50853,"application/pdf":177,"application/octet-stream":37,"video/mpeg":1,"application/zip":378,"audio/x-aiff":2061,"audio/x-wav":2624,"application/andrew-inset":2,"application/msword":13},"2007":{"application/x-director":17,"video/quicktime":1610,"application/x-troff-man":5,"image/jpeg":50576,"audio/x-pn-realaudio":593,"audio/mpeg":26,"audio/x-wav":1131,"image/png":272,"application/xml":546,"text/html":659372,"image/gif":48034,"text/plain":5472,"application/octet-stream":21,"application/x-shockwave-flash":781,"audio/x-aiff":1070,"application/zip":5,"text/css":4883,"application/pdf":80,"application/msword":5},"2004":{"image/jpeg":73901,"video/quicktime":950,"image/png":125,"application/pdf":243,"application/x-director":148,"application/x-shockwave-flash":317,"image/gif":40713,"application/xml":1,"application/java-vm":2,"x-world/x-vrml":26,"audio/x-pn-realaudio":401,"audio/x-wav":55,"video/x-ms-asf":338,"application/octet-stream":1,"text/plain":5916,"application/x-troff-man":3,"text/html":1189596,"application/java-archive":1,"application/zip":569,"audio/x-aiff":43945,"text/css":4091,"application/msword":4},"2005":{"application/x-netcdf":1,"video/quicktime":341,"application/x-troff-man":1,"x-world/x-vrml":30,"audio/x-pn-realaudio":74,"audio/mpeg":1,"audio/x-wav":202,"image/png":133,"application/xml":52,"text/html":603784,"image/gif":57225,"application/x-director":49,"text/plain":6591,"image/jpeg":62006,"application/octet-stream":45,"application/x-shockwave-flash":686,"audio/x-aiff":12243,"application/zip":353,"text/css":4689,"application/pdf":839,"application/msword":10},"2015":{"image/x-ms-bmp":1,"image/jpeg":63876,"image/svg+xml":5569,"image/png":13602,"text/x-component":299,"application/json":821,"application/pdf":73,"application/vnd.ms-fontobject":3286,"application/x-shockwave-flash":2156,"image/gif":19805,"application/javascript":21336,"application/xml":42118,"audio/mpeg":14,"audio/x-wav":70,"video/x-ms-asf":51,"application/font-woff":6977,"application/octet-stream":2459,"application/x-redhat-package-manager":34,"text/plain":7332,"application/x-troff-man":1,"text/html":5487755,"application/zip":270,"audio/x-aiff":79,"text/css":15155},"2014":{"image/jpeg":169505,"image/svg+xml":47,"image/png":17835,"text/x-component":16,"application/json":1774,"application/pdf":335,"application/x-director":1,"application/vnd.ms-fontobject":9,"application/x-shockwave-flash":1161,"image/gif":39583,"application/javascript":9,"application/xml":22317,"audio/x-pn-realaudio":8,"audio/mpeg":6,"audio/x-wav":70,"video/x-flv":5,"video/x-ms-asf":32,"application/font-woff":9,"application/octet-stream":41,"application/x-redhat-package-manager":3,"text/plain":1064,"text/html":4979420,"application/zip":10,"audio/x-aiff":75,"text/css":14878},"2008":{"application/x-director":57,"video/quicktime":3090,"application/x-troff-man":21,"image/jpeg":134460,"audio/x-pn-realaudio":4461,"audio/mpeg":94,"audio/x-wav":1215,"image/png":491,"application/xml":2124,"video/x-flv":16,"text/html":1152615,"video/x-ms-asf":5333,"image/gif":80276,"text/plain":15533,"application/octet-stream":59,"application/x-shockwave-flash":2504,"audio/x-aiff":869,"application/zip":27,"text/css":3329,"application/pdf":208,"application/msword":17},"2016":{"image/jpeg":82172,"image/svg+xml":888,"image/png":20377,"text/x-component":348,"application/json":4909,"application/pdf":28,"application/x-director":5,"application/x-shockwave-flash":1341,"image/gif":18057,"application/javascript":1192,"application/xml":50044,"audio/x-pn-realaudio":3,"audio/mpeg":1144,"audio/x-wav":28,"video/x-flv":1,"video/x-ms-asf":61,"application/octet-stream":2,"application/vnd.google-earth.kml+xml":1,"text/plain":3564,"text/html":12953600,"application/zip":20,"audio/x-aiff":31,"text/css":18661},"2011":{"image/x-ms-bmp":2,"image/jpeg":161159,"image/svg+xml":50,"video/quicktime":102,"image/png":5329,"application/json":1293,"application/pdf":48,"application/x-director":8,"application/x-shockwave-flash":3295,"image/gif":34934,"application/xml":17827,"audio/x-pn-realaudio":24,"audio/x-wav":680,"video/x-ms-asf":44,"application/octet-stream":6,"application/x-redhat-package-manager":4,"text/plain":5863,"text/html":1074223,"video/mp4":6,"application/zip":2,"audio/x-aiff":509,"text/css":9337},"2010":{"application/x-director":36,"application/x-troff-man":2,"x-world/x-vrml":21,"audio/x-pn-realaudio":12106,"audio/mpeg":23,"audio/x-wav":4234,"image/png":652,"image/svg+xml":30,"video/x-flv":100,"text/html":854231,"video/x-ms-asf":14750,"image/gif":36649,"text/plain":11845,"image/jpeg":113068,"application/octet-stream":18,"application/xml":8736,"application/x-shockwave-flash":2459,"audio/x-aiff":3578,"application/zip":140,"text/css":2658,"application/pdf":62},"2013":{"text/html":1873444,"image/jpeg":51572,"image/svg+xml":13,"audio/x-wav":9,"image/png":7319,"application/xml":11473,"video/x-ms-asf":48,"text/css":25039,"application/x-shockwave-flash":1852,"image/gif":81402,"text/plain":4119,"text/x-component":132,"application/octet-stream":22,"application/zip":136,"audio/x-aiff":7,"application/json":2572,"application/vnd.google-earth.kml+xml":3,"application/pdf":33},"2012":{"video/quicktime":182,"text/html":3718360,"image/jpeg":363285,"image/svg+xml":36,"audio/mpeg":7,"application/x-shockwave-flash":6505,"image/png":14876,"application/xml":41944,"video/x-ms-asf":188,"text/css":20047,"image/gif":603193,"text/plain":8500,"text/x-component":63,"application/octet-stream":104,"application/json":19954,"application/x-redhat-package-manager":4,"application/pdf":588}},"urls_total_compressed_size":{"2009":{"application/x-director":1312850,"image/jpeg":321423459,"x-world/x-vrml":188076,"audio/x-pn-realaudio":1683558,"audio/mpeg":322199,"audio/x-wav":141549635,"image/png":129592,"application/xml":1634061,"video/x-flv":9281882,"text/html":2601913340,"video/x-ms-asf":2170972,"image/gif":43199914,"text/plain":40712327,"application/octet-stream":13429,"application/x-shockwave-flash":23263676,"audio/x-aiff":74765165,"application/zip":8924056,"text/css":195657,"application/pdf":87660837},"2017":{"text/css":26263600,"application/xml":56662264,"text/html":84633459730,"image/jpeg":647203305,"audio/x-pn-realaudio":517,"audio/mpeg":8088784,"audio/x-wav":12756445,"image/png":23386316,"image/svg+xml":2750029,"video/x-ms-asf":15078,"application/x-redhat-package-manager":1848,"image/gif":12324092,"application/javascript":2061442,"text/plain":219478468,"text/x-component":4369,"application/octet-stream":2512539,"application/x-shockwave-flash":5754267,"audio/x-aiff":3898146,"application/json":337229,"application/vnd.google-earth.kml+xml":5498,"application/pdf":1086053},"1999":{"text/html":65317245},"2018":{"application/x-director":32987,"text/html":9022084460,"image/jpeg":392250754,"image/svg+xml":2883301,"audio/mpeg":2835757,"audio/x-wav":1146090,"image/png":7660414,"application/xml":175945,"video/x-ms-asf":4035,"image/gif":14527446,"application/javascript":7480929,"text/plain":70421519,"text/x-component":4369,"application/octet-stream":2631460,"application/x-shockwave-flash":1128526,"audio/x-aiff":608130,"application/json":111040,"text/css":10367837,"application/pdf":436276},"2002":{"application/java-vm":50346,"video/quicktime":65869112,"image/jpeg":29623081,"x-world/x-vrml":1491049,"audio/x-pn-realaudio":53205,"audio/mpeg":970858,"audio/x-wav":25634336,"image/png":767953,"text/html":1892402093,"video/x-ms-asf":398294,"image/gif":10862250,"application/x-director":9378460,"text/plain":17523509,"application/x-shockwave-flash":986172,"application/zip":8865042,"audio/x-aiff":2566767442,"text/css":240687,"application/pdf":58810091},"2003":{"application/x-director":9401200,"video/quicktime":36729436,"image/jpeg":908183568,"x-world/x-vrml":1833461,"audio/x-pn-realaudio":24405,"audio/mpeg":517167,"audio/x-wav":12149839,"image/png":513384,"text/html":2415733164,"video/x-ms-asf":3475,"image/gif":107221822,"text/plain":23470437,"application/x-shockwave-flash":7998323,"application/zip":8865043,"audio/x-aiff":2712341430,"text/css":283722,"application/pdf":132930019,"application/msword":8733},"2000":{"application/x-director":4435654,"video/quicktime":1075863701,"application/x-troff-man":340,"x-world/x-vrml":142235,"audio/x-pn-realaudio":56720,"audio/mpeg":52695814,"audio/x-wav":500573236,"image/png":1387842,"text/html":775180286,"video/x-ms-asf":57213,"image/gif":92235258,"text/plain":94673184,"image/jpeg":710867407,"application/x-shockwave-flash":2909198,"application/zip":7065528,"audio/x-aiff":436816038,"text/css":22944,"application/pdf":84718092},"2001":{"image/jpeg":1052593679,"video/quicktime":519648079,"image/png":1944557,"application/pdf":47267698,"application/x-director":2641526,"application/x-shockwave-flash":3943933,"image/gif":142581342,"application/java-vm":128889,"x-world/x-vrml":188068,"audio/x-pn-realaudio":404641,"audio/mpeg":43580896,"audio/x-wav":406963867,"video/x-ms-asf":323292,"application/x-maker":336,"application/octet-stream":6662618,"text/plain":108696157,"application/x-tar":102945,"text/html":1890951365,"application/java-archive":134399,"video/mpeg":1171041,"application/zip":8864462,"audio/x-aiff":2265478498,"text/css":123205},"2006":{"text/plain":28435065,"application/x-director":3926490,"video/quicktime":2148613768,"application/x-troff-man":1651,"image/jpeg":641531302,"audio/x-pn-realaudio":478939,"audio/mpeg":6105519,"application/x-shockwave-flash":14430824,"image/png":2217593,"application/xml":115255,"text/html":1515790441,"text/css":343769,"image/gif":119552494,"application/pdf":32053023,"application/octet-stream":7623477,"video/mpeg":7165915,"application/zip":9001770,"audio/x-aiff":262942004,"audio/x-wav":367609492,"application/andrew-inset":867,"application/msword":14565},"2007":{"application/x-director":2888356,"video/quicktime":1266942272,"application/x-troff-man":1308,"image/jpeg":475358055,"audio/x-pn-realaudio":162209,"audio/mpeg":5819497,"audio/x-wav":165130709,"image/png":1166343,"application/xml":968693,"text/html":1987316431,"image/gif":72027437,"text/plain":20278825,"application/octet-stream":7621984,"application/x-shockwave-flash":28741778,"audio/x-aiff":138026858,"application/zip":105546,"text/css":422932,"application/pdf":20138443,"application/msword":14582},"2004":{"image/jpeg":942004245,"video/quicktime":74879398,"image/png":1618071,"application/pdf":110295560,"application/x-director":10545224,"application/x-shockwave-flash":14034457,"image/gif":127583996,"application/xml":671,"application/java-vm":18275,"x-world/x-vrml":1833450,"audio/x-pn-realaudio":84006,"audio/x-wav":5713572,"video/x-ms-asf":134483,"application/octet-stream":6563,"text/plain":160184004,"application/x-troff-man":436859,"text/html":3012886269,"application/java-archive":96107,"application/zip":8859883,"audio/x-aiff":2756804648,"text/css":310467,"application/msword":82702},"2005":{"application/x-netcdf":330,"video/quicktime":250581634,"application/x-troff-man":313,"x-world/x-vrml":1558297,"audio/x-pn-realaudio":11196,"audio/mpeg":399,"audio/x-wav":17837713,"image/png":1736925,"application/xml":11782,"text/html":1498489076,"image/gif":57808674,"application/x-director":7495061,"text/plain":147253562,"image/jpeg":327790427,"application/octet-stream":33478,"application/x-shockwave-flash":15625530,"audio/x-aiff":1104597104,"application/zip":8792649,"text/css":312664,"application/pdf":166436196,"application/msword":15342},"2015":{"image/x-ms-bmp":29669,"image/jpeg":975975074,"image/svg+xml":837385,"image/png":20612109,"text/x-component":3972,"application/json":1177329,"application/pdf":4735145,"application/vnd.ms-fontobject":1181765,"application/x-shockwave-flash":34111806,"image/gif":19965707,"application/javascript":8209254,"application/xml":66209867,"audio/mpeg":1326343,"audio/x-wav":17867663,"video/x-ms-asf":37589,"application/font-woff":1369108,"application/octet-stream":1283975,"application/x-redhat-package-manager":29114,"text/plain":155230989,"application/x-troff-man":398,"text/html":21579465281,"application/zip":8958444,"audio/x-aiff":16957930,"text/css":7992835},"2014":{"image/jpeg":912160553,"image/svg+xml":307132,"image/png":14111830,"text/x-component":3857,"application/json":178581,"application/pdf":24634658,"application/x-director":44453,"application/vnd.ms-fontobject":448944,"application/x-shockwave-flash":5029059,"image/gif":15513966,"application/javascript":132662,"application/xml":8940615,"audio/x-pn-realaudio":2974,"audio/mpeg":1069681,"audio/x-wav":10975911,"video/x-flv":3579283,"video/x-ms-asf":13260,"application/font-woff":499814,"application/octet-stream":37491,"application/x-redhat-package-manager":1916,"text/plain":56633394,"text/html":6466817762,"application/zip":41007,"audio/x-aiff":9365578,"text/css":733582},"2008":{"application/x-director":6548877,"video/quicktime":736404123,"application/x-troff-man":7469,"image/jpeg":611156603,"audio/x-pn-realaudio":713179,"audio/mpeg":5773513,"audio/x-wav":115542004,"image/png":1622322,"application/xml":1563760,"video/x-flv":2773016,"text/html":2881907996,"video/x-ms-asf":1708883,"image/gif":173589555,"text/plain":38960983,"application/octet-stream":61792,"application/x-shockwave-flash":42922567,"audio/x-aiff":90109797,"application/zip":131004,"text/css":385133,"application/pdf":23557569,"application/msword":6247},"2016":{"image/jpeg":1059253089,"image/svg+xml":264568,"image/png":21704528,"text/x-component":4307,"application/json":538040,"application/pdf":11345585,"application/x-director":13800,"application/x-shockwave-flash":7455985,"image/gif":25518220,"application/javascript":14720241,"application/xml":75681786,"audio/x-pn-realaudio":1691,"audio/mpeg":11055339,"audio/x-wav":5626750,"video/x-flv":17739721,"video/x-ms-asf":46341,"application/octet-stream":21808,"application/vnd.google-earth.kml+xml":5498,"text/plain":355518446,"text/html":63983575698,"application/zip":1326974,"audio/x-aiff":3336004,"text/css":3520887},"2011":{"image/x-ms-bmp":89490,"image/jpeg":943309751,"image/svg+xml":25308,"video/quicktime":429,"image/png":8195923,"application/json":21397,"application/pdf":91917522,"application/x-director":775034,"application/x-shockwave-flash":14015567,"image/gif":20023580,"application/xml":2479687,"audio/x-pn-realaudio":9918,"audio/x-wav":93621033,"video/x-ms-asf":14275,"application/octet-stream":13956,"application/x-redhat-package-manager":1115,"text/plain":75062789,"text/html":1838904497,"video/mp4":37058524,"application/zip":152123,"audio/x-aiff":68011845,"text/css":251966},"2010":{"application/x-director":3529137,"application/x-troff-man":1136,"x-world/x-vrml":1558297,"audio/x-pn-realaudio":2870086,"audio/mpeg":5110817,"audio/x-wav":338855065,"image/png":270050,"image/svg+xml":25311,"video/x-flv":87228452,"text/html":2885912827,"video/x-ms-asf":4972766,"image/gif":100788813,"text/plain":109366483,"image/jpeg":1182008980,"application/octet-stream":27651,"application/xml":3530838,"application/x-shockwave-flash":33101833,"audio/x-aiff":290715490,"application/zip":8959284,"text/css":248555,"application/pdf":4821565},"2013":{"text/html":5159041110,"image/jpeg":795672771,"image/svg+xml":38613,"audio/x-wav":1263989,"image/png":12282395,"application/xml":2241960,"video/x-ms-asf":19212,"text/css":591094,"application/x-shockwave-flash":8359002,"image/gif":25183509,"text/plain":98064197,"text/x-component":6338,"application/octet-stream":57535,"application/zip":8941882,"audio/x-aiff":785046,"application/json":132409,"application/vnd.google-earth.kml+xml":5498,"application/pdf":4738842},"2012":{"video/quicktime":429,"text/html":4365923254,"image/jpeg":831587427,"image/svg+xml":25138,"audio/mpeg":2692136,"application/x-shockwave-flash":10221446,"image/png":11373727,"application/xml":5459638,"video/x-ms-asf":28358,"text/css":295930,"image/gif":60331025,"text/plain":73371716,"text/x-component":6360,"application/octet-stream":21163,"application/json":242967,"application/x-redhat-package-manager":1163,"application/pdf":97219911}},"type":"host","urls":{"2009":{"application/x-director":8,"image/jpeg":17641,"x-world/x-vrml":1,"audio/x-pn-realaudio":3535,"audio/mpeg":2,"audio/x-wav":923,"image/png":17,"application/xml":257,"video/x-flv":11,"text/html":218988,"video/x-ms-asf":3642,"image/gif":4788,"text/plain":792,"application/octet-stream":2,"application/x-shockwave-flash":253,"audio/x-aiff":549,"application/zip":136,"text/css":151,"application/pdf":153},"2017":{"text/css":915,"application/xml":14907,"text/html":2959146,"image/jpeg":9824,"audio/x-pn-realaudio":1,"audio/mpeg":18,"audio/x-wav":72,"image/png":675,"image/svg+xml":197,"video/x-ms-asf":17,"application/x-redhat-package-manager":2,"image/gif":1738,"application/javascript":34,"text/plain":739,"text/x-component":2,"application/octet-stream":34,"application/x-shockwave-flash":35,"audio/x-aiff":33,"application/json":64,"application/vnd.google-earth.kml+xml":1,"application/pdf":9},"1999":{"text/html":13129},"2018":{"application/x-director":1,"text/html":255188,"image/jpeg":6035,"image/svg+xml":211,"audio/mpeg":11,"audio/x-wav":6,"image/png":382,"application/xml":17,"video/x-ms-asf":5,"image/gif":981,"application/javascript":100,"text/plain":161,"text/x-component":2,"application/octet-stream":35,"application/x-shockwave-flash":18,"audio/x-aiff":6,"application/json":27,"text/css":417,"application/pdf":3},"2002":{"application/java-vm":12,"video/quicktime":21,"image/jpeg":2352,"x-world/x-vrml":6,"audio/x-pn-realaudio":171,"audio/mpeg":1,"audio/x-wav":72,"image/png":36,"text/html":275089,"video/x-ms-asf":7,"image/gif":2156,"application/x-director":40,"text/plain":369,"application/x-shockwave-flash":13,"application/zip":135,"audio/x-aiff":11953,"text/css":388,"application/pdf":61},"2003":{"application/x-director":40,"video/quicktime":25,"image/jpeg":57538,"x-world/x-vrml":8,"audio/x-pn-realaudio":74,"audio/mpeg":2,"audio/x-wav":36,"image/png":19,"text/html":347260,"video/x-ms-asf":9,"image/gif":10219,"text/plain":409,"application/x-shockwave-flash":90,"application/zip":135,"audio/x-aiff":12601,"text/css":451,"application/pdf":172,"application/msword":1},"2000":{"application/x-director":18,"video/quicktime":1460,"application/x-troff-man":1,"x-world/x-vrml":1,"audio/x-pn-realaudio":176,"audio/mpeg":185,"audio/x-wav":2542,"image/png":60,"text/html":110041,"video/x-ms-asf":142,"image/gif":10718,"text/plain":74440,"image/jpeg":47599,"application/x-shockwave-flash":23,"application/zip":108,"audio/x-aiff":2293,"text/css":27,"application/pdf":220},"2001":{"image/jpeg":73382,"video/quicktime":1133,"image/png":110,"application/pdf":107,"application/x-director":25,"application/x-shockwave-flash":37,"image/gif":16839,"application/java-vm":37,"x-world/x-vrml":1,"audio/x-pn-realaudio":1276,"audio/mpeg":51,"audio/x-wav":1375,"video/x-ms-asf":116,"application/x-maker":1,"application/octet-stream":8,"text/plain":78110,"application/x-tar":2,"text/html":386947,"application/java-archive":7,"video/mpeg":1,"application/zip":135,"audio/x-aiff":10423,"text/css":197},"2006":{"text/plain":2104,"application/x-director":12,"video/quicktime":1741,"application/x-troff-man":4,"image/jpeg":40432,"audio/x-pn-realaudio":624,"audio/mpeg":20,"application/x-shockwave-flash":261,"image/png":54,"application/xml":25,"text/html":209045,"text/css":405,"image/gif":13995,"application/pdf":85,"application/octet-stream":13,"video/mpeg":1,"application/zip":140,"audio/x-aiff":1504,"audio/x-wav":1932,"application/andrew-inset":2,"application/msword":3},"2007":{"application/x-director":13,"video/quicktime":940,"application/x-troff-man":3,"image/jpeg":27795,"audio/x-pn-realaudio":471,"audio/mpeg":25,"audio/x-wav":1023,"image/png":32,"application/xml":81,"text/html":248132,"image/gif":11817,"text/plain":2243,"application/octet-stream":9,"application/x-shockwave-flash":326,"audio/x-aiff":926,"application/zip":4,"text/css":460,"application/pdf":22,"application/msword":3},"2004":{"application/java-vm":2,"video/quicktime":220,"image/jpeg":60336,"x-world/x-vrml":8,"audio/x-pn-realaudio":272,"audio/x-wav":19,"image/png":38,"application/xml":1,"application/x-director":43,"text/html":400535,"video/x-ms-asf":311,"application/java-archive":1,"image/gif":12954,"application/x-troff-man":2,"text/plain":2512,"application/x-shockwave-flash":194,"application/zip":135,"audio/x-aiff":12839,"text/css":516,"application/pdf":194,"application/msword":4},"2005":{"application/x-netcdf":1,"video/quicktime":176,"application/x-troff-man":1,"x-world/x-vrml":7,"audio/x-pn-realaudio":36,"audio/mpeg":1,"audio/x-wav":96,"image/png":34,"application/xml":8,"text/html":219110,"image/gif":10723,"application/x-director":26,"text/plain":3836,"image/jpeg":22144,"application/octet-stream":5,"application/x-shockwave-flash":235,"audio/x-aiff":6709,"application/zip":134,"text/css":480,"application/pdf":223,"application/msword":5},"2015":{"image/x-ms-bmp":1,"image/jpeg":15600,"image/svg+xml":29,"image/png":729,"text/x-component":2,"application/json":93,"application/pdf":8,"application/vnd.ms-fontobject":28,"application/x-shockwave-flash":589,"image/gif":2991,"application/javascript":877,"application/xml":20523,"audio/mpeg":14,"audio/x-wav":63,"video/x-ms-asf":47,"application/font-woff":28,"application/octet-stream":56,"application/x-redhat-package-manager":34,"text/plain":1287,"application/x-troff-man":1,"text/html":978345,"application/zip":135,"audio/x-aiff":68,"text/css":679},"2014":{"image/jpeg":15620,"image/svg+xml":10,"image/png":362,"text/x-component":2,"application/json":33,"application/pdf":24,"application/x-director":1,"application/vnd.ms-fontobject":9,"application/x-shockwave-flash":68,"image/gif":2740,"application/javascript":9,"application/xml":2932,"audio/x-pn-realaudio":6,"audio/mpeg":6,"audio/x-wav":63,"video/x-flv":5,"video/x-ms-asf":21,"application/font-woff":9,"application/octet-stream":3,"application/x-redhat-package-manager":3,"text/plain":580,"text/html":381230,"application/zip":1,"audio/x-aiff":65,"text/css":290},"2008":{"application/x-director":27,"video/quicktime":1529,"application/x-troff-man":17,"image/jpeg":40023,"audio/x-pn-realaudio":1579,"audio/mpeg":21,"audio/x-wav":580,"image/png":25,"application/xml":179,"video/x-flv":6,"text/html":293349,"video/x-ms-asf":2018,"image/gif":18852,"text/plain":3644,"application/octet-stream":11,"application/x-shockwave-flash":511,"audio/x-aiff":474,"application/zip":5,"text/css":467,"application/pdf":79,"application/msword":3},"2016":{"image/jpeg":19233,"image/svg+xml":36,"image/png":583,"text/x-component":2,"application/json":165,"application/pdf":17,"application/x-director":1,"application/x-shockwave-flash":57,"image/gif":3725,"application/javascript":150,"application/xml":22097,"audio/x-pn-realaudio":3,"audio/mpeg":27,"audio/x-wav":28,"video/x-flv":1,"video/x-ms-asf":57,"application/octet-stream":2,"application/vnd.google-earth.kml+xml":1,"text/plain":1323,"text/html":2264066,"application/zip":20,"audio/x-aiff":31,"text/css":527},"2011":{"image/x-ms-bmp":2,"image/jpeg":19903,"image/svg+xml":2,"video/quicktime":1,"image/png":349,"application/json":12,"application/pdf":34,"application/x-director":4,"application/x-shockwave-flash":148,"image/gif":3086,"application/xml":855,"audio/x-pn-realaudio":20,"audio/x-wav":644,"video/x-ms-asf":25,"application/octet-stream":3,"application/x-redhat-package-manager":1,"text/plain":714,"text/html":151215,"video/mp4":4,"application/zip":2,"audio/x-aiff":483,"text/css":157},"2010":{"application/x-director":13,"application/x-troff-man":2,"x-world/x-vrml":7,"audio/x-pn-realaudio":5529,"audio/mpeg":20,"audio/x-wav":2091,"image/png":56,"image/svg+xml":2,"video/x-flv":22,"text/html":262485,"video/x-ms-asf":7645,"image/gif":9158,"text/plain":1147,"image/jpeg":32273,"application/octet-stream":4,"application/xml":1382,"application/x-shockwave-flash":338,"audio/x-aiff":1816,"application/zip":137,"text/css":185,"application/pdf":19},"2013":{"text/html":325555,"image/jpeg":14678,"image/svg+xml":3,"audio/x-wav":9,"image/png":343,"application/xml":700,"video/x-ms-asf":34,"text/css":245,"application/x-shockwave-flash":87,"image/gif":3230,"text/plain":898,"text/x-component":3,"application/octet-stream":2,"application/zip":136,"audio/x-aiff":7,"application/json":38,"application/vnd.google-earth.kml+xml":1,"application/pdf":14},"2012":{"video/quicktime":1,"text/html":309246,"image/jpeg":14846,"image/svg+xml":2,"audio/mpeg":7,"application/x-shockwave-flash":123,"image/png":254,"application/xml":1814,"video/x-ms-asf":46,"text/css":180,"image/gif":73928,"text/plain":670,"text/x-component":3,"application/octet-stream":3,"application/json":210,"application/x-redhat-package-manager":3,"application/pdf":36}},"new_urls":{"2009":{"text/html":109347,"image/jpeg":2146,"audio/x-pn-realaudio":2982,"audio/x-wav":324,"image/png":6,"application/xml":116,"video/x-flv":5,"video/x-ms-asf":3189,"application/x-shockwave-flash":43,"image/gif":273,"text/plain":173,"application/octet-stream":1,"application/zip":1,"text/css":13,"application/pdf":4},"2017":{"text/html":2401250,"image/jpeg":699,"image/svg+xml":192,"application/x-shockwave-flash":7,"image/png":337,"application/xml":10223,"video/x-ms-asf":2,"image/gif":74,"application/javascript":27,"application/octet-stream":33,"application/json":22,"text/css":634,"text/plain":267},"1999":{"text/html":10836},"2018":{"image/jpeg":462,"text/html":148969,"image/svg+xml":22,"image/png":125,"video/x-ms-asf":4,"image/gif":44,"application/javascript":86,"application/octet-stream":6,"application/json":7,"text/css":226,"text/plain":44},"2002":{"application/x-director":13,"video/quicktime":6,"image/jpeg":974,"x-world/x-vrml":3,"audio/x-pn-realaudio":123,"audio/x-wav":32,"text/html":112570,"video/x-ms-asf":1,"image/gif":566,"text/plain":83,"application/x-shockwave-flash":7,"audio/x-aiff":2310,"text/css":221,"application/pdf":13},"2003":{"application/x-director":2,"video/quicktime":14,"image/jpeg":8965,"x-world/x-vrml":1,"audio/x-pn-realaudio":18,"audio/mpeg":2,"audio/x-wav":18,"text/html":120668,"video/x-ms-asf":8,"image/gif":1892,"text/plain":71,"application/x-shockwave-flash":61,"audio/x-aiff":477,"text/css":114,"application/pdf":55,"application/msword":1},"2000":{"application/x-director":15,"video/quicktime":1394,"application/x-troff-man":1,"x-world/x-vrml":1,"audio/x-pn-realaudio":121,"audio/mpeg":180,"audio/x-wav":2515,"image/png":60,"text/html":87719,"video/x-ms-asf":87,"image/gif":8430,"text/plain":41978,"image/jpeg":42328,"application/x-shockwave-flash":23,"application/zip":103,"audio/x-aiff":2233,"text/css":10,"application/pdf":207},"2001":{"image/jpeg":27130,"video/quicktime":921,"image/png":43,"application/pdf":17,"application/x-director":14,"application/x-shockwave-flash":12,"image/gif":7166,"application/java-vm":30,"x-world/x-vrml":1,"audio/x-pn-realaudio":1236,"audio/mpeg":23,"audio/x-wav":886,"video/x-ms-asf":72,"application/x-maker":1,"application/octet-stream":8,"text/plain":3448,"application/x-tar":2,"text/html":262179,"application/java-archive":6,"video/mpeg":1,"application/zip":31,"audio/x-aiff":7882,"text/css":127},"2006":{"text/plain":380,"video/quicktime":933,"application/x-troff-man":1,"image/jpeg":8262,"audio/x-pn-realaudio":221,"audio/mpeg":7,"application/x-shockwave-flash":120,"image/png":13,"application/xml":19,"text/html":78972,"image/gif":1391,"audio/x-wav":1248,"application/pdf":23,"application/octet-stream":7,"application/zip":3,"audio/x-aiff":33,"text/css":87,"application/andrew-inset":2,"video/mpeg":1},"2007":{"application/x-director":1,"video/quicktime":190,"application/x-troff-man":1,"image/jpeg":5834,"audio/x-pn-realaudio":13,"audio/mpeg":14,"audio/x-wav":473,"image/png":6,"application/xml":53,"text/html":91429,"image/gif":1141,"text/plain":641,"application/x-shockwave-flash":157,"text/css":80,"application/pdf":6},"2004":{"application/java-vm":1,"video/quicktime":191,"application/x-troff-man":1,"image/jpeg":12799,"audio/x-pn-realaudio":227,"audio/x-wav":15,"image/png":5,"application/xml":1,"text/html":111739,"video/x-ms-asf":232,"application/java-archive":1,"image/gif":3754,"application/x-director":3,"text/plain":138,"application/x-shockwave-flash":126,"audio/x-aiff":418,"text/css":99,"application/pdf":21,"application/msword":3},"2005":{"application/x-director":3,"video/quicktime":95,"text/html":65071,"image/jpeg":6165,"audio/x-pn-realaudio":13,"audio/x-wav":43,"image/png":2,"application/xml":8,"image/gif":2645,"text/plain":187,"application/x-shockwave-flash":124,"audio/x-aiff":60,"text/css":81,"application/pdf":8,"application/msword":1},"2015":{"application/vnd.ms-fontobject":19,"text/html":789503,"image/jpeg":1629,"image/svg+xml":20,"audio/mpeg":9,"application/x-shockwave-flash":113,"image/png":400,"application/xml":19375,"video/x-ms-asf":11,"text/css":403,"image/gif":226,"application/javascript":810,"application/font-woff":19,"text/plain":164,"application/octet-stream":55,"application/json":77,"application/x-redhat-package-manager":14,"application/pdf":2},"2014":{"application/vnd.ms-fontobject":9,"text/html":237314,"image/jpeg":1848,"image/svg+xml":10,"audio/mpeg":2,"application/x-shockwave-flash":22,"image/png":203,"application/xml":2751,"video/x-flv":5,"video/x-ms-asf":6,"text/css":105,"image/gif":397,"application/javascript":9,"application/font-woff":9,"text/plain":175,"application/octet-stream":1,"application/json":18,"application/x-redhat-package-manager":1,"application/pdf":12},"2008":{"application/x-director":2,"video/quicktime":845,"text/html":136337,"image/jpeg":6768,"audio/x-pn-realaudio":796,"audio/x-wav":128,"image/png":12,"application/xml":111,"video/x-flv":6,"video/x-ms-asf":969,"image/gif":2173,"text/plain":471,"application/x-shockwave-flash":249,"audio/x-aiff":3,"text/css":47,"application/pdf":8},"2016":{"text/html":1939503,"image/jpeg":1117,"image/svg+xml":36,"audio/mpeg":18,"application/x-shockwave-flash":8,"image/png":213,"application/xml":15197,"video/x-flv":1,"video/x-ms-asf":8,"image/gif":230,"application/javascript":149,"application/octet-stream":1,"application/json":145,"text/css":189,"text/plain":370},"2011":{"text/html":98294,"image/jpeg":8970,"audio/x-pn-realaudio":5,"audio/x-wav":7,"image/png":300,"application/xml":617,"video/mp4":4,"video/x-ms-asf":11,"text/css":41,"image/gif":244,"text/plain":97,"application/x-shockwave-flash":60,"application/zip":1,"audio/x-aiff":2,"application/x-redhat-package-manager":1,"application/pdf":24},"2010":{"application/x-director":2,"application/x-troff-man":1,"image/jpeg":13684,"audio/x-pn-realaudio":1716,"audio/mpeg":6,"audio/x-wav":686,"image/png":39,"image/svg+xml":2,"video/x-flv":14,"text/html":137516,"video/x-ms-asf":1955,"image/gif":1516,"text/plain":194,"application/x-shockwave-flash":153,"application/xml":1218,"text/css":45,"application/pdf":9},"2013":{"text/html":206806,"image/jpeg":2193,"image/svg+xml":1,"application/x-shockwave-flash":37,"image/png":149,"application/xml":560,"video/x-ms-asf":3,"image/gif":309,"text/plain":253,"application/octet-stream":2,"application/zip":1,"application/json":22,"text/css":51,"application/pdf":7},"2012":{"text/html":223306,"image/jpeg":3231,"application/x-shockwave-flash":31,"image/png":140,"video/x-ms-asf":7,"image/gif":71665,"text/plain":101,"text/x-component":2,"application/xml":1439,"application/json":145,"text/css":43,"application/pdf":7}}} From fa153acef01e2da24174c1479491a6787831bac4 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Fri, 13 Jul 2018 20:14:06 -0400 Subject: [PATCH 20/27] Changed promise pipeline to avoid callback --- functions/src/actions/wayback-machine.js | 44 ++++++++++++------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 666ce3d7..eb42732a 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -48,27 +48,27 @@ function handler (app) { return Promise.all([axios.get(archiveQueryURL), axios.get(alexaQueryURL)]) .then(function (requestData) { - return Promise.all([archiveEngine(requestData[0].data, waybackObject), xmlConverter(requestData[1].data)]) - .then(function (response) { - return Promise.all([alexaEngine(response[1], waybackObject)]) - .then(function () { - // Construct response dialog for action - if (waybackObject.alexaUSRank !== 0) { - waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - waybackObject.speech += mustache.render(waybackStrings.additionalSpeech, waybackObject); - } else { - waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); - waybackObject.speech += '.'; - } - dialog.close(app, waybackObject); - }); - }); - }); // End of REQUEST promise + return Promise.all([archiveEngine(requestData[0].data, waybackObject), xmlConverter(requestData[1].data)]); + }) + .then(response => { + return Promise.all([alexaEngine(response[1], waybackObject)]); + }) + .then(a => { + // Construct response dialog for action + if (waybackObject.alexaUSRank !== 0) { + waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); + waybackObject.speech += mustache.render(waybackStrings.additionalSpeech, waybackObject); + } else { + waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); + waybackObject.speech += '.'; + } + dialog.close(app, waybackObject); + }); } // End of handler function archiveEngine (archiveJSON, waybackObject) { return new Promise(function (resolve, reject) { - // debug('Inside archiveEngine promise...'); + debug('Inside archiveEngine promise...'); // Create array of capture years and then find earliest year // and most recent year. let yearsArray = Object.keys(archiveJSON.captures); @@ -85,7 +85,7 @@ function archiveEngine (archiveJSON, waybackObject) { // debug('Final url count: ' + waybackObject.totalUniqueURLs); if (waybackObject.totalUniqueURLs > 0) { - // debug('archiveEngine promise successful!'); + debug('archiveEngine promise successful!'); resolve(); } }); @@ -93,7 +93,7 @@ function archiveEngine (archiveJSON, waybackObject) { function alexaEngine (alexaJSON, waybackObject) { return new Promise(function (resolve, reject) { - // debug('Inside alexaEngine promise...'); + debug('Inside alexaEngine promise...'); waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; try { waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; @@ -102,7 +102,7 @@ function alexaEngine (alexaJSON, waybackObject) { debug(e); } if (waybackObject.alexaWorldRank > 0) { - // debug('alexaEngine promise successful!'); + debug('alexaEngine promise successful!'); resolve(); } }); @@ -110,14 +110,14 @@ function alexaEngine (alexaJSON, waybackObject) { function xmlConverter (data) { return new Promise(function (resolve, reject) { - // debug('Inside xmlConverter promise...'); + debug('Inside xmlConverter promise...'); let XMLparser = new xml2js.Parser(); XMLparser.parseString(data, function (err, result) { if (err) { let error = new Error('The XML parser didn\'t work.'); reject(error); } else { - // debug('xmlConverter promise successful!'); + debug('xmlConverter promise successful!'); resolve(JSON.parse(JSON.stringify(result))); } }); From 698830e134a909385a603cbc0ec067dda34b4968 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Fri, 13 Jul 2018 20:22:54 -0400 Subject: [PATCH 21/27] Took out missing axios variable in wayback spec --- functions/tests/actions/wayback-machine.spec.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/functions/tests/actions/wayback-machine.spec.js b/functions/tests/actions/wayback-machine.spec.js index f12a43bc..5fc32a62 100644 --- a/functions/tests/actions/wayback-machine.spec.js +++ b/functions/tests/actions/wayback-machine.spec.js @@ -1,11 +1,10 @@ -const axios = require('axios'); const {expect} = require('chai'); const rewire = require('rewire'); const action = rewire('../../src/actions/wayback-machine'); -const archiveRequest = require('../../fixtures/wayback-archive.json'); -const alexaRequest = require('../../fixtures/wayback-alexa.xml'); +const archiveRequest = require('../fixtures/wayback-archive.json'); +const alexaRequest = require('../fixtures/wayback-alexa.xml'); const mockApp = require('../_utils/mocking/platforms/app'); const mockDialog = require('../_utils/mocking/dialog'); @@ -13,8 +12,6 @@ describe('actions', () => { describe('wayback machine', () => { let app; let dialog; - console.log('ARCHIVE REQUEST: ' + archiveRequest); - console.log('ALEXA REQUEST: ' + alexaRequest); beforeEach(() => { app = mockApp(); From 74f93e5f5afea595f11a9787ac63584c411bc2a7 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Sat, 14 Jul 2018 16:43:07 -0400 Subject: [PATCH 22/27] Rewrote archive and alexa engines to be pure functions --- functions/src/actions/wayback-machine.js | 56 +++++++++++++++++------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index eb42732a..864d210f 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -48,12 +48,26 @@ function handler (app) { return Promise.all([axios.get(archiveQueryURL), axios.get(alexaQueryURL)]) .then(function (requestData) { - return Promise.all([archiveEngine(requestData[0].data, waybackObject), xmlConverter(requestData[1].data)]); + return Promise.all([archiveEngine(requestData[0].data), xmlConverter(requestData[1].data)]); }) .then(response => { - return Promise.all([alexaEngine(response[1], waybackObject)]); + let res = {}; + // Merge response into object for assignment to WB obj + res = Object.assign({}, res, response[0]); + + waybackObject.earliestYear = res.earliestYear; + waybackObject.latestYear = res.latestYear; + waybackObject.totalUniqueURLs = res.totalUniqueURLs; + + return alexaEngine(response[1]); }) - .then(a => { + .then(response => { + let res = {}; + // Merge response into object for assignment to WB obj + res = Object.assign({}, res, response); + waybackObject.alexaUSRank = res.alexaUSRank; + waybackObject.alexaWorldRank = res.alexaWorldRank; + // Construct response dialog for action if (waybackObject.alexaUSRank !== 0) { waybackObject.speech = mustache.render(waybackStrings.speech, waybackObject); @@ -66,44 +80,52 @@ function handler (app) { }); } // End of handler -function archiveEngine (archiveJSON, waybackObject) { +function archiveEngine (archiveJSON) { return new Promise(function (resolve, reject) { debug('Inside archiveEngine promise...'); + let obj = { earliestYear: 0, latestYear: 0, totalUniqueURLs: 0 }; // Create array of capture years and then find earliest year // and most recent year. let yearsArray = Object.keys(archiveJSON.captures); - waybackObject.earliestYear = yearsArray[0]; - waybackObject.latestYear = yearsArray[yearsArray.length - 1]; + obj.earliestYear = yearsArray[0]; + obj.latestYear = yearsArray[yearsArray.length - 1]; // Traverse URL category // Find baseline of URL count - waybackObject.totalUniqueURLs += traverse(archiveJSON.urls[waybackObject.earliestYear]); - // debug('Baseline url count: ' + waybackObject.totalUniqueURLs); + obj.totalUniqueURLs += traverse(archiveJSON.urls[obj.earliestYear]); + // debug('Baseline url count: ' + obj.totalUniqueURLs); - waybackObject.totalUniqueURLs += traverse(archiveJSON.new_urls); - // debug('Final url count: ' + waybackObject.totalUniqueURLs); + obj.totalUniqueURLs += traverse(archiveJSON.new_urls); + // debug('Final url count: ' + obj.totalUniqueURLs); - if (waybackObject.totalUniqueURLs > 0) { + if (obj.totalUniqueURLs > 0) { debug('archiveEngine promise successful!'); - resolve(); + resolve(obj); + } else { + let error = new Error('archiveEngine didn\'t work.'); + reject(error); } }); } -function alexaEngine (alexaJSON, waybackObject) { +function alexaEngine (alexaJSON) { return new Promise(function (resolve, reject) { debug('Inside alexaEngine promise...'); - waybackObject.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; + let obj = { alexaWorldRank: 0, alexaUSRank: 0 }; + obj.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; try { - waybackObject.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; + obj.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; } catch (e) { debug('Country not found'); debug(e); } - if (waybackObject.alexaWorldRank > 0) { + if (obj.alexaWorldRank > 0) { debug('alexaEngine promise successful!'); - resolve(); + resolve(obj); + } else { + let error = new Error('alexaEngine didn\'t work.'); + reject(error); } }); } From a3364d755f85d7124953127875c233da43d127d8 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Sun, 15 Jul 2018 23:16:09 -0400 Subject: [PATCH 23/27] Rewrote pipeline responses to use obj assign --- functions/src/actions/wayback-machine.js | 101 ++++++++++------------- 1 file changed, 43 insertions(+), 58 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 864d210f..4a68bd26 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -21,7 +21,7 @@ const waybackStrings = require('../strings').intents.wayback; */ function handler (app) { // Create wayback object - const waybackObject = { + let waybackObject = { url: '', earliestYear: 0, latestYear: 0, @@ -51,22 +51,11 @@ function handler (app) { return Promise.all([archiveEngine(requestData[0].data), xmlConverter(requestData[1].data)]); }) .then(response => { - let res = {}; - // Merge response into object for assignment to WB obj - res = Object.assign({}, res, response[0]); - - waybackObject.earliestYear = res.earliestYear; - waybackObject.latestYear = res.latestYear; - waybackObject.totalUniqueURLs = res.totalUniqueURLs; - + waybackObject = Object.assign(waybackObject, response[0]); return alexaEngine(response[1]); }) .then(response => { - let res = {}; - // Merge response into object for assignment to WB obj - res = Object.assign({}, res, response); - waybackObject.alexaUSRank = res.alexaUSRank; - waybackObject.alexaWorldRank = res.alexaWorldRank; + waybackObject = Object.assign(waybackObject, response); // Construct response dialog for action if (waybackObject.alexaUSRank !== 0) { @@ -81,53 +70,49 @@ function handler (app) { } // End of handler function archiveEngine (archiveJSON) { - return new Promise(function (resolve, reject) { - debug('Inside archiveEngine promise...'); - let obj = { earliestYear: 0, latestYear: 0, totalUniqueURLs: 0 }; - // Create array of capture years and then find earliest year - // and most recent year. - let yearsArray = Object.keys(archiveJSON.captures); - obj.earliestYear = yearsArray[0]; - obj.latestYear = yearsArray[yearsArray.length - 1]; - - // Traverse URL category - - // Find baseline of URL count - obj.totalUniqueURLs += traverse(archiveJSON.urls[obj.earliestYear]); - // debug('Baseline url count: ' + obj.totalUniqueURLs); - - obj.totalUniqueURLs += traverse(archiveJSON.new_urls); - // debug('Final url count: ' + obj.totalUniqueURLs); - - if (obj.totalUniqueURLs > 0) { - debug('archiveEngine promise successful!'); - resolve(obj); - } else { - let error = new Error('archiveEngine didn\'t work.'); - reject(error); - } - }); + debug('Inside archiveEngine...'); + const res = { earliestYear: 0, latestYear: 0, totalUniqueURLs: 0 }; + // Create array of capture years and then find earliest year + // and most recent year. + let yearsArray = Object.keys(archiveJSON.captures); + res.earliestYear = yearsArray[0]; + res.latestYear = yearsArray[yearsArray.length - 1]; + + // Traverse URL category + + // Find baseline of URL count + res.totalUniqueURLs += traverse(archiveJSON.urls[res.earliestYear]); + // debug('Baseline url count: ' + obj.totalUniqueURLs); + + res.totalUniqueURLs += traverse(archiveJSON.new_urls); + // debug('Final url count: ' + obj.totalUniqueURLs); + + if (res.totalUniqueURLs > 0) { + debug('archiveEngine successful!'); + return res; + } else { + let error = new Error('archiveEngine didn\'t work.'); + return error; + } } function alexaEngine (alexaJSON) { - return new Promise(function (resolve, reject) { - debug('Inside alexaEngine promise...'); - let obj = { alexaWorldRank: 0, alexaUSRank: 0 }; - obj.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; - try { - obj.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; - } catch (e) { - debug('Country not found'); - debug(e); - } - if (obj.alexaWorldRank > 0) { - debug('alexaEngine promise successful!'); - resolve(obj); - } else { - let error = new Error('alexaEngine didn\'t work.'); - reject(error); - } - }); + debug('Inside alexaEngine...'); + const res = { alexaWorldRank: 0, alexaUSRank: 0 }; + res.alexaWorldRank = alexaJSON['ALEXA']['SD'][0]['POPULARITY'][0]['$']['TEXT']; + try { + res.alexaUSRank = alexaJSON['ALEXA']['SD'][0]['COUNTRY'][0]['$']['RANK']; + } catch (e) { + debug('Country not found'); + debug(e); + } + if (res.alexaWorldRank > 0) { + debug('alexaEngine successful!'); + return res; + } else { + let error = new Error('alexaEngine didn\'t work.'); + return error; + } } function xmlConverter (data) { From d916d1e110d0128df4c6c0d0c93d74cf74867bfc Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Mon, 16 Jul 2018 10:08:13 -0400 Subject: [PATCH 24/27] Took unneeded promises out of pipeline, used unpacking to make code clearer --- functions/src/actions/wayback-machine.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 4a68bd26..77d317b1 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -47,12 +47,12 @@ function handler (app) { ); return Promise.all([axios.get(archiveQueryURL), axios.get(alexaQueryURL)]) - .then(function (requestData) { - return Promise.all([archiveEngine(requestData[0].data), xmlConverter(requestData[1].data)]); + .then(([archiveRes, alexaRes]) => { + return ([archiveEngine(archiveRes.data), xmlConverter(alexaRes.data)]); }) - .then(response => { - waybackObject = Object.assign(waybackObject, response[0]); - return alexaEngine(response[1]); + .then(([archiveEngineRes, xmlRes]) => { + waybackObject = Object.assign(waybackObject, archiveEngineRes); + return alexaEngine(xmlRes); }) .then(response => { waybackObject = Object.assign(waybackObject, response); From f53d4f396e69d8ee0eef1c820bd3112f9a1f71ea Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Mon, 16 Jul 2018 10:17:41 -0400 Subject: [PATCH 25/27] Added catch statement for WB handler --- functions/src/actions/wayback-machine.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 77d317b1..04e72267 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -66,6 +66,11 @@ function handler (app) { waybackObject.speech += '.'; } dialog.close(app, waybackObject); + }) + .catch(err => { + debug('Wayback handler has an error: ', err); + waybackObject.speech = waybackObject.error; + dialog.ask(app, waybackObject); }); } // End of handler From d76ca18ab23ab5b503c6fc1df739f20820e72e78 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Mon, 16 Jul 2018 10:25:22 -0400 Subject: [PATCH 26/27] Simplified pipeline for WB machine --- functions/src/actions/wayback-machine.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index 04e72267..cd97a201 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -52,10 +52,7 @@ function handler (app) { }) .then(([archiveEngineRes, xmlRes]) => { waybackObject = Object.assign(waybackObject, archiveEngineRes); - return alexaEngine(xmlRes); - }) - .then(response => { - waybackObject = Object.assign(waybackObject, response); + waybackObject = Object.assign(waybackObject, alexaEngine(xmlRes)); // Construct response dialog for action if (waybackObject.alexaUSRank !== 0) { @@ -97,7 +94,7 @@ function archiveEngine (archiveJSON) { return res; } else { let error = new Error('archiveEngine didn\'t work.'); - return error; + throw error; } } @@ -116,7 +113,7 @@ function alexaEngine (alexaJSON) { return res; } else { let error = new Error('alexaEngine didn\'t work.'); - return error; + throw error; } } From 8e3ec2f80cfddfde8b541fae513a2404234e8fc2 Mon Sep 17 00:00:00 2001 From: adaveinthelife Date: Mon, 16 Jul 2018 10:34:32 -0400 Subject: [PATCH 27/27] Fixed typo in catch statement --- functions/src/actions/wayback-machine.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/src/actions/wayback-machine.js b/functions/src/actions/wayback-machine.js index cd97a201..c70ac606 100644 --- a/functions/src/actions/wayback-machine.js +++ b/functions/src/actions/wayback-machine.js @@ -66,7 +66,7 @@ function handler (app) { }) .catch(err => { debug('Wayback handler has an error: ', err); - waybackObject.speech = waybackObject.error; + waybackObject.speech = waybackStrings.error; dialog.ask(app, waybackObject); }); } // End of handler