function download_video(req, res) {
  var userId = req.userId;

  var id = buffer_search[userId].id;
  console.log("Requesting download ... " + id);

  return new Promise((resolve, reject) => {
    var download = heroku + "/alexa/v3/download/" + id; 

    // Make download request to server
    request(download, function(err, res, body) {
      if (err) {
        // Error in the request
        reject(err.message);
      } else {
        // Convert body text in response to JSON object
        var body_json = JSON.parse(body);

        // Set last search & token to equal the current video's parameters
        last_search[userId] = heroku + body_json.link;

        // NOTE: this is somewhat of hack to get Alexa to ignore an errant PlaybackNearlyFinished event
        repeat_once[userId] = true;
        repeat_infinitely[userId] = false;

        // Wait until video is downloaded by repeatedly pinging cache
        console.log("Waiting for ... " + last_search[userId]);
        wait_for_video(id, function() {
          console.log(last_search[userId] + " has finished downloading.");
          resolve();
        });
      }
    });
  }).then(function() {
    // Have Alexa tell the user that the video is finished downloading
    var speech = new ssml();
    speech.say(response_messages[req.data.request.locale]["NOW_PLAYING"].formatUnicorn(buffer_search[userId].title));
    res.say(speech.ssml(true));

    // Start playing the video!
    restart_video(req, res, 0);

    // Send response to Alexa device
    res.send();
  }).catch(function(reason) {
    // Error occurred in the promise
    res.fail(reason);
  });
}
/**
 * Downloads the mostly recent video the user requested. 
 *
 * @param  {Object} req  A request from an Alexa device
 * @param  {Object} res  A response that will be sent to the device
 * @return {Promise} Execution of the request
 */
Downloads the mostly recent video the user requested.
@param  {Object} req  A request from an Alexa device
@param  {Object} res  A response that will be sent to the device
@return {Promise} Execution of the request
Downloads the mostly recent video the user requested . @param { Object } req A request from an Alexa device @param { Object } res A response that will be sent to the device @return { Promise } Execution of the request
function download_video(req, res) {
  var userId = req.userId;
  var id = buffer_search[userId].id;
  console.log("Requesting download ... " + id);
  return new Promise((resolve, reject) => {
    var download = heroku + "/alexa/v3/download/" + id; 
    request(download, function(err, res, body) {
      if (err) {
        reject(err.message);
      } else {
        var body_json = JSON.parse(body);
        last_search[userId] = heroku + body_json.link;
        repeat_once[userId] = true;
        repeat_infinitely[userId] = false;
        console.log("Waiting for ... " + last_search[userId]);
        wait_for_video(id, function() {
          console.log(last_search[userId] + " has finished downloading.");
          resolve();
        });
      }
    });
  }).then(function() {
    var speech = new ssml();
    speech.say(response_messages[req.data.request.locale]["NOW_PLAYING"].formatUnicorn(buffer_search[userId].title));
    res.say(speech.ssml(true));
    restart_video(req, res, 0);
    res.send();
  }).catch(function(reason) {
    res.fail(reason);
  });
}
function download_video ( req , res ) { var userId = req . userId ; var id = buffer_search [ userId ] . id ; console . log ( "Requesting download ... " + id ) ; return new Promise ( ( resolve , reject ) => { var download = heroku + "/alexa/v3/download/" + id ; request ( download , function ( err , res , body ) { if ( err ) { reject ( err . message ) ; } else { var body_json = JSON . parse ( body ) ; last_search [ userId ] = heroku + body_json . link ; repeat_once [ userId ] = true ; repeat_infinitely [ userId ] = false ; console . log ( "Waiting for ... " + last_search [ userId ] ) ; wait_for_video ( id , function ( ) { console . log ( last_search [ userId ] + " has finished downloading." ) ; resolve ( ) ; } ) ; } } ) ; } ) . then ( function ( ) { var speech = new ssml ( ) ; speech . say ( response_messages [ req . data . request . locale ] [ "NOW_PLAYING" ] . formatUnicorn ( buffer_search [ userId ] . title ) ) ; res . say ( speech . ssml ( true ) ) ; restart_video ( req , res , 0 ) ; res . send ( ) ; } ) . catch ( function ( reason ) { res . fail ( reason ) ; } ) ; }
Downloads the mostly recent video the user requested.
Downloads the mostly recent video the user requested .
// Make download request to server // Error in the request // Convert body text in response to JSON object // Set last search & token to equal the current video's parameters // NOTE: this is somewhat of hack to get Alexa to ignore an errant PlaybackNearlyFinished event // Wait until video is downloaded by repeatedly pinging cache // Have Alexa tell the user that the video is finished downloading // Start playing the video! // Send response to Alexa device // Error occurred in the promise