diff --git a/lib/plugins/webpagetest/analyzer.js b/lib/plugins/webpagetest/analyzer.js index a7bce09be9..b9a4fd07c3 100644 --- a/lib/plugins/webpagetest/analyzer.js +++ b/lib/plugins/webpagetest/analyzer.js @@ -1,6 +1,7 @@ 'use strict'; const fs = require('fs'); +const http = require('https'); const Promise = require('bluebird'); const log = require('intel').getLogger('sitespeedio.plugin.webpagetest'); const clone = require('lodash.clonedeep'); @@ -99,6 +100,42 @@ module.exports = { ) ); + if (options.video) { + let videoId; + promises.push( + wptClient + .createVideoAsync(id, { run, repeatView }) + .then(result => { + videoId = result.data.videoId; + return storageManager.getWriteStreamForUrl( + videoId + '-' + run + '.mp4', + url, + 'video' + ); + }) + .then(file => { + return new Promise((resolve, reject) => { + http.get( + options.host + '/video/download.php?id=' + videoId, + function(response) { + response.pipe(file); + file.on('finish', function() { + file.close(function() { + resolve(); + }); + }); + } + ); + }); + }) + .catch(WPTAPIError, error => + log.warn( + `Couldn't get video for id ${id}, run ${run}: ${error.message} (url = ${url})` + ) + ) + ); + } + promises.push( wptClient .getWaterfallImageAsync(id, { diff --git a/lib/support/resultsStorage/storageManager.js b/lib/support/resultsStorage/storageManager.js index 93e576c29f..78186f0b63 100644 --- a/lib/support/resultsStorage/storageManager.js +++ b/lib/support/resultsStorage/storageManager.js @@ -46,6 +46,15 @@ module.exports = function storageManager(baseDir, storagePathPrefix) { mkdirp(dirPath) ); }, + getWriteStreamForUrl(filename, url, subDir) { + const dirPath = ['data', subDir].filter(Boolean).join(path.sep); + return Promise.join( + this.createDirForUrl(url, dirPath), + filename, + (dirPath, filename) => + fs.createWriteStream(path.join(dirPath, filename)) + ); + }, writeData(filename, data) { return write(this.createDataDir('data'), filename, data); },