Ejemplo n.º 1
0
  Video.count().exec(function(err, numVideos) {
    if (err) {
      return cb(err);
    }

    if (numVideos > 0) {
      console.log('Existing video records: ', numVideos)
      return cb();
    }
    var Youtube = require('machinepack-youtube');

    // List Youtube videos which match the specified search query.
    Youtube.searchVideos({
      query: 'grumpy cat',
      apiKey: sails.config.google.apiKey,
      limit: 15,
    }).exec({
      // An unexpected error occurred.
      error: function(err) {
        console.log('an error: ', err);
        return cb(err);

      },
      // OK.
      success: function(foundVideos) {

        // Transform the incoming foundVideos to match the front end expected format
        _.each(foundVideos, function(video) { 
          video.src = 'https://www.youtube.com/embed/' + video.id; 
          delete video.description; 
          delete video.publishedAt; 
          delete video.id; 
          delete video.url; 
        });

        // Add the transformed video records to the video model
        Video.create(foundVideos).exec(function(err, videoRecordsCreated) {
          if (err) {
            return cb(err);
          }

          console.log(videoRecordsCreated); 
          return cb();
        });
      },
    });
  });
Ejemplo n.º 2
0
  Video.count().exec(function(err, numVideos) {
    if (err) {
      return cb(err);
    }

    // If there's at least one log the number to the console.
    if (numVideos > 0) {
      // return cb();
      return createTestUsers();   
    }

    // Add machinepack-youtube as a depedency
    var Youtube = require('machinepack-youtube');

    // List Youtube videos which match the specified search query.
    Youtube.searchVideos({
      query: 'grumpy cat',
      apiKey: sails.config.google.apiKey,
      limit: 15,
    }).exec({
      // An unexpected error occurred.
      error: function(err) {
        console.log('the error', err);

      },
      // OK.
      success: function(foundVideos) {
        _.each(foundVideos, function(video) {
          video.src = 'https://www.youtube.com/embed/' + video.id;
          delete video.description;
          delete video.publishedAt;
          delete video.id;
          delete video.url;
        });

        Video.create(foundVideos).exec(function(err, videoRecordsCreated) {
          if (err) {
            return cb(err);
          }
          // return cb();
          return createTestUsers();   
        });
      },
    });
  });